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