vendor/symfony/security-http/Firewall/RememberMeListener.php line 34

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\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  18. use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
  19. use Symfony\Component\Security\Http\SecurityEvents;
  20. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;
  21. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  22. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  23. /**
  24.  * RememberMeListener implements authentication capabilities via a cookie.
  25.  *
  26.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  27.  *
  28.  * @final
  29.  */
  30. class RememberMeListener extends AbstractListener
  31. {
  32.     private $tokenStorage;
  33.     private $rememberMeServices;
  34.     private $authenticationManager;
  35.     private $logger;
  36.     private $dispatcher;
  37.     private $catchExceptions true;
  38.     private $sessionStrategy;
  39.     public function __construct(TokenStorageInterface $tokenStorageRememberMeServicesInterface $rememberMeServicesAuthenticationManagerInterface $authenticationManagerLoggerInterface $logger nullEventDispatcherInterface $dispatcher nullbool $catchExceptions trueSessionAuthenticationStrategyInterface $sessionStrategy null)
  40.     {
  41.         $this->tokenStorage $tokenStorage;
  42.         $this->rememberMeServices $rememberMeServices;
  43.         $this->authenticationManager $authenticationManager;
  44.         $this->logger $logger;
  45.         $this->dispatcher $dispatcher;
  46.         $this->catchExceptions $catchExceptions;
  47.         $this->sessionStrategy null === $sessionStrategy ? new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE) : $sessionStrategy;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public function supports(Request $request): ?bool
  53.     {
  54.         return null// always run authenticate() lazily with lazy firewalls
  55.     }
  56.     /**
  57.      * Handles remember-me cookie based authentication.
  58.      */
  59.     public function authenticate(RequestEvent $event)
  60.     {
  61.         if (null !== $this->tokenStorage->getToken()) {
  62.             return;
  63.         }
  64.         $request $event->getRequest();
  65.         try {
  66.             if (null === $token $this->rememberMeServices->autoLogin($request)) {
  67.                 return;
  68.             }
  69.         } catch (AuthenticationException $e) {
  70.             if (null !== $this->logger) {
  71.                 $this->logger->warning(
  72.                     'The token storage was not populated with remember-me token as the'
  73.                    .' RememberMeServices was not able to create a token from the remember'
  74.                    .' me information.', ['exception' => $e]
  75.                 );
  76.             }
  77.             $this->rememberMeServices->loginFail($request);
  78.             if (!$this->catchExceptions) {
  79.                 throw $e;
  80.             }
  81.             return;
  82.         }
  83.         try {
  84.             $token $this->authenticationManager->authenticate($token);
  85.             if ($request->hasSession() && $request->getSession()->isStarted()) {
  86.                 $this->sessionStrategy->onAuthentication($request$token);
  87.             }
  88.             $this->tokenStorage->setToken($token);
  89.             if (null !== $this->dispatcher) {
  90.                 $loginEvent = new InteractiveLoginEvent($request$token);
  91.                 $this->dispatcher->dispatch($loginEventSecurityEvents::INTERACTIVE_LOGIN);
  92.             }
  93.             if (null !== $this->logger) {
  94.                 $this->logger->debug('Populated the token storage with a remember-me token.');
  95.             }
  96.         } catch (AuthenticationException $e) {
  97.             if (null !== $this->logger) {
  98.                 $this->logger->warning(
  99.                     'The token storage was not populated with remember-me token as the'
  100.                    .' AuthenticationManager rejected the AuthenticationToken returned'
  101.                    .' by the RememberMeServices.', ['exception' => $e]
  102.                 );
  103.             }
  104.             $this->rememberMeServices->loginFail($request$e);
  105.             if (!$this->catchExceptions) {
  106.                 throw $e;
  107.             }
  108.         }
  109.     }
  110. }