PreAuthenticatedToken.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * PreAuthenticatedToken implements a pre-authenticated token.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class PreAuthenticatedToken extends AbstractToken
  18. {
  19. private $credentials;
  20. private $firewallName;
  21. /**
  22. * @param string|\Stringable|UserInterface $user
  23. * @param mixed $credentials
  24. * @param string[] $roles
  25. */
  26. public function __construct($user, $credentials, string $firewallName, array $roles = [])
  27. {
  28. parent::__construct($roles);
  29. if ('' === $firewallName) {
  30. throw new \InvalidArgumentException('$firewallName must not be empty.');
  31. }
  32. $this->setUser($user);
  33. $this->credentials = $credentials;
  34. $this->firewallName = $firewallName;
  35. if ($roles) {
  36. $this->setAuthenticated(true);
  37. }
  38. }
  39. /**
  40. * Returns the provider key.
  41. *
  42. * @return string The provider key
  43. *
  44. * @deprecated since 5.2, use getFirewallName() instead
  45. */
  46. public function getProviderKey()
  47. {
  48. if (1 !== \func_num_args() || true !== func_get_arg(0)) {
  49. trigger_deprecation('symfony/security-core', '5.2', 'Method "%s" is deprecated, use "getFirewallName()" instead.', __METHOD__);
  50. }
  51. return $this->firewallName;
  52. }
  53. public function getFirewallName(): string
  54. {
  55. return $this->getProviderKey(true);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getCredentials()
  61. {
  62. return $this->credentials;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function eraseCredentials()
  68. {
  69. parent::eraseCredentials();
  70. $this->credentials = null;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function __serialize(): array
  76. {
  77. return [$this->credentials, $this->firewallName, parent::__serialize()];
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function __unserialize(array $data): void
  83. {
  84. [$this->credentials, $this->firewallName, $parentData] = $data;
  85. $parentData = \is_array($parentData) ? $parentData : unserialize($parentData);
  86. parent::__unserialize($parentData);
  87. }
  88. }