|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Server\Api; |
|
20
|
|
|
|
|
21
|
|
|
use Base32\Base32; |
|
22
|
|
|
use Otp\Otp; |
|
23
|
|
|
use PDO; |
|
24
|
|
|
use PHPUnit_Framework_TestCase; |
|
25
|
|
|
use SURFnet\VPN\Common\Config; |
|
26
|
|
|
use SURFnet\VPN\Common\Http\BasicAuthenticationHook; |
|
27
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
28
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
29
|
|
|
use SURFnet\VPN\Server\Acl\Provider\StaticProvider; |
|
30
|
|
|
use SURFnet\VPN\Server\Storage; |
|
31
|
|
|
|
|
32
|
|
|
class UsersModuleTest extends PHPUnit_Framework_TestCase |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var \SURFnet\VPN\Common\Http\Service */ |
|
35
|
|
|
private $service; |
|
36
|
|
|
|
|
37
|
|
|
public function setUp() |
|
38
|
|
|
{ |
|
39
|
|
|
$random = $this->getMockBuilder('SURFnet\VPN\Common\RandomInterface')->getMock(); |
|
40
|
|
|
$random->method('get')->will($this->onConsecutiveCalls('random_1', 'random_2')); |
|
41
|
|
|
|
|
42
|
|
|
$storage = new Storage( |
|
43
|
|
|
new PDO('sqlite::memory:'), |
|
44
|
|
|
$random |
|
45
|
|
|
); |
|
46
|
|
|
$storage->init(); |
|
47
|
|
|
|
|
48
|
|
|
$storage->addCertificate('foo', 'abcd1234', 'ABCD1234', 12345678, 23456789); |
|
49
|
|
|
$storage->disableUser('bar'); |
|
50
|
|
|
$storage->setTotpSecret('bar', 'CN2XAL23SIFTDFXZ'); |
|
51
|
|
|
$storage->setVootToken('bar', '123456'); |
|
52
|
|
|
|
|
53
|
|
|
$config = Config::fromFile(sprintf('%s/data/user_groups_config.yaml', __DIR__)); |
|
54
|
|
|
$groupProviders = [ |
|
55
|
|
|
new StaticProvider( |
|
56
|
|
|
new Config($config->v('groupProviders', 'StaticProvider')) |
|
57
|
|
|
), |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
$this->service = new Service(); |
|
61
|
|
|
$this->service->addModule( |
|
62
|
|
|
new UsersModule( |
|
63
|
|
|
$storage, |
|
64
|
|
|
$groupProviders |
|
65
|
|
|
) |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$bearerAuthentication = new BasicAuthenticationHook( |
|
69
|
|
|
[ |
|
70
|
|
|
'vpn-user-portal' => 'aabbcc', |
|
71
|
|
|
'vpn-admin-portal' => 'bbccdd', |
|
72
|
|
|
] |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$this->service->addBeforeHook('auth', $bearerAuthentication); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testListUsers() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->assertSame( |
|
81
|
|
|
[ |
|
82
|
|
|
[ |
|
83
|
|
|
'user_id' => 'foo', |
|
84
|
|
|
'is_disabled' => false, |
|
85
|
|
|
], |
|
86
|
|
|
[ |
|
87
|
|
|
'user_id' => 'bar', |
|
88
|
|
|
'is_disabled' => false, |
|
89
|
|
|
], |
|
90
|
|
|
], |
|
91
|
|
|
$this->makeRequest( |
|
92
|
|
|
['vpn-admin-portal', 'bbccdd'], |
|
93
|
|
|
'GET', |
|
94
|
|
|
'user_list', |
|
95
|
|
|
['profile_id' => 'internet'], |
|
96
|
|
|
[] |
|
97
|
|
|
) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
View Code Duplication |
public function testSetTotpSecret() |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
$otp = new Otp(); |
|
104
|
|
|
$totpSecret = 'MM7TTLHPA7WZOJFB'; |
|
105
|
|
|
$totpKey = $otp->totp(Base32::decode($totpSecret)); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertTrue( |
|
108
|
|
|
$this->makeRequest( |
|
109
|
|
|
['vpn-user-portal', 'aabbcc'], |
|
110
|
|
|
'POST', |
|
111
|
|
|
'set_totp_secret', |
|
112
|
|
|
[], |
|
113
|
|
|
[ |
|
114
|
|
|
'user_id' => 'foo', |
|
115
|
|
|
'totp_secret' => $totpSecret, |
|
116
|
|
|
'totp_key' => $totpKey, |
|
117
|
|
|
] |
|
118
|
|
|
) |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
View Code Duplication |
public function testVerifyOtpKey() |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$otp = new Otp(); |
|
125
|
|
|
$totpSecret = 'CN2XAL23SIFTDFXZ'; |
|
126
|
|
|
$totpKey = $otp->totp(Base32::decode($totpSecret)); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertTrue( |
|
129
|
|
|
$this->makeRequest( |
|
130
|
|
|
['vpn-user-portal', 'aabbcc'], |
|
131
|
|
|
'POST', |
|
132
|
|
|
'verify_totp_key', |
|
133
|
|
|
[], |
|
134
|
|
|
[ |
|
135
|
|
|
'user_id' => 'bar', |
|
136
|
|
|
'totp_key' => $totpKey, |
|
137
|
|
|
] |
|
138
|
|
|
) |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testHasTotpSecret() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->assertTrue( |
|
145
|
|
|
$this->makeRequest( |
|
146
|
|
|
['vpn-user-portal', 'aabbcc'], |
|
147
|
|
|
'GET', |
|
148
|
|
|
'has_totp_secret', |
|
149
|
|
|
[ |
|
150
|
|
|
'user_id' => 'bar', |
|
151
|
|
|
], |
|
152
|
|
|
[] |
|
153
|
|
|
) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
View Code Duplication |
public function testDeleteTotpSecret() |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
$this->assertTrue( |
|
160
|
|
|
$this->makeRequest( |
|
161
|
|
|
['vpn-admin-portal', 'bbccdd'], |
|
162
|
|
|
'POST', |
|
163
|
|
|
'delete_totp_secret', |
|
164
|
|
|
[], |
|
165
|
|
|
[ |
|
166
|
|
|
'user_id' => 'bar', |
|
167
|
|
|
] |
|
168
|
|
|
) |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function testSetVootToken() |
|
173
|
|
|
{ |
|
174
|
|
|
$this->assertTrue( |
|
175
|
|
|
$this->makeRequest( |
|
176
|
|
|
['vpn-user-portal', 'aabbcc'], |
|
177
|
|
|
'POST', |
|
178
|
|
|
'set_voot_token', |
|
179
|
|
|
[], |
|
180
|
|
|
[ |
|
181
|
|
|
'user_id' => 'foo', |
|
182
|
|
|
'voot_token' => 'bar', |
|
183
|
|
|
] |
|
184
|
|
|
) |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
View Code Duplication |
public function testDeleteVootToken() |
|
|
|
|
|
|
189
|
|
|
{ |
|
190
|
|
|
$this->assertTrue( |
|
191
|
|
|
$this->makeRequest( |
|
192
|
|
|
['vpn-admin-portal', 'bbccdd'], |
|
193
|
|
|
'POST', |
|
194
|
|
|
'delete_voot_token', |
|
195
|
|
|
[], |
|
196
|
|
|
[ |
|
197
|
|
|
'user_id' => 'bar', |
|
198
|
|
|
] |
|
199
|
|
|
) |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
View Code Duplication |
public function testDisableUser() |
|
|
|
|
|
|
204
|
|
|
{ |
|
205
|
|
|
$this->assertTrue( |
|
206
|
|
|
$this->makeRequest( |
|
207
|
|
|
['vpn-admin-portal', 'bbccdd'], |
|
208
|
|
|
'POST', |
|
209
|
|
|
'disable_user', |
|
210
|
|
|
[], |
|
211
|
|
|
[ |
|
212
|
|
|
'user_id' => 'foo', |
|
213
|
|
|
] |
|
214
|
|
|
) |
|
215
|
|
|
); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
View Code Duplication |
public function testEnableUser() |
|
|
|
|
|
|
219
|
|
|
{ |
|
220
|
|
|
$this->assertTrue( |
|
221
|
|
|
$this->makeRequest( |
|
222
|
|
|
['vpn-admin-portal', 'bbccdd'], |
|
223
|
|
|
'POST', |
|
224
|
|
|
'enable_user', |
|
225
|
|
|
[], |
|
226
|
|
|
[ |
|
227
|
|
|
'user_id' => 'bar', |
|
228
|
|
|
] |
|
229
|
|
|
) |
|
230
|
|
|
); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
View Code Duplication |
public function testDeleteUser() |
|
|
|
|
|
|
234
|
|
|
{ |
|
235
|
|
|
$this->assertTrue( |
|
236
|
|
|
$this->makeRequest( |
|
237
|
|
|
['vpn-admin-portal', 'bbccdd'], |
|
238
|
|
|
'POST', |
|
239
|
|
|
'delete_user', |
|
240
|
|
|
[], |
|
241
|
|
|
[ |
|
242
|
|
|
'user_id' => 'foo', |
|
243
|
|
|
] |
|
244
|
|
|
) |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
public function testUserGroups() |
|
249
|
|
|
{ |
|
250
|
|
|
$this->assertSame( |
|
251
|
|
|
[ |
|
252
|
|
|
[ |
|
253
|
|
|
'id' => 'all', |
|
254
|
|
|
'displayName' => 'All', |
|
255
|
|
|
], |
|
256
|
|
|
[ |
|
257
|
|
|
'id' => 'employees', |
|
258
|
|
|
'displayName' => 'Employees', |
|
259
|
|
|
], |
|
260
|
|
|
], |
|
261
|
|
|
$this->makeRequest( |
|
262
|
|
|
['vpn-user-portal', 'aabbcc'], |
|
263
|
|
|
'GET', |
|
264
|
|
|
'user_groups', |
|
265
|
|
|
[ |
|
266
|
|
|
'user_id' => 'bar', |
|
267
|
|
|
], |
|
268
|
|
|
[] |
|
269
|
|
|
) |
|
270
|
|
|
); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
View Code Duplication |
private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = []) |
|
|
|
|
|
|
274
|
|
|
{ |
|
275
|
|
|
$response = $this->service->run( |
|
276
|
|
|
new Request( |
|
277
|
|
|
[ |
|
278
|
|
|
'SERVER_PORT' => 80, |
|
279
|
|
|
'SERVER_NAME' => 'vpn.example', |
|
280
|
|
|
'REQUEST_METHOD' => $requestMethod, |
|
281
|
|
|
'PATH_INFO' => sprintf('/%s', $pathInfo), |
|
282
|
|
|
'REQUEST_URI' => sprintf('/%s', $pathInfo), |
|
283
|
|
|
'PHP_AUTH_USER' => $basicAuth[0], |
|
284
|
|
|
'PHP_AUTH_PW' => $basicAuth[1], |
|
285
|
|
|
], |
|
286
|
|
|
$getData, |
|
287
|
|
|
$postData |
|
288
|
|
|
) |
|
289
|
|
|
); |
|
290
|
|
|
|
|
291
|
|
|
$responseArray = json_decode($response->getBody(), true)[$pathInfo]; |
|
292
|
|
|
if ($responseArray['ok']) { |
|
293
|
|
|
if (array_key_exists('data', $responseArray)) { |
|
294
|
|
|
return $responseArray['data']; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return true; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
// in case of errors... |
|
301
|
|
|
return $responseArray; |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.