vendor/pimcore/pimcore/bundles/AdminBundle/Security/Authenticator/AdminLoginAuthenticator.php line 36

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\Event\Admin\Login\LoginFailedEvent;
  16. use Pimcore\Event\AdminEvents;
  17. use Pimcore\Security\User\User;
  18. use Pimcore\Tool\Authentication;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  23. use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface;
  24. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  25. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  26. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
  27. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  28. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  29. /**
  30.  * @internal
  31.  */
  32. class AdminLoginAuthenticator extends AdminAbstractAuthenticator implements AuthenticationEntryPointInterfaceInteractiveAuthenticatorInterface
  33. {
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function supports(Request $request): ?bool
  38.     {
  39.         return $request->attributes->get('_route') === self::PIMCORE_ADMIN_LOGIN_CHECK
  40.             && $request->getMethod() === 'POST' && $request->get('password');
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function start(Request $requestAuthenticationException $authException null): Response
  46.     {
  47.         if ($request->isXmlHttpRequest()) {
  48.             $response = new Response('Session expired or unauthorized request. Please reload and try again!');
  49.             $response->setStatusCode(Response::HTTP_FORBIDDEN);
  50.             return $response;
  51.         }
  52.         return new RedirectResponse($this->router->generate(self::PIMCORE_ADMIN_LOGIN, ['perspective' => strip_tags($request->get('perspective'''))]));
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function authenticate(Request $request): Passport
  58.     {
  59.         if (!$username $request->get('username')) {
  60.             throw new AuthenticationException('Missing username or password');
  61.         }
  62.         $passport = new Passport(
  63.             new UserBadge($username),
  64.             new CustomCredentials(function ($credentials) {
  65.                 $pimcoreUser Authentication::authenticatePlaintext($credentials['username'], $credentials['password']);
  66.                 if ($pimcoreUser) {
  67.                     $user = new User($pimcoreUser);
  68.                     $this->saveUserToSession($user);
  69.                 } else {
  70.                     // trigger LOGIN_FAILED event if user could not be authenticated via username/password
  71.                     $event = new LoginFailedEvent($credentials);
  72.                     $this->dispatcher->dispatch($eventAdminEvents::LOGIN_FAILED);
  73.                     if ($event->hasUser()) {
  74.                         $user = new User($event->getUser());
  75.                         $this->saveUserToSession($user);
  76.                     } else {
  77.                         return false;
  78.                     }
  79.                 }
  80.                 return true;
  81.             }, ['username' => $username'password' => $request->get('password')])
  82.         );
  83.         if ($csrfToken $request->get('csrf_token')) {
  84.             $passport->addBadge(new CsrfTokenBadge('pimcore_admin_authenticate'$csrfToken));
  85.         }
  86.         return $passport;
  87.     }
  88.     /**
  89.      * {@inheritdoc}
  90.      */
  91.     public function isInteractive(): bool
  92.     {
  93.         return true;
  94.     }
  95. }