|
35 | 35 | use Symfony\Component\Security\Http\Authenticator\FallbackUserLoader; |
36 | 36 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
37 | 37 | use Symfony\Contracts\Cache\CacheInterface; |
| 38 | +use Symfony\Contracts\Cache\ItemInterface; |
38 | 39 | use Symfony\Contracts\HttpClient\HttpClientInterface; |
39 | 40 |
|
40 | 41 | /** |
@@ -107,43 +108,7 @@ public function getUserBadgeFrom(string $accessToken): UserBadge |
107 | 108 |
|
108 | 109 | $jwkset = $this->signatureKeyset; |
109 | 110 | if ($this->discoveryClients) { |
110 | | - $clients = $this->discoveryClients; |
111 | | - $logger = $this->logger; |
112 | | - $keys = $this->discoveryCache->get($this->oidcConfigurationCacheKey, static function () use ($clients, $logger): array { |
113 | | - try { |
114 | | - $configResponses = []; |
115 | | - foreach ($clients as $client) { |
116 | | - $configResponses[] = $client->request('GET', '.well-known/openid-configuration', [ |
117 | | - 'user_data' => $client, |
118 | | - ]); |
119 | | - } |
120 | | - |
121 | | - $jwkSetResponses = []; |
122 | | - foreach ($client->stream($configResponses) as $response => $chunk) { |
123 | | - if ($chunk->isLast()) { |
124 | | - $jwkSetResponses[] = $response->getInfo('user_data')->request('GET', $response->toArray()['jwks_uri']); |
125 | | - } |
126 | | - } |
127 | | - |
128 | | - $keys = []; |
129 | | - foreach ($jwkSetResponses as $response) { |
130 | | - foreach ($response->toArray()['keys'] as $key) { |
131 | | - if ('sig' === $key['use']) { |
132 | | - $keys[] = $key; |
133 | | - } |
134 | | - } |
135 | | - } |
136 | | - |
137 | | - return $keys; |
138 | | - } catch (\Exception $e) { |
139 | | - $logger?->error('An error occurred while requesting OIDC certs.', [ |
140 | | - 'error' => $e->getMessage(), |
141 | | - 'trace' => $e->getTraceAsString(), |
142 | | - ]); |
143 | | - |
144 | | - throw new BadCredentialsException('Invalid credentials.', $e->getCode(), $e); |
145 | | - } |
146 | | - }); |
| 111 | + $keys = $this->fetchDiscoveryKeys(); |
147 | 112 |
|
148 | 113 | $jwkset = JWKSet::createFromKeyData(['keys' => $keys]); |
149 | 114 | } |
@@ -260,4 +225,78 @@ private function decryptIfNeeded(string $accessToken): string |
260 | 225 | return $accessToken; |
261 | 226 | } |
262 | 227 | } |
| 228 | + |
| 229 | + /** |
| 230 | + * Fetches the JWKS keys from the cache or computes them via callback. |
| 231 | + */ |
| 232 | + private function fetchDiscoveryKeys(): array |
| 233 | + { |
| 234 | + return $this->discoveryCache->get($this->oidcConfigurationCacheKey, [$this, 'computeDiscoveryKeys']); |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Computes the JWKS and sets the cache item TTL from provider headers. |
| 239 | + * |
| 240 | + * The cache entry lifetime is automatically adjusted based on the lowest TTL |
| 241 | + * advertised by the providers (via "Cache-Control: max-age" or "Expires" headers). |
| 242 | + */ |
| 243 | + public function computeDiscoveryKeys(ItemInterface $item): array |
| 244 | + { |
| 245 | + $clients = $this->discoveryClients; |
| 246 | + $logger = $this->logger; |
| 247 | + |
| 248 | + try { |
| 249 | + $configResponses = []; |
| 250 | + foreach ($clients as $client) { |
| 251 | + $configResponses[] = $client->request('GET', '.well-known/openid-configuration', [ |
| 252 | + 'user_data' => $client, |
| 253 | + ]); |
| 254 | + } |
| 255 | + |
| 256 | + $jwkSetResponses = []; |
| 257 | + foreach ($client->stream($configResponses) as $response => $chunk) { |
| 258 | + if ($chunk->isLast()) { |
| 259 | + $jwkSetResponses[] = $response->getInfo('user_data')->request('GET', $response->toArray()['jwks_uri']); |
| 260 | + } |
| 261 | + } |
| 262 | + $keys = []; |
| 263 | + $minTtl = null; |
| 264 | + foreach ($jwkSetResponses as $response) { |
| 265 | + $currentTtl = null; |
| 266 | + $headers = $response->getHeaders(); |
| 267 | + if (preg_match('/max-age=(\d+)/', $headers['cache-control'][0] ?? '', $m)) { |
| 268 | + $currentTtl = (int) $m[1]; |
| 269 | + } elseif (($headers['expires'][0] ?? '') !== '') { |
| 270 | + if (0 < $ttl = strtotime($headers['expires'][0]) - time()) { |
| 271 | + $currentTtl = $ttl; |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + // Apply the lowest TTL found to ensure all keys in the set are still valid |
| 276 | + if (null !== $currentTtl && (null === $minTtl || $currentTtl < $minTtl)) { |
| 277 | + $minTtl = $currentTtl; |
| 278 | + } |
| 279 | + |
| 280 | + foreach ($response->toArray()['keys'] as $key) { |
| 281 | + if ('sig' === $key['use']) { |
| 282 | + $keys[] = $key; |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + if (null !== $minTtl && $minTtl > 0) { |
| 288 | + // Cap the TTL to 30 days to avoid keeping JWKS indefinitely |
| 289 | + $ttl = min($minTtl, 30 * 24 * 60 * 60); |
| 290 | + $item->expiresAfter($ttl); |
| 291 | + } |
| 292 | + |
| 293 | + return $keys; |
| 294 | + } catch (\Exception $e) { |
| 295 | + $logger?->error('An error occurred while requesting OIDC certs.', [ |
| 296 | + 'error' => $e->getMessage(), |
| 297 | + 'trace' => $e->getTraceAsString(), |
| 298 | + ]); |
| 299 | + throw new BadCredentialsException('Invalid credentials.', $e->getCode(), $e); |
| 300 | + } |
| 301 | + } |
263 | 302 | } |
0 commit comments