Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache discovery endpoint results #367

Merged
merged 1 commit into from
Jan 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions lib/Service/DiscoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,47 @@
use OCA\UserOIDC\Db\Provider;
use OCA\UserOIDC\Vendor\Firebase\JWT\JWK;
use OCP\Http\Client\IClientService;
use OCP\ICache;
use OCP\ICacheFactory;
use Psr\Log\LoggerInterface;

class DiscoveryService {
public const INVALIDATE_DISCOVERY_CACHE_AFTER_SECONDS = 3600;
public const INVALIDATE_JWKS_CACHE_AFTER_SECONDS = 3600;

/** @var LoggerInterface */
private $logger;

/** @var IClientService */
private $clientService;
/**
* @var ProviderService
*/

/** @var ProviderService */
private $providerService;

public function __construct(LoggerInterface $logger, IClientService $clientService, ProviderService $providerService) {
/** @var ICache */
private $cache;

public function __construct(LoggerInterface $logger, IClientService $clientService, ProviderService $providerService, ICacheFactory $cacheFactory) {
$this->logger = $logger;
$this->clientService = $clientService;
$this->providerService = $providerService;
$this->cache = $cacheFactory->createDistributed('user_oidc');
}

public function obtainDiscovery(Provider $provider): array {
$url = $provider->getDiscoveryEndpoint();
$client = $this->clientService->newClient();
$cacheKey = 'discovery-' . $provider->getId();
$cachedDiscovery = $this->cache->get($cacheKey);
if ($cachedDiscovery === null) {
$url = $provider->getDiscoveryEndpoint();
$this->logger->debug('Obtaining discovery endpoint: ' . $url);

$this->logger->debug('Obtaining discovery endpoint: ' . $url);
$response = $client->get($url);
$client = $this->clientService->newClient();
$response = $client->get($url);
$cachedDiscovery = $response->getBody();
$this->cache->set($cacheKey, $cachedDiscovery, self::INVALIDATE_DISCOVERY_CACHE_AFTER_SECONDS);
}

return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
return json_decode($cachedDiscovery, true, 512, JSON_THROW_ON_ERROR);
}

public function obtainJWK(Provider $provider): array {
Expand Down