src/EventSubscriber/TotpTokenCreator.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpAuthenticatorInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  11. use Symfony\Component\Routing\RouterInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  14. use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
  15. use Symfony\Component\Security\Http\Event\AuthenticationTokenCreatedEvent;
  16. use Symfony\Component\Security\Http\Event\CheckPassportEvent;
  17. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  18. class TotpTokenCreator implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var TotpAuthenticatorInterface
  22.      */
  23.     private $totpAuthenticator;
  24.     /**
  25.      * @var EntityManagerInterface
  26.      */
  27.     private $entityManager;
  28.     /**
  29.      * @var TokenStorageInterface
  30.      */
  31.     private $tokenStorage;
  32.     /**
  33.      * @var RouterInterface
  34.      */
  35.     private $router;
  36.     private $exclude_routes = [
  37.         'app_2fa_showcode',
  38.         'app_2fa_png'
  39.     ];
  40.     /**
  41.      * @var SessionInterface
  42.      */
  43.     private $session;
  44.     public function __construct(TotpAuthenticatorInterface $totpAuthenticatorEntityManagerInterface $entityManagerTokenStorageInterface $tokenStorageRouterInterface $routerSessionInterface $session)
  45.     {
  46.         $this->totpAuthenticator $totpAuthenticator;
  47.         $this->entityManager $entityManager;
  48.         $this->tokenStorage $tokenStorage;
  49.         $this->router $router;
  50.         $this->session $session;
  51.     }
  52.     public function onLoginSuccess(LoginSuccessEvent $event)
  53.     {
  54.         $passport $event->getPassport();
  55.         $user $passport->getUser();
  56.         if (!$user->isTotpAuthenticationEnabled()) {
  57.             $user->setTotpSecret($this->totpAuthenticator->generateSecret());
  58.             $this->entityManager->flush();
  59.             $this->session->set('newTotp'true);
  60.         }
  61.     }
  62.     public function onKernelRequest(RequestEvent $event)
  63.     {
  64.         $token $this->tokenStorage->getToken();
  65.         if (!$token) {
  66.             return ;
  67.         }
  68.         $user $token->getUser();
  69.         if (!$user) {
  70.             return ;
  71.         }
  72.         $session_flag $this->session->has('newTotp');
  73.         if ($session_flag === true && null !== $event->getRequest()->attributes->get('_route') && !in_array($event->getRequest()->attributes->get('_route'), $this->exclude_routes)) {
  74.             $event->setResponse(new RedirectResponse(
  75.                 $this->router->generate('app_2fa_showcode')
  76.             ));
  77.         }
  78.     }
  79.     public static function getSubscribedEvents()
  80.     {
  81.         return [
  82.             LoginSuccessEvent::class => 'onLoginSuccess',
  83.             RequestEvent::class => 'onKernelRequest'
  84.         ];
  85.     }
  86. }