|
28 | 28 | use Symfony\Component\Security\Core\User\OidcUser; |
29 | 29 | use Symfony\Component\Security\Http\AccessToken\Oidc\OidcTokenHandler; |
30 | 30 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 31 | +use Symfony\Contracts\Cache\ItemInterface; |
31 | 32 |
|
32 | 33 | #[RequiresPhpExtension('openssl')] |
33 | 34 | class OidcTokenHandlerTest extends TestCase |
@@ -316,7 +317,8 @@ public function testDiscoveryCachesJwksAccordingToCacheControl() |
316 | 317 | { |
317 | 318 | $time = time(); |
318 | 319 | $claims = [ |
319 | | - 'iat' => $time, 'nbf' => $time, |
| 320 | + 'iat' => $time, |
| 321 | + 'nbf' => $time, |
320 | 322 | 'exp' => $time + 3600, |
321 | 323 | 'iss' => 'https://www.example.com', |
322 | 324 | 'aud' => self::AUDIENCE, |
@@ -355,7 +357,8 @@ public function testDiscoveryCachesJwksAccordingToExpires() |
355 | 357 | { |
356 | 358 | $time = time(); |
357 | 359 | $claims = [ |
358 | | - 'iat' => $time, 'nbf' => $time, |
| 360 | + 'iat' => $time, |
| 361 | + 'nbf' => $time, |
359 | 362 | 'exp' => $time + 3600, |
360 | 363 | 'iss' => 'https://www.example.com', |
361 | 364 | 'aud' => self::AUDIENCE, |
@@ -390,4 +393,80 @@ public function testDiscoveryCachesJwksAccordingToExpires() |
390 | 393 | $this->assertSame('user-expires', $handler->getUserBadgeFrom($token)->getUserIdentifier()); |
391 | 394 | $this->assertSame(2, $requestCount); |
392 | 395 | } |
| 396 | + |
| 397 | + public function testComputeDiscoveryKeysReturnsEmptyWhenNoClients() |
| 398 | + { |
| 399 | + $cache = new ArrayAdapter(); |
| 400 | + $handler = new OidcTokenHandler( |
| 401 | + new AlgorithmManager([new ES256()]), |
| 402 | + null, |
| 403 | + self::AUDIENCE, |
| 404 | + ['https://www.example.com'] |
| 405 | + ); |
| 406 | + |
| 407 | + $handler->enableDiscovery($cache, [], 'oidc_empty_clients'); |
| 408 | + |
| 409 | + $item = $this->createMock(ItemInterface::class); |
| 410 | + $item->expects($this->never())->method('expiresAfter'); |
| 411 | + |
| 412 | + $this->expectException(\LogicException::class); |
| 413 | + $this->expectExceptionMessage('No OIDC discovery client configured.'); |
| 414 | + $handler->computeDiscoveryKeys($item); |
| 415 | + } |
| 416 | + |
| 417 | + public function testDiscoveryThrowsWhenJwksUriIsMissing() |
| 418 | + { |
| 419 | + $time = time(); |
| 420 | + $claims = [ |
| 421 | + 'iat' => $time, |
| 422 | + 'nbf' => $time, |
| 423 | + 'exp' => $time + 3600, |
| 424 | + 'iss' => 'https://www.example.com', |
| 425 | + 'aud' => self::AUDIENCE, |
| 426 | + 'sub' => 'user-missing-jwks-uri', |
| 427 | + ]; |
| 428 | + $token = self::buildJWS(json_encode($claims)); |
| 429 | + |
| 430 | + $httpClient = new MockHttpClient([ |
| 431 | + new JsonMockResponse(['issuer' => 'https://www.example.com']), |
| 432 | + ]); |
| 433 | + |
| 434 | + $cache = new ArrayAdapter(); |
| 435 | + $handler = new OidcTokenHandler( |
| 436 | + new AlgorithmManager([new ES256()]), |
| 437 | + null, |
| 438 | + self::AUDIENCE, |
| 439 | + ['https://www.example.com'] |
| 440 | + ); |
| 441 | + $handler->enableDiscovery($cache, $httpClient, 'oidc_missing_jwks_uri'); |
| 442 | + |
| 443 | + $this->expectException(BadCredentialsException::class); |
| 444 | + $handler->getUserBadgeFrom($token); |
| 445 | + } |
| 446 | + |
| 447 | + public function testDiscoveryIgnoresNonSignatureKeys() |
| 448 | + { |
| 449 | + $httpClient = new MockHttpClient([ |
| 450 | + new JsonMockResponse(['jwks_uri' => 'https://www.example.com/jwks.json']), |
| 451 | + new JsonMockResponse([ |
| 452 | + 'keys' => [ |
| 453 | + array_merge(self::getJWK()->all(), ['use' => 'enc']), |
| 454 | + array_merge(self::getSecondJWK()->all(), []), |
| 455 | + ], |
| 456 | + ]), |
| 457 | + ]); |
| 458 | + |
| 459 | + $cache = new ArrayAdapter(); |
| 460 | + $handler = new OidcTokenHandler( |
| 461 | + new AlgorithmManager([new ES256()]), |
| 462 | + null, |
| 463 | + self::AUDIENCE, |
| 464 | + ['https://www.example.com'] |
| 465 | + ); |
| 466 | + $handler->enableDiscovery($cache, $httpClient, 'oidc_non_sig_keys'); |
| 467 | + |
| 468 | + $item = $this->createMock(ItemInterface::class); |
| 469 | + $item->expects($this->never())->method('expiresAfter'); |
| 470 | + $this->assertSame([], $handler->computeDiscoveryKeys($item)); |
| 471 | + } |
393 | 472 | } |
0 commit comments