src/Core/Security/LoginFormAuthenticator.php line 40
<?php/*** Copyright (c) 2019 TECLA Consulting Group oü.* All rights reserved.** This unpublished material is proprietary to TECLA Consulting Group oü.* All rights reserved. The methods and* techniques described herein are considered trade secrets* and/or confidential. Reproduction or distribution, in whole* or in part, is forbidden except by express written permission* of TECLA Consulting Group oü.** @author Matúš Sýkorjak <matus@tecla.no>* @copyright 2019 TECLA Consulting Group oü*/namespace App\Core\Security;use App\Core\Model\UserInterface;use App\Core\Security\Encoder\LegacyUserPasswordEncoderInterface;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Exception\BadCredentialsException;use Symfony\Component\Security\Core\Security;use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;use Symfony\Component\Security\Core\User\UserProviderInterface;use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;use Symfony\Component\Security\Http\Authenticator\Passport\Passport;use Symfony\Component\Security\Http\Util\TargetPathTrait;class LoginFormAuthenticator extends AbstractLoginFormAuthenticator{use TargetPathTrait;public const LOGIN_ROUTE = 'app_login';public const SUCCESS_ROUTE = 'app_homepage';private UserProviderInterface $userProvider;private UrlGeneratorInterface $urlGenerator;private LegacyUserPasswordEncoderInterface $legacyUserPasswordEncoder;private PasswordHasherFactoryInterface $hasherFactory;public function __construct(UserProviderInterface $userProvider,UrlGeneratorInterface $urlGenerator,LegacyUserPasswordEncoderInterface $legacyUserPasswordEncoder,PasswordHasherFactoryInterface $hasherFactory) {$this->userProvider = $userProvider;$this->urlGenerator = $urlGenerator;$this->legacyUserPasswordEncoder = $legacyUserPasswordEncoder;$this->hasherFactory = $hasherFactory;}public function authenticate(Request $request): Passport{$credentials = $this->getCredentials($request);$passport = new Passport(new UserBadge($credentials['username']),new CustomCredentials(function ($password, UserInterface $user) {if ('' === $password) {throw new BadCredentialsException('The presented password cannot be empty.');}if (null === $user->getPassword()) {throw new BadCredentialsException('The presented password is invalid.');}if (null !== $user->getLegacyPassword() && '' !== $user->getLegacyPassword()) {return $this->legacyUserPasswordEncoder->isPasswordValid($user, $password);}return $this->hasherFactory->getPasswordHasher($user)->verify($user->getPassword(),$password);},$credentials['password']),[new RememberMeBadge(),new CsrfTokenBadge('authenticate', $credentials['csrf_token']),]);if ($this->userProvider instanceof PasswordUpgraderInterface) {$passport->addBadge(new PasswordUpgradeBadge($request->request->get('password'), $this->userProvider));}return $passport;}public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response{$targetPath = $this->getTargetPath($request->getSession(), $firewallName);if (true === str_contains($targetPath, 'cid=')) {return new RedirectResponse($targetPath);}$routeParams = [];$user = $token->getUser();if (true === $user instanceof UserInterface) {$routeParams = ['cid' => $user->getContact()->getId()];}return new RedirectResponse($this->urlGenerator->generate(self::SUCCESS_ROUTE, $routeParams));}protected function getLoginUrl(Request $request): string{return $this->urlGenerator->generate(self::LOGIN_ROUTE);}private function getCredentials(Request $request): array{$credentials = ['username' => $request->request->get('username'),'password' => $request->request->get('password'),'csrf_token' => $request->request->get('_token'),];$request->getSession()->set(Security::LAST_USERNAME,$credentials['username']);return $credentials;}}