|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory; |
| 13 | + |
| 14 | +use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
| 15 | +use Symfony\Component\DependencyInjection\ChildDefinition; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\Reference; |
| 18 | + |
| 19 | +/** |
| 20 | + * AccessTokenFactory creates services for Access Token authentication. |
| 21 | + * |
| 22 | + * @author Florent Morselli <florent.morselli@spomky-labs.com> |
| 23 | + * |
| 24 | + * @internal |
| 25 | + */ |
| 26 | +final class AccessTokenFactory extends AbstractFactory |
| 27 | +{ |
| 28 | + private const PRIORITY = -40; |
| 29 | + |
| 30 | + public function __construct() |
| 31 | + { |
| 32 | + $this->options = []; |
| 33 | + $this->defaultFailureHandlerOptions = []; |
| 34 | + $this->defaultSuccessHandlerOptions = []; |
| 35 | + } |
| 36 | + |
| 37 | + public function addConfiguration(NodeDefinition $node): void |
| 38 | + { |
| 39 | + $builder = $node->children(); |
| 40 | + |
| 41 | + $builder |
| 42 | + ->scalarNode('token_handler')->isRequired()->end() |
| 43 | + ->scalarNode('user_provider')->defaultNull()->end() |
| 44 | + ->scalarNode('realm')->defaultNull()->end() |
| 45 | + ->scalarNode('success_handler')->defaultNull()->end() |
| 46 | + ->scalarNode('failure_handler')->defaultNull()->end() |
| 47 | + ->arrayNode('token_extractors') |
| 48 | + ->fixXmlConfig('token_extractors') |
| 49 | + ->beforeNormalization() |
| 50 | + ->ifString() |
| 51 | + ->then(static function (string $v): array { return [$v]; }) |
| 52 | + ->end() |
| 53 | + ->cannotBeEmpty() |
| 54 | + ->defaultValue([ |
| 55 | + 'security.access_token_extractor.header', |
| 56 | + ]) |
| 57 | + ->scalarPrototype()->end() |
| 58 | + ->end() |
| 59 | + ; |
| 60 | + } |
| 61 | + |
| 62 | + public function getPriority(): int |
| 63 | + { |
| 64 | + return self::PRIORITY; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function getKey(): string |
| 71 | + { |
| 72 | + return 'access_token'; |
| 73 | + } |
| 74 | + |
| 75 | + public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string |
| 76 | + { |
| 77 | + $userProvider = new Reference($config['user_provider'] ?? $userProviderId); |
| 78 | + $successHandler = isset($config['success_handler']) ? new Reference($this->createAuthenticationSuccessHandler($container, $firewallName, $config)) : null; |
| 79 | + $failureHandler = isset($config['failure_handler']) ? new Reference($this->createAuthenticationFailureHandler($container, $firewallName, $config)) : null; |
| 80 | + $authenticatorId = sprintf('security.authenticator.access_token.%s', $firewallName); |
| 81 | + $extractorId = $this->createExtractor($container, $firewallName, $config['token_extractors']); |
| 82 | + |
| 83 | + $container |
| 84 | + ->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.access_token')) |
| 85 | + ->replaceArgument(0, $userProvider) |
| 86 | + ->replaceArgument(1, new Reference($config['token_handler'])) |
| 87 | + ->replaceArgument(2, new Reference($extractorId)) |
| 88 | + ->replaceArgument(3, $successHandler) |
| 89 | + ->replaceArgument(4, $failureHandler) |
| 90 | + ->replaceArgument(5, $config['realm']) |
| 91 | + ; |
| 92 | + |
| 93 | + return $authenticatorId; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param array<string> $extractors |
| 98 | + */ |
| 99 | + private function createExtractor(ContainerBuilder $container, string $firewallName, array $extractors): string |
| 100 | + { |
| 101 | + $aliases = [ |
| 102 | + 'query_string' => 'security.access_token_extractor.query_string', |
| 103 | + 'request_body' => 'security.access_token_extractor.request_body', |
| 104 | + 'header' => 'security.access_token_extractor.header', |
| 105 | + ]; |
| 106 | + $extractors = array_map(static function (string $extractor) use ($aliases): string { |
| 107 | + return $aliases[$extractor] ?? $extractor; |
| 108 | + }, $extractors); |
| 109 | + |
| 110 | + if (1 === \count($extractors)) { |
| 111 | + return current($extractors); |
| 112 | + } |
| 113 | + $extractorId = sprintf('security.authenticator.access_token.chain_extractor.%s', $firewallName); |
| 114 | + $container |
| 115 | + ->setDefinition($extractorId, new ChildDefinition('security.authenticator.access_token.chain_extractor')) |
| 116 | + ->replaceArgument(0, array_map(function (string $extractorId): Reference {return new Reference($extractorId); }, $extractors)) |
| 117 | + ; |
| 118 | + |
| 119 | + return $extractorId; |
| 120 | + } |
| 121 | +} |
0 commit comments