vendor/symfony/security-http/EventListener/CheckCredentialsListener.php line 41

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\Exception\BadCredentialsException;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
  18. use Symfony\Component\Security\Http\Event\CheckPassportEvent;
  19. /**
  20.  * This listeners uses the interfaces of authenticators to
  21.  * determine how to check credentials.
  22.  *
  23.  * @author Wouter de Jong <wouter@driveamber.com>
  24.  *
  25.  * @final
  26.  * @experimental in 5.2
  27.  */
  28. class CheckCredentialsListener implements EventSubscriberInterface
  29. {
  30.     private $encoderFactory;
  31.     public function __construct(EncoderFactoryInterface $encoderFactory)
  32.     {
  33.         $this->encoderFactory $encoderFactory;
  34.     }
  35.     public function checkPassport(CheckPassportEvent $event): void
  36.     {
  37.         $passport $event->getPassport();
  38.         if ($passport instanceof UserPassportInterface && $passport->hasBadge(PasswordCredentials::class)) {
  39.             // Use the password encoder to validate the credentials
  40.             $user $passport->getUser();
  41.             /** @var PasswordCredentials $badge */
  42.             $badge $passport->getBadge(PasswordCredentials::class);
  43.             if ($badge->isResolved()) {
  44.                 return;
  45.             }
  46.             $presentedPassword $badge->getPassword();
  47.             if ('' === $presentedPassword) {
  48.                 throw new BadCredentialsException('The presented password cannot be empty.');
  49.             }
  50.             if (null === $user->getPassword()) {
  51.                 throw new BadCredentialsException('The presented password is invalid.');
  52.             }
  53.             if (!$this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword$user->getSalt())) {
  54.                 throw new BadCredentialsException('The presented password is invalid.');
  55.             }
  56.             $badge->markResolved();
  57.             if (!$passport->hasBadge(PasswordUpgradeBadge::class)) {
  58.                 $passport->addBadge(new PasswordUpgradeBadge($presentedPassword));
  59.             }
  60.             return;
  61.         }
  62.         if ($passport->hasBadge(CustomCredentials::class)) {
  63.             /** @var CustomCredentials $badge */
  64.             $badge $passport->getBadge(CustomCredentials::class);
  65.             if ($badge->isResolved()) {
  66.                 return;
  67.             }
  68.             $badge->executeCustomChecker($passport->getUser());
  69.             return;
  70.         }
  71.     }
  72.     public static function getSubscribedEvents(): array
  73.     {
  74.         return [CheckPassportEvent::class => 'checkPassport'];
  75.     }
  76. }