diff --git a/.env.dist b/.env.dist index 8faa486..e9cb6c1 100644 --- a/.env.dist +++ b/.env.dist @@ -39,3 +39,8 @@ SCW_DEFAULT_PROJECT_ID=changeme # Uniquement requis en environnement de test MAILPIT_URL=http://mailpit:8025 + +# ProConnect (OIDC connect) +PRO_CONNECT_WELL_KNOWN_URL=https://example/api/v2/.well-known/config.json +PRO_CONNECT_CLIENT_ID= +PRO_CONNECT_CLIENT_SECRET= \ No newline at end of file diff --git a/config/packages/cache.yaml b/config/packages/cache.yaml index 4ae1649..a65b7a1 100644 --- a/config/packages/cache.yaml +++ b/config/packages/cache.yaml @@ -2,3 +2,6 @@ framework: cache: app: cache.adapter.filesystem system: cache.adapter.system + pools: + oidc: + adapter: cache.adapter.filesystem diff --git a/src/Security/Oidc/OidcClient.php b/src/Security/Oidc/OidcClient.php index ec2df53..74a2690 100644 --- a/src/Security/Oidc/OidcClient.php +++ b/src/Security/Oidc/OidcClient.php @@ -2,14 +2,22 @@ namespace MonIndemnisationJustice\Security\Oidc; -use Firebase\JWT\JWT; use GuzzleHttp\Client as HttpClient; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\RequestException; use Ramsey\Uuid\Uuid; +use Symfony\Component\DependencyInjection\Attribute\Target; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; +use Symfony\Contracts\Cache\CacheInterface; + +final class OidcConnectionContext +{ + public string $code; + public string $nonce; + public ?string $token; +} class OidcClient { @@ -22,6 +30,7 @@ public function __construct( protected readonly string $clientSecret, protected readonly string $loginCheckRoute, protected readonly UrlGeneratorInterface $urlGenerator, + #[Target('oidc')] protected readonly CacheInterface $cache, ) { $this->client = new HttpClient([]); } @@ -29,13 +38,15 @@ public function __construct( protected function configure(): void { if (null === $this->configuration) { - $response = $this->client->get($this->wellKnownUrl); - - if (200 !== $response->getStatusCode()) { - throw new AuthenticationException('Fetch of OIDC server well known configuration failed.'); - } - - $this->configuration = json_decode($response->getBody()->getContents(), true); + $this->configuration = $this->cache->get('oidc_well_known_configuration', function () { + try { + $response = $this->client->get($this->wellKnownUrl); + + return json_decode($response->getBody()->getContents(), true); + } catch (GuzzleException $e) { + throw new AuthenticationException('Fetch of OIDC server well known configuration failed.'); + } + }); } }