|
| 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\Tests\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterEntryPointPass; |
| 16 | +use Symfony\Bundle\SecurityBundle\Security\FirewallConfig; |
| 17 | +use Symfony\Component\DependencyInjection\Argument\AbstractArgument; |
| 18 | +use Symfony\Component\DependencyInjection\ChildDefinition; |
| 19 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 20 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 21 | +use Symfony\Component\HttpFoundation\Request; |
| 22 | +use Symfony\Component\HttpFoundation\Response; |
| 23 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 24 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 25 | +use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
| 26 | +use Symfony\Component\Security\Http\Authentication\AuthenticatorManager; |
| 27 | +use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; |
| 28 | +use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; |
| 29 | +use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; |
| 30 | +use Symfony\Component\Security\Http\Firewall\ExceptionListener; |
| 31 | + |
| 32 | +class RegisterEntryPointsPassTest extends TestCase |
| 33 | +{ |
| 34 | + public function testProcessResolvesChildDefinitionsClass() |
| 35 | + { |
| 36 | + $container = new ContainerBuilder(); |
| 37 | + |
| 38 | + $container->setParameter('security.firewalls', ['main']); |
| 39 | + $container->setParameter('security.main._indexed_authenticators', ['custom' => 'security.authenticator.custom_authenticator.main']); |
| 40 | + |
| 41 | + $container->register('security.authenticator.manager.main', AuthenticatorManager::class); |
| 42 | + $container->register('security.exception_listener.main', ExceptionListener::class)->setArguments([ |
| 43 | + new AbstractArgument(), |
| 44 | + new AbstractArgument(), |
| 45 | + new AbstractArgument(), |
| 46 | + new AbstractArgument(), |
| 47 | + null, // entry point |
| 48 | + ]); |
| 49 | + $config = $container->register('security.firewall.map.config.main', FirewallConfig::class); |
| 50 | + $config->setArguments([ |
| 51 | + new AbstractArgument(), |
| 52 | + new AbstractArgument(), |
| 53 | + new AbstractArgument(), |
| 54 | + new AbstractArgument(), |
| 55 | + new AbstractArgument(), |
| 56 | + new AbstractArgument(), |
| 57 | + new AbstractArgument(), |
| 58 | + null, // entry point, |
| 59 | + ]); |
| 60 | + |
| 61 | + $container->register('custom_authenticator', CustomAuthenticator::class) |
| 62 | + ->setAbstract(true); |
| 63 | + |
| 64 | + $container->setDefinition('security.authenticator.custom_authenticator.main', new ChildDefinition('custom_authenticator')); |
| 65 | + |
| 66 | + (new RegisterEntryPointPass())->process($container); |
| 67 | + |
| 68 | + $this->assertSame('security.authenticator.custom_authenticator.main', $config->getArgument(7)); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class CustomAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface |
| 73 | +{ |
| 74 | + public function supports(Request $request): ?bool |
| 75 | + { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + public function authenticate(Request $request): PassportInterface |
| 80 | + { |
| 81 | + throw new BadCredentialsException(); |
| 82 | + } |
| 83 | + |
| 84 | + public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
| 85 | + { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
| 90 | + { |
| 91 | + return new JsonResponse([ |
| 92 | + 'error' => $exception->getMessageKey(), |
| 93 | + ], JsonResponse::HTTP_FORBIDDEN); |
| 94 | + } |
| 95 | + |
| 96 | + public function start(Request $request, AuthenticationException $authException = null) |
| 97 | + { |
| 98 | + } |
| 99 | +} |
0 commit comments