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