Skip to content

Commit 7a9bac9

Browse files
committed
[Security] Pass Passport to LoginFailureEvent
1 parent 050eef7 commit 7a9bac9

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

Authentication/AuthenticatorManager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ private function executeAuthenticators(array $authenticators, Request $request):
158158

159159
private function executeAuthenticator(AuthenticatorInterface $authenticator, Request $request): ?Response
160160
{
161+
$passport = null;
162+
161163
try {
162164
// get the passport from the Authenticator
163165
$passport = $authenticator->authenticate($request);
@@ -198,7 +200,7 @@ private function executeAuthenticator(AuthenticatorInterface $authenticator, Req
198200
return null;
199201
} catch (AuthenticationException $e) {
200202
// oh no! Authentication failed!
201-
$response = $this->handleAuthenticationFailure($e, $request, $authenticator);
203+
$response = $this->handleAuthenticationFailure($e, $request, $authenticator, $passport);
202204
if ($response instanceof Response) {
203205
return $response;
204206
}
@@ -229,7 +231,7 @@ private function handleAuthenticationSuccess(TokenInterface $authenticatedToken,
229231
/**
230232
* Handles an authentication failure and returns the Response for the authenticator.
231233
*/
232-
private function handleAuthenticationFailure(AuthenticationException $authenticationException, Request $request, AuthenticatorInterface $authenticator): ?Response
234+
private function handleAuthenticationFailure(AuthenticationException $authenticationException, Request $request, AuthenticatorInterface $authenticator, ?PassportInterface $passport): ?Response
233235
{
234236
if (null !== $this->logger) {
235237
$this->logger->info('Authenticator failed.', ['exception' => $authenticationException, 'authenticator' => \get_class($authenticator)]);
@@ -240,7 +242,7 @@ private function handleAuthenticationFailure(AuthenticationException $authentica
240242
$this->logger->debug('The "{authenticator}" authenticator set the failure response.', ['authenticator' => \get_class($authenticator)]);
241243
}
242244

243-
$this->eventDispatcher->dispatch($loginFailureEvent = new LoginFailureEvent($authenticationException, $authenticator, $request, $response, $this->firewallName));
245+
$this->eventDispatcher->dispatch($loginFailureEvent = new LoginFailureEvent($authenticationException, $authenticator, $request, $response, $this->firewallName, $passport));
244246

245247
// returning null is ok, it means they want the request to continue
246248
return $loginFailureEvent->getResponse();

Event/LoginFailureEvent.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Response;
1616
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1717
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
18+
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
1819
use Symfony\Contracts\EventDispatcher\Event;
1920

2021
/**
@@ -32,14 +33,16 @@ class LoginFailureEvent extends Event
3233
private $request;
3334
private $response;
3435
private $firewallName;
36+
private $passport;
3537

36-
public function __construct(AuthenticationException $exception, AuthenticatorInterface $authenticator, Request $request, ?Response $response, string $firewallName)
38+
public function __construct(AuthenticationException $exception, AuthenticatorInterface $authenticator, Request $request, ?Response $response, string $firewallName, ?PassportInterface $passport = null)
3739
{
3840
$this->exception = $exception;
3941
$this->authenticator = $authenticator;
4042
$this->request = $request;
4143
$this->response = $response;
4244
$this->firewallName = $firewallName;
45+
$this->passport = $passport;
4346
}
4447

4548
public function getException(): AuthenticationException
@@ -71,4 +74,9 @@ public function getResponse(): ?Response
7174
{
7275
return $this->response;
7376
}
77+
78+
public function getPassport(): ?PassportInterface
79+
{
80+
return $this->passport;
81+
}
7482
}

Tests/EventListener/RememberMeListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ private function createLoginSuccessfulEvent($providerKey, $response, PassportInt
8686

8787
private function createLoginFailureEvent($providerKey)
8888
{
89-
return new LoginFailureEvent(new AuthenticationException(), $this->createMock(AuthenticatorInterface::class), $this->request, null, $providerKey);
89+
return new LoginFailureEvent(new AuthenticationException(), $this->createMock(AuthenticatorInterface::class), $this->request, null, $providerKey, null);
9090
}
9191
}

0 commit comments

Comments
 (0)