GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UsersModule   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 309
Duplicated Lines 42.72 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 12
dl 132
loc 309
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
D init() 132 290 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Server\Api;
11
12
use fkooman\OAuth\Client\AccessToken;
13
use SURFnet\VPN\Common\Config;
14
use SURFnet\VPN\Common\Http\ApiErrorResponse;
15
use SURFnet\VPN\Common\Http\ApiResponse;
16
use SURFnet\VPN\Common\Http\AuthUtils;
17
use SURFnet\VPN\Common\Http\InputValidation;
18
use SURFnet\VPN\Common\Http\Request;
19
use SURFnet\VPN\Common\Http\Service;
20
use SURFnet\VPN\Common\Http\ServiceModuleInterface;
21
use SURFnet\VPN\Server\Exception\TotpException;
22
use SURFnet\VPN\Server\Exception\YubiKeyException;
23
use SURFnet\VPN\Server\Storage;
24
use SURFnet\VPN\Server\Totp;
25
use SURFnet\VPN\Server\YubiKey;
26
27
class UsersModule implements ServiceModuleInterface
28
{
29
    /** @var \SURFnet\VPN\Common\Config */
30
    private $config;
31
32
    /** @var \SURFnet\VPN\Server\Storage */
33
    private $storage;
34
35
    /** @var array */
36
    private $groupProviders;
37
38
    public function __construct(Config $config, Storage $storage, array $groupProviders)
39
    {
40
        $this->config = $config;
41
        $this->storage = $storage;
42
        $this->groupProviders = $groupProviders;
43
    }
44
45
    public function init(Service $service)
46
    {
47
        $service->get(
48
            '/user_list',
49
            function (Request $request, array $hookData) {
50
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
51
52
                return new ApiResponse('user_list', $this->storage->getUsers());
53
            }
54
        );
55
56
        $service->post(
57
            '/set_yubi_key_id',
58 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
59
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
60
61
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
62
                $yubiKeyOtp = InputValidation::yubiKeyOtp($request->getPostParameter('yubi_key_otp'));
63
64
                // check if there is already a YubiKey ID registered for this user
65
                if ($this->storage->hasYubiKeyId($userId)) {
66
                    return new ApiErrorResponse('set_yubi_key_id', 'YubiKey ID already set');
67
                }
68
69
                $yubiKey = new YubiKey();
70
                try {
71
                    $yubiKeyId = $yubiKey->verify($userId, $yubiKeyOtp);
72
                    $this->storage->setYubiKeyId($userId, $yubiKeyId);
73
                    $this->storage->addUserMessage($userId, 'notification', sprintf('YubiKey ID "%s" registered', $yubiKeyId));
74
75
                    return new ApiResponse('set_yubi_key_id');
76
                } catch (YubiKeyException $e) {
77
                    $msg = sprintf('YubiKey OTP verification failed: %s', $e->getMessage());
78
                    $this->storage->addUserMessage($userId, 'notification', $msg);
79
80
                    return new ApiErrorResponse('set_yubi_key_id', $msg);
81
                }
82
            }
83
        );
84
85
        $service->post(
86
            '/verify_yubi_key_otp',
87
            function (Request $request, array $hookData) {
88
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
89
90
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
91
                $yubiKeyOtp = InputValidation::yubiKeyOtp($request->getPostParameter('yubi_key_otp'));
92
                $yubiKeyId = $this->storage->getYubiKeyId($userId);
93
94
                // XXX make sure we have a registered yubiKeyID first?!
95
96
                $yubiKey = new YubiKey();
97
                try {
98
                    $yubiKey->verify($userId, $yubiKeyOtp, $yubiKeyId);
99
100
                    return new ApiResponse('verify_yubi_key_otp');
101
                } catch (YubiKeyException $e) {
102
                    $msg = sprintf('YubiKey OTP verification failed: %s', $e->getMessage());
103
                    $this->storage->addUserMessage($userId, 'notification', $msg);
104
105
                    return new ApiErrorResponse('verify_yubi_key_otp', $msg);
106
                }
107
            }
108
        );
109
110
        $service->get(
111
            '/has_yubi_key_id',
112 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
113
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
114
115
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
116
117
                return new ApiResponse('has_yubi_key_id', $this->storage->hasYubiKeyId($userId));
118
            }
119
        );
120
121
        $service->get(
122
            '/yubi_key_id',
123
            function (Request $request, array $hookData) {
124
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
125
126
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
127
128
                $yubiKeyId = $this->storage->getYubiKeyId($userId);
129
                if (is_null($yubiKeyId)) {
130
                    return new ApiResponse('yubi_key_id', false);
131
                }
132
133
                return new ApiResponse('yubi_key_id', $yubiKeyId);
134
            }
135
        );
136
137
        $service->post(
138
            '/delete_yubi_key_id',
139 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
140
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
141
142
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
143
144
                $yubiKeyId = $this->storage->getYubiKeyId($userId);
145
                $this->storage->deleteYubiKeyId($userId);
146
                $this->storage->addUserMessage($userId, 'notification', sprintf('YubiKey ID "%s" deleted', $yubiKeyId));
147
148
                return new ApiResponse('delete_yubi_key_id');
149
            }
