vendor/scheb/2fa-trusted-device/Security/Http/EventListener/TrustedDeviceListener.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Scheb\TwoFactorBundle\Security\Http\EventListener;
  4. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  5. use Scheb\TwoFactorBundle\Security\Http\Authenticator\Passport\Badge\TrustedDeviceBadge;
  6. use Scheb\TwoFactorBundle\Security\Http\Authenticator\Passport\TwoFactorPassport;
  7. use Scheb\TwoFactorBundle\Security\TwoFactor\Trusted\TrustedDeviceManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  10. /**
  11.  * @final
  12.  */
  13. class TrustedDeviceListener implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var TrustedDeviceManagerInterface
  17.      */
  18.     private $trustedDeviceManager;
  19.     public function __construct(TrustedDeviceManagerInterface $trustedDeviceManager)
  20.     {
  21.         $this->trustedDeviceManager $trustedDeviceManager;
  22.     }
  23.     public function onSuccessfulLogin(LoginSuccessEvent $loginSuccessEvent): void
  24.     {
  25.         if ($loginSuccessEvent->getAuthenticatedToken() instanceof TwoFactorTokenInterface) {
  26.             // Two-factor authentication still not complete
  27.             return;
  28.         }
  29.         $passport $loginSuccessEvent->getPassport();
  30.         if (!($passport instanceof TwoFactorPassport)) {
  31.             return;
  32.         }
  33.         if (!$passport->hasBadge(TrustedDeviceBadge::class)) {
  34.             return;
  35.         }
  36.         $user $loginSuccessEvent->getAuthenticatedToken()->getUser();
  37.         $firewallName $passport->getFirewallName();
  38.         if ($this->trustedDeviceManager->canSetTrustedDevice($user$loginSuccessEvent->getRequest(), $firewallName)) {
  39.             $this->trustedDeviceManager->addTrustedDevice($user$firewallName);
  40.         }
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             LoginSuccessEvent::class => 'onSuccessfulLogin',
  46.         ];
  47.     }
  48. }