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.
Completed
Push — master ( c87823...3d2057 )
by François
02:04
created

UsersModule::init()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 167
Code Lines 96

Duplication

Lines 74
Ratio 44.31 %

Importance

Changes 0
Metric Value
dl 74
loc 167
rs 8.1935
c 0
b 0
f 0
cc 4
eloc 96
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 DateTime;
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\TwoFactorException;
30
use SURFnet\VPN\Server\Storage;
31
use SURFnet\VPN\Server\TwoFactor;
32
33
class UsersModule implements ServiceModuleInterface
34
{
35
    /** @var \SURFnet\VPN\Server\Storage */
36
    private $storage;
37
38
    /** @var array */
39
    private $groupProviders;
40
41
    public function __construct(Storage $storage, array $groupProviders)
42
    {
43
        $this->storage = $storage;
44
        $this->groupProviders = $groupProviders;
45
    }
46
47
    public function init(Service $service)
48
    {
49
        $service->get(
50
            '/user_list',
51
            function (Request $request, array $hookData) {
52
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
53
54
                return new ApiResponse('user_list', $this->storage->getUsers());
55
            }
56
        );
57
58
        $service->post(
59
            '/set_totp_secret',
60 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...
61
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
62
63
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
64
                $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key'));
65
                $totpSecret = InputValidation::totpSecret($request->getPostParameter('totp_secret'));
66
67
                $twoFactor = new TwoFactor($this->storage, new DateTime('now'));
68
                try {
69
                    $twoFactor->verifyTotp($userId, $totpKey, $totpSecret);
70
                } catch (TwoFactorException $e) {
71
                    return new ApiErrorResponse('set_totp_secret', $e->getMessage());
72
                }
73
                $this->storage->setTotpSecret($userId, $totpSecret);
74
75
                return new ApiResponse('set_totp_secret');
76
            }
77
        );
78
79
        $service->post(
80
            '/verify_totp_key',
81
            function (Request $request, array $hookData) {
82
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
83
84
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
85
                $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key'));
86
87
                $twoFactor = new TwoFactor($this->storage, new DateTime('now'));
88
                try {
89
                    $twoFactor->verifyTotp($userId, $totpKey);
90
                } catch (TwoFactorException $e) {
91
                    return new ApiErrorResponse('verify_totp_key', $e->getMessage());
92
                }
93
94
                return new ApiResponse('verify_totp_key');
95
            }
96
        );
97
98
        $service->get(
99
            '/has_totp_secret',
100 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...
101
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
102
103
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
104
105
                return new ApiResponse('has_totp_secret', $this->storage->hasTotpSecret($userId));
106
            }
107
        );
108
109
        $service->post(
110
            '/delete_totp_secret',
111 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...
112
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
113
114
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
115
116
                return new ApiResponse('delete_totp_secret', $this->storage->deleteTotpSecret($userId));
117
            }
118
        );
119
120
        $service->post(
121
            '/set_voot_token',
122 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...
123
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
124
125
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
126
                $vootToken = InputValidation::vootToken($request->getPostParameter('voot_token'));
127
128
                return new ApiResponse('set_voot_token', $this->storage->setVootToken($userId, $vootToken));
129
            }
130
        );
131
132
        $service->post(
133
            '/delete_voot_token',
134 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...
135
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
136
137
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
138
139
                return new ApiResponse('delete_voot_token', $this->storage->deleteVootToken($userId));
140
            }
141
        );
142
143
        $service->get(
144
            '/has_voot_token',
145 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...
146
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
147
148
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
149
150
                return new ApiResponse('has_voot_token', $this->storage->hasVootToken($userId));
151
            }
152
        );
153
154
        $service->get(
155
            '/is_disabled_user',
156 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...
157
                AuthUtils::requireUser($hookData, ['vpn-admin-portal', 'vpn-user-portal']);
158
159
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
160
161
                return new ApiResponse('is_disabled_user', $this->storage->isDisabledUser($userId));
162
            }
163
        );
164
165
        $service->post(
166
            '/disable_user',
167 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...
168
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
169
170
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
171
172
                return new ApiResponse('disable_user', $this->storage->disableUser($userId));
173
            }
174
        );
175
176
        $service->post(
177
            '/enable_user',
178
            function (Request $request, array $hookData) {
179
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
180
181
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
182
183
                return new ApiResponse('enable_user', $this->storage->enableUser($userId));
184
            }
185
        );
186
187
        $service->post(
188
            '/delete_user',
189 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...
190
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
191
192
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
193
194
                return new ApiResponse('delete_user', $this->storage->deleteUser($userId));
195
            }
196
        );
197
198
        $service->get(
199
            '/user_groups',
200
            function (Request $request, array $hookData) {
201
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
202
203
                $userId = $request->getQueryParameter('user_id');
204
205
                $userGroups = [];
206
                foreach ($this->groupProviders as $groupProvider) {
207
                    $userGroups = array_merge($userGroups, $groupProvider->getGroups($userId));
208
                }
209
210
                return new ApiResponse('user_groups', $userGroups);
211
            }
212
        );
213
    }
214
}
215