vendor/symfony/security-csrf/TokenStorage/SessionTokenStorage.php line 73

  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\Csrf\TokenStorage;
  11. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Symfony\Component\Security\Csrf\Exception\TokenNotFoundException;
  15. /**
  16.  * Token storage that uses a Symfony Session object.
  17.  *
  18.  * @author Bernhard Schussek <bschussek@gmail.com>
  19.  */
  20. class SessionTokenStorage implements ClearableTokenStorageInterface
  21. {
  22.     /**
  23.      * The namespace used to store values in the session.
  24.      */
  25.     public const SESSION_NAMESPACE '_csrf';
  26.     private RequestStack $requestStack;
  27.     private string $namespace;
  28.     /**
  29.      * Initializes the storage with a RequestStack object and a session namespace.
  30.      *
  31.      * @param string $namespace The namespace under which the token is stored in the requestStack
  32.      */
  33.     public function __construct(RequestStack $requestStackstring $namespace self::SESSION_NAMESPACE)
  34.     {
  35.         $this->requestStack $requestStack;
  36.         $this->namespace $namespace;
  37.     }
  38.     public function getToken(string $tokenId): string
  39.     {
  40.         $session $this->getSession();
  41.         if (!$session->isStarted()) {
  42.             $session->start();
  43.         }
  44.         if (!$session->has($this->namespace.'/'.$tokenId)) {
  45.             throw new TokenNotFoundException('The CSRF token with ID '.$tokenId.' does not exist.');
  46.         }
  47.         return (string) $session->get($this->namespace.'/'.$tokenId);
  48.     }
  49.     public function setToken(string $tokenId, #[\SensitiveParameterstring $token): void
  50.     {
  51.         $session $this->getSession();
  52.         if (!$session->isStarted()) {
  53.             $session->start();
  54.         }
  55.         $session->set($this->namespace.'/'.$tokenId$token);
  56.     }
  57.     public function hasToken(string $tokenId): bool
  58.     {
  59.         $session $this->getSession();
  60.         if (!$session->isStarted()) {
  61.             $session->start();
  62.         }
  63.         return $session->has($this->namespace.'/'.$tokenId);
  64.     }
  65.     public function removeToken(string $tokenId): ?string
  66.     {
  67.         $session $this->getSession();
  68.         if (!$session->isStarted()) {
  69.             $session->start();
  70.         }
  71.         return $session->remove($this->namespace.'/'.$tokenId);
  72.     }
  73.     public function clear(): void
  74.     {
  75.         $session $this->getSession();
  76.         foreach (array_keys($session->all()) as $key) {
  77.             if (str_starts_with($key$this->namespace.'/')) {
  78.                 $session->remove($key);
  79.             }
  80.         }
  81.     }
  82.     /**
  83.      * @throws SessionNotFoundException
  84.      */
  85.     private function getSession(): SessionInterface
  86.     {
  87.         return $this->requestStack->getSession();
  88.     }
  89. }