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

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.     public const REQUEST_ATTRIBUTE_SESSION_AUTHENTICATED '_pimcore_admin_session_authenticated';
  31.     /**
  32.      * @var User|null
  33.      */
  34.     protected ?User $user;
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function supports(Request $request): ?bool
  39.     {
  40.         $this->user Authentication::authenticateSession($request);
  41.         return (bool) $this->user;
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function authenticate(Request $request): Passport
  47.     {
  48.         if (!$this->user instanceof UserModel) {
  49.             throw new AuthenticationException('Invalid User!');
  50.         }
  51.         $session Session::getReadOnly();
  52.         if ($session->has('2fa_required') && $session->get('2fa_required') === true) {
  53.             $this->twoFactorRequired true;
  54.         }
  55.         $badges = [
  56.             new PreAuthenticatedUserBadge(),
  57.         ];
  58.         // Mark request as "session authenticated" to prevent login throttling
  59.         /** @see \Pimcore\Bundle\AdminBundle\RateLimiter\AdminRequestRateLimiter */
  60.         $request->attributes->set(static::REQUEST_ATTRIBUTE_SESSION_AUTHENTICATEDtrue);
  61.         return new SelfValidatingPassport(
  62.             new UserBadge($this->user->getUsername()),
  63.             $badges
  64.         );
  65.     }
  66. }