|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* main checks for airlock authenticator |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\SecurityBundle\Authentication; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\SecurityBundle\Authentication\Strategies\StrategyInterface; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class AirlockAuthenticationKeyAuthenticatorTest |
|
14
|
|
|
* |
|
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
17
|
|
|
* @link http://swisscom.ch |
|
18
|
|
|
*/ |
|
19
|
|
|
class SecurityAuthenticatorTest extends \PHPUnit_Framework_TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject logger */ |
|
22
|
|
|
private $logger; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject logger */ |
|
30
|
|
|
$this->logger = $this->getMockBuilder('\Psr\Log\LoggerInterface') |
|
31
|
|
|
->setMethods(array('warning', 'info')) |
|
32
|
|
|
->getMockForAbstractClass(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @dataProvider stringProvider |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $headerFieldValue value to check with |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testCreateToken($headerFieldValue) |
|
44
|
|
|
{ |
|
45
|
|
|
$userProviderMock = $this |
|
46
|
|
|
->getMockBuilder('Graviton\SecurityBundle\Authentication\Provider\AuthenticationProvider') |
|
47
|
|
|
->disableOriginalConstructor() |
|
48
|
|
|
->setMethods(array('loadUserByUsername')) |
|
49
|
|
|
->getMock(); |
|
50
|
|
|
|
|
51
|
|
|
$strategy = $this->getMockBuilder('\Graviton\SecurityBundle\Authentication\Strategies\StrategyInterface') |
|
52
|
|
|
->setMethods(array('apply')) |
|
53
|
|
|
->getMockForAbstractClass(); |
|
54
|
|
|
$strategy |
|
55
|
|
|
->expects($this->once()) |
|
56
|
|
|
->method('apply') |
|
57
|
|
|
->will($this->returnValue($headerFieldValue)); |
|
58
|
|
|
|
|
59
|
|
|
$authenticator = new SecurityAuthenticator(true, true, true, $userProviderMock, $strategy, $this->logger); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$server = array( |
|
62
|
|
|
'HTTP_X_IDP_USERNAME' => $headerFieldValue, //"example-authentication-header", |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$request = new Request(array(), array(), array(), array(), array(), $server); |
|
66
|
|
|
|
|
67
|
|
|
$token = $authenticator->createToken($request, 'AirlockProviderKey'); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertInstanceOf( |
|
70
|
|
|
'\Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken', |
|
71
|
|
|
$token |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertFalse($token->isAuthenticated()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return array<string> |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function stringProvider() |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
return array( |
|
83
|
|
|
'plain string, no special chars' => array('exampleAuthenticationHeader'), |
|
84
|
|
|
'string with special chars' => array("$-_.+!*'(),{}|\\^~[]`<>#%;/?:@&=."), |
|
85
|
|
|
'string with octal chars' => array("a: \141, A: \101"), |
|
86
|
|
|
'string with hex chars' => array("a: \x61, A: \x41"), |
|
87
|
|
|
'live example' => array("10N0000188"), |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
public function testAuthenticateToken() |
|
95
|
|
|
{ |
|
96
|
|
|
$providerKey = 'some providerKey'; |
|
97
|
|
|
$apiKey = 'exampleAuthenticationHeader'; |
|
98
|
|
|
|
|
99
|
|
|
$securityUserMock = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface') |
|
100
|
|
|
->setMethods(array('getRoles')) |
|
101
|
|
|
->getMockForAbstractClass(); |
|
102
|
|
|
$securityUserMock |
|
103
|
|
|
->expects($this->never()) |
|
104
|
|
|
->method('getRoles') |
|
105
|
|
|
->will($this->returnValue(array('ROLE_GRAVITON_USER'))); |
|
106
|
|
|
|
|
107
|
|
|
$userProviderMock = $this->getProviderMock(array('loadUserByUsername')); |
|
108
|
|
|
$userProviderMock |
|
109
|
|
|
->expects($this->once()) |
|
110
|
|
|
->method('loadUserByUsername') |
|
111
|
|
|
->will($this->returnValue($securityUserMock)); |
|
112
|
|
|
|
|
113
|
|
|
$anonymousToken = new PreAuthenticatedToken( |
|
114
|
|
|
'anon.', |
|
115
|
|
|
$apiKey, |
|
116
|
|
|
$providerKey |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
$authenticator = new SecurityAuthenticator( |
|
120
|
|
|
true, |
|
121
|
|
|
true, |
|
|
|
|
|
|
122
|
|
|
true, |
|
123
|
|
|
$userProviderMock, |
|
|
|
|
|
|
124
|
|
|
$this->getStrategyMock(), |
|
125
|
|
|
$this->logger |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
$token = $authenticator->authenticateToken($anonymousToken, $userProviderMock, $providerKey); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
$this->assertInstanceOf( |
|
131
|
|
|
'\Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken', |
|
132
|
|
|
$token |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertTrue($token->isAuthenticated()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return void |
|
140
|
|
|
*/ |
|
141
|
|
|
public function testAuthenticateTokenExpectingException() |
|
142
|
|
|
{ |
|
143
|
|
|
$providerKey = 'some providerKey'; |
|
144
|
|
|
$apiKey = 'exampleAuthenticationHeader'; |
|
145
|
|
|
|
|
146
|
|
|
$userProviderMock = $this->getProviderMock(array('loadUserByUsername')); |
|
147
|
|
|
$userProviderMock |
|
148
|
|
|
->expects($this->once()) |
|
149
|
|
|
->method('loadUserByUsername') |
|
150
|
|
|
->with($this->equalTo($apiKey)) |
|
151
|
|
|
->will($this->returnValue(false)); |
|
152
|
|
|
|
|
153
|
|
|
$anonymousToken = new PreAuthenticatedToken( |
|
154
|
|
|
'anon.', |
|
155
|
|
|
$apiKey, |
|
156
|
|
|
$providerKey |
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
$authenticator = new SecurityAuthenticator( |
|
160
|
|
|
true, |
|
161
|
|
|
false, |
|
|
|
|
|
|
162
|
|
|
false, |
|
163
|
|
|
$userProviderMock, |
|
|
|
|
|
|
164
|
|
|
$this->getStrategyMock(), |
|
165
|
|
|
$this->logger |
|
166
|
|
|
); |
|
167
|
|
|
|
|
168
|
|
|
$this->setExpectedException('\Symfony\Component\Security\Core\Exception\AuthenticationException'); |
|
169
|
|
|
|
|
170
|
|
|
$authenticator->authenticateToken($anonymousToken, $userProviderMock, $providerKey); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return void |
|
175
|
|
|
*/ |
|
176
|
|
|
public function testSupportsToken() |
|
177
|
|
|
{ |
|
178
|
|
|
$providerKey = 'some providerKey'; |
|
179
|
|
|
$apiKey = 'exampleAuthenticationHeader'; |
|
180
|
|
|
|
|
181
|
|
|
$anonymousToken = new PreAuthenticatedToken( |
|
182
|
|
|
'anon.', |
|
183
|
|
|
$apiKey, |
|
184
|
|
|
$providerKey |
|
185
|
|
|
); |
|
186
|
|
|
|
|
187
|
|
|
$authenticator = new SecurityAuthenticator( |
|
188
|
|
|
true, |
|
189
|
|
|
true, |
|
|
|
|
|
|
190
|
|
|
true, |
|
191
|
|
|
$this->getProviderMock(), |
|
|
|
|
|
|
192
|
|
|
$this->getStrategyMock(), |
|
193
|
|
|
$this->logger |
|
194
|
|
|
); |
|
195
|
|
|
|
|
196
|
|
|
$this->assertTrue($authenticator->supportsToken($anonymousToken, $providerKey)); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return void |
|
201
|
|
|
*/ |
|
202
|
|
|
public function testOnAuthenticationFailure() |
|
203
|
|
|
{ |
|
204
|
|
|
$exceptionDouble = $this->getMockBuilder('\Symfony\Component\Security\Core\Exception\AuthenticationException') |
|
205
|
|
|
->disableOriginalConstructor() |
|
206
|
|
|
->setMethods(array('getMessageKey')) |
|
207
|
|
|
->getMock(); |
|
208
|
|
|
$exceptionDouble |
|
209
|
|
|
->expects($this->once()) |
|
210
|
|
|
->method('getMessageKey') |
|
211
|
|
|
->will($this->returnValue('test_message')); |
|
212
|
|
|
|
|
213
|
|
|
$authenticator = new SecurityAuthenticator( |
|
214
|
|
|
true, |
|
215
|
|
|
true, |
|
|
|
|
|
|
216
|
|
|
true, |
|
217
|
|
|
$this->getProviderMock(), |
|
|
|
|
|
|
218
|
|
|
$this->getStrategyMock(), |
|
219
|
|
|
$this->logger |
|
220
|
|
|
); |
|
221
|
|
|
|
|
222
|
|
|
$response = $authenticator->onAuthenticationFailure(new Request(), $exceptionDouble); |
|
223
|
|
|
|
|
224
|
|
|
$this->assertEquals('test_message', $response->getContent()); |
|
225
|
|
|
$this->assertEquals(511, $response->getStatusCode()); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param string[] $methods methods to mock |
|
230
|
|
|
* |
|
231
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|SecurityAuthenticator |
|
232
|
|
|
*/ |
|
233
|
|
|
private function getProviderMock(array $methods = array()) |
|
234
|
|
|
{ |
|
235
|
|
|
$userProviderMock = $this |
|
236
|
|
|
->getMockBuilder('Graviton\SecurityBundle\Authentication\Provider\AuthenticationProvider') |
|
237
|
|
|
->disableOriginalConstructor() |
|
238
|
|
|
->setMethods($methods) |
|
239
|
|
|
->getMock(); |
|
240
|
|
|
return $userProviderMock; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param array $methods methods to mock |
|
245
|
|
|
* |
|
246
|
|
|
* @return StrategyInterface|\PHPUnit_Framework_MockObject_MockObject |
|
247
|
|
|
*/ |
|
248
|
|
|
private function getStrategyMock(array $methods = array('apply')) |
|
249
|
|
|
{ |
|
250
|
|
|
return $this->getMockBuilder('\Graviton\SecurityBundle\Authentication\Strategies\StrategyInterface') |
|
251
|
|
|
->setMethods($methods) |
|
252
|
|
|
->getMockForAbstractClass(); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: