|
33 | 33 | use Symfony\Component\Security\Http\Authenticator\FallbackUserLoader; |
34 | 34 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
35 | 35 | use Symfony\Contracts\Cache\CacheInterface; |
| 36 | +use Symfony\Contracts\Cache\ItemInterface; |
36 | 37 | use Symfony\Contracts\HttpClient\HttpClientInterface; |
37 | 38 |
|
38 | 39 | /** |
@@ -93,43 +94,7 @@ public function getUserBadgeFrom(string $accessToken): UserBadge |
93 | 94 |
|
94 | 95 | $jwkset = $this->signatureKeyset; |
95 | 96 | if ($this->discoveryClients) { |
96 | | - $clients = $this->discoveryClients; |
97 | | - $logger = $this->logger; |
98 | | - $keys = $this->discoveryCache->get($this->oidcConfigurationCacheKey, static function () use ($clients, $logger): array { |
99 | | - try { |
100 | | - $configResponses = []; |
101 | | - foreach ($clients as $client) { |
102 | | - $configResponses[] = $client->request('GET', '.well-known/openid-configuration', [ |
103 | | - 'user_data' => $client, |
104 | | - ]); |
105 | | - } |
106 | | - |
107 | | - $jwkSetResponses = []; |
108 | | - foreach ($client->stream($configResponses) as $response => $chunk) { |
109 | | - if ($chunk->isLast()) { |
110 | | - $jwkSetResponses[] = $response->getInfo('user_data')->request('GET', $response->toArray()['jwks_uri']); |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - $keys = []; |
115 | | - foreach ($jwkSetResponses as $response) { |
116 | | - foreach ($response->toArray()['keys'] as $key) { |
117 | | - if ('sig' === $key['use']) { |
118 | | - $keys[] = $key; |
119 | | - } |
120 | | - } |
121 | | - } |
122 | | - |
123 | | - return $keys; |
124 | | - } catch (\Exception $e) { |
125 | | - $logger?->error('An error occurred while requesting OIDC certs.', [ |
126 | | - 'error' => $e->getMessage(), |
127 | | - 'trace' => $e->getTraceAsString(), |
128 | | - ]); |
129 | | - |
130 | | - throw new BadCredentialsException('Invalid credentials.', $e->getCode(), $e); |
131 | | - } |
132 | | - }); |
| 97 | + $keys = $this->discoveryCache->get($this->oidcConfigurationCacheKey, [$this, 'computeDiscoveryKeys']); |
133 | 98 |
|
134 | 99 | $jwkset = JWKSet::createFromKeyData(['keys' => $keys]); |
135 | 100 | } |
@@ -159,6 +124,70 @@ public function getUserBadgeFrom(string $accessToken): UserBadge |
159 | 124 | } |
160 | 125 | } |
161 | 126 |
|
| 127 | + /** |
| 128 | + * Computes the JWKS and sets the cache item TTL from provider headers. |
| 129 | + * |
| 130 | + * The cache entry lifetime is automatically adjusted based on the lowest TTL |
| 131 | + * advertised by the providers (via "Cache-Control: max-age" or "Expires" headers). |
| 132 | + * |
| 133 | + * @internal this method is public to enable async offline cache population |
| 134 | + */ |
| 135 | + public function computeDiscoveryKeys(ItemInterface $item): array |
| 136 | + { |
| 137 | + $clients = $this->discoveryClients; |
| 138 | + $logger = $this->logger; |
| 139 | + |
| 140 | + try { |
| 141 | + $configResponses = []; |
| 142 | + foreach ($clients as $client) { |
| 143 | + $configResponses[] = $client->request('GET', '.well-known/openid-configuration', [ |
| 144 | + 'user_data' => $client, |
| 145 | + ]); |
| 146 | + } |
| 147 | + |
| 148 | + $jwkSetResponses = []; |
| 149 | + foreach ($client->stream($configResponses) as $response => $chunk) { |
| 150 | + if ($chunk->isLast()) { |
| 151 | + $jwkSetResponses[] = $response->getInfo('user_data')->request('GET', $response->toArray()['jwks_uri']); |
| 152 | + } |
| 153 | + } |
| 154 | + $keys = []; |
| 155 | + $minTtl = null; |
| 156 | + foreach ($jwkSetResponses as $response) { |
| 157 | + $headers = $response->getHeaders(); |
| 158 | + if (preg_match('/max-age=(\d+)/', $headers['cache-control'][0] ?? '', $m)) { |
| 159 | + $currentTtl = (int) $m[1]; |
| 160 | + } elseif (0 >= $currentTtl = strtotime($headers['expires'][0] ?? '@0') - time()) { |
| 161 | + $currentTtl = null; |
| 162 | + } |
| 163 | + |
| 164 | + // Apply the lowest TTL found to ensure all keys in the set are still valid |
| 165 | + if (null !== $currentTtl && (null === $minTtl || $currentTtl < $minTtl)) { |
| 166 | + $minTtl = $currentTtl; |
| 167 | + } |
| 168 | + |
| 169 | + foreach ($response->toArray()['keys'] as $key) { |
| 170 | + if ('sig' === $key['use']) { |
| 171 | + $keys[] = $key; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if (0 < ($minTtl ?? -1)) { |
| 177 | + // Cap the TTL to 30 days to avoid keeping JWKS indefinitely |
| 178 | + $item->expiresAfter(min($minTtl, 30 * 24 * 60 * 60)); |
| 179 | + } |
| 180 | + |
| 181 | + return $keys; |
| 182 | + } catch (\Exception $e) { |
| 183 | + $logger?->error('An error occurred while requesting OIDC certs.', [ |
| 184 | + 'error' => $e->getMessage(), |
| 185 | + 'trace' => $e->getTraceAsString(), |
| 186 | + ]); |
| 187 | + throw new BadCredentialsException('Invalid credentials.', $e->getCode(), $e); |
| 188 | + } |
| 189 | + } |
| 190 | + |
162 | 191 | private function loadAndVerifyJws(string $accessToken, JWKSet $jwkset): array |
163 | 192 | { |
164 | 193 | // Decode the token |
|
0 commit comments