Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
15 / 20 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| LoginFormAuthenticator | |
75.00% |
15 / 20 |
|
75.00% |
3 / 4 |
7.77 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLoginUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| authenticate | |
58.33% |
7 / 12 |
|
0.00% |
0 / 1 |
3.65 | |||
| 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\Http\Authenticator\AbstractLoginFormAuthenticator; |
| 13 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge; |
| 14 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge; |
| 15 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 16 | use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
| 17 | use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; |
| 18 | use Symfony\Component\Security\Http\Util\TargetPathTrait; |
| 19 | use UnexpectedValueException; |
| 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 | protected function getLoginUrl(Request $request): string |
| 31 | { |
| 32 | return $this->router->generate('login'); |
| 33 | } |
| 34 | |
| 35 | public function authenticate(Request $request): Passport |
| 36 | { |
| 37 | if (false === in_array($this->appEnv, ['dev', 'test'], true)) { |
| 38 | throw new UnexpectedValueException('GTFO!'); |
| 39 | } |
| 40 | |
| 41 | $badges = [new RememberMeBadge()]; |
| 42 | if ('test' !== $this->appEnv) { |
| 43 | $badges[] = new CsrfTokenBadge( |
| 44 | 'login', |
| 45 | (string) $request->request->get('_csrf_token') |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | return new SelfValidatingPassport( |
| 50 | new UserBadge((string) $request->request->get('identifier')), |
| 51 | $badges |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | public function onAuthenticationSuccess( |
| 56 | Request $request, |
| 57 | TokenInterface $token, |
| 58 | string $firewallName |
| 59 | ): RedirectResponse |
| 60 | { |
| 61 | if ($targetPath = $this->getTargetPath( |
| 62 | $request->getSession(), |
| 63 | $firewallName |
| 64 | ) |
| 65 | ) { |
| 66 | return new RedirectResponse($targetPath); |
| 67 | } |
| 68 | |
| 69 | return new RedirectResponse($this->router->generate('default')); |
| 70 | } |
| 71 | } |