Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
14.29% |
2 / 14 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| GandalfAuthenticator | |
14.29% |
2 / 14 |
|
50.00% |
2 / 4 |
20.74 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| authenticate | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| onAuthenticationSuccess | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getLoginUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Security; |
| 4 | |
| 5 | use Symfony\Component\HttpFoundation\RedirectResponse; |
| 6 | use Symfony\Component\HttpFoundation\Request; |
| 7 | use Symfony\Component\HttpFoundation\Response; |
| 8 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 9 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 10 | use Symfony\Component\Security\Core\Security; |
| 11 | use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator; |
| 12 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge; |
| 13 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 14 | use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; |
| 15 | use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
| 16 | use Symfony\Component\Security\Http\Util\TargetPathTrait; |
| 17 | |
| 18 | class GandalfAuthenticator extends AbstractLoginFormAuthenticator |
| 19 | { |
| 20 | use TargetPathTrait; |
| 21 | |
| 22 | public const LOGIN_ROUTE = 'app_login'; |
| 23 | |
| 24 | private UrlGeneratorInterface $urlGenerator; |
| 25 | |
| 26 | public function __construct(UrlGeneratorInterface $urlGenerator) |
| 27 | { |
| 28 | $this->urlGenerator = $urlGenerator; |
| 29 | } |
| 30 | |
| 31 | public function authenticate(Request $request): Passport |
| 32 | { |
| 33 | $email = $request->request->get('email', ''); |
| 34 | |
| 35 | $request->getSession()->set(Security::LAST_USERNAME, $email); |
| 36 | |
| 37 | return new Passport( |
| 38 | new UserBadge($email), |
| 39 | new PasswordCredentials($request->request->get('password', '')), |
| 40 | [ |
| 41 | new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')), |
| 42 | ] |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
| 47 | { |
| 48 | if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) { |
| 49 | return new RedirectResponse($targetPath); |
| 50 | } |
| 51 | |
| 52 | // For example: |
| 53 | return new RedirectResponse($this->urlGenerator->generate('default')); |
| 54 | // throw new \Exception('TODO: provide a valid redirect inside '.__FILE__); |
| 55 | } |
| 56 | |
| 57 | protected function getLoginUrl(Request $request): string |
| 58 | { |
| 59 | return $this->urlGenerator->generate(self::LOGIN_ROUTE); |
| 60 | } |
| 61 | } |