vendor/symfony/security-http/EventListener/PasswordMigratingListener.php line 37

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\Http\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  13. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
  17. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  18. /**
  19.  * @author Wouter de Jong <wouter@wouterj.nl>
  20.  *
  21.  * @final
  22.  * @experimental in 5.2
  23.  */
  24. class PasswordMigratingListener implements EventSubscriberInterface
  25. {
  26.     private $encoderFactory;
  27.     public function __construct(EncoderFactoryInterface $encoderFactory)
  28.     {
  29.         $this->encoderFactory $encoderFactory;
  30.     }
  31.     public function onLoginSuccess(LoginSuccessEvent $event): void
  32.     {
  33.         $passport $event->getPassport();
  34.         if (!$passport instanceof UserPassportInterface || !$passport->hasBadge(PasswordUpgradeBadge::class)) {
  35.             return;
  36.         }
  37.         /** @var PasswordUpgradeBadge $badge */
  38.         $badge $passport->getBadge(PasswordUpgradeBadge::class);
  39.         $plaintextPassword $badge->getAndErasePlaintextPassword();
  40.         if ('' === $plaintextPassword) {
  41.             return;
  42.         }
  43.         $user $passport->getUser();
  44.         if (null === $user->getPassword()) {
  45.             return;
  46.         }
  47.         $passwordEncoder $this->encoderFactory->getEncoder($user);
  48.         if (!$passwordEncoder->needsRehash($user->getPassword())) {
  49.             return;
  50.         }
  51.         $passwordUpgrader $badge->getPasswordUpgrader();
  52.         if (null === $passwordUpgrader) {
  53.             if (!$passport->hasBadge(UserBadge::class)) {
  54.                 return;
  55.             }
  56.             /** @var UserBadge $userBadge */
  57.             $userBadge $passport->getBadge(UserBadge::class);
  58.             $userLoader $userBadge->getUserLoader();
  59.             if (\is_array($userLoader) && $userLoader[0] instanceof PasswordUpgraderInterface) {
  60.                 $passwordUpgrader $userLoader[0];
  61.             } else {
  62.                 return;
  63.             }
  64.         }
  65.         $passwordUpgrader->upgradePassword($user$passwordEncoder->encodePassword($plaintextPassword$user->getSalt()));
  66.     }
  67.     public static function getSubscribedEvents(): array
  68.     {
  69.         return [LoginSuccessEvent::class => 'onLoginSuccess'];
  70.     }
  71. }