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 $providerKey;
  21.     /**
  22.      * @param string|\Stringable|UserInterface $user        The username (like a nickname, email address, etc.) or a UserInterface instance
  23.      * @param mixed                            $credentials
  24.      * @param string                           $providerKey
  25.      * @param string[]                         $roles
  26.      *
  27.      * @throws \InvalidArgumentException
  28.      */
  29.     public function __construct($user$credentialsstring $providerKey, array $roles = [])
  30.     {
  31.         parent::__construct($roles);
  32.         if (empty($providerKey)) {
  33.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  34.         }
  35.         $this->setUser($user);
  36.         $this->credentials $credentials;
  37.         $this->providerKey $providerKey;
  38.         parent::setAuthenticated(\count($roles) > 0);
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public function setAuthenticated($isAuthenticated)
  44.     {
  45.         if ($isAuthenticated) {
  46.             throw new \LogicException('Cannot set this token to trusted after instantiation.');
  47.         }
  48.         parent::setAuthenticated(false);
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function getCredentials()
  54.     {
  55.         return $this->credentials;
  56.     }
  57.     /**
  58.      * Returns the provider key.
  59.      *
  60.      * @return string The provider key
  61.      */
  62.     public function getProviderKey()
  63.     {
  64.         return $this->providerKey;
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      */
  69.     public function eraseCredentials()
  70.     {
  71.         parent::eraseCredentials();
  72.         $this->credentials null;
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public function __serialize(): array
  78.     {
  79.         return [$this->credentials$this->providerKeyparent::__serialize()];
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function __unserialize(array $data): void
  85.     {
  86.         [$this->credentials$this->providerKey$parentData] = $data;
  87.         parent::__unserialize($parentData);
  88.     }
  89. }