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

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\Bundle\AdminBundle\Security\Authentication\Token\TwoFactorRequiredToken;
  16. use Pimcore\Tool\Session;
  17. use Psr\Log\LoggerAwareTrait;
  18. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  19. use Scheb\TwoFactorBundle\Security\TwoFactor\Event\TwoFactorAuthenticationEvent;
  20. use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\PreparationRecorderInterface;
  21. use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderRegistry;
  22. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
  23. /**
  24.  * @internal
  25.  */
  26. class TwoFactorListener
  27. {
  28.     use LoggerAwareTrait;
  29.     /**
  30.      * @var TwoFactorProviderRegistry
  31.      */
  32.     private $providerRegistry;
  33.     /**
  34.      * @var PreparationRecorderInterface
  35.      */
  36.     private $preparationRecorder;
  37.     public function __construct(TwoFactorProviderRegistry $providerRegistryPreparationRecorderInterface $preparationRecorder)
  38.     {
  39.         $this->providerRegistry $providerRegistry;
  40.         $this->preparationRecorder $preparationRecorder;
  41.     }
  42.     public function onAuthenticationComplete(TwoFactorAuthenticationEvent $event)
  43.     {
  44.         // this session flag is set in \Pimcore\Bundle\AdminBundle\Security\AdminAuthenticator
  45.         // or \Pimcore\Bundle\AdminBundle\Security\AdminAuthenticator (Authenticator Based Security)
  46.         // @TODO: check if there's a nicer way of doing this, actually it feels a bit like a hack :)
  47.         Session::useSession(function (AttributeBagInterface $adminSession) {
  48.             $adminSession->set('2fa_required'false);
  49.         });
  50.     }
  51.     public function onAuthenticationAttempt(TwoFactorAuthenticationEvent $event)
  52.     {
  53.         $twoFactorToken $event->getToken();
  54.         if (!$twoFactorToken instanceof TwoFactorTokenInterface) {
  55.             return;
  56.         }
  57.         $providerName $twoFactorToken->getCurrentTwoFactorProvider();
  58.         if (null === $providerName) {
  59.             return;
  60.         }
  61.         $twoFactorToken->setTwoFactorProviderPrepared($providerName);
  62.         if ($twoFactorToken instanceof TwoFactorRequiredToken) {
  63.             $firewallName $twoFactorToken->getFirewallName();
  64.         } else {
  65.             $firewallName $twoFactorToken->getProviderKey();
  66.         }
  67.         if ($this->preparationRecorder->isTwoFactorProviderPrepared($firewallName$providerName)) {
  68.             $this->logger->info(sprintf('Two-factor provider "%s" was already prepared.'$providerName));
  69.             return;
  70.         }
  71.         $user $twoFactorToken->getUser();
  72.         $this->providerRegistry->getProvider($providerName)->prepareAuthentication($user);
  73.         $this->preparationRecorder->setTwoFactorProviderPrepared($firewallName$providerName);
  74.         $this->logger->info(sprintf('Two-factor provider "%s" prepared.'$providerName));
  75.     }
  76. }