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