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\EventDispatcher\LegacyEventDispatcherProxy;
  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 since Symfony 4.3
  29.  */
  30. class RememberMeListener implements ListenerInterface
  31. {
  32.     use LegacyListenerTrait;
  33.     private $tokenStorage;
  34.     private $rememberMeServices;
  35.     private $authenticationManager;
  36.     private $logger;
  37.     private $dispatcher;
  38.     private $catchExceptions true;
  39.     private $sessionStrategy;
  40.     public function __construct(TokenStorageInterface $tokenStorageRememberMeServicesInterface $rememberMeServicesAuthenticationManagerInterface $authenticationManagerLoggerInterface $logger nullEventDispatcherInterface $dispatcher nullbool $catchExceptions trueSessionAuthenticationStrategyInterface $sessionStrategy null)
  41.     {
  42.         $this->tokenStorage $tokenStorage;
  43.         $this->rememberMeServices $rememberMeServices;
  44.         $this->authenticationManager $authenticationManager;
  45.         $this->logger $logger;
  46.         $this->dispatcher LegacyEventDispatcherProxy::decorate($dispatcher);
  47.         $this->catchExceptions $catchExceptions;
  48.         $this->sessionStrategy null === $sessionStrategy ? new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE) : $sessionStrategy;
  49.     }
  50.     /**
  51.      * Handles remember-me cookie based authentication.
  52.      */
  53.     public function __invoke(RequestEvent $event)
  54.     {
  55.         if (null !== $this->tokenStorage->getToken()) {
  56.             return;
  57.         }
  58.         $request $event->getRequest();
  59.         try {
  60.             if (null === $token $this->rememberMeServices->autoLogin($request)) {
  61.                 return;
  62.             }
  63.         } catch (AuthenticationException $e) {
  64.             if (null !== $this->logger) {
  65.                 $this->logger->warning(
  66.                     'The token storage was not populated with remember-me token as the'
  67.                    .' RememberMeServices was not able to create a token from the remember'
  68.                    .' me information.', ['exception' => $e]
  69.                 );
  70.             }
  71.             $this->rememberMeServices->loginFail($request);
  72.             if (!$this->catchExceptions) {
  73.                 throw $e;
  74.             }
  75.             return;
  76.         }
  77.         try {
  78.             $token $this->authenticationManager->authenticate($token);
  79.             if ($request->hasSession() && $request->getSession()->isStarted()) {
  80.                 $this->sessionStrategy->onAuthentication($request$token);
  81.             }
  82.             $this->tokenStorage->setToken($token);
  83.             if (null !== $this->dispatcher) {
  84.                 $loginEvent = new InteractiveLoginEvent($request$token);
  85.                 $this->dispatcher->dispatch($loginEventSecurityEvents::INTERACTIVE_LOGIN);
  86.             }
  87.             if (null !== $this->logger) {
  88.                 $this->logger->debug('Populated the token storage with a remember-me token.');
  89.             }
  90.         } catch (AuthenticationException $e) {
  91.             if (null !== $this->logger) {
  92.                 $this->logger->warning(
  93.                     'The token storage was not populated with remember-me token as the'
  94.                    .' AuthenticationManager rejected the AuthenticationToken returned'
  95.                    .' by the RememberMeServices.', ['exception' => $e]
  96.                 );
  97.             }
  98.             $this->rememberMeServices->loginFail($request$e);
  99.             if (!$this->catchExceptions) {
  100.                 throw $e;
  101.             }
  102.         }
  103.     }
  104. }