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