PostAuthenticationToken.php 1.8 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\Http\Authenticator\Token;
  11. use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. class PostAuthenticationToken extends AbstractToken
  14. {
  15. private $firewallName;
  16. /**
  17. * @param string[] $roles An array of roles
  18. *
  19. * @throws \InvalidArgumentException
  20. */
  21. public function __construct(UserInterface $user, string $firewallName, array $roles)
  22. {
  23. parent::__construct($roles);
  24. if ('' === $firewallName) {
  25. throw new \InvalidArgumentException('$firewallName must not be empty.');
  26. }
  27. $this->setUser($user);
  28. $this->firewallName = $firewallName;
  29. // this token is meant to be used after authentication success, so it is always authenticated
  30. // you could set it as non authenticated later if you need to
  31. $this->setAuthenticated(true);
  32. }
  33. /**
  34. * This is meant to be only an authenticated token, where credentials
  35. * have already been used and are thus cleared.
  36. *
  37. * {@inheritdoc}
  38. */
  39. public function getCredentials()
  40. {
  41. return [];
  42. }
  43. public function getFirewallName(): string
  44. {
  45. return $this->firewallName;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function __serialize(): array
  51. {
  52. return [$this->firewallName, parent::__serialize()];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function __unserialize(array $data): void
  58. {
  59. [$this->firewallName, $parentData] = $data;
  60. parent::__unserialize($parentData);
  61. }
  62. }