vendor/symfony/security/Http/Firewall/RememberMeListener.php line31

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