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 $firewallName;
  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 $firewallName, array $roles = [])
  29.     {
  30.         parent::__construct($roles);
  31.         if ('' === $firewallName) {
  32.             throw new \InvalidArgumentException('$firewallName must not be empty.');
  33.         }
  34.         $this->setUser($user);
  35.         $this->credentials $credentials;
  36.         $this->firewallName $firewallName;
  37.         parent::setAuthenticated(\count($roles) > 0);
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function setAuthenticated(bool $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.      * @deprecated since 5.2, use getFirewallName() instead
  62.      */
  63.     public function getProviderKey()
  64.     {
  65.         if (!== \func_num_args() || true !== func_get_arg(0)) {
  66.             trigger_deprecation('symfony/security-core''5.2''Method "%s" is deprecated, use "getFirewallName()" instead.'__METHOD__);
  67.         }
  68.         return $this->firewallName;
  69.     }
  70.     public function getFirewallName(): string
  71.     {
  72.         return $this->getProviderKey(true);
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public function eraseCredentials()
  78.     {
  79.         parent::eraseCredentials();
  80.         $this->credentials null;
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function __serialize(): array
  86.     {
  87.         return [$this->credentials$this->firewallNameparent::__serialize()];
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function __unserialize(array $data): void
  93.     {
  94.         [$this->credentials$this->firewallName$parentData] = $data;
  95.         $parentData = \is_array($parentData) ? $parentData unserialize($parentData);
  96.         parent::__unserialize($parentData);
  97.     }
  98. }