Skip to content

Commit 18bcae9

Browse files
bug #23256 [Security] Fix authentication.failure event not dispatched on AccountStatusException (chalasr)
This PR was merged into the 2.7 branch. Discussion ---------- [Security] Fix authentication.failure event not dispatched on AccountStatusException | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony/symfony#18807 | License | MIT | Doc PR | n/a Authentication fails if the user exists but its account is disabled/expired/locked, the failure event should be dispatched in this case, so that you can hook into as for any authentication exception. Commits ------- 64c2efd [Security] Fix authentication.failure event not dispatched on AccountStatusException
2 parents a3268fe + 02f88f2 commit 18bcae9

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

Authentication/AuthenticationProviderManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public function authenticate(TokenInterface $token)
8383
break;
8484
}
8585
} catch (AccountStatusException $e) {
86-
$e->setToken($token);
86+
$lastException = $e;
8787

88-
throw $e;
88+
break;
8989
} catch (AuthenticationException $e) {
9090
$lastException = $e;
9191
}

Tests/Authentication/AuthenticationProviderManagerTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
16+
use Symfony\Component\Security\Core\AuthenticationEvents;
17+
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
18+
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
1619
use Symfony\Component\Security\Core\Exception\ProviderNotFoundException;
1720
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1821
use Symfony\Component\Security\Core\Exception\AccountStatusException;
@@ -124,6 +127,50 @@ public function testEraseCredentialFlag()
124127
$this->assertEquals('bar', $token->getCredentials());
125128
}
126129

130+
public function testAuthenticateDispatchesAuthenticationFailureEvent()
131+
{
132+
$token = new UsernamePasswordToken('foo', 'bar', 'key');
133+
$provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock();
134+
$provider->expects($this->once())->method('supports')->willReturn(true);
135+
$provider->expects($this->once())->method('authenticate')->willThrowException($exception = new AuthenticationException());
136+
137+
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
138+
$dispatcher
139+
->expects($this->once())
140+
->method('dispatch')
141+
->with(AuthenticationEvents::AUTHENTICATION_FAILURE, $this->equalTo(new AuthenticationFailureEvent($token, $exception)));
142+
143+
$manager = new AuthenticationProviderManager(array($provider));
144+
$manager->setEventDispatcher($dispatcher);
145+
146+
try {
147+
$manager->authenticate($token);
148+
$this->fail('->authenticate() should rethrow exceptions');
149+
} catch (AuthenticationException $e) {
150+
$this->assertSame($token, $exception->getToken());
151+
}
152+
}
153+
154+
public function testAuthenticateDispatchesAuthenticationSuccessEvent()
155+
{
156+
$token = new UsernamePasswordToken('foo', 'bar', 'key');
157+
158+
$provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock();
159+
$provider->expects($this->once())->method('supports')->willReturn(true);
160+
$provider->expects($this->once())->method('authenticate')->willReturn($token);
161+
162+
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
163+
$dispatcher
164+
->expects($this->once())
165+
->method('dispatch')
166+
->with(AuthenticationEvents::AUTHENTICATION_SUCCESS, $this->equalTo(new AuthenticationEvent($token)));
167+
168+
$manager = new AuthenticationProviderManager(array($provider));
169+
$manager->setEventDispatcher($dispatcher);
170+
171+
$this->assertSame($token, $manager->authenticate($token));
172+
}
173+
127174
protected function getAuthenticationProvider($supports, $token = null, $exception = null)
128175
{
129176
$provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock();

0 commit comments

Comments
 (0)