|
16 | 16 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
17 | 17 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
18 | 18 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
| 19 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
19 | 20 | use Symfony\Component\Security\Core\Exception\BadCredentialsException;
|
20 | 21 | use Symfony\Component\Security\Core\Security;
|
21 | 22 | use Symfony\Component\Security\Core\User\UserInterface;
|
22 | 23 | use Symfony\Component\Security\Core\User\UserProviderInterface;
|
| 24 | +use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; |
| 25 | +use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; |
23 | 26 | use Symfony\Component\Security\Http\HttpUtils;
|
24 | 27 | use Symfony\Component\Security\Http\ParameterBagUtils;
|
25 |
| -use Symfony\Component\Security\Http\Util\TargetPathTrait; |
26 | 28 |
|
27 | 29 | /**
|
28 | 30 | * @author Wouter de Jong <wouter@wouterj.nl>
|
|
33 | 35 | */
|
34 | 36 | class FormLoginAuthenticator extends AbstractLoginFormAuthenticator implements PasswordAuthenticatedInterface, CsrfProtectedAuthenticatorInterface
|
35 | 37 | {
|
36 |
| - use TargetPathTrait; |
37 |
| - |
38 |
| - private $options; |
39 | 38 | private $httpUtils;
|
40 | 39 | private $userProvider;
|
| 40 | + private $successHandler; |
| 41 | + private $failureHandler; |
| 42 | + private $options; |
41 | 43 |
|
42 |
| - public function __construct(HttpUtils $httpUtils, UserProviderInterface $userProvider, array $options) |
| 44 | + public function __construct(HttpUtils $httpUtils, UserProviderInterface $userProvider, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, array $options) |
43 | 45 | {
|
44 | 46 | $this->httpUtils = $httpUtils;
|
| 47 | + $this->userProvider = $userProvider; |
| 48 | + $this->successHandler = $successHandler; |
| 49 | + $this->failureHandler = $failureHandler; |
45 | 50 | $this->options = array_merge([
|
46 | 51 | 'username_parameter' => '_username',
|
47 | 52 | 'password_parameter' => '_password',
|
48 |
| - 'csrf_parameter' => '_csrf_token', |
49 |
| - 'csrf_token_id' => 'authenticate', |
| 53 | + 'check_path' => '/login_check', |
50 | 54 | 'post_only' => true,
|
51 | 55 |
|
52 |
| - 'always_use_default_target_path' => false, |
53 |
| - 'default_target_path' => '/', |
54 |
| - 'login_path' => '/login', |
55 |
| - 'target_path_parameter' => '_target_path', |
56 |
| - 'use_referer' => false, |
| 56 | + 'csrf_parameter' => '_csrf_token', |
| 57 | + 'csrf_token_id' => 'authenticate', |
57 | 58 | ], $options);
|
58 |
| - $this->userProvider = $userProvider; |
59 | 59 | }
|
60 | 60 |
|
61 |
| - protected function getLoginUrl(): string |
| 61 | + protected function getLoginUrl(Request $request): string |
62 | 62 | {
|
63 |
| - return $this->options['login_path']; |
| 63 | + return $this->httpUtils->generateUri($request, $this->options['login_path']); |
64 | 64 | }
|
65 | 65 |
|
66 | 66 | public function supports(Request $request): bool
|
@@ -122,36 +122,13 @@ public function createAuthenticatedToken(UserInterface $user, $providerKey): Tok
|
122 | 122 | return new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
|
123 | 123 | }
|
124 | 124 |
|
125 |
| - public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey): Response |
| 125 | + public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey): ?Response |
126 | 126 | {
|
127 |
| - return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request, $providerKey)); |
| 127 | + return $this->successHandler->onAuthenticationSuccess($request, $token); |
128 | 128 | }
|
129 | 129 |
|
130 |
| - private function determineTargetUrl(Request $request, string $providerKey) |
| 130 | + public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response |
131 | 131 | {
|
132 |
| - if ($this->options['always_use_default_target_path']) { |
133 |
| - return $this->options['default_target_path']; |
134 |
| - } |
135 |
| - |
136 |
| - if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) { |
137 |
| - return $targetUrl; |
138 |
| - } |
139 |
| - |
140 |
| - if ($targetUrl = $this->getTargetPath($request->getSession(), $providerKey)) { |
141 |
| - $this->removeTargetPath($request->getSession(), $providerKey); |
142 |
| - |
143 |
| - return $targetUrl; |
144 |
| - } |
145 |
| - |
146 |
| - if ($this->options['use_referer'] && $targetUrl = $request->headers->get('Referer')) { |
147 |
| - if (false !== $pos = strpos($targetUrl, '?')) { |
148 |
| - $targetUrl = substr($targetUrl, 0, $pos); |
149 |
| - } |
150 |
| - if ($targetUrl && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) { |
151 |
| - return $targetUrl; |
152 |
| - } |
153 |
| - } |
154 |
| - |
155 |
| - return $this->options['default_target_path']; |
| 132 | + return $this->failureHandler->onAuthenticationFailure($request, $exception); |
156 | 133 | }
|
157 | 134 | }
|
0 commit comments