vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/RememberMeListener.php line 31

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\HttpKernel\Event\GetResponseEvent;
  13. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  16. use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
  17. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  18. use Symfony\Component\Security\Http\SecurityEvents;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  21. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;
  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.         if (null === $token $this->rememberMeServices->autoLogin($request)) {
  65.             return;
  66.         }
  67.         try {
  68.             $token $this->authenticationManager->authenticate($token);
  69.             if ($request->hasSession() && $request->getSession()->isStarted()) {
  70.                 $this->sessionStrategy->onAuthentication($request$token);
  71.             }
  72.             $this->tokenStorage->setToken($token);
  73.             if (null !== $this->dispatcher) {
  74.                 $loginEvent = new InteractiveLoginEvent($request$token);
  75.                 $this->dispatcher->dispatch(SecurityEvents::INTERACTIVE_LOGIN$loginEvent);
  76.             }
  77.             if (null !== $this->logger) {
  78.                 $this->logger->debug('Populated the token storage with a remember-me token.');
  79.             }
  80.         } catch (AuthenticationException $e) {
  81.             if (null !== $this->logger) {
  82.                 $this->logger->warning(
  83.                     'The token storage was not populated with remember-me token as the'
  84.                    .' AuthenticationManager rejected the AuthenticationToken returned'
  85.                    .' by the RememberMeServices.', array('exception' => $e)
  86.                 );
  87.             }
  88.             $this->rememberMeServices->loginFail($request$e);
  89.             if (!$this->catchExceptions) {
  90.                 throw $e;
  91.             }
  92.         }
  93.     }
  94. }