vendor/symfony/password-hasher/Hasher/UserPasswordHasher.php line 99

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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\PasswordHasher\Hasher;
  11. use Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. /**
  15.  * Hashes passwords based on the user and the PasswordHasherFactory.
  16.  *
  17.  * @author Ariel Ferrandini <[email protected]>
  18.  *
  19.  * @final
  20.  */
  21. class UserPasswordHasher implements UserPasswordHasherInterface
  22. {
  23.     private $hasherFactory;
  24.     public function __construct(PasswordHasherFactoryInterface $hasherFactory)
  25.     {
  26.         $this->hasherFactory $hasherFactory;
  27.     }
  28.     /**
  29.      * @param PasswordAuthenticatedUserInterface $user
  30.      */
  31.     public function hashPassword($userstring $plainPassword): string
  32.     {
  33.         if (!$user instanceof PasswordAuthenticatedUserInterface) {
  34.             if (!$user instanceof UserInterface) {
  35.                 throw new \TypeError(sprintf('Expected an instance of "%s" as first argument, but got "%s".'UserInterface::class, get_debug_type($user)));
  36.             }
  37.             trigger_deprecation('symfony/password-hasher''5.3''The "%s()" method expects a "%s" instance as first argument. Not implementing it in class "%s" is deprecated.'__METHOD__PasswordAuthenticatedUserInterface::class, get_debug_type($user));
  38.         }
  39.         $salt null;
  40.         if ($user instanceof LegacyPasswordAuthenticatedUserInterface) {
  41.             $salt $user->getSalt();
  42.         } elseif ($user instanceof UserInterface) {
  43.             $salt method_exists($user'getSalt') ? $user->getSalt() : null;
  44.             if ($salt) {
  45.                 trigger_deprecation('symfony/password-hasher''5.3''Returning a string from "getSalt()" without implementing the "%s" interface is deprecated, the "%s" class should implement it.'LegacyPasswordAuthenticatedUserInterface::class, get_debug_type($user));
  46.             }
  47.         }
  48.         $hasher $this->hasherFactory->getPasswordHasher($user);
  49.         return $hasher->hash($plainPassword$salt);
  50.     }
  51.     /**
  52.      * @param PasswordAuthenticatedUserInterface $user
  53.      */
  54.     public function isPasswordValid($userstring $plainPassword): bool
  55.     {
  56.         if (!$user instanceof PasswordAuthenticatedUserInterface) {
  57.             if (!$user instanceof UserInterface) {
  58.                 throw new \TypeError(sprintf('Expected an instance of "%s" as first argument, but got "%s".'UserInterface::class, get_debug_type($user)));
  59.             }
  60.             trigger_deprecation('symfony/password-hasher''5.3''The "%s()" method expects a "%s" instance as first argument. Not implementing it in class "%s" is deprecated.'__METHOD__PasswordAuthenticatedUserInterface::class, get_debug_type($user));
  61.         }
  62.         $salt null;
  63.         if ($user instanceof LegacyPasswordAuthenticatedUserInterface) {
  64.             $salt $user->getSalt();
  65.         } elseif ($user instanceof UserInterface) {
  66.             $salt $user->getSalt();
  67.             if (null !== $salt) {
  68.                 trigger_deprecation('symfony/password-hasher''5.3''Returning a string from "getSalt()" without implementing the "%s" interface is deprecated, the "%s" class should implement it.'LegacyPasswordAuthenticatedUserInterface::class, get_debug_type($user));
  69.             }
  70.         }
  71.         if (null === $user->getPassword()) {
  72.             return false;
  73.         }
  74.         $hasher $this->hasherFactory->getPasswordHasher($user);
  75.         return $hasher->verify($user->getPassword(), $plainPassword$salt);
  76.     }
  77.     /**
  78.      * @param PasswordAuthenticatedUserInterface $user
  79.      */
  80.     public function needsRehash($user): bool
  81.     {
  82.         if (null === $user->getPassword()) {
  83.             return false;
  84.         }
  85.         if (!$user instanceof PasswordAuthenticatedUserInterface) {
  86.             if (!$user instanceof UserInterface) {
  87.                 throw new \TypeError(sprintf('Expected an instance of "%s" as first argument, but got "%s".'UserInterface::class, get_debug_type($user)));
  88.             }
  89.             trigger_deprecation('symfony/password-hasher''5.3''The "%s()" method expects a "%s" instance as first argument. Not implementing it in class "%s" is deprecated.'__METHOD__PasswordAuthenticatedUserInterface::class, get_debug_type($user));
  90.         }
  91.         $hasher $this->hasherFactory->getPasswordHasher($user);
  92.         return $hasher->needsRehash($user->getPassword());
  93.     }
  94. }