vendor/symfony/security-http/Firewall/RememberMeListener.php line 34

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