8000 test: update AccessTokenFactoryTest with discovery · symfony/symfony@00bf574 · GitHub
[go: up one dir, main page]

Skip to content

Commit 00bf574

Browse files
test: update AccessTokenFactoryTest with discovery
1 parent b60b767 commit 00bf574

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/AccessToken/OidcTokenHandlerFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function create(ContainerBuilder $container, string $id, array|string $co
4040
}
4141

4242
$clientDefinition = (new ChildDefinition('security.access_token_handler.oidc_discovery.http_client'))
43-
->replaceArgument(0, ['base_uri' => $config['base_uri']]);
43+
->replaceArgument(0, ['base_uri' => $config['discovery']['base_uri']]);
4444

4545
$container->setDefinition($id, (new ChildDefinition('security.access_token_handler.oidc_discovery'))
4646
->replaceArgument(0, $clientDefinition)
@@ -127,7 +127,6 @@ public function addConfiguration(NodeBuilder $node): void
127127
->integerNode('default_lifetime')
128128
->info('Cache default lifetime.')
129129
->defaultValue(86400) // 1 day
130-
->isRequired()
131130
->end()
132131
->end()
133132
->end()

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/AccessToken/OidcUserInfoTokenHandlerFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ public function addConfiguration(NodeBuilder $node): void
8888
->integerNode('default_lifetime')
8989
->info('Cache default lifetime.')
9090
->defaultValue(86400) // 1 day
91-
->isRequired()
9291
->end()
9392
->end()
9493
->end()

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Security/Factory/AccessTokenFactoryTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,99 @@ public function testMultipleTokenHandlersSet()
354354
$this->processConfig($config, $factory);
355355
}
356356

357+
public function testOidcTokenHandlerConfigurationWithDiscovery()
358+
{
359+
$container = new ContainerBuilder();
360+
$config = [
361+
'token_handler' => [
362+
'oidc' => [
363+
'discovery' => [
364+
'base_uri' => 'https://www.example.com/realms/demo/',
365+
'cache' => [
366+
'id' => 'oidc_cache',
367+
],
368+
],
369+
'audience' => 'audience',
370+
'issuers' => ['https://www.example.com'],
371+
'algorithms' => ['RS256', 'ES256'],
372+
],
373+
],
374+
];
375+
376+
$factory = new AccessTokenFactory($this->createTokenHandlerFactories());
377+
$finalizedConfig = $this->processConfig($config, $factory);
378+
379+
$factory->createAuthenticator($container, 'firewall1', $finalizedConfig, 'userprovider');
380+
381+
$this->assertTrue($container->hasDefinition('security.authenticator.access_token.firewall1'));
382+
$this->assertTrue($container->hasDefinition('security.access_token_handler.firewall1'));
383+
384+
$expected = [
385+
'index_0' => (new ChildDefinition('security.access_token_handler.oidc_discovery.http_client'))
386+
->replaceArgument(0, ['base_uri' => 'https://www.example.com/realms/demo/']),
387+
'index_1' => new Reference('oidc_cache'),
388+
'index_2' => (new Definition(JWSLoader::class)) // 'security.access_token_handler.firewall1.jws_loader'
389+
->setFactory([new Reference('security.access_token_handler.firewall1.signature_loader_factory'), 'create'])
390+
->setArguments([
391+
[CompactSerializer::NAME],
392+
['RS256', 'ES256'],
393+
['iat', 'nbf', 'exp', 'alg', 'aud', 'iss'],
394+
])
395+
,
396+
'index_3' => 'security.access_token_handler.firewall1.oidc_configuration',
397+
'index_4' => 'security.access_token_handler.firewall1.oidc_jwk_set',
398+
'index_6' => 'sub',
399+
];
400+
401+
if (!interface_exists(HttpClientInterface::class)) {
402+
$this->expectException(LogicException::class);
403+
$this->expectExceptionMessage('You cannot use the "oidc" token handler since the HttpClient component is not installed. Try running "composer require symfony/http-client".');
404+
}
405+
406+
$this->assertEquals($expected, $container->getDefinition('security.access_token_handler.firewall1')->getArguments());
407+
}
408+
409+
public function testOidcUserInfoTokenHandlerConfigurationWithDiscovery()
410+
{
411+
$container = new ContainerBuilder();
412+
$config = [
413+
'token_handler' => [
414+
'oidc_user_info' => [
415+
'base_uri' => 'https://www.example.com/realms/demo/',
416+
'discovery' => [
417+
'cache' => [
418+
'id' => 'oidc_cache',
419+
],
420+
],
421+
],
422+
],
423+
];
424+
425+
$factory = new AccessTokenFactory($this->createTokenHandlerFactories());
426+
$finalizedConfig = $this->processConfig($config, $factory);
427+
428+
$factory->createAuthenticator($container, 'firewall1', $finalizedConfig, 'userprovider');
429+
430+
$this->assertTrue($container->hasDefinition('security.authenticator.access_token.firewall1'));
431+
$this->assertTrue($container->hasDefinition('security.access_token_handler.firewall1'));
432+
433+
$expected = [
434+
'index_0' => (new ChildDefinition('security.access_token_handler.oidc_user_info.http_client'))
435+
->replaceArgument(0, ['base_uri' => 'https://www.example.com/realms/demo/']),
436+
'index_1' => new Reference('oidc_cache'),
437+
8F85 'index_2' => 86400,
438+
'index_3' => 'security.access_token_handler.firewall1.oidc_configuration',
439+
'index_4' => 'sub',
440+
];
441+
442+
if (!interface_exists(HttpClientInterface::class)) {
443+
$this->expectException(LogicException::class);
444+
$this->expectExceptionMessage('You cannot use the "oidc" token handler since the HttpClient component is not installed. Try running "composer require symfony/http-client".');
445+
}
446+
447+
$this->assertEquals($expected, $container->getDefinition('security.access_token_handler.firewall1')->getArguments());
448+
}
449+
357450
public function testNoTokenHandlerSet()
358451
{
359452
$this->expectException(InvalidConfigurationException::class);

0 commit comments

Comments
 (0)
0