SessionTokenStorage.php 2.7 KB

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