-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Security] Add OidcUserInfoTokenHandler and OidcUser #48272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fabpot
merged 1 commit into
symfony:6.3
from
vincentchalamon:feat/oidc-access-token-handler
Apr 14, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...undle/SecurityBundle/DependencyInjection/Security/AccessToken/OidcTokenHandlerFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?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\Bundle\SecurityBundle\DependencyInjection\Security\AccessToken; | ||
|
||
use Jose\Component\Core\Algorithm; | ||
use Jose\Component\Core\JWK; | ||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SignatureAlgorithmFactory; | ||
use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Configures a token handler for decoding and validating an OIDC token. | ||
* | ||
* @experimental | ||
*/ | ||
class OidcTokenHandlerFactory implements TokenHandlerFactoryInterface | ||
{ | ||
public function create(ContainerBuilder $container, string $id, array|string $config): void | ||
{ | ||
$tokenHandlerDefinition = $container->setDefinition($id, new ChildDefinition('security.access_token_handler.oidc')); | ||
$tokenHandlerDefinition->replaceArgument(3, $config['claim']); | ||
$tokenHandlerDefinition->replaceArgument(4, $config['audience']); | ||
|
||
// Create the signature algorithm and the JWK | ||
if (!ContainerBuilder::willBeAvailable('web-token/jwt-core', Algorithm::class, ['symfony/security-bundle'])) { | ||
$container->register('security.access_token_handler.oidc.signature', 'stdClass') | ||
->addError('You cannot use the "oidc" token handler since "web-token/jwt-core" is not installed. Try running "web-token/jwt-core".'); | ||
$container->register('security.access_token_handler.oidc.jwk', 'stdClass') | ||
->addError('You cannot use the "oidc" token handler since "web-token/jwt-core" is not installed. Try running "web-token/jwt-core".'); | ||
} else { | ||
$container->register('security.access_token_handler.oidc.signature', Algorithm::class) | ||
->setFactory([SignatureAlgorithmFactory::class, 'create']) | ||
->setArguments([$config['signature']['algorithm']]); | ||
$container->register('security.access_token_handler.oidc.jwk', JWK::class) | ||
->setFactory([JWK::class, 'createFromJson']) | ||
->setArguments([$config['signature']['key']]); | ||
} | ||
$tokenHandlerDefinition->replaceArgument(0, new Reference('security.access_token_handler.oidc.signature')); | ||
$tokenHandlerDefinition->replaceArgument(1, new Reference('security.access_token_handler.oidc.jwk')); | ||
} | ||
|
||
public function getKey(): string | ||
{ | ||
return 'oidc'; | ||
} | ||
|
||
public function addConfiguration(NodeBuilder $node): void | ||
{ | ||
$node | ||
->arrayNode($this->getKey()) | ||
->fixXmlConfig($this->getKey()) | ||
->children() | ||
->scalarNode('claim') | ||
->info('Claim which contains the user identifier (e.g.: sub, email..).') | ||
->defaultValue('sub') | ||
->end() | ||
->scalarNode('audience') | ||
->info('Audience set in the token, for validation purpose.') | ||
->defaultNull() | ||
->end() | ||
->arrayNode('signature') | ||
->isRequired() | ||
->children() | ||
->scalarNode('algorithm') | ||
->info('Algorithm used to sign the token.') | ||
->isRequired() | ||
->end() | ||
->scalarNode('key') | ||
->info('JSON-encoded JWK used to sign the token (must contain a "kty" key).') | ||
->isRequired() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...curityBundle/DependencyInjection/Security/AccessToken/OidcUserInfoTokenHandlerFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?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\Bundle\SecurityBundle\DependencyInjection\Security\AccessToken; | ||
|
||
use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
|
||
/** | ||
* Configures a token handler for an OIDC server. | ||
* | ||
* @experimental | ||
*/ | ||
class OidcUserInfoTokenHandlerFactory implements TokenHandlerFactoryInterface | ||
{ | ||
public function create(ContainerBuilder $container, string $id, array|string $config): void | ||
{ | ||
$tokenHandlerDefinition = $container->setDefinition($id, new ChildDefinition('security.access_token_handler.oidc_user_info')); | ||
$tokenHandlerDefinition->replaceArgument(2, $config['claim']); | ||
|
||
// Create the client service | ||
if (!isset($config['client']['id'])) { | ||
$clientDefinitionId = 'http_client.security.access_token_handler.oidc_user_info'; | ||
if (!ContainerBuilder::willBeAvailable('symfony/http-client', HttpClient::class, ['symfony/security-bundle'])) { | ||
$container->register($clientDefinitionId, 'stdClass') | ||
->addError('You cannot use the "oidc_user_info" token handler since the HttpClient component is not installed. Try running "composer require symfony/http-client".'); | ||
} else { | ||
$container->register($clientDefinitionId, HttpClient::class) | ||
->setFactory([HttpClient::class, 'create']) | ||
->setArguments([$config['client']]) | ||
->addTag('http_client.client'); | ||
} | ||
} | ||
|
||
$tokenHandlerDefinition->replaceArgument(0, new Reference($config['client']['id'] ?? $clientDefinitionId)); | ||
} | ||
|
||
public function getKey(): string | ||
{ | ||
return 'oidc_user_info'; | ||
} | ||
|
||
public function addConfiguration(NodeBuilder $node): void | ||
{ | ||
$node | ||
->arrayNode($this->getKey()) | ||
->fixXmlConfig($this->getKey()) | ||
->children() | ||
->scalarNode('claim') | ||
->info('Claim which contains the user identifier (e.g.: sub, email..).') | ||
->defaultValue('sub') | ||
->end() | ||
->arrayNode('client') | ||
->info('HttpClient to call the OIDC server.') | ||
->isRequired() | ||
->beforeNormalization() | ||
->ifString() | ||
->then(static function ($v): array { return ['id' => $v]; }) | ||
->end() | ||
->prototype('scalar')->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...le/SecurityBundle/DependencyInjection/Security/AccessToken/ServiceTokenHandlerFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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\Bundle\SecurityBundle\DependencyInjection\Security\AccessToken; | ||
|
||
use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Configures a token handler from a service id. | ||
* | ||
* @see \Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Security\Factory\AccessTokenFactoryTest | ||
* | ||
* @experimental | ||
*/ | ||
class ServiceTokenHandlerFactory implements TokenHandlerFactoryInterface | ||
{ | ||
public function create(ContainerBuilder $container, string $id, array|string $config): void | ||
{ | ||
$container->setDefinition($id, new ChildDefinition($config)); | ||
} | ||
|
||
public function getKey(): string | ||
{ | ||
return 'id'; | ||
} | ||
|
||
public function addConfiguration(NodeBuilder $node): void | ||
{ | ||
$node->scalarNode($this->getKey())->end(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../SecurityBundle/DependencyInjection/Security/AccessToken/TokenHandlerFactoryInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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\Bundle\SecurityBundle\DependencyInjection\Security\AccessToken; | ||
|
||
use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Allows creating configurable token handlers. | ||
* | ||
* @experimental | ||
*/ | ||
interface TokenHandlerFactoryInterface | ||
{ | ||
/** | ||
* Creates a generic token handler service. | ||
*/ | ||
public function create(ContainerBuilder $container, string $id, array|string $config): void; | ||
|
||
/** | ||
* Gets a generic token handler configuration key. | ||
*/ | ||
public function getKey(): string; | ||
|
||
/** | ||
* Adds a generic token handler configuration. | ||
*/ | ||
public function addConfiguration(NodeBuilder $node): void; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.