vendor/pimcore/pimcore/bundles/AdminBundle/Security/Authenticator/AdminSessionAuthenticator.php line 32

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\Security\Authenticator;
  15. use Pimcore\Model\User;
  16. use Pimcore\Model\User as UserModel;
  17. use Pimcore\Tool\Authentication;
  18. use Pimcore\Tool\Session;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  21. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PreAuthenticatedUserBadge;
  22. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  23. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  24. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  25. /**
  26.  * @internal
  27.  */
  28. class AdminSessionAuthenticator extends AdminAbstractAuthenticator
  29. {
  30.     /**
  31.      * @var User|null
  32.      */
  33.     protected ?User $user;
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function supports(Request $request): ?bool
  38.     {
  39.         $this->user Authentication::authenticateSession($request);
  40.         return (bool) $this->user;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function authenticate(Request $request): Passport
  46.     {
  47.         if (!$this->user instanceof UserModel) {
  48.             throw new AuthenticationException('Invalid User!');
  49.         }
  50.         $session Session::getReadOnly();
  51.         if ($session->has('2fa_required') && $session->get('2fa_required') === true) {
  52.             $this->twoFactorRequired true;
  53.         }
  54.         $badges = [
  55.             new PreAuthenticatedUserBadge(),
  56.         ];
  57.         return new SelfValidatingPassport(
  58.             new UserBadge($this->user->getUsername()),
  59.             $badges
  60.         );
  61.     }
  62. }