vendor/symfony/security-guard/Token/PostAuthenticationGuardToken.php line 25

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\Guard\Token;
  11. use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. /**
  14.  * Used as an "authenticated" token, though it could be set to not-authenticated later.
  15.  *
  16.  * If you're using Guard authentication, you *must* use a class that implements
  17.  * GuardTokenInterface as your authenticated token (like this class).
  18.  *
  19.  * @author Ryan Weaver <ryan@knpuniversity.com>
  20.  */
  21. class PostAuthenticationGuardToken extends AbstractToken implements GuardTokenInterface
  22. {
  23.     private $providerKey;
  24.     /**
  25.      * @param UserInterface $user        The user!
  26.      * @param string        $providerKey The provider (firewall) key
  27.      * @param string[]      $roles       An array of roles
  28.      *
  29.      * @throws \InvalidArgumentException
  30.      */
  31.     public function __construct(UserInterface $userstring $providerKey, array $roles)
  32.     {
  33.         parent::__construct($roles);
  34.         if (empty($providerKey)) {
  35.             throw new \InvalidArgumentException('$providerKey (i.e. firewall key) must not be empty.');
  36.         }
  37.         $this->setUser($user);
  38.         $this->providerKey $providerKey;
  39.         // this token is meant to be used after authentication success, so it is always authenticated
  40.         // you could set it as non authenticated later if you need to
  41.         $this->setAuthenticated(true);
  42.     }
  43.     /**
  44.      * This is meant to be only an authenticated token, where credentials
  45.      * have already been used and are thus cleared.
  46.      *
  47.      * {@inheritdoc}
  48.      */
  49.     public function getCredentials()
  50.     {
  51.         return [];
  52.     }
  53.     /**
  54.      * Returns the provider (firewall) key.
  55.      *
  56.      * @return string
  57.      */
  58.     public function getProviderKey()
  59.     {
  60.         return $this->providerKey;
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function __serialize(): array
  66.     {
  67.         return [$this->providerKeyparent::__serialize()];
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function __unserialize(array $data): void
  73.     {
  74.         [$this->providerKey$parentData] = $data;
  75.         parent::__unserialize($parentData);
  76.     }
  77. }