Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22f6bd9

Browse files
committed
Move AuthenticationSuccessEvent outside try/catch block
1 parent a2f0734 commit 22f6bd9

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

src/Symfony/Component/Security/Http/Authentication/AuthenticatorManager.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,6 @@ private function executeAuthenticator(AuthenticatorInterface $authenticator, Req
169169
if (true === $this->eraseCredentials) {
170170
$authenticatedToken->eraseCredentials();
171171
}
172-
173-
$this->eventDispatcher->dispatch(new AuthenticationSuccessEvent($authenticatedToken), AuthenticationEvents::AUTHENTICATION_SUCCESS);
174-
175-
if (null !== $this->logger) {
176-
$this->logger->info('Authenticator successful!', ['token' => $authenticatedToken, 'authenticator' => \get_class($authenticator)]);
177-
}
178-
179-
// success! (sets the token on the token storage, etc)
180-
$response = $this->handleAuthenticationSuccess($authenticatedToken, $passport, $request, $authenticator);
181-
if ($response instanceof Response) {
182-
return $response;
183-
}
184-
185-
if (null !== $this->logger) {
186-
$this->logger->debug('Authenticator set no success response: request continues.', ['authenticator' => \get_class($authenticator)]);
187-
}
188-
189-
return null;
190172
} catch (AuthenticationException $e) {
191173
// oh no! Authentication failed!
192174
$response = $this->handleAuthenticationFailure($e, $request, $authenticator);
@@ -196,6 +178,24 @@ private function executeAuthenticator(AuthenticatorInterface $authenticator, Req
196178

197179
return null;
198180
}
181+
182+
$this->eventDispatcher->dispatch(new AuthenticationSuccessEvent($authenticatedToken), AuthenticationEvents::AUTHENTICATION_SUCCESS);
183+
184+
if (null !== $this->logger) {
185+
$this->logger->info('Authenticator successful!', ['token' => $authenticatedToken, 'authenticator' => \get_class($authenticator)]);
186+
}
187+
188+
// success! (sets the token on the token storage, etc)
189+
$response = $this->handleAuthenticationSuccess($authenticatedToken, $passport, $request, $authenticator);
190+
if ($response instanceof Response) {
191+
return $response;
192+
}
193+
194+
if (null !== $this->logger) {
195+
$this->logger->debug('Authenticator set no success response: request continues.', ['authenticator' => \get_class($authenticator)]);
196+
}
197+
198+
return null;
199199
}
200200

201201
private function handleAuthenticationSuccess(TokenInterface $authenticatedToken, PassportInterface $passport, Request $request, AuthenticatorInterface $authenticator): ?Response

src/Symfony/Component/Security/Http/EventListener/UserCheckerListener.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\Security\Http\EventListener;
1313

1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
1516
use Symfony\Component\Security\Core\User\UserCheckerInterface;
17+
use Symfony\Component\Security\Core\User\UserInterface;
1618
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PreAuthenticatedUserBadge;
1719
use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
1820
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
@@ -43,21 +45,21 @@ public function preCheckCredentials(CheckPassportEvent $event): void
4345
$this->userChecker->checkPreAuth($passport->getUser());
4446
}
4547

46-
public function postCheckCredentials(LoginSuccessEvent $event): void
48+
public function postCheckCredentials(AuthenticationSuccessEvent $event): void
4749
{
48-
$passport = $event->getPassport();
49-
if (!$passport instanceof UserPassportInterface || null === $passport->getUser()) {
50+
$user = $event->getAuthenticationToken()->getUser();
51+
if (!$user instanceof UserInterface) {
5052
return;
5153
}
5254

53-
$this->userChecker->checkPostAuth($passport->getUser());
55+
$this->userChecker->checkPostAuth($user);
5456
}
5557

5658
public static function getSubscribedEvents(): array
5759
{
5860
return [
5961
CheckPassportEvent::class => ['preCheckCredentials', 256],
60-
LoginSuccessEvent::class => ['postCheckCredentials', 256],
62+
AuthenticationSuccessEvent::class => ['postCheckCredentials', 256],
6163
];
6264
}
6365
}

src/Symfony/Component/Security/Http/Tests/EventListener/UserCheckerListenerTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
namespace Symfony\Component\Security\Http\Tests\EventListener;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\HttpFoundation\Request;
16-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
16+
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
1717
use Symfony\Component\Security\Core\User\User;
1818
use Symfony\Component\Security\Core\User\UserCheckerInterface;
1919
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
2020
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PreAuthenticatedUserBadge;
2121
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
2222
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
23+
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
2324
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
24-
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
2525
use Symfony\Component\Security\Http\EventListener\UserCheckerListener;
2626

2727
class UserCheckerListenerTest extends TestCase
@@ -62,14 +62,14 @@ public function testPostAuthValidCredentials()
6262
{
6363
$this->userChecker->expects($this->once())->method('checkPostAuth')->with($this->user);
6464

65-
$this->listener->postCheckCredentials($this->createLoginSuccessEvent());
65+
$this->listener->postCheckCredentials(new AuthenticationSuccessEvent(new PostAuthenticationToken($this->user, 'main', [])));
6666
}
6767

6868
public function testPostAuthNoUser()
6969
{
7070
$this->userChecker->expects($this->never())->method('checkPostAuth');
7171

72-
$this->listener->postCheckCredentials($this->createLoginSuccessEvent($this->createMock(PassportInterface::class)));
72+
$this->listener->postCheckCredentials(new AuthenticationSuccessEvent(new PreAuthenticatedToken('nobody', null, 'main')));
7373
}
7474

7575
private function createCheckPassportEvent($passport = null)
@@ -81,12 +81,8 @@ private function createCheckPassportEvent($passport = null)
8181
return new CheckPassportEvent($this->createMock(AuthenticatorInterface::class), $passport);
8282
}
8383

84-
private function createLoginSuccessEvent($passport = null)
84+
private function createAuthenticationSuccessEvent()
8585
{
86-
if (null === $passport) {
87-
$passport = new SelfValidatingPassport($this->user);
88-
}
89-
90-
return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class), $passport, $this->createMock(TokenInterface::class), new Request(), null, 'main');
86+
return new AuthenticationSuccessEvent(new PostAuthenticationToken($this->user, 'main', []));
9187
}
9288
}

0 commit comments

Comments
 (0)