15
15
use Symfony \Component \DependencyInjection \ChildDefinition ;
16
16
use Symfony \Component \DependencyInjection \ContainerBuilder ;
17
17
use Symfony \Component \DependencyInjection \Reference ;
18
- use Symfony \Component \HttpClient \HttpClient ;
18
+ use Symfony \Contracts \HttpClient \HttpClientInterface ;
19
19
20
20
/**
21
21
* Configures a token handler for an OIDC server.
@@ -30,20 +30,19 @@ public function create(ContainerBuilder $container, string $id, array|string $co
30
30
$ tokenHandlerDefinition ->replaceArgument (2 , $ config ['claim ' ]);
31
31
32
32
// Create the client service
33
- if (! isset ($ config ['client ' ][ ' id ' ])) {
33
+ if (isset ($ config ['base_uri ' ])) {
34
34
$ clientDefinitionId = 'http_client.security.access_token_handler.oidc_user_info ' ;
35
- if (!ContainerBuilder::willBeAvailable ('symfony/http-client ' , HttpClient ::class, ['symfony/security-bundle ' ])) {
35
+ if (!ContainerBuilder::willBeAvailable ('symfony/http-client ' , HttpClientInterface ::class, ['symfony/security-bundle ' ])) {
36
36
$ container ->register ($ clientDefinitionId , 'stdClass ' )
37
37
->addError ('You cannot use the "oidc_user_info" token handler since the HttpClient component is not installed. Try running "composer require symfony/http-client". ' );
38
38
} else {
39
- $ container ->register ($ clientDefinitionId , HttpClient::class)
40
- ->setFactory ([HttpClient::class, 'create ' ])
41
- ->setArguments ([$ config ['client ' ]])
39
+ $ container ->setDefinition ($ clientDefinitionId , new ChildDefinition ('security.access_token_handler.oidc_user_info.http_client ' ))
40
+ ->setArguments (['base_uri ' => $ config ['base_uri ' ]])
42
41
->addTag ('http_client.client ' );
43
42
}
44
43
}
45
44
46
- $ tokenHandlerDefinition ->replaceArgument (0 , new Reference ($ config ['client ' ][ ' id ' ] ?? $ clientDefinitionId ));
45
+ $ tokenHandlerDefinition ->replaceArgument (0 , new Reference ($ clientDefinitionId ?? $ config ['client ' ]));
47
46
}
48
47
49
48
public function getKey (): string
@@ -56,19 +55,28 @@ public function addConfiguration(NodeBuilder $node): void
56
55
$ node
57
56
->arrayNode ($ this ->getKey ())
58
57
->fixXmlConfig ($ this ->getKey ())
58
+ ->beforeNormalization ()
59
+ ->ifString ()
60
+ ->then (static function ($ v ): array { return ['claim ' => 'sub ' , 'base_uri ' => $ v ]; })
61
+ ->end ()
62
+ ->validate ()
63
+ ->ifTrue (function ($ v ) { return !empty ($ v ['base_uri ' ]) && !empty ($ v ['client ' ]); })
64
+ ->thenInvalid ('You cannot configure the base_uri and the client together. ' )
65
+ ->end ()
66
+ ->validate ()
67
+ ->ifTrue (function ($ v ) { return empty ($ v ['base_uri ' ]) && empty ($ v ['client ' ]); })
68
+ ->thenInvalid ('You must configure the "base_uri" or the "client" option. ' )
69
+ ->end ()
59
70
->children ()
60
71
->scalarNode ('claim ' )
61
72
->info ('Claim which contains the user identifier (e.g.: sub, email..). ' )
62
73
->defaultValue ('sub ' )
63
74
->end ()
64
- ->arrayNode ('client ' )
65
- ->info ('HttpClient to call the OIDC server. ' )
66
- ->isRequired ()
67
- ->beforeNormalization ()
68
- ->ifString ()
69
- ->then (static function ($ v ): array { return ['id ' => $ v ]; })
70
- ->end ()
71
- ->prototype ('scalar ' )->end ()
75
+ ->scalarNode ('base_uri ' )
76
+ ->info ('HttpClient base_uri to call the OIDC server. ' )
77
+ ->end ()
78
+ ->scalarNode ('client ' )
79
+ ->info ('HttpClient service id to call the OIDC server. ' )
72
80
->end ()
73
81
->end ()
74
82
->end ()
0 commit comments