CsrfTokenBadge.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Authenticator\Passport\Badge;
  11. use Symfony\Component\Security\Http\EventListener\CsrfProtectionListener;
  12. /**
  13. * Adds automatic CSRF tokens checking capabilities to this authenticator.
  14. *
  15. * @see CsrfProtectionListener
  16. *
  17. * @author Wouter de Jong <wouter@wouterj.nl>
  18. *
  19. * @final
  20. * @experimental in 5.2
  21. */
  22. class CsrfTokenBadge implements BadgeInterface
  23. {
  24. private $resolved = false;
  25. private $csrfTokenId;
  26. private $csrfToken;
  27. /**
  28. * @param string $csrfTokenId An arbitrary string used to generate the value of the CSRF token.
  29. * Using a different string for each authenticator improves its security.
  30. * @param string|null $csrfToken The CSRF token presented in the request, if any
  31. */
  32. public function __construct(string $csrfTokenId, ?string $csrfToken)
  33. {
  34. $this->csrfTokenId = $csrfTokenId;
  35. $this->csrfToken = $csrfToken;
  36. }
  37. public function getCsrfTokenId(): string
  38. {
  39. return $this->csrfTokenId;
  40. }
  41. public function getCsrfToken(): ?string
  42. {
  43. return $this->csrfToken;
  44. }
  45. /**
  46. * @internal
  47. */
  48. public function markResolved(): void
  49. {
  50. $this->resolved = true;
  51. }
  52. public function isResolved(): bool
  53. {
  54. return $this->resolved;
  55. }
  56. }