|
12 | 12 | namespace Symfony\Component\Security\Core\Tests\Authentication;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 16 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15 | 17 | use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
|
| 18 | +use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface; |
16 | 19 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
17 | 20 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
18 | 21 | use Symfony\Component\Security\Core\AuthenticationEvents;
|
19 | 22 | use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
|
| 23 | +use Symfony\Component\Security\Core\Event\AuthenticationSensitiveEvent; |
20 | 24 | use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
|
21 | 25 | use Symfony\Component\Security\Core\Exception\AccountStatusException;
|
22 | 26 | use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
@@ -152,29 +156,58 @@ public function testAuthenticateDispatchesAuthenticationFailureEvent()
|
152 | 156 | }
|
153 | 157 | }
|
154 | 158 |
|
155 |
| - public function testAuthenticateDispatchesAuthenticationSuccessEvent() |
| 159 | + public function testAuthenticateDispatchesAuthenticationSuccessEvents() |
156 | 160 | {
|
157 |
| - $token = new UsernamePasswordToken('foo', 'bar', 'key'); |
| 161 | + $finalToken = new UsernamePasswordToken('foo', 'bar', 'baz', ['role-01', 'role-02']); |
| 162 | + $priorToken = new UsernamePasswordToken('foo', 'bar', 'baz'); |
158 | 163 |
|
159 |
| - $provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock(); |
160 |
| - $provider->expects($this->once())->method('supports')->willReturn(true); |
161 |
| - $provider->expects($this->once())->method('authenticate')->willReturn($token); |
| 164 | + $provider = $this->getAuthenticationProvider(true, $finalToken); |
| 165 | + $providerCN = \get_class($provider); |
162 | 166 |
|
163 |
| - $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); |
| 167 | + $dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); |
164 | 168 | $dispatcher
|
165 |
| - ->expects($this->once()) |
| 169 | + ->expects($this->exactly(2)) |
166 | 170 | ->method('dispatch')
|
167 |
| - ->with($this->equalTo(new AuthenticationSuccessEvent($token)), AuthenticationEvents::AUTHENTICATION_SUCCESS); |
| 171 | + ->withConsecutive([ |
| 172 | + $this->equalTo(new AuthenticationSensitiveEvent($priorToken, $finalToken, $providerCN)), AuthenticationEvents::AUTHENTICATION_SUCCESS_SENSITIVE, |
| 173 | + ], [ |
| 174 | + $this->equalTo(new AuthenticationSuccessEvent($finalToken)), AuthenticationEvents::AUTHENTICATION_SUCCESS, |
| 175 | + ]); |
168 | 176 |
|
169 | 177 | $manager = new AuthenticationProviderManager([$provider]);
|
170 | 178 | $manager->setEventDispatcher($dispatcher);
|
171 | 179 |
|
172 |
| - $this->assertSame($token, $manager->authenticate($token)); |
| 180 | + $this->assertSame($finalToken, $manager->authenticate($priorToken)); |
| 181 | + } |
| 182 | + |
| 183 | + public function testAuthenticateDispatchesAuthenticationSuccessEventsWithCredentialsAvailableAndRemovedForSuccessiveDispatches() |
| 184 | + { |
| 185 | + $finalToken = new UsernamePasswordToken('foo', 'bar', 'baz', ['role-01', 'role-02']); |
| 186 | + $priorToken = new UsernamePasswordToken('foo', 'bar', 'baz'); |
| 187 | + |
| 188 | + $provider = $this->getAuthenticationProvider(true, $finalToken); |
| 189 | + $providerCN = \get_class($provider); |
| 190 | + |
| 191 | + $dispatcher = new EventDispatcher(); |
| 192 | + $dispatcher->addListener(AuthenticationEvents::AUTHENTICATION_SUCCESS_SENSITIVE, function (AuthenticationSensitiveEvent $event) use ($providerCN) { |
| 193 | + $this->assertSame($providerCN, $event->getAuthenticationProviderClassName()); |
| 194 | + $this->assertSame('bar', $event->getAuthenticationTokenPassword()); |
| 195 | + $this->assertEquals('bar', $event->getPreAuthenticationToken()->getCredentials()); |
| 196 | + $this->assertEquals('bar', $event->getAuthenticationToken()->getCredentials()); |
| 197 | + }); |
| 198 | + $dispatcher->addListener(AuthenticationEvents::AUTHENTICATION_SUCCESS, function (AuthenticationSuccessEvent $event) { |
| 199 | + $this->assertEquals('', $event->getAuthenticationToken()->getCredentials()); |
| 200 | + }); |
| 201 | + |
| 202 | + $manager = new AuthenticationProviderManager([$provider]); |
| 203 | + $manager->setEventDispatcher($dispatcher); |
| 204 | + |
| 205 | + $this->assertSame($finalToken, $manager->authenticate($priorToken)); |
173 | 206 | }
|
174 | 207 |
|
175 | 208 | protected function getAuthenticationProvider($supports, $token = null, $exception = null)
|
176 | 209 | {
|
177 |
| - $provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock(); |
| 210 | + $provider = $this->getMockBuilder(AuthenticationProviderInterface::class)->getMock(); |
178 | 211 | $provider->expects($this->once())
|
179 | 212 | ->method('supports')
|
180 | 213 | ->will($this->returnValue($supports))
|
|
0 commit comments