Lines
100.00%
23 / 23
Functions and Methods
100.00%
4 / 4
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| LoginFormAuthenticator | 100.00% | 23 / 23 | 100.00% | 4 / 4 | 7 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| getLoginUrl | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| authenticate | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 3 | |||||
| onAuthenticationSuccess | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace App\Security; | |
| 6 | ||
| 7 | use Symfony\Component\DependencyInjection\Attribute\Autowire; | |
| 8 | use Symfony\Component\HttpFoundation\RedirectResponse; | |
| 9 | use Symfony\Component\HttpFoundation\Request; | |
| 10 | use Symfony\Component\Routing\RouterInterface; | |
| 11 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
| 12 | use Symfony\Component\Security\Core\Exception\AuthenticationException; | |
| 13 | use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator; | |
| 14 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge; | |
| 15 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge; | |
| 16 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | |
| 17 | use Symfony\Component\Security\Http\Authenticator\Passport\Passport; | |
| 18 | use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; | |
| 19 | use Symfony\Component\Security\Http\Util\TargetPathTrait; | |
| 20 | ||
| 21 | class LoginFormAuthenticator extends AbstractLoginFormAuthenticator | |
| 22 | { | |
| 23 | use TargetPathTrait; | |
| 24 | ||
| 25 | public function __construct( | |
| 26 | private readonly RouterInterface $router, | |
| 27 | #[Autowire(env: 'APP_ENV')] private readonly string $appEnv, | |
| 28 | ) { | |
| 29 | } | |
| 30 | ||
| 31 | #[\Override] | |
| 32 | protected function getLoginUrl(Request $request): string | |
| 33 | { | |
| 34 | return $this->router->generate('app_login'); | |
| 35 | } | |
| 36 | ||
| 37 | #[\Override] | |
| 38 | public function authenticate(Request $request): Passport | |
| 39 | { | |
| 40 | if (false === \in_array($this->appEnv, ['dev', 'test'], true)) { | |
| 41 | throw new \UnexpectedValueException('GTFO!'); | |
| 42 | } | |
| 43 | ||
| 44 | $identifier = (string) $request->request->get('identifier'); | |
| 45 | ||
| 46 | if ('' === $identifier) { | |
| 47 | throw new AuthenticationException('User identifier cannot be empty.'); | |
| 48 | } | |
| 49 | ||
| 50 | return new SelfValidatingPassport( | |
| 51 | new UserBadge($identifier), | |
| 52 | [ | |
| 53 | new CsrfTokenBadge( | |
| 54 | 'login', | |
| 55 | (string) $request->request->get('_csrf_token') | |
| 56 | ), | |
| 57 | new RememberMeBadge(), | |
| 58 | ] | |
| 59 | ); | |
| 60 | } | |
| 61 | ||
| 62 | #[\Override] | |
| 63 | public function onAuthenticationSuccess( | |
| 64 | Request $request, | |
| 65 | TokenInterface $token, | |
| 66 | string $firewallName | |
| 67 | ): RedirectResponse { | |
| 68 | if ($targetPath = $this->getTargetPath( | |
| 69 | $request->getSession(), | |
| 70 | $firewallName | |
| 71 | ) | |
| 72 | ) { | |
| 73 | return new RedirectResponse($targetPath); | |
| 74 | } | |
| 75 | ||
| 76 | return new RedirectResponse($this->router->generate('app_default')); | |
| 77 | } | |
| 78 | } |