RememberMeToken.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Core\Authentication\Token;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. /**
  13. * Authentication Token for "Remember-Me".
  14. *
  15. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  16. */
  17. class RememberMeToken extends AbstractToken
  18. {
  19. private $secret;
  20. private $firewallName;
  21. /**
  22. * @param string $secret A secret used to make sure the token is created by the app and not by a malicious client
  23. *
  24. * @throws \InvalidArgumentException
  25. */
  26. public function __construct(UserInterface $user, string $firewallName, string $secret)
  27. {
  28. parent::__construct($user->getRoles());
  29. if (empty($secret)) {
  30. throw new \InvalidArgumentException('$secret must not be empty.');
  31. }
  32. if ('' === $firewallName) {
  33. throw new \InvalidArgumentException('$firewallName must not be empty.');
  34. }
  35. $this->firewallName = $firewallName;
  36. $this->secret = $secret;
  37. $this->setUser($user);
  38. parent::setAuthenticated(true);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function setAuthenticated(bool $authenticated)
  44. {
  45. if ($authenticated) {
  46. throw new \LogicException('You cannot set this token to authenticated after creation.');
  47. }
  48. parent::setAuthenticated(false);
  49. }
  50. /**
  51. * Returns the provider secret.
  52. *
  53. * @return string The provider secret
  54. *
  55. * @deprecated since 5.2, use getFirewallName() instead
  56. */
  57. public function getProviderKey()
  58. {
  59. if (1 !== \func_num_args() || true !== func_get_arg(0)) {
  60. trigger_deprecation('symfony/security-core', '5.2', 'Method "%s" is deprecated, use "getFirewallName()" instead.', __METHOD__);
  61. }
  62. return $this->firewallName;
  63. }
  64. public function getFirewallName(): string
  65. {
  66. return $this->getProviderKey(true);
  67. }
  68. /**
  69. * Returns the secret.
  70. *
  71. * @return string
  72. */
  73. public function getSecret()
  74. {
  75. return $this->secret;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getCredentials()
  81. {
  82. return '';
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function __serialize(): array
  88. {
  89. return [$this->secret, $this->firewallName, parent::__serialize()];
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function __unserialize(array $data): void
  95. {
  96. [$this->secret, $this->firewallName, $parentData] = $data;
  97. $parentData = \is_array($parentData) ? $parentData : unserialize($parentData);
  98. parent::__unserialize($parentData);
  99. }
  100. }