diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php index f9d28a1d7b35c..6e3d806cd5551 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php @@ -249,10 +249,13 @@ private function createFirewalls(array $config, ContainerBuilder $container) } $arguments[1] = $userProviderIteratorsArgument = new IteratorArgument($userProviders); $contextListenerDefinition->setArguments($arguments); + $nbUserProviders = \count($userProviders); - if (\count($userProviders) > 1) { + if ($nbUserProviders > 1) { $container->setDefinition('security.user_providers', new Definition(ChainUserProvider::class, [$userProviderIteratorsArgument])) ->setPublic(false); + } elseif (0 === $nbUserProviders) { + $container->removeDefinition('security.listener.user_provider'); } else { $container->setAlias('security.user_providers', new Alias(current($providerIds)))->setPublic(false); } diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/AuthenticatorTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/AuthenticatorTest.php index 201e446e04370..5aeb0b83352f7 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/AuthenticatorTest.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/AuthenticatorTest.php @@ -44,6 +44,20 @@ public function testFirewallUserProvider($email, $withinFirewall) } } + /** + * @dataProvider provideEmails + */ + public function testWithoutUserProvider($email) + { + $client = $this->createClient(['test_case' => 'Authenticator', 'root_config' => 'no_user_provider.yml']); + + $client->request('GET', '/profile', [], [], [ + 'HTTP_X-USER-EMAIL' => $email, + ]); + + $this->assertJsonStringEqualsJsonString('{"email":"'.$email.'"}', $client->getResponse()->getContent()); + } + public function provideEmails() { yield ['jane@example.org', true]; diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/AuthenticatorBundle/ApiAuthenticator.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/AuthenticatorBundle/ApiAuthenticator.php index 6bff3145c9dd5..6885f22938fa0 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/AuthenticatorBundle/ApiAuthenticator.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/AuthenticatorBundle/ApiAuthenticator.php @@ -17,6 +17,7 @@ use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\BadCredentialsException; +use Symfony\Component\Security\Core\User\User; use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; @@ -24,6 +25,13 @@ class ApiAuthenticator extends AbstractAuthenticator { + private $selfLoadingUser = false; + + public function __construct(bool $selfLoadingUser = false) + { + $this->selfLoadingUser = $selfLoadingUser; + } + public function supports(Request $request): ?bool { return $request->headers->has('X-USER-EMAIL'); @@ -36,7 +44,12 @@ public function authenticate(Request $request): PassportInterface throw new BadCredentialsException('Email is not a valid email address.'); } - return new SelfValidatingPassport(new UserBadge($email)); + $userLoader = null; + if ($this->selfLoadingUser) { + $userLoader = function ($username) { return new User($username, 'test', ['ROLE_USER']); }; + } + + return new SelfValidatingPassport(new UserBadge($email, $userLoader)); } public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/config.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/config.yml index 5e55d065fffd6..45bde5bda3f22 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/config.yml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/config.yml @@ -15,19 +15,3 @@ services: - ['setContainer', ['@Psr\Container\ContainerInterface']] tags: [container.service_subscriber] Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator: ~ - -security: - enable_authenticator_manager: true - - encoders: - Symfony\Component\Security\Core\User\User: plaintext - - providers: - in_memory: - memory: - users: - 'jane@example.org': { password: test, roles: [ROLE_USER] } - in_memory2: - memory: - users: - 'john@example.org': { password: test, roles: [ROLE_USER] } diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/firewall_user_provider.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/firewall_user_provider.yml index 59e5e5b536e2b..4fb5ce880aacd 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/firewall_user_provider.yml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/firewall_user_provider.yml @@ -1,5 +1,6 @@ imports: - { resource: ./config.yml } +- { resource: ./security.yml } security: firewalls: @@ -7,4 +8,3 @@ security: pattern: / provider: in_memory custom_authenticator: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator - diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/implicit_user_provider.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/implicit_user_provider.yml index ce62733725055..1cb8b0c6786e7 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/implicit_user_provider.yml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/implicit_user_provider.yml @@ -1,9 +1,9 @@ imports: - { resource: ./config.yml } +- { resource: ./security.yml } security: firewalls: api: pattern: / custom_authenticator: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator - diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/no_user_provider.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/no_user_provider.yml new file mode 100644 index 0000000000000..3983d567c5572 --- /dev/null +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/no_user_provider.yml @@ -0,0 +1,14 @@ +imports: +- { resource: ./config.yml } + +services: + Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator: + - true + +security: + enable_authenticator_manager: true + + firewalls: + api: + pattern: / + custom_authenticator: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/security.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/security.yml new file mode 100644 index 0000000000000..a364148198d31 --- /dev/null +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/security.yml @@ -0,0 +1,15 @@ +security: + enable_authenticator_manager: true + + encoders: + Symfony\Component\Security\Core\User\User: plaintext + + providers: + in_memory: + memory: + users: + 'jane@example.org': { password: test, roles: [ROLE_USER] } + in_memory2: + memory: + users: + 'john@example.org': { password: test, roles: [ROLE_USER] }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: