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 ( a4ad96...a25367 )
by François
02:17
created

ConfigModule::init()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 125
Code Lines 64

Duplication

Lines 16
Ratio 12.8 %

Importance

Changes 10
Bugs 1 Features 1
Metric Value
c 10
b 1
f 1
dl 16
loc 125
rs 8.1463
cc 5
eloc 64
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 2015 François Kooman <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace fkooman\VPN\Server\Config;
19
20
use fkooman\Http\Request;
21
use fkooman\Rest\Service;
22
use fkooman\Rest\ServiceModuleInterface;
23
use fkooman\Http\JsonResponse;
24
use Psr\Log\LoggerInterface;
25
use fkooman\VPN\Server\InputValidation;
26
use fkooman\Json\Json;
27
use fkooman\Http\Exception\ForbiddenException;
28
use fkooman\Rest\Plugin\Authentication\Bearer\TokenInfo;
29
30
class ConfigModule implements ServiceModuleInterface
31
{
32
    /** @var ConfigStorageInterface */
33
    private $configStorage;
34
35
    /** @var \Psr\Log\LoggerInterface */
36
    private $logger;
37
38
    public function __construct(ConfigStorageInterface $configStorage, LoggerInterface $logger)
39
    {
40
        $this->configStorage = $configStorage;
41
        $this->logger = $logger;
42
    }
43
44
    public function init(Service $service)
45
    {
46
        // get all configurations
47
        $service->get(
48
            '/config/common_names/',
49
            function (Request $request, TokenInfo $tokenInfo) {
50
                self::requireScope($tokenInfo, ['admin', 'portal']);
51
52
                $userId = $request->getUrl()->getQueryParameter('user_id');
53
                if (is_null($userId)) {
54
                    self::requireScope($tokenInfo, ['admin']);
55
                } else {
56
                    InputValidation::userId($userId);
57
                }
58
59
                $response = new JsonResponse();
60
                $response->setBody(
61
                    [
62
                        'items' => $this->configStorage->getAllCommonNameConfig($userId),
63
                    ]
64
                );
65
66
                return $response;
67
            }
68
        );
69
70
        // get configuration for a particular common_name
71
        $service->get(
72
            '/config/common_names/:commonName',
73
            function (Request $request, TokenInfo $tokenInfo, $commonName) {
74
                self::requireScope($tokenInfo, ['admin', 'portal']);
75
76
                InputValidation::commonName($commonName);
77
78
                $response = new JsonResponse();
79
                $response->setBody(
80
                    $this->configStorage->getCommonNameConfig($commonName)->toArray()
81
                );
82
83
                return $response;
84
            }
85
        );
86
87
        // set configuration for a particular common_name
88
        $service->put(
89
            '/config/common_names/:commonName',
90
            function (Request $request, TokenInfo $tokenInfo, $commonName) {
91
                self::requireScope($tokenInfo, ['admin']);
92
93
                InputValidation::commonName($commonName);
94
95
                // XXX check content type
96
                // XXX allow for disconnect as well when updating config
97
98
                $configData = new CommonNameConfig(Json::decode($request->getBody()));
99
                $this->configStorage->setCommonNameConfig($commonName, $configData);
100
101
                $response = new JsonResponse();
102
                $response->setBody(
103
                    ['ok' => true]
104
                );
105
106
                return $response;
107
            }
108
        );
109
110
        // get configuration for a particular user_id
111
        $service->get(
112
            '/config/users/:userId',
113 View Code Duplication
            function (Request $request, TokenInfo $tokenInfo, $userId) {
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
                self::requireScope($tokenInfo, ['admin', 'portal']);
115
116
                InputValidation::userId($userId);
117
118
                $userConfig = $this->configStorage->getUserConfig($userId);
119
                // we never want the OTP secret to leave the system
120
                $userConfig->hideOtpSecret();
121
122
                $response = new JsonResponse();
123
                $response->setBody(
124
                    $userConfig->toArray()
125
                );
126
127
                return $response;
128
            }
129
        );
130
131
        // set configuration for a particular user_id
132
        $service->put(
133
            '/config/users/:userId',
134
            function (Request $request, TokenInfo $tokenInfo, $userId) {
135
                self::requireScope($tokenInfo, ['admin', 'portal']);
136
137
                InputValidation::userId($userId);
138
139
                $userConfig = $this->configStorage->getUserConfig($userId);
140
                $newUserConfig = new UserConfig(Json::decode($request->getBody()));
141
142
                if ($userConfig->getDisable() !== $newUserConfig->getDisable()) {
143
                    // only 'admin' can change disable state of user
144
                    self::requireScope($tokenInfo, ['admin']);
145
                }
146
147
                if (false !== $userConfig->getOtpSecret()) {
148
                    // currently an OTP secret it set, the value has to be 'true'
149
                    // if not, admin is required to be able to update it
150
                    if (true !== $newUserConfig->getOtpSecret()) {
151
                        self::requireScope($tokenInfo, ['admin']);
152
                    } else {
153
                        // we use the old value
154
                        $newUserConfig->setOtpSecret($userConfig->getOtpSecret());
155
                    }
156
                }
157
158
                $this->configStorage->setUserConfig($userId, $newUserConfig);
159
160
                $response = new JsonResponse();
161
                $response->setBody(
162
                    ['ok' => true]
163
                );
164
165
                return $response;
166
            }
167
        );
168
    }
169
170
    private static function requireScope(TokenInfo $tokenInfo, array $requiredScope)
171
    {
172
        foreach ($requiredScope as $s) {
173
            if ($tokenInfo->getScope()->hasScope($s)) {
174
                return;
175
            }
176
        }
177
178
        throw new ForbiddenException('insufficient_scope', sprintf('"%s" scope required', implode(',', $requiredScope)));
179
    }
180
}
181