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