AnonymousToken.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. * AnonymousToken represents an anonymous token.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class AnonymousToken extends AbstractToken
  18. {
  19. private $secret;
  20. /**
  21. * @param string $secret A secret used to make sure the token is created by the app and not by a malicious client
  22. * @param string|\Stringable|UserInterface $user
  23. * @param string[] $roles
  24. */
  25. public function __construct(string $secret, $user, array $roles = [])
  26. {
  27. parent::__construct($roles);
  28. $this->secret = $secret;
  29. $this->setUser($user);
  30. $this->setAuthenticated(true);
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getCredentials()
  36. {
  37. return '';
  38. }
  39. /**
  40. * Returns the secret.
  41. *
  42. * @return string
  43. */
  44. public function getSecret()
  45. {
  46. return $this->secret;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function __serialize(): array
  52. {
  53. return [$this->secret, parent::__serialize()];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function __unserialize(array $data): void
  59. {
  60. [$this->secret, $parentData] = $data;
  61. $parentData = \is_array($parentData) ? $parentData : unserialize($parentData);
  62. parent::__unserialize($parentData);
  63. }
  64. }