150
        );
151
152
        $service->post(
153
            '/set_totp_secret',
154 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
155
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
156
157
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
158
                $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key'));
159
                $totpSecret = InputValidation::totpSecret($request->getPostParameter('totp_secret'));
160
161
                // check if there is already a TOTP secret registered for this user
162
                if ($this->storage->hasTotpSecret($userId)) {
163
                    return new ApiErrorResponse('set_totp_secret', 'TOTP secret already set');
164
                }
165
166
                $totp = new Totp($this->storage);
167
                try {
168
                    $totp->verify($userId, $totpKey, $totpSecret);
169
                } catch (TotpException $e) {
170
                    $msg = sprintf('TOTP verification failed: %s', $e->getMessage());
171
                    $this->storage->addUserMessage($userId, 'notification', $msg);
172
173
                    return new ApiErrorResponse('set_totp_secret', $msg);
174
                }
175
176
                $this->storage->setTotpSecret($userId, $totpSecret);
177
                $this->storage->addUserMessage($userId, 'notification', 'TOTP secret registered');
178
179
                return new ApiResponse('set_totp_secret');
180
            }
181
        );
182
183
        $service->post(
184
            '/verify_totp_key',
185
            function (Request $request, array $hookData) {
186
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
187
188
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
189
                $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key'));
190
191
                $totp = new Totp($this->storage);
192
                try {
193
                    $totp->verify($userId, $totpKey);
194
                } catch (TotpException $e) {
195
                    $msg = sprintf('TOTP validation failed: %s', $e->getMessage());
196
                    $this->storage->addUserMessage($userId, 'notification', $msg);
197
198
                    return new ApiErrorResponse('verify_totp_key', $msg);
199
                }
200
201
                return new ApiResponse('verify_totp_key');
202
            }
203
        );
204
205
        $service->get(
206
            '/has_totp_secret',
207 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
208
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
209
210
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
211
212
                return new ApiResponse('has_totp_secret', $this->storage->hasTotpSecret($userId));
213
            }
214
        );
215
216
        $service->post(
217
            '/delete_totp_secret',
218 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
219
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
220
221
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
222
223
                $this->storage->deleteTotpSecret($userId);
224
                $this->storage->addUserMessage($userId, 'notification', 'TOTP secret deleted');
225
226
                return new ApiResponse('delete_totp_secret');
227
            }
228
        );
229
230
        $service->post(
231
            '/set_voot_token',
232
            function (Request $request, array $hookData) {
233
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
234
235
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
236
237
                // load the POSTed AccessToken from JSON
238
                $vootToken = AccessToken::fromJson($request->getPostParameter('voot_token'));
239
                $this->storage->setVootToken($userId, $vootToken);
240
241
                return new ApiResponse('set_voot_token');
242
            }
243
        );
244
245
        $service->post(
246
            '/delete_voot_token',
247 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
248
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
249
250
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
251
                $this->storage->deleteVootToken($userId);
252
253
                return new ApiResponse('delete_voot_token');
254
            }
255
        );
256
257
        $service->get(
258
            '/has_voot_token',
259 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
260
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
261
262
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
263
264
                return new ApiResponse('has_voot_token', $this->storage->hasVootToken($userId));
265
            }
266
        );
267
268
        $service->get(
269
            '/is_disabled_user',
270 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
271
                AuthUtils::requireUser($hookData, ['vpn-admin-portal', 'vpn-user-portal']);
272
273
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
274
275
                return new ApiResponse('is_disabled_user', $this->storage->isDisabledUser($userId));
276
            }
277
        );
278
279
        $service->post(
280
            '/disable_user',
281 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
282
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
283
284
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
285
286
                $this->storage->disableUser($userId);
287
                $this->storage->addUserMessage($userId, 'notification', 'account disabled');
288
289
                return new ApiResponse('disable_user');
290
            }
291
        );
292
293
        $service->post(
294
            '/enable_user',
295 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
296
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
297
298
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
299
300
                $this->storage->enableUser($userId);
301
                $this->storage->addUserMessage($userId, 'notification', 'account (re)enabled');
302
303
                return new ApiResponse('enable_user');
304
            }
305
        );
306
307
        $service->post(
308
            '/delete_user',
309 View Code Duplication
            function (Request $request, array $hookData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
310
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
311
312
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
313
                $this->storage->deleteUser($userId);
314
315
                return new ApiResponse('delete_user');
316
            }
317
        );
318
319
        $service->get(
320
            '/user_groups',
321
            function (Request $request, array $hookData) {
322
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
323
324
                $userId = $request->getQueryParameter('user_id');
325
326
                $userGroups = [];
327
                foreach ($this->groupProviders as $groupProvider) {
328
                    $userGroups = array_merge($userGroups, $groupProvider->getGroups($userId));
329
                }
330
331
                return new ApiResponse('user_groups', $userGroups);
332
            }
333
        );
334
    }
335
}
336