|
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; |
17 |
| -use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager; |
| 15 | +use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface; |
18 | 16 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
19 |
| -use Symfony\Component\Security\Core\AuthenticationEvents; |
20 |
| -use Symfony\Component\Security\Core\Event\AuthenticationEvent; |
21 | 17 | use Symfony\Component\Security\Core\Event\AuthenticationSensitiveEvent;
|
22 |
| -use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
23 | 18 |
|
24 | 19 | class AuthenticationSensitiveEventTest extends TestCase
|
25 | 20 | {
|
26 |
| - public function testAuthenticateDispatchesAuthenticationSuccessEvents() |
| 21 | + public static function provideTestAccessorMethodsData(): \Iterator |
27 | 22 | {
|
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)); |
| 23 | + $getExtractorFunction = function ($default = null) { |
| 24 | + return function ($token, $event) use ($default): ?string { |
| 25 | + self::assertInstanceOf(TokenInterface::class, $token); |
| 26 | + self::assertInstanceOf(AuthenticationSensitiveEvent::class, $event); |
| 27 | + |
| 28 | + if (method_exists($token, 'getCredentials')) { |
| 29 | + $c = $token->getCredentials(); |
| 30 | + |
| 31 | + if ($c instanceof \Closure) { |
| 32 | + return ($c)(); |
| 33 | + } |
| 34 | + |
| 35 | + if (method_exists($c, 'getInnerCredentials')) { |
| 36 | + return $c->getInnerCredentials(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return $c ?? $default; |
| 41 | + }; |
| 42 | + }; |
| 43 | + |
| 44 | + $getHasCastableClass = function ($return = null) { |
| 45 | + return new class($return) { |
| 46 | + private $inner; |
| 47 | + |
| 48 | + public function __construct(?string $return = null) |
| 49 | + { |
| 50 | + $this->return = $return; |
| 51 | + } |
| 52 | + |
| 53 | + public function __toString(): string |
| 54 | + { |
| 55 | + return $this->return ?? ''; |
| 56 | + } |
| 57 | + }; |
| 58 | + }; |
| 59 | + |
| 60 | + $getNotCastableClass = function ($return = null) { |
| 61 | + return new class($return) { |
| 62 | + private $return; |
| 63 | + |
| 64 | + public function __construct(?string $return = null) |
| 65 | + { |
| 66 | + $this->return = $return; |
| 67 | + } |
| 68 | + |
| 69 | + public function getInnerCredentials(): string |
| 70 | + { |
| 71 | + return $this->return ?? ''; |
| 72 | + } |
| 73 | + }; |
| 74 | + }; |
| 75 | + |
| 76 | + $getTokenCredentialsValue = function ($return = null) { |
| 77 | + return function () use ($return) { |
| 78 | + return $return; |
| 79 | + }; |
| 80 | + }; |
| 81 | + |
| 82 | + // expects credential password of "null" type |
| 83 | + yield array(null); |
| 84 | + yield array(null, $getHasCastableClass('')); |
| 85 | + yield array(null, $getNotCastableClass('foo')); |
| 86 | + yield array(null, array('unknown-index-foo' => 'foo')); |
| 87 | + yield array(null, null, $getHasCastableClass('')); |
| 88 | + yield array(null, null, $getNotCastableClass('foo')); |
| 89 | + yield array(null, null, array('unknown-index-bar' => 'bar')); |
| 90 | + yield array(null, null, null, $getExtractorFunction(null)); |
| 91 | + |
| 92 | + // expects credential password of "foo" value |
| 93 | + yield array('foo', 'foo'); |
| 94 | + yield array('foo', 'foo', 'bar'); |
| 95 | + yield array('foo', $getHasCastableClass('foo')); |
| 96 | + yield array('foo', $getNotCastableClass('foo'), null, $getExtractorFunction()); |
| 97 | + yield array('foo', $getTokenCredentialsValue('foo'), null, $getExtractorFunction()); |
| 98 | + |
| 99 | + // expects credential password of "bar" value |
| 100 | + yield array('bar', null, 'bar'); |
| 101 | + yield array('bar', null, $getHasCastableClass('bar')); |
| 102 | + yield array('bar', null, $getNotCastableClass('bar'), $getExtractorFunction()); |
| 103 | + yield array('bar', null, $getTokenCredentialsValue('bar'), $getExtractorFunction()); |
| 104 | + |
| 105 | + // expects credential password of "baz" value |
| 106 | + yield array('baz', null, null, $getExtractorFunction('baz')); |
| 107 | + |
| 108 | + // expects array value will be extracted for all supported indexes |
| 109 | + foreach (array('password', 'secret', 'api_key') as $index) { |
| 110 | + // expects credential password of "null" type |
| 111 | + yield array(null, array($index => null)); |
| 112 | + yield array(null, null, array($index => '')); |
| 113 | + yield array(null, array($index => ''), array($index => null)); |
| 114 | + |
| 115 | + // expects credential password of "foo" value |
| 116 | + yield array('foo', array($index => 'foo')); |
| 117 | + yield array('foo', array($index => 'foo'), array($index => null)); |
| 118 | + yield array('foo', array($index => 'foo'), array($index => '')); |
| 119 | + yield array('foo', array($index => 'foo'), array('unknown-index-bar' => 'bar')); |
| 120 | + yield array('foo', array($index => 'foo'), array($index => 'bar')); |
| 121 | + |
| 122 | + // expects credential password of "bar" value |
| 123 | + yield array('bar', null, array($index => 'bar')); |
| 124 | + yield array('bar', array($index => null), array($index => 'bar')); |
| 125 | + yield array('bar', array($index => ''), array($index => 'bar')); |
| 126 | + yield array('bar', array('unknown-index-foo' => 'foo'), array($index => 'bar')); |
| 127 | + yield array('bar', array($index => $getNotCastableClass), array($index => 'bar')); |
| 128 | + } |
48 | 129 | }
|
49 | 130 |
|
50 |
| - public function testAuthenticateDispatchesAuthenticationSuccessEventsWithCredentialsAvailableAndRemovedForSuccessiveDispatches() |
| 131 | + /** |
| 132 | + * @dataProvider provideTestAccessorMethodsData |
| 133 | + * |
| 134 | + * @param string $expectedPassword |
| 135 | + * @param string|array|null $finalCredentials |
| 136 | + * @param string|array|null $priorCredentials |
| 137 | + * @param \Closure|null $passwordExtractor |
| 138 | + */ |
| 139 | + public function testAccessorMethods(string $expectedPassword = null, $finalCredentials = null, $priorCredentials = null, \Closure $passwordExtractor = null) |
51 | 140 | {
|
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); |
| 141 | + $event = new AuthenticationSensitiveEvent( |
| 142 | + $priorToken = $this->getTokenInterfaceMock($priorCredentials), |
| 143 | + $finalToken = $this->getTokenInterfaceMock($finalCredentials), |
| 144 | + AuthenticationProviderInterface::class |
| 145 | + ); |
| 146 | + |
| 147 | + $this->assertSame($priorToken, $event->getPreAuthenticationToken()); |
| 148 | + $this->assertSame($finalToken, $event->getAuthenticationToken()); |
| 149 | + $this->assertSame(AuthenticationProviderInterface::class, $event->getAuthenticationProviderClassName()); |
| 150 | + $this->assertSame($expectedPassword, $event->getAuthenticationTokenPassword($passwordExtractor)); |
71 | 151 | }
|
72 | 152 |
|
73 | 153 | private function getTokenInterfaceMock($credentials = null): TokenInterface
|
74 | 154 | {
|
75 |
| - $token = $this->getMockBuilder(TokenInterface::class)->getMock(); |
| 155 | + $token = $this |
| 156 | + ->getMockBuilder(TokenInterface::class) |
| 157 | + ->getMock(); |
76 | 158 |
|
77 |
| - if (null !== $credentials) { |
78 |
| - $token |
79 |
| - ->expects($this->atLeastOnce()) |
80 |
| - ->method('getCredentials') |
81 |
| - ->will($this->returnValue($credentials)); |
82 |
| - } |
| 159 | + $token->expects($this->any()) |
| 160 | + ->method('getCredentials') |
| 161 | + ->will($this->returnValue($credentials)); |
83 | 162 |
|
84 | 163 | return $token;
|
85 | 164 | }
|
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 | 165 | }
|
0 commit comments