vendor/symfony/security-core/Authentication/Token/UsernamePasswordToken.php line 21

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\Core\Authentication\Token;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. /**
  13. * UsernamePasswordToken implements a username and password token.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class UsernamePasswordToken extends AbstractToken
  18. {
  19. private $credentials;
  20. private $firewallName;
  21. /**
  22. * @param UserInterface $user
  23. * @param string[] $roles
  24. *
  25. * @throws \InvalidArgumentException
  26. */
  27. public function __construct($user, /* string */ $firewallName, /* array */ $roles = [])
  28. {
  29. if (\is_string($roles)) {
  30. trigger_deprecation('symfony/security-core', '5.4', 'The $credentials argument of "%s" is deprecated.', static::class.'::__construct');
  31. $credentials = $firewallName;
  32. $firewallName = $roles;
  33. $roles = \func_num_args() > 3 ? func_get_arg(3) : [];
  34. }
  35. parent::__construct($roles);
  36. if ('' === $firewallName) {
  37. throw new \InvalidArgumentException('$firewallName must not be empty.');
  38. }
  39. $this->setUser($user);
  40. $this->credentials = $credentials ?? null;
  41. $this->firewallName = $firewallName;
  42. parent::setAuthenticated(\count($roles) > 0, false);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function setAuthenticated(bool $isAuthenticated)
  48. {
  49. if ($isAuthenticated) {
  50. throw new \LogicException('Cannot set this token to trusted after instantiation.');
  51. }
  52. parent::setAuthenticated(false, false);
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getCredentials()
  58. {
  59. trigger_deprecation('symfony/security-core', '5.4', 'Method "%s" is deprecated.', __METHOD__);
  60. return $this->credentials;
  61. }
  62. /**
  63. * Returns the provider key.
  64. *
  65. * @return string The provider key
  66. *
  67. * @deprecated since Symfony 5.2, use getFirewallName() instead
  68. */
  69. public function getProviderKey()
  70. {
  71. if (1 !== \func_num_args() || true !== func_get_arg(0)) {
  72. trigger_deprecation('symfony/security-core', '5.2', 'Method "%s" is deprecated, use "getFirewallName()" instead.', __METHOD__);
  73. }
  74. return $this->firewallName;
  75. }
  76. public function getFirewallName(): string
  77. {
  78. return $this->getProviderKey(true);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function eraseCredentials()
  84. {
  85. parent::eraseCredentials();
  86. $this->credentials = null;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function __serialize(): array
  92. {
  93. return [$this->credentials, $this->firewallName, parent::__serialize()];
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function __unserialize(array $data): void
  99. {
  100. [$this->credentials, $this->firewallName, $parentData] = $data;
  101. $parentData = \is_array($parentData) ? $parentData : unserialize($parentData);
  102. parent::__unserialize($parentData);
  103. }
  104. }