vendor/pimcore/pimcore/bundles/AdminBundle/EventListener/TwoFactorListener.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\AdminBundle\EventListener;
  15. use Pimcore\Tool\Session;
  16. use Psr\Log\LoggerAwareTrait;
  17. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  18. use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvent;
  19. use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\PreparationRecorderInterface;
  20. use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderRegistry;
  21. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
  22. /**
  23.  * @internal
  24.  */
  25. class TwoFactorListener
  26. {
  27.     use LoggerAwareTrait;
  28.     /**
  29.      * @var TwoFactorProviderRegistry
  30.      */
  31.     private $providerRegistry;
  32.     /**
  33.      * @var PreparationRecorderInterface
  34.      */
  35.     private $preparationRecorder;
  36.     public function __construct(TwoFactorProviderRegistry $providerRegistryPreparationRecorderInterface $preparationRecorder)
  37.     {
  38.         $this->providerRegistry $providerRegistry;
  39.         $this->preparationRecorder $preparationRecorder;
  40.     }
  41.     public function onAuthenticationComplete(TwoFactorAuthenticationEvent $event)
  42.     {
  43.         // this session flag is set in \Pimcore\Bundle\AdminBundle\Security\Guard\AdminAuthenticator
  44.         // @TODO: check if there's a nicer way of doing this, actually it feels a bit like a hack :)
  45.         Session::useSession(function (AttributeBagInterface $adminSession) {
  46.             $adminSession->set('2fa_required'false);
  47.         });
  48.     }
  49.     public function onAuthenticationAttempt(TwoFactorAuthenticationEvent $event)
  50.     {
  51.         $twoFactorToken $event->getToken();
  52.         if (!$twoFactorToken instanceof TwoFactorTokenInterface) {
  53.             return;
  54.         }
  55.         $providerName $twoFactorToken->getCurrentTwoFactorProvider();
  56.         if (null === $providerName) {
  57.             return;
  58.         }
  59.         $twoFactorToken->setTwoFactorProviderPrepared($providerName);
  60.         $firewallName $twoFactorToken->getProviderKey();
  61.         if ($this->preparationRecorder->isTwoFactorProviderPrepared($firewallName$providerName)) {
  62.             $this->logger->info(sprintf('Two-factor provider "%s" was already prepared.'$providerName));
  63.             return;
  64.         }
  65.         $user $twoFactorToken->getUser();
  66.         $this->providerRegistry->getProvider($providerName)->prepareAuthentication($user);
  67.         $this->preparationRecorder->setTwoFactorProviderPrepared($firewallName$providerName);
  68.         $this->logger->info(sprintf('Two-factor provider "%s" prepared.'$providerName));
  69.     }
  70. }