vendor/symfony/security/Http/Firewall/SimplePreAuthenticationListener.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Http\Firewall;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  18. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
  19. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  21. use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  24. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  25. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  26. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  27. use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
  28. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  29. use Symfony\Component\Security\Http\SecurityEvents;
  30. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  31. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use Guard instead.'SimplePreAuthenticationListener::class), \E_USER_DEPRECATED);
  32. /**
  33.  * SimplePreAuthenticationListener implements simple proxying to an authenticator.
  34.  *
  35.  * @author Jordi Boggiano <j.boggiano@seld.be>
  36.  *
  37.  * @deprecated since Symfony 4.2, use Guard instead.
  38.  */
  39. class SimplePreAuthenticationListener extends AbstractListener implements ListenerInterface
  40. {
  41.     use LegacyListenerTrait;
  42.     private $tokenStorage;
  43.     private $authenticationManager;
  44.     private $providerKey;
  45.     private $simpleAuthenticator;
  46.     private $logger;
  47.     private $dispatcher;
  48.     private $sessionStrategy;
  49.     private $trustResolver;
  50.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerstring $providerKeySimplePreAuthenticatorInterface $simpleAuthenticatorLoggerInterface $logger nullEventDispatcherInterface $dispatcher nullAuthenticationTrustResolverInterface $trustResolver null)
  51.     {
  52.         if (empty($providerKey)) {
  53.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  54.         }
  55.         $this->tokenStorage $tokenStorage;
  56.         $this->authenticationManager $authenticationManager;
  57.         $this->providerKey $providerKey;
  58.         $this->simpleAuthenticator $simpleAuthenticator;
  59.         $this->logger $logger;
  60.         if (null !== $dispatcher && class_exists(LegacyEventDispatcherProxy::class)) {
  61.             $this->dispatcher LegacyEventDispatcherProxy::decorate($dispatcher);
  62.         } else {
  63.             $this->dispatcher $dispatcher;
  64.         }
  65.         $this->trustResolver $trustResolver ?? new AuthenticationTrustResolver(AnonymousToken::class, RememberMeToken::class);
  66.     }
  67.     /**
  68.      * Call this method if your authentication token is stored to a session.
  69.      *
  70.      * @final
  71.      */
  72.     public function setSessionAuthenticationStrategy(SessionAuthenticationStrategyInterface $sessionStrategy)
  73.     {
  74.         $this->sessionStrategy $sessionStrategy;
  75.     }
  76.     public function supports(Request $request): ?bool
  77.     {
  78.         if ((null !== $token $this->tokenStorage->getToken()) && !$this->trustResolver->isAnonymous($token)) {
  79.             return false;
  80.         }
  81.         $token $this->simpleAuthenticator->createToken($request$this->providerKey);
  82.         // allow null to be returned to skip authentication
  83.         if (null === $token) {
  84.             return false;
  85.         }
  86.         $request->attributes->set('_simple_pre_authenticator_token'$token);
  87.         return true;
  88.     }
  89.     /**
  90.      * Handles basic authentication.
  91.      */
  92.     public function authenticate(RequestEvent $event)
  93.     {
  94.         $request $event->getRequest();
  95.         if (null !== $this->logger) {
  96.             $this->logger->info('Attempting SimplePreAuthentication.', ['key' => $this->providerKey'authenticator' => \get_class($this->simpleAuthenticator)]);
  97.         }
  98.         if ((null !== $token $this->tokenStorage->getToken()) && !$this->trustResolver->isAnonymous($token)) {
  99.             $request->attributes->remove('_simple_pre_authenticator_token');
  100.             return;
  101.         }
  102.         try {
  103.             $token $request->attributes->get('_simple_pre_authenticator_token');
  104.             $request->attributes->remove('_simple_pre_authenticator_token');
  105.             $token $this->authenticationManager->authenticate($token);
  106.             $this->migrateSession($request$token);
  107.             $this->tokenStorage->setToken($token);
  108.             if (null !== $this->dispatcher) {
  109.                 $loginEvent = new InteractiveLoginEvent($request$token);
  110.                 $this->dispatcher->dispatch($loginEventSecurityEvents::INTERACTIVE_LOGIN);
  111.             }
  112.         } catch (AuthenticationException $e) {
  113.             $this->tokenStorage->setToken(null);
  114.             if (null !== $this->logger) {
  115.                 $this->logger->info('SimplePreAuthentication request failed.', ['exception' => $e'authenticator' => \get_class($this->simpleAuthenticator)]);
  116.             }
  117.             if ($this->simpleAuthenticator instanceof AuthenticationFailureHandlerInterface) {
  118.                 $response $this->simpleAuthenticator->onAuthenticationFailure($request$e);
  119.                 if ($response instanceof Response) {
  120.                     $event->setResponse($response);
  121.                 } elseif (null !== $response) {
  122.                     throw new \UnexpectedValueException(sprintf('The "%s::onAuthenticationFailure()" method must return null or a Response object.', \get_class($this->simpleAuthenticator)));
  123.                 }
  124.             }
  125.             return;
  126.         }
  127.         if ($this->simpleAuthenticator instanceof AuthenticationSuccessHandlerInterface) {
  128.             $response $this->simpleAuthenticator->onAuthenticationSuccess($request$token);
  129.             if ($response instanceof Response) {
  130.                 $event->setResponse($response);
  131.             } elseif (null !== $response) {
  132.                 throw new \UnexpectedValueException(sprintf('The "%s::onAuthenticationSuccess()" method must return null or a Response object.', \get_class($this->simpleAuthenticator)));
  133.             }
  134.         }
  135.     }
  136.     private function migrateSession(Request $requestTokenInterface $token)
  137.     {
  138.         if (!$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession()) {
  139.             return;
  140.         }
  141.         $this->sessionStrategy->onAuthentication($request$token);
  142.     }
  143. }