|
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 SURFnet\VPN\Common\Config; |
|
22
|
|
|
use SURFnet\VPN\Common\Http\ApiErrorResponse; |
|
23
|
|
|
use SURFnet\VPN\Common\Http\ApiResponse; |
|
24
|
|
|
use SURFnet\VPN\Common\Http\AuthUtils; |
|
25
|
|
|
use SURFnet\VPN\Common\Http\InputValidation; |
|
26
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
27
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
28
|
|
|
use SURFnet\VPN\Common\Http\ServiceModuleInterface; |
|
29
|
|
|
use SURFnet\VPN\Server\Exception\TotpException; |
|
30
|
|
|
use SURFnet\VPN\Server\Exception\YubiKeyException; |
|
31
|
|
|
use SURFnet\VPN\Server\Storage; |
|
32
|
|
|
use SURFnet\VPN\Server\Totp; |
|
33
|
|
|
use SURFnet\VPN\Server\YubiKey; |
|
34
|
|
|
|
|
35
|
|
|
class UsersModule implements ServiceModuleInterface |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var \SURFnet\VPN\Common\Config */ |
|
38
|
|
|
private $config; |
|
39
|
|
|
|
|
40
|
|
|
/** @var \SURFnet\VPN\Server\Storage */ |
|
41
|
|
|
private $storage; |
|
42
|
|
|
|
|
43
|
|
|
/** @var array */ |
|
44
|
|
|
private $groupProviders; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct(Config $config, Storage $storage, array $groupProviders) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->config = $config; |
|
49
|
|
|
$this->storage = $storage; |
|
50
|
|
|
$this->groupProviders = $groupProviders; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function init(Service $service) |
|
54
|
|
|
{ |
|
55
|
|
|
$service->get( |
|
56
|
|
|
'/user_list', |
|
57
|
|
|
function (Request $request, array $hookData) { |
|
58
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
59
|
|
|
|
|
60
|
|
|
return new ApiResponse('user_list', $this->storage->getUsers()); |
|
61
|
|
|
} |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$service->post( |
|
65
|
|
|
'/set_yubi_key_id', |
|
66
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
67
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
|
68
|
|
|
|
|
69
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
70
|
|
|
$yubiKeyOtp = InputValidation::yubiKeyOtp($request->getPostParameter('yubi_key_otp')); |
|
71
|
|
|
|
|
72
|
|
|
// check if there is already a YubiKey ID registered for this user |
|
73
|
|
|
if ($this->storage->hasYubiKeyId($userId)) { |
|
74
|
|
|
return new ApiErrorResponse('set_yubi_key_id', 'YubiKey ID already set'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$yubiKey = new YubiKey(); |
|
78
|
|
|
try { |
|
79
|
|
|
$yubiKeyId = $yubiKey->verify($userId, $yubiKeyOtp); |
|
80
|
|
|
$this->storage->setYubiKeyId($userId, $yubiKeyId); |
|
81
|
|
|
$this->storage->addUserMessage($userId, 'notification', sprintf('YubiKey ID "%s" registered', $yubiKeyId)); |
|
82
|
|
|
|
|
83
|
|
|
return new ApiResponse('set_yubi_key_id'); |
|
84
|
|
|
} catch (YubiKeyException $e) { |
|
85
|
|
|
$msg = sprintf('YubiKey OTP verification failed: %s', $e->getMessage()); |
|
86
|
|
|
$this->storage->addUserMessage($userId, 'notification', $msg); |
|
87
|
|
|
|
|
88
|
|
|
return new ApiErrorResponse('set_yubi_key_id', $msg); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$service->post( |
|
94
|
|
|
'/verify_yubi_key_otp', |
|
95
|
|
|
function (Request $request, array $hookData) { |
|
96
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
97
|
|
|
|
|
98
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
99
|
|
|
$yubiKeyOtp = InputValidation::yubiKeyOtp($request->getPostParameter('yubi_key_otp')); |
|
100
|
|
|
$yubiKeyId = $this->storage->getYubiKeyId($userId); |
|
101
|
|
|
|
|
102
|
|
|
// XXX make sure we have a registered yubiKeyID first?! |
|
103
|
|
|
|
|
104
|
|
|
$yubiKey = new YubiKey(); |
|
105
|
|
|
try { |
|
106
|
|
|
$yubiKey->verify($userId, $yubiKeyOtp, $yubiKeyId); |
|
107
|
|
|
|
|
108
|
|
|
return new ApiResponse('verify_yubi_key_otp'); |
|
109
|
|
|
} catch (YubiKeyException $e) { |
|
110
|
|
|
$msg = sprintf('YubiKey OTP verification failed: %s', $e->getMessage()); |
|
111
|
|
|
$this->storage->addUserMessage($userId, 'notification', $msg); |
|
112
|
|
|
|
|
113
|
|
|
return new ApiErrorResponse('verify_yubi_key_otp', $msg); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
$service->get( |
|
119
|
|
|
'/has_yubi_key_id', |
|
120
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
121
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
122
|
|
|
|
|
123
|
|
|
$userId = InputValidation::userId($request->getQueryParameter('user_id')); |
|
124
|
|
|
|
|
125
|
|
|
return new ApiResponse('has_yubi_key_id', $this->storage->hasYubiKeyId($userId)); |
|
126
|
|
|
} |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$service->get( |
|
130
|
|
|
'/yubi_key_id', |
|
131
|
|
|
function (Request $request, array $hookData) { |
|
132
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
133
|
|
|
|
|
134
|
|
|
$userId = InputValidation::userId($request->getQueryParameter('user_id')); |
|
135
|
|
|
|
|
136
|
|
|
$yubiKeyId = $this->storage->getYubiKeyId($userId); |
|
137
|
|
|
if (is_null($yubiKeyId)) { |
|
138
|
|
|
return new ApiResponse('yubi_key_id', false); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return new ApiResponse('yubi_key_id', $yubiKeyId); |
|
142
|
|
|
} |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$service->post( |
|
146
|
|
|
'/delete_yubi_key_id', |
|
147
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
148
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
149
|
|
|
|
|
150
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
151
|
|
|
|
|
152
|
|
|
$yubiKeyId = $this->storage->getYubiKeyId($userId); |
|
153
|
|
|
$this->storage->deleteYubiKeyId($userId); |
|
154
|
|
|
$this->storage->addUserMessage($userId, 'notification', sprintf('YubiKey ID "%s" deleted', $yubiKeyId)); |
|
155
|
|
|
|
|
156
|
|
|
return new ApiResponse('delete_yubi_key_id'); |
|
157
|
|
|
} |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
$service->post( |
|
161
|
|
|
'/set_totp_secret', |
|
162
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
163
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
|
164
|
|
|
|
|
165
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
166
|
|
|
$totpKey = InputValidation::totpKey($request->getPostParameter('totp_key')); |
|
167
|
|
|
$totpSecret = InputValidation::totpSecret($request->getPostParameter('totp_secret')); |
|
168
|
|
|
|
|
169
|
|
|
// check if there is already a TOTP secret registered for this user |
|
170
|
|
|
if ($this->storage->hasTotpSecret($userId)) { |
|
171
|
|
|
return new ApiErrorResponse('set_totp_secret', 'TOTP secret already set'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$totp = new Totp($this->storage); |
|
175
|
|
|
try { |
|
176
|
|
|
$totp->verify($userId, $totpKey, $totpSecret); |
|
177
|
|
|
} catch (TotpException $e) { |
|
178
|
|
|
$msg = sprintf('TOTP verification failed: %s', $e->getMessage()); |
|
179
|
|
|
$this->storage->addUserMessage($userId, 'notification', $msg); |
|
180
|
|
|
|
|
181
|
|
|
return new ApiErrorResponse('set_totp_secret', $msg); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$this->storage->setTotpSecret($userId, $totpSecret); |
|
185
|
|
|
$this->storage->addUserMessage($userId, 'notification', 'TOTP secret registered'); |
|
186
|
|
|
|
|
187
|
|
|
return new ApiResponse('set_totp_secret'); |
|
188
|
|
|
} |
|
189
|
|
|
); |
|
190
|
|
|
|
|
191
|
|
|
$service->post( |
|
192
|
|
|
'/verify_totp_key', |
|
193
|
|
|
function (Request $request, array $hookData) { |
|
194
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
195
|
|
|
|
|
196
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
197
|
|
|
$totpKey = InputValidation::totpKey($request->getPostParameter('totp_key')); |
|
198
|
|
|
|
|
199
|
|
|
$totp = new Totp($this->storage); |
|
200
|
|
|
try { |
|
201
|
|
|
$totp->verify($userId, $totpKey); |
|
202
|
|
|
} catch (TotpException $e) { |
|
203
|
|
|
$msg = sprintf('TOTP validation failed: %s', $e->getMessage()); |
|
204
|
|
|
$this->storage->addUserMessage($userId, 'notification', $msg); |
|
205
|
|
|
|
|
206
|
|
|
return new ApiErrorResponse('verify_totp_key', $msg); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return new ApiResponse('verify_totp_key'); |
|
210
|
|
|
} |
|
211
|
|
|
); |
|
212
|
|
|
|
|
213
|
|
|
$service->get( |
|
214
|
|
|
'/has_totp_secret', |
|
215
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
216
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
217
|
|
|
|
|
218
|
|
|
$userId = InputValidation::userId($request->getQueryParameter('user_id')); |
|
219
|
|
|
|
|
220
|
|
|
return new ApiResponse('has_totp_secret', $this->storage->hasTotpSecret($userId)); |
|
221
|
|
|
} |
|
222
|
|
|
); |
|
223
|
|
|
|
|
224
|
|
|
$service->post( |
|
225
|
|
|
'/delete_totp_secret', |
|
226
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
227
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
228
|
|
|
|
|
229
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
230
|
|
|
|
|
231
|
|
|
$this->storage->deleteTotpSecret($userId); |
|
232
|
|
|
$this->storage->addUserMessage($userId, 'notification', 'TOTP secret deleted'); |
|
233
|
|
|
|
|
234
|
|
|
return new ApiResponse('delete_totp_secret'); |
|
235
|
|
|
} |
|
236
|
|
|
); |
|
237
|
|
|
|
|
238
|
|
|
$service->post( |
|
239
|
|
|
'/set_voot_token', |
|
240
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
241
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
|
242
|
|
|
|
|
243
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
244
|
|
|
$vootToken = InputValidation::vootToken($request->getPostParameter('voot_token')); |
|
245
|
|
|
$this->storage->setVootToken($userId, $vootToken); |
|
246
|
|
|
|
|
247
|
|
|
return new ApiResponse('set_voot_token'); |
|
248
|
|
|
} |
|
249
|
|
|
); |
|
250
|
|
|
|
|
251
|
|
|
$service->post( |
|
252
|
|
|
'/delete_voot_token', |
|
253
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
254
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
255
|
|
|
|
|
256
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
257
|
|
|
$this->storage->deleteVootToken($userId); |
|
258
|
|
|
|
|
259
|
|
|
return new ApiResponse('delete_voot_token'); |
|
260
|
|
|
} |
|
261
|
|
|
); |
|
262
|
|
|
|
|
263
|
|
|
$service->get( |
|
264
|
|
|
'/has_voot_token', |
|
265
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
266
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
267
|
|
|
|
|
268
|
|
|
$userId = InputValidation::userId($request->getQueryParameter('user_id')); |
|
269
|
|
|
|
|
270
|
|
|
return new ApiResponse('has_voot_token', $this->storage->hasVootToken($userId)); |
|
271
|
|
|
} |
|
272
|
|
|
); |
|
273
|
|
|
|
|
274
|
|
|
$service->get( |
|
275
|
|
|
'/is_disabled_user', |
|
276
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
277
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal', 'vpn-user-portal']); |
|
278
|
|
|
|
|
279
|
|
|
$userId = InputValidation::userId($request->getQueryParameter('user_id')); |
|
280
|
|
|
|
|
281
|
|
|
return new ApiResponse('is_disabled_user', $this->storage->isDisabledUser($userId)); |
|
282
|
|
|
} |
|
283
|
|
|
); |
|
284
|
|
|
|
|
285
|
|
|
$service->post( |
|
286
|
|
|
'/disable_user', |
|
287
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
288
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
289
|
|
|
|
|
290
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
291
|
|
|
|
|
292
|
|
|
$this->storage->disableUser($userId); |
|
293
|
|
|
$this->storage->addUserMessage($userId, 'notification', 'account disabled'); |
|
294
|
|
|
|
|
295
|
|
|
return new ApiResponse('disable_user'); |
|
296
|
|
|
} |
|
297
|
|
|
); |
|
298
|
|
|
|
|
299
|
|
|
$service->post( |
|
300
|
|
|
'/enable_user', |
|
301
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
302
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
303
|
|
|
|
|
304
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
305
|
|
|
|
|
306
|
|
|
$this->storage->enableUser($userId); |
|
307
|
|
|
$this->storage->addUserMessage($userId, 'notification', 'account (re)enabled'); |
|
308
|
|
|
|
|
309
|
|
|
return new ApiResponse('enable_user'); |
|
310
|
|
|
} |
|
311
|
|
|
); |
|
312
|
|
|
|
|
313
|
|
|
$service->post( |
|
314
|
|
|
'/delete_user', |
|
315
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
316
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
317
|
|
|
|
|
318
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
319
|
|
|
$this->storage->deleteUser($userId); |
|
320
|
|
|
|
|
321
|
|
|
return new ApiResponse('delete_user'); |
|
322
|
|
|
} |
|
323
|
|
|
); |
|
324
|
|
|
|
|
325
|
|
|
$service->get( |
|
326
|
|
|
'/user_groups', |
|
327
|
|
|
function (Request $request, array $hookData) { |
|
328
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
|
329
|
|
|
|
|
330
|
|
|
$userId = $request->getQueryParameter('user_id'); |
|
331
|
|
|
|
|
332
|
|
|
$userGroups = []; |
|
333
|
|
|
foreach ($this->groupProviders as $groupProvider) { |
|
334
|
|
|
$userGroups = array_merge($userGroups, $groupProvider->getGroups($userId)); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
return new ApiResponse('user_groups', $userGroups); |
|
338
|
|
|
} |
|
339
|
|
|
); |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|
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.