src/AdminBundle/Security/EventListener/CheckCredentialsListener.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\AdminBundle\Security\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
  5. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  6. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  7. use Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
  10. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
  11. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
  13. use Symfony\Component\Security\Http\Event\CheckPassportEvent;
  14. /**
  15.  * This listeners uses the interfaces of authenticators to
  16.  * determine how to check credentials.
  17.  */
  18. class CheckCredentialsListener implements EventSubscriberInterface
  19. {
  20.     private $hasherFactory;
  21.     /**
  22.      * @param PasswordHasherFactoryInterface $hasherFactory
  23.      */
  24.     public function __construct($hasherFactory)
  25.     {
  26.         if ($hasherFactory instanceof EncoderFactoryInterface) {
  27.             trigger_deprecation('symfony/security-core''5.3',
  28.                 'Passing a "%s" instance to the "%s" constructor is deprecated, use "%s" instead.',
  29.                 EncoderFactoryInterface::class, __CLASS__,
  30.                 PasswordHasherFactoryInterface::class);
  31.         }
  32.         $this->hasherFactory $hasherFactory;
  33.     }
  34.     public function checkPassport(CheckPassportEvent $event): void
  35.     {
  36.         $passport $event->getPassport();
  37.         if ($passport instanceof UserPassportInterface && $passport->hasBadge(PasswordCredentials::class)) {
  38.             // Use the password hasher to validate the credentials
  39.             $user $passport->getUser();
  40.             if (!$user instanceof PasswordAuthenticatedUserInterface) {
  41.                 trigger_deprecation('symfony/security-http''5.3',
  42.                     'Not implementing the "%s" interface in class "%s" while using password-based authentication is deprecated.',
  43.                     PasswordAuthenticatedUserInterface::class, get_debug_type($user));
  44.             }
  45.             /** @var PasswordCredentials $badge */
  46.             $badge $passport->getBadge(PasswordCredentials::class);
  47.             if ($badge->isResolved()) {
  48.                 return;
  49.             }
  50.             $presentedPassword $badge->getPassword();
  51.             if ('' === $presentedPassword) {
  52.                 throw new BadCredentialsException('CCL____404 - User ' $user->getId() . ': the presented password cannot be empty.');
  53.             }
  54.             if (null === $user->getPassword()) {
  55.                 throw new BadCredentialsException('CCL____405 - User ' $user->getId() . ': the presented password is invalid.');
  56.             }
  57.             $salt method_exists($user'getSalt') ? $user->getSalt() : '';
  58.             if ($salt && !$user instanceof LegacyPasswordAuthenticatedUserInterface) {
  59.                 trigger_deprecation('symfony/security-http''5.3',
  60.                     'Returning a string from "getSalt()" without implementing the "%s" interface is deprecated, the "%s" class should implement it.',
  61.                     LegacyPasswordAuthenticatedUserInterface::class, get_debug_type($user));
  62.             }
  63.             // @deprecated since Symfony 5.3
  64.             if ($this->hasherFactory instanceof EncoderFactoryInterface) {
  65.                 if (!$this->hasherFactory->getEncoder($user)->isPasswordValid($user->getPassword(),
  66.                         $presentedPassword$salt)) {
  67.                     throw new BadCredentialsException('CCL____405 - User ' $user->getId() . ': the presented password is invalid.');
  68.                 }
  69.             } else {
  70.                 if (!$this->hasherFactory->getPasswordHasher($user)->verify($user->getPassword(),
  71.                         $presentedPassword$salt)) {
  72.                     throw new BadCredentialsException('CCL____405 - User ' $user->getId() . ': the presented password is invalid.');
  73.                 }
  74.             }
  75.             $badge->markResolved();
  76.             if (!$passport->hasBadge(PasswordUpgradeBadge::class)) {
  77.                 $passport->addBadge(new PasswordUpgradeBadge($presentedPassword));
  78.             }
  79.         }
  80.         if ($passport->hasBadge(CustomCredentials::class)) {
  81.             
  82.             $badge $passport->getBadge(CustomCredentials::class);
  83.             if ($badge->isResolved()) {
  84.                 return;
  85.             }
  86.             $badge->executeCustomChecker($passport->getUser());
  87.         }
  88.     }
  89.     public static function getSubscribedEvents(): array
  90.     {
  91.         return [CheckPassportEvent::class => 'checkPassport'];
  92.     }
  93. }