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 ( aff5f5...d962fa )
by François
02:15
created

UsersModule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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 SURFnet\VPN\Common\Http\ApiErrorResponse;
24
use SURFnet\VPN\Common\Http\ApiResponse;
25
use SURFnet\VPN\Common\Http\AuthUtils;
26
use SURFnet\VPN\Common\Http\InputValidation;
27
use SURFnet\VPN\Common\Http\Request;
28
use SURFnet\VPN\Common\Http\Service;
29
use SURFnet\VPN\Common\Http\ServiceModuleInterface;
30
use SURFnet\VPN\Server\Storage;
31
32
class UsersModule implements ServiceModuleInterface
33
{
34
    /** @var \SURFnet\VPN\Server\Storage */
35
    private $storage;
36
37
    /** @var array */
38
    private $groupProviders;
39
40
    public function __construct(Storage $storage, array $groupProviders)
41
    {
42
        $this->storage = $storage;
43
        $this->groupProviders = $groupProviders;
44
    }
45
46
    public function init(Service $service)
47
    {
48
        $service->get(
49
            '/user_list',
50
            function (Request $request, array $hookData) {
51
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
52
53
                return new ApiResponse('user_list', $this->storage->getUsers());
54
            }
55
        );
56
57
        $service->post(
58
            '/set_totp_secret',
59
            function (Request $request, array $hookData) {
60
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
61
62
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
63
                $totpSecret = InputValidation::totpSecret($request->getPostParameter('totp_secret'));
64
                $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key'));
65
66
                $otp = new Otp();
67
                if (false === $otp->checkTotp(Base32::decode($totpSecret), $totpKey)) {
68
                    // wrong otp key
69
                    return new ApiErrorResponse('set_totp_secret', 'invalid OTP key');
70
                }
71
72
                // XXX use DateTime here, easier for testing
73
74
                // XXX check if all these things worked!
75
76 View Code Duplication
                if (false === $this->storage->recordTotpKey($userId, $totpKey, time())) {
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...
77
                    return new ApiErrorResponse('set_totp_secret', 'OTP key replay');
78
                }
79
                $this->storage->setTotpSecret($userId, $totpSecret);
80
81
                return new ApiResponse('set_totp_secret');
82
            }
83
        );
84
85
        $service->post(
86
            '/verify_totp_key',
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
                $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key'));
92
93
                if (!$this->storage->hasTotpSecret($userId)) {
94
                    return new ApiErrorResponse('verify_totp_key', 'user has no OTP secret');
95
                }
96
                $totpSecret = $this->storage->getTotpSecret($userId);
97
98
                $otp = new Otp();
99
                if (!$otp->checkTotp(Base32::decode($totpSecret), $totpKey)) {
100
                    return new ApiErrorResponse('verify_totp_key', 'invalid OTP key');
101
                }
102
103 View Code Duplication
                if (false === $this->storage->recordTotpKey($userId, $totpKey, time())) {
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...
104
                    return new ApiErrorResponse('verify_totp_key', 'OTP key replay');
105
                }
106
107
                return new ApiResponse('verify_totp_key');
108
            }
109
        );
110
111
        $service->get(
112
            '/has_totp_secret',
113 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...
114
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
115
116
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
117
118
                return new ApiResponse('has_totp_secret', $this->storage->hasTotpSecret($userId));
119
            }
120
        );
121
122
        $service->post(
123
            '/delete_totp_secret',
124 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...
125
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
126
127
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
128
129
                return new ApiResponse('delete_totp_secret', $this->storage->deleteTotpSecret($userId));
130
            }
131
        );
132
133
        $service->post(
134
            '/set_voot_token',
135
            function (Request $request, array $hookData) {
136
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
137
138
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
139
                $vootToken = InputValidation::vootToken($request->getPostParameter('voot_token'));
140
141
                return new ApiResponse('set_voot_token', $this->storage->setVootToken($userId, $vootToken));
142
            }
143
        );
144
145
        $service->post(
146
            '/delete_voot_token',
147 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...
148
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
149
150
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
151
152
                return new ApiResponse('delete_voot_token', $this->storage->deleteVootToken($userId));
153
            }
154
        );
155
156
        $service->get(
157
            '/has_voot_token',
158 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...
159
                AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']);
160
161
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
162
163
                return new ApiResponse('has_voot_token', $this->storage->hasVootToken($userId));
164
            }
165
        );
166
167
        $service->get(
168
            '/is_disabled_user',
169 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...
170
                AuthUtils::requireUser($hookData, ['vpn-admin-portal', 'vpn-user-portal']);
171
172
                $userId = InputValidation::userId($request->getQueryParameter('user_id'));
173
174
                return new ApiResponse('is_disabled_user', $this->storage->isDisabledUser($userId));
175
            }
176
        );
177
178
        $service->post(
179
            '/disable_user',
180 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...
181
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
182
183
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
184
185
                return new ApiResponse('disable_user', $this->storage->disableUser($userId));
186
            }
187
        );
188
189
        $service->post(
190
            '/enable_user',
191
            function (Request $request, array $hookData) {
192
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
193
194
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
195
196
                return new ApiResponse('enable_user', $this->storage->enableUser($userId));
197
            }
198
        );
199
200
        $service->post(
201
            '/delete_user',
202 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...
203
                AuthUtils::requireUser($hookData, ['vpn-admin-portal']);
204
205
                $userId = InputValidation::userId($request->getPostParameter('user_id'));
206
207
                return new ApiResponse('delete_user', $this->storage->deleteUser($userId));
208
            }
209
        );
210
211
        $service->get(
212
            '/user_groups',
213
            function (Request $request, array $hookData) {
214
                AuthUtils::requireUser($hookData, ['vpn-user-portal']);
215
216
                $userId = $request->getQueryParameter('user_id');
217
218
                $userGroups = [];
219
                foreach ($this->groupProviders as $groupProvider) {
220
                    $userGroups = array_merge($userGroups, $groupProvider->getGroups($userId));
221
                }
222
223
                return new ApiResponse('user_groups', $userGroups);
224
            }
225
        );
226
    }
227
}
228