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

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