Skip to content

Commit 51a2195

Browse files
committed
[Security] Fix authentication.failure event not dispatched on AccountStatusException
1 parent 7cc97b6 commit 51a2195

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/Symfony/Component/Security/Core/Authentication/AuthenticationProviderManager.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ public function authenticate(TokenInterface $token)
8282
if (null !== $result) {
8383
break;
8484
}
85-
} catch (AccountStatusException $e) {
86-
$e->setToken($token);
87-
88-
throw $e;
8985
} catch (AuthenticationException $e) {
9086
$lastException = $e;
9187
}

src/Symfony/Component/Security/Core/Tests/Authentication/AuthenticationProviderManagerTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
namespace Symfony\Component\Security\Core\Tests\Authentication;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1516
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
17+
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
18+
use Symfony\Component\Security\Core\AuthenticationEvents;
19+
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
20+
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
1621
use Symfony\Component\Security\Core\Exception\ProviderNotFoundException;
1722
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1823
use Symfony\Component\Security\Core\Exception\AccountStatusException;
@@ -124,6 +129,50 @@ public function testEraseCredentialFlag()
124129
$this->assertEquals('bar', $token->getCredentials());
125130
}
126131

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy