1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
From: =?utf-8?q?David_Pr=C3=A9vot?= <david@tilapin.org>
Date: Tue, 23 May 2023 23:30:52 +0200
Subject: Group jwt for tests depending on it
Forwarded: no
---
.../CompleteConfigurationTestCase.php | 3 +
.../Security/Factory/AccessTokenFactoryTest.php | 9 +
.../Tests/Functional/AccessTokenTest.php | 4 +
.../AccessToken/Oidc/OidcTokenGeneratorTest.php | 3 +
.../AccessToken/Oidc/OidcTokenHandlerTest.php | 393 ---------------------
5 files changed, 19 insertions(+), 393 deletions(-)
delete mode 100644 src/Symfony/Component/Security/Http/Tests/AccessToken/Oidc/OidcTokenHandlerTest.php
diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTestCase.php b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTestCase.php
index dcb6701..3344ce2 100644
--- a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTestCase.php
+++ b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTestCase.php
@@ -11,6 +11,7 @@
namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection;
+use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
@@ -726,6 +727,7 @@ abstract class CompleteConfigurationTestCase extends TestCase
$this->assertSame('(?:^/register$|^/documentation$)', $container->getDefinition($requestMatcherId)->getArgument(0));
}
+ #[Group('jwt')]
public function testAccessTokenOidc()
{
$container = $this->getContainer('access_token_oidc');
@@ -739,6 +741,7 @@ abstract class CompleteConfigurationTestCase extends TestCase
$this->assertSame('sub', $def->getArgument(4));
}
+ #[Group('jwt')]
public function testAccessTokenOidcWithEncryption()
{
$container = $this->getContainer('access_token_oidc_encryption');
diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Security/Factory/AccessTokenFactoryTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Security/Factory/AccessTokenFactoryTest.php
index b021d4d..e43e4be 100644
--- a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Security/Factory/AccessTokenFactoryTest.php
+++ b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Security/Factory/AccessTokenFactoryTest.php
@@ -189,6 +189,7 @@ class AccessTokenFactoryTest extends TestCase
#[IgnoreDeprecations]
#[Group('legacy')]
+ #[Group('jwt')]
public function testOidcTokenHandlerConfigurationWithSingleAlgorithm()
{
$this->expectUserDeprecationMessage('Since symfony/security-bundle 7.1: The "key" option is deprecated and will be removed in 8.0. Use the "keyset" option instead.');
@@ -226,6 +227,7 @@ class AccessTokenFactoryTest extends TestCase
$this->assertEquals($expected, $container->getDefinition('security.access_token_handler.firewall1')->getArguments());
}
+ #[Group('jwt')]
public function testOidcTokenHandlerConfigurationWithMultipleAlgorithms()
{
$container = new ContainerBuilder();
@@ -261,6 +263,7 @@ class AccessTokenFactoryTest extends TestCase
$this->assertEquals($expected, $container->getDefinition('security.access_token_handler.firewall1')->getArguments());
}
+ #[Group('jwt')]
public function testOidcTokenHandlerConfigurationWithEncryption()
{
$container = new ContainerBuilder();
@@ -343,6 +346,10 @@ class AccessTokenFactoryTest extends TestCase
$this->processConfig($config, $factory);
}
+ /**
+ * @group jwt
+ */
+ #[Group('jwt')]
public function testOidcTokenHandlerConfigurationWithDiscovery()
{
$container = new ContainerBuilder();
@@ -395,6 +402,7 @@ class AccessTokenFactoryTest extends TestCase
$this->assertEquals($expectedCalls, $container->getDefinition('security.access_token_handler.firewall1')->getMethodCalls());
}
+ #[Group('jwt')]
public function testOidcTokenHandlerConfigurationWithMultipleDiscoveryBaseUri()
{
$container = new ContainerBuilder();
@@ -655,6 +663,7 @@ class AccessTokenFactoryTest extends TestCase
];
}
+ #[Group('jwt')]
public function testOidcTokenGenerator()
{
if (!class_exists(OidcTokenGenerator::class)) {
diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/AccessTokenTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/AccessTokenTest.php
index 7a2dc84..d985860 100644
--- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/AccessTokenTest.php
+++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/AccessTokenTest.php
@@ -21,6 +21,7 @@ use Jose\Component\Signature\Algorithm\ES256;
use Jose\Component\Signature\JWSBuilder;
use Jose\Component\Signature\Serializer\CompactSerializer as JwsCompactSerializer;
use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\HttpClient\MockHttpClient;
@@ -339,6 +340,7 @@ class AccessTokenTest extends AbstractWebTestCase
#[DataProvider('validAccessTokens')]
#[RequiresPhpExtension('openssl')]
+ #[Group('jwt')]
public function testOidcSuccess(callable $tokenFactory)
{
try {
@@ -358,6 +360,7 @@ class AccessTokenTest extends AbstractWebTestCase
#[DataProvider('invalidAccessTokens')]
#[RequiresPhpExtension('openssl')]
+ #[Group('jwt')]
public function testOidcFailure(callable $tokenFactory)
{
try {
@@ -376,6 +379,7 @@ class AccessTokenTest extends AbstractWebTestCase
}
#[RequiresPhpExtension('openssl')]
+ #[Group('jwt')]
public function testOidcFailureWithJweEnforced()
{
$client = $this->createClient(['test_case' => 'AccessToken', 'root_config' => 'config_oidc_jwe.yml']);
diff --git a/src/Symfony/Component/Security/Http/Tests/AccessToken/Oidc/OidcTokenGeneratorTest.php b/src/Symfony/Component/Security/Http/Tests/AccessToken/Oidc/OidcTokenGeneratorTest.php
index ebd666e..2f0f7d4 100644
--- a/src/Symfony/Component/Security/Http/Tests/AccessToken/Oidc/OidcTokenGeneratorTest.php
+++ b/src/Symfony/Component/Security/Http/Tests/AccessToken/Oidc/OidcTokenGeneratorTest.php
@@ -17,6 +17,7 @@ use Jose\Component\Core\JWKSet;
use Jose\Component\Signature\Algorithm\ES256;
use Jose\Component\Signature\Algorithm\ES512;
use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Clock\MockClock;
@@ -26,6 +27,7 @@ use Symfony\Component\Security\Http\AccessToken\Oidc\OidcTokenHandler;
#[RequiresPhpExtension('openssl')]
class OidcTokenGeneratorTest extends TestCase
{
+ #[Group('jwt')]
public function testGenerate()
{
$algorithmManager = new AlgorithmManager([new ES256()]);
@@ -49,6 +51,7 @@ class OidcTokenGeneratorTest extends TestCase
}
#[DataProvider('provideGenerateWithInvalid')]
+ #[Group('jwt')]
public function testGenerateWithInvalid(?string $algorithm, ?string $issuer, ?int $ttl, ?int $notBefore, string $expectedMessage)
{
$this->expectException(\InvalidArgumentException::class);
diff --git a/src/Symfony/Component/Security/Http/Tests/AccessToken/Oidc/OidcTokenHandlerTest.php b/src/Symfony/Component/Security/Http/Tests/AccessToken/Oidc/OidcTokenHandlerTest.php
deleted file mode 100644
index 0c1fc9d..0000000
--- a/src/Symfony/Component/Security/Http/Tests/AccessToken/Oidc/OidcTokenHandlerTest.php
+++ /dev/null
@@ -1,393 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Component\Security\Http\Tests\AccessToken\Oidc;
-
-use Jose\Component\Core\AlgorithmManager;
-use Jose\Component\Core\JWK;
-use Jose\Component\Core\JWKSet;
-use Jose\Component\Signature\Algorithm\ES256;
-use Jose\Component\Signature\JWSBuilder;
-use Jose\Component\Signature\Serializer\CompactSerializer;
-use PHPUnit\Framework\Attributes\DataProvider;
-use PHPUnit\Framework\Attributes\RequiresPhpExtension;
-use PHPUnit\Framework\TestCase;
-use Psr\Log\LoggerInterface;
-use Symfony\Component\Cache\Adapter\ArrayAdapter;
-use Symfony\Component\HttpClient\MockHttpClient;
-use Symfony\Component\HttpClient\Response\JsonMockResponse;
-use Symfony\Component\Security\Core\Exception\BadCredentialsException;
-use Symfony\Component\Security\Core\User\OidcUser;
-use Symfony\Component\Security\Http\AccessToken\Oidc\OidcTokenHandler;
-use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
-
-#[RequiresPhpExtension('openssl')]
-class OidcTokenHandlerTest extends TestCase
-{
- private const AUDIENCE = 'Symfony OIDC';
-
- #[DataProvider('getClaims')]
- public function testGetsUserIdentifierFromSignedToken(string $claim, string $expected)
- {
- $time = time();
- $claims = [
- 'iat' => $time,
- 'nbf' => $time,
- 'exp' => $time + 3600,
- 'iss' => 'https://www.example.com',
- 'aud' => self::AUDIENCE,
- 'sub' => 'e21bf182-1538-406e-8ccb-e25a17aba39f',
- 'email' => 'foo@example.com',
- ];
- $token = self::buildJWS(json_encode($claims));
- $expectedUser = new OidcUser(...$claims, userIdentifier: $claims[$claim]);
-
- $loggerMock = $this->createMock(LoggerInterface::class);
- $loggerMock->expects($this->never())->method('error');
-
- $userBadge = (new OidcTokenHandler(
- new AlgorithmManager([new ES256()]),
- self::getJWKSet(),
- self::AUDIENCE,
- ['https://www.example.com'],
- $claim,
- $loggerMock,
- ))->getUserBadgeFrom($token);
- $actualUser = $userBadge->getUserLoader()();
-
- $this->assertInstanceOf(UserBadge::class, $userBadge);
- $this->assertSame($expected, $userBadge->getUserIdentifier());
- $this->assertSame($claims, $userBadge->getAttributes());
- $this->assertInstanceOf(OidcUser::class, $actualUser);
- $this->assertEquals($expectedUser, $actualUser);
- $this->assertEquals($claims, $userBadge->getAttributes());
- $this->assertEquals($claims[$claim], $actualUser->getUserIdentifier());
- }
-
- public static function getClaims(): iterable
- {
- yield ['sub', 'e21bf182-1538-406e-8ccb-e25a17aba39f'];
- yield ['email', 'foo@example.com'];
- }
-
- #[DataProvider('getInvalidTokens')]
- public function testThrowsAnErrorIfTokenIsInvalid(string $token)
- {
- $loggerMock = $this->createMock(LoggerInterface::class);
- $loggerMock->expects($this->once())->method('error');
-
- $this->expectException(BadCredentialsException::class);
- $this->expectExceptionMessage('Invalid credentials.');
-
- (new OidcTokenHandler(
- new AlgorithmManager([new ES256()]),
- self::getJWKSet(),
- self::AUDIENCE,
- ['https://www.example.com'],
- 'sub',
- $loggerMock,
- ))->getUserBadgeFrom($token);
- }
-
- public static function getInvalidTokens(): iterable
- {
- // Invalid token
- yield ['invalid'];
- // Token is expired
- yield [
- self::buildJWS(json_encode([
- 'iat' => time() - 3600,
- 'nbf' => time() - 3600,
- 'exp' => time() - 3590,
- 'iss' => 'https://www.example.com',
- 'aud' => self::AUDIENCE,
- 'sub' => 'e21bf182-1538-406e-8ccb-e25a17aba39f',
- 'email' => 'foo@example.com',
- ])),
- ];
- // Invalid audience
- yield [
- self::buildJWS(json_encode([
- 'iat' => time(),
- 'nbf' => time(),
- 'exp' => time() + 3590,
- 'iss' => 'https://www.example.com',
- 'aud' => 'invalid',
- 'sub' => 'e21bf182-1538-406e-8ccb-e25a17aba39f',
- 'email' => 'foo@example.com',
- ])),
- ];
- }
-
- public function testThrowsAnErrorIfUserPropertyIsMissing()
- {
- $loggerMock = $this->createMock(LoggerInterface::class);
- $loggerMock->expects($this->once())->method('error');
-
- $time = time();
- $claims = [
- 'iat' => $time,
- 'nbf' => $time,
- 'exp' => $time + 3600,
- 'iss' => 'https://www.example.com',
- 'aud' => self::AUDIENCE,
- 'sub' => 'e21bf182-1538-406e-8ccb-e25a17aba39f',
- ];
- $token = $this->buildJWS(json_encode($claims));
-
- $this->expectException(BadCredentialsException::class);
- $this->expectExceptionMessage('Invalid credentials.');
-
- (new OidcTokenHandler(
- new AlgorithmManager([new ES256()]),
- self::getJWKSet(),
- self::AUDIENCE,
- ['https://www.example.com'],
- 'email',
- $loggerMock,
- ))->getUserBadgeFrom($token);
- }
-
- private static function buildJWS(string $payload): string
- {
- return (new CompactSerializer())->serialize((new JWSBuilder(new AlgorithmManager([
- new ES256(),
- ])))->create()
- ->withPayload($payload)
- ->addSignature(self::getJWK(), ['alg' => 'ES256'])
- ->build()
- );
- }
-
- private static function getJWK(): JWK
- {
- // tip: use https://mkjwk.org/ to generate a JWK
- return new JWK([
- 'kty' => 'EC',
- 'crv' => 'P-256',
- 'x' => '0QEAsI1wGI-dmYatdUZoWSRWggLEpyzopuhwk-YUnA4',
- 'y' => 'KYl-qyZ26HobuYwlQh-r0iHX61thfP82qqEku7i0woo',
- 'd' => 'iA_TV2zvftni_9aFAQwFO_9aypfJFCSpcCyevDvz220',
- ]);
- }
-
- private static function getSecondJWK(): JWK
- {
- return new JWK([
- 'kty' => 'EC',
- 'd' => '0LCBSOYvrksazPnC0pzwY0P5MWEESUhEzbc2zJEnOsc',
- 'crv' => 'P-256',
- 'x' => 'N1aUu8Pd2WdClkpCQ4QCPnGjYe_bTmDgEaSoxy5LhTw',
- 'y' => 'Yr1v-tCNxE8QgAGlartrJAi343bI8VlAaNvgCOp8Azs',
- ]);
- }
-
- private static function getJWKSet(): JWKSet
- {
- return new JWKSet([
- new JWK([
- 'kty' => 'EC',
- 'crv' => 'P-256',
- 'x' => 'FtgMtrsKDboRO-Zo0XC7tDJTATHVmwuf9GK409kkars',
- 'y' => 'rWDE0ERU2SfwGYCo1DWWdgFEbZ0MiAXLRBBOzBgs_jY',
- 'd' => '4G7bRIiKih0qrFxc0dtvkHUll19tTyctoCR3eIbOrO0',
- ]),
- self::getJWK(),
- ]);
- }
-
- public function testGetsUserIdentifierWithSingleDiscoveryEndpoint()
- {
- $time = time();
- $claims = [
- 'iat' => $time,
- 'nbf' => $time,
- 'exp' => $time + 3600,
- 'iss' => 'https://www.example.com',
- 'aud' => self::AUDIENCE,
- 'sub' => 'e21bf182-1538-406e-8ccb-e25a17aba39f',
- 'email' => 'foo@example.com',
- ];
- $token = $this->buildJWS(json_encode($claims));
-
- $httpClient = new MockHttpClient([
- new JsonMockResponse(['jwks_uri' => 'https://www.example.com/.well-known/jwks.json']),
- new JsonMockResponse(['keys' => [array_merge(self::getJWK()->all(), ['use' => 'sig'])]]),
- ]);
-
- $cache = new ArrayAdapter();
- $handler = new OidcTokenHandler(
- new AlgorithmManager([new ES256()]),
- null,
- self::AUDIENCE,
- ['https://www.example.com']
- );
- $handler->enableDiscovery($cache, $httpClient, 'oidc_config');
-
- $userBadge = $handler->getUserBadgeFrom($token);
-
- $this->assertInstanceOf(UserBadge::class, $userBadge);
- $this->assertSame('e21bf182-1538-406e-8ccb-e25a17aba39f', $userBadge->getUserIdentifier());
- }
-
- public function testGetsUserIdentifierWithMultipleDiscoveryEndpoints()
- {
- $time = time();
-
- $httpClient1 = new MockHttpClient(function ($method, $url) {
- if (str_contains($url, 'openid-configuration')) {
- return new JsonMockResponse(['jwks_uri' => 'https://provider1.example.com/.well-known/jwks.json']);
- }
-
- return new JsonMockResponse(['keys' => [array_merge(self::getJWK()->all(), ['use' => 'sig'])]]);
- });
-
- $httpClient2 = new MockHttpClient(function ($method, $url) {
- if (str_contains($url, 'openid-configuration')) {
- return new JsonMockResponse(['jwks_uri' => 'https://provider2.example.com/.well-known/jwks.json']);
- }
-
- return new JsonMockResponse(['keys' => [array_merge(self::getSecondJWK()->all(), ['use' => 'sig'])]]);
- });
-
- $cache = new ArrayAdapter();
-
- $handler = new OidcTokenHandler(
- new AlgorithmManager([new ES256()]),
- null,
- self::AUDIENCE,
- ['https://www.example.com']
- );
- $handler->enableDiscovery($cache, [$httpClient1, $httpClient2], 'oidc_config');
-
- $claims1 = [
- 'iat' => $time,
- 'nbf' => $time,
- 'exp' => $time + 3600,
- 'iss' => 'https://www.example.com',
- 'aud' => self::AUDIENCE,
- 'sub' => 'user-from-provider1',
- 'email' => 'user1@example.com',
- ];
- $token1 = self::buildJWSWithKey(json_encode($claims1), self::getJWK());
- $userBadge1 = $handler->getUserBadgeFrom($token1);
-
- $this->assertInstanceOf(UserBadge::class, $userBadge1);
- $this->assertSame('user-from-provider1', $userBadge1->getUserIdentifier());
-
- $claims2 = [
- 'iat' => $time,
- 'nbf' => $time,
- 'exp' => $time + 3600,
- 'iss' => 'https://www.example.com',
- 'aud' => self::AUDIENCE,
- 'sub' => 'user-from-provider2',
- 'email' => 'user2@example.com',
- ];
- $token2 = self::buildJWSWithKey(json_encode($claims2), self::getSecondJWK());
- $userBadge2 = $handler->getUserBadgeFrom($token2);
-
- $this->assertInstanceOf(UserBadge::class, $userBadge2);
- $this->assertSame('user-from-provider2', $userBadge2->getUserIdentifier());
-
- $this->assertTrue($cache->hasItem('oidc_config'));
- }
-
- private static function buildJWSWithKey(string $payload, JWK $jwk): string
- {
- return (new CompactSerializer())->serialize((new JWSBuilder(new AlgorithmManager([
- new ES256(),
- ])))->create()
- ->withPayload($payload)
- ->addSignature($jwk, ['alg' => 'ES256'])
- ->build()
- );
- }
-
- public function testDiscoveryCachesJwksAccordingToCacheControl()
- {
- $time = time();
- $claims = [
- 'iat' => $time, 'nbf' => $time,
- 'exp' => $time + 3600,
- 'iss' => 'https://www.example.com',
- 'aud' => self::AUDIENCE,
- 'sub' => 'user-cache-control',
- ];
- $token = self::buildJWS(json_encode($claims));
-
- $requestCount = 0;
- $httpClient = new MockHttpClient(function ($method, $url) use (&$requestCount) {
- ++$requestCount;
- if (str_contains($url, 'openid-configuration')) {
- return new JsonMockResponse(['jwks_uri' => 'https://www.example.com/jwks.json']);
- }
-
- return new JsonMockResponse(
- ['keys' => [array_merge(self::getJWK()->all(), ['use' => 'sig'])]],
- ['response_headers' => ['Cache-Control' => 'public, max-age=120']]
- );
- });
-
- $cache = new ArrayAdapter();
- $handler = new OidcTokenHandler(
- new AlgorithmManager([new ES256()]),
- null,
- self::AUDIENCE,
- ['https://www.example.com']
- );
- $handler->enableDiscovery($cache, $httpClient, 'oidc_ttl_cc');
- $this->assertSame('user-cache-control', $handler->getUserBadgeFrom($token)->getUserIdentifier());
- $this->assertSame(2, $requestCount);
- $this->assertSame('user-cache-control', $handler->getUserBadgeFrom($token)->getUserIdentifier());
- $this->assertSame(2, $requestCount);
- }
-
- public function testDiscoveryCachesJwksAccordingToExpires()
- {
- $time = time();
- $claims = [
- 'iat' => $time, 'nbf' => $time,
- 'exp' => $time + 3600,
- 'iss' => 'https://www.example.com',
- 'aud' => self::AUDIENCE,
- 'sub' => 'user-expires',
- ];
-
- $token = self::buildJWS(json_encode($claims));
-
- $requestCount = 0;
- $httpClient = new MockHttpClient(function ($method, $url) use (&$requestCount) {
- ++$requestCount;
- if (str_contains($url, 'openid-configuration')) {
- return new JsonMockResponse(['jwks_uri' => 'https://www.example.com/jwks.json']);
- }
-
- return new JsonMockResponse(
- ['keys' => [array_merge(self::getJWK()->all(), ['use' => 'sig'])]],
- ['response_headers' => ['Expires' => gmdate('D, d M Y H:i:s \G\M\T', time() + 60)]]
- );
- });
-
- $cache = new ArrayAdapter();
- $handler = new OidcTokenHandler(
- new AlgorithmManager([new ES256()]),
- null,
- self::AUDIENCE,
- ['https://www.example.com']
- );
- $handler->enableDiscovery($cache, $httpClient, 'oidc_ttl_expires');
- $this->assertSame('user-expires', $handler->getUserBadgeFrom($token)->getUserIdentifier());
- $this->assertSame(2, $requestCount);
- $this->assertSame('user-expires', $handler->getUserBadgeFrom($token)->getUserIdentifier());
- $this->assertSame(2, $requestCount);
- }
-}
|