vendor/symfony/security/Http/Firewall/LogoutListener.php line 33

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 Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Exception\LogoutException;
  16. use Symfony\Component\Security\Csrf\CsrfToken;
  17. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  18. use Symfony\Component\Security\Http\HttpUtils;
  19. use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
  20. use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
  21. use Symfony\Component\Security\Http\ParameterBagUtils;
  22. /**
  23.  * LogoutListener logout users.
  24.  *
  25.  * @author Fabien Potencier <fabien@symfony.com>
  26.  *
  27.  * @final since Symfony 4.3
  28.  */
  29. class LogoutListener extends AbstractListener implements ListenerInterface
  30. {
  31.     use LegacyListenerTrait;
  32.     private $tokenStorage;
  33.     private $options;
  34.     private $handlers;
  35.     private $successHandler;
  36.     private $httpUtils;
  37.     private $csrfTokenManager;
  38.     /**
  39.      * @param array $options An array of options to process a logout attempt
  40.      */
  41.     public function __construct(TokenStorageInterface $tokenStorageHttpUtils $httpUtilsLogoutSuccessHandlerInterface $successHandler, array $options = [], CsrfTokenManagerInterface $csrfTokenManager null)
  42.     {
  43.         $this->tokenStorage $tokenStorage;
  44.         $this->httpUtils $httpUtils;
  45.         $this->options array_merge([
  46.             'csrf_parameter' => '_csrf_token',
  47.             'csrf_token_id' => 'logout',
  48.             'logout_path' => '/logout',
  49.         ], $options);
  50.         $this->successHandler $successHandler;
  51.         $this->csrfTokenManager $csrfTokenManager;
  52.         $this->handlers = [];
  53.     }
  54.     public function addHandler(LogoutHandlerInterface $handler)
  55.     {
  56.         $this->handlers[] = $handler;
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function supports(Request $request): ?bool
  62.     {
  63.         return $this->requiresLogout($request);
  64.     }
  65.     /**
  66.      * Performs the logout if requested.
  67.      *
  68.      * If a CsrfTokenManagerInterface instance is available, it will be used to
  69.      * validate the request.
  70.      *
  71.      * @throws LogoutException   if the CSRF token is invalid
  72.      * @throws \RuntimeException if the LogoutSuccessHandlerInterface instance does not return a response
  73.      */
  74.     public function authenticate(RequestEvent $event)
  75.     {
  76.         $request $event->getRequest();
  77.         if (null !== $this->csrfTokenManager) {
  78.             $csrfToken ParameterBagUtils::getRequestParameterValue($request$this->options['csrf_parameter']);
  79.             if (!\is_string($csrfToken) || false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['csrf_token_id'], $csrfToken))) {
  80.                 throw new LogoutException('Invalid CSRF token.');
  81.             }
  82.         }
  83.         $response $this->successHandler->onLogoutSuccess($request);
  84.         if (!$response instanceof Response) {
  85.             throw new \RuntimeException('Logout Success Handler did not return a Response.');
  86.         }
  87.         // handle multiple logout attempts gracefully
  88.         if ($token $this->tokenStorage->getToken()) {
  89.             foreach ($this->handlers as $handler) {
  90.                 $handler->logout($request$response$token);
  91.             }
  92.         }
  93.         $this->tokenStorage->setToken(null);
  94.         $event->setResponse($response);
  95.     }
  96.     /**
  97.      * Whether this request is asking for logout.
  98.      *
  99.      * The default implementation only processed requests to a specific path,
  100.      * but a subclass could change this to logout requests where
  101.      * certain parameters is present.
  102.      *
  103.      * @return bool
  104.      */
  105.     protected function requiresLogout(Request $request)
  106.     {
  107.         return isset($this->options['logout_path']) && $this->httpUtils->checkRequestPath($request$this->options['logout_path']);
  108.     }
  109. }