vendor/pimcore/pimcore/bundles/AdminBundle/Security/Authenticator/AdminTokenAuthenticator.php line 34

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\Bundle\AdminBundle\Security\User\User;
  16. use Pimcore\Tool\Authentication;
  17. use Pimcore\Tool\Session;
  18. use Psr\Log\LoggerAwareInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
  21. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  22. use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
  23. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PreAuthenticatedUserBadge;
  24. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  25. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  26. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  27. /**
  28.  * @internal
  29.  */
  30. class AdminTokenAuthenticator extends AdminAbstractAuthenticator implements AuthenticatorInterfaceLoggerAwareInterface
  31. {
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function supports(Request $request): ?bool
  36.     {
  37.         return $request->attributes->get('_route') === self::PIMCORE_ADMIN_LOGIN_CHECK
  38.             && $request->get('token');
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public function authenticate(Request $request): Passport
  44.     {
  45.         $pimcoreUser Authentication::authenticateToken($request->get('token'));
  46.         if ($pimcoreUser) {
  47.             //disable two factor authentication for token based credentials e.g. reset password, admin access links
  48.             $pimcoreUser->setTwoFactorAuthentication('required'false);
  49.             $user = new User($pimcoreUser);
  50.             $this->saveUserToSession($user);
  51.         } else {
  52.             throw new AuthenticationException('Failed to authenticate with username and token');
  53.         }
  54.         if ($request->get('reset')) {
  55.             // save the information to session when the user want's to reset the password
  56.             // this is because otherwise the old password is required => see also PIMCORE-1468
  57.             Session::useSession(function (AttributeBagInterface $adminSession) {
  58.                 $adminSession->set('password_reset'true);
  59.             });
  60.         }
  61.         $badges = [
  62.             new PreAuthenticatedUserBadge(),
  63.         ];
  64.         return new SelfValidatingPassport(
  65.             new UserBadge($pimcoreUser->getUsername()),
  66.             $badges
  67.         );
  68.     }
  69. }