vendor/scheb/2fa-bundle/Security/TwoFactor/Event/AuthenticationSuccessEventSuppressor.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Scheb\TwoFactorBundle\Security\TwoFactor\Event;
  4. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  5. use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderPreparationListener;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Security\Core\AuthenticationEvents;
  8. use Symfony\Component\Security\Core\Event\AuthenticationEvent;
  9. /**
  10.  * @final
  11.  */
  12. class AuthenticationSuccessEventSuppressor implements EventSubscriberInterface
  13. {
  14.     // Must trigger after TwoFactorProviderPreparationListener::onLogin to stop event propagation immediately
  15.     public const LISTENER_PRIORITY TwoFactorProviderPreparationListener::AUTHENTICATION_SUCCESS_LISTENER_PRIORITY 1;
  16.     public function onLogin(AuthenticationEvent $event): void
  17.     {
  18.         $token $event->getAuthenticationToken();
  19.         // We have a TwoFactorToken, make sure the security.authentication.success is not propagated to other
  20.         // listeners, since we do not have a successful login (yet)
  21.         if (!($token instanceof TwoFactorTokenInterface)) {
  22.             return;
  23.         }
  24.         $event->stopPropagation();
  25.     }
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             AuthenticationEvents::AUTHENTICATION_SUCCESS => ['onLogin'self::LISTENER_PRIORITY],
  33.         ];
  34.     }
  35. }