|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Core\Tests\Authentication; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 16 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 17 | +use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager; |
| 18 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 19 | +use Symfony\Component\Security\Core\AuthenticationEvents; |
| 20 | +use Symfony\Component\Security\Core\Event\AuthenticationEvent; |
| 21 | +use Symfony\Component\Security\Core\Event\AuthenticationSensitiveEvent; |
| 22 | +use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
| 23 | + |
| 24 | +class AuthenticationSensitiveEventTest extends TestCase |
| 25 | +{ |
| 26 | + public function testAuthenticateDispatchesAuthenticationSuccessEvents() |
| 27 | + { |
| 28 | + $finalToken = new UsernamePasswordToken('foo', 'bar', 'baz', array('role-01', 'role-02')); |
| 29 | + $priorToken = new UsernamePasswordToken('foo', 'bar', 'baz'); |
| 30 | + |
| 31 | + $provider = $this->getAuthenticationProvider(true, $finalToken); |
| 32 | + $providerCN = get_class($provider); |
| 33 | + |
| 34 | + $dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); |
| 35 | + $dispatcher |
| 36 | + ->expects($this->exactly(2)) |
| 37 | + ->method('dispatch') |
| 38 | + ->withConsecutive(array( |
| 39 | + AuthenticationEvents::AUTHENTICATION_SUCCESS_SENSITIVE, $this->equalTo(new AuthenticationSensitiveEvent($finalToken, $priorToken, $providerCN)), |
| 40 | + ), array( |
| 41 | + AuthenticationEvents::AUTHENTICATION_SUCCESS, $this->equalTo(new AuthenticationEvent($finalToken)), |
| 42 | + )); |
| 43 | + |
| 44 | + $manager = new AuthenticationProviderManager(array($provider)); |
| 45 | + $manager->setEventDispatcher($dispatcher); |
| 46 | + |
| 47 | + $this->assertSame($finalToken, $manager->authenticate($priorToken)); |
| 48 | + } |
| 49 | + |
| 50 | + public function testAuthenticateDispatchesAuthenticationSuccessEventsWithCredentialsAvailableAndRemovedForSuccessiveDispatches() |
| 51 | + { |
| 52 | + $finalToken = $this->getTokenInterfaceMock(); |
| 53 | + $priorToken = $this->getTokenInterfaceMock($credentials = array('password' => 'foobar')); |
| 54 | + |
| 55 | + $provider = $this->getAuthenticationProvider(true, $finalToken); |
| 56 | + $providerCN = get_class($provider); |
| 57 | + |
| 58 | + $dispatcher = new EventDispatcher(); |
| 59 | + $dispatcher->addListener(AuthenticationEvents::AUTHENTICATION_SUCCESS_SENSITIVE, function (AuthenticationSensitiveEvent $event) use ($credentials, $providerCN) { |
| 60 | + $this->assertSame($providerCN, $event->getAuthenticationProviderClassName()); |
| 61 | + $this->assertSame('foobar', $event->getAuthenticationTokenPassword()); |
| 62 | + $this->assertEquals($credentials, $event->getPreAuthenticationToken()->getCredentials()); |
| 63 | + }); |
| 64 | + $dispatcher->addListener(AuthenticationEvents::AUTHENTICATION_SUCCESS, function (AuthenticationEvent $event) { |
| 65 | + $this->assertEquals('', $event->getAuthenticationToken()->getCredentials()); |
| 66 | + }); |
| 67 | + |
| 68 | + $manager = new AuthenticationProviderManager(array($provider)); |
| 69 | + $manager->setEventDispatcher($dispatcher); |
| 70 | + $manager->authenticate($priorToken); |
| 71 | + } |
| 72 | + |
| 73 | + private function getTokenInterfaceMock($credentials = null): TokenInterface |
| 74 | + { |
| 75 | + $token = $this->getMockBuilder(TokenInterface::class)->getMock(); |
| 76 | + |
| 77 | + if (null !== $credentials) { |
| 78 | + $token |
| 79 | + ->expects($this->atLeastOnce()) |
| 80 | + ->method('getCredentials') |
| 81 | + ->will($this->returnValue($credentials)); |
| 82 | + } |
| 83 | + |
| 84 | + return $token; |
| 85 | + } |
| 86 | + |
| 87 | + private function getAuthenticationProvider($supports, $token = null, $exception = null) |
| 88 | + { |
| 89 | + $provider = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock(); |
| 90 | + $provider->expects($this->once()) |
| 91 | + ->method('supports') |
| 92 | + ->will($this->returnValue($supports)) |
| 93 | + ; |
| 94 | + |
| 95 | + if (null !== $token) { |
| 96 | + $provider->expects($this->once()) |
| 97 | + ->method('authenticate') |
| 98 | + ->will($this->returnValue($token)) |
| 99 | + ; |
| 100 | + } elseif (null !== $exception) { |
| 101 | + $provider->expects($this->once()) |
| 102 | + ->method('authenticate') |
| 103 | + ->will($this->throwException($this->getMockBuilder($exception)->setMethods(null)->getMock())) |
| 104 | + ; |
| 105 | + } |
| 106 | + |
| 107 | + return $provider; |
| 108 | + } |
| 109 | +} |
0 commit comments