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

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\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 $requestStack, string $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. /**
  50. * @return void
  51. */
  52. public function setToken(string $tokenId, #[\SensitiveParameter] string $token)
  53. {
  54. $session = $this->getSession();
  55. if (!$session->isStarted()) {
  56. $session->start();
  57. }
  58. $session->set($this->namespace.'/'.$tokenId, $token);
  59. }
  60. public function hasToken(string $tokenId): bool
  61. {
  62. $session = $this->getSession();
  63. if (!$session->isStarted()) {
  64. $session->start();
  65. }
  66. return $session->has($this->namespace.'/'.$tokenId);
  67. }
  68. public function removeToken(string $tokenId): ?string
  69. {
  70. $session = $this->getSession();
  71. if (!$session->isStarted()) {
  72. $session->start();
  73. }
  74. return $session->remove($this->namespace.'/'.$tokenId);
  75. }
  76. /**
  77. * @return void
  78. */
  79. public function clear()
  80. {
  81. $session = $this->getSession();
  82. foreach (array_keys($session->all()) as $key) {
  83. if (str_starts_with($key, $this->namespace.'/')) {
  84. $session->remove($key);
  85. }
  86. }
  87. }
  88. /**
  89. * @throws SessionNotFoundException
  90. */
  91. private function getSession(): SessionInterface
  92. {
  93. return $this->requestStack->getSession();
  94. }
  95. }