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 ( c9e896...cb5f18 )
by François
02:52
created

CnConfigModule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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\CnConfig;
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\Rest\Plugin\Authentication\Bearer\TokenInfo;
28
use fkooman\IO\IOInterface;
29
use fkooman\VPN\Server\Utils;
30
31
class CnConfigModule implements ServiceModuleInterface
32
{
33
    /** @var string */
34
    private $configDir;
35
36
    /** @var \Psr\Log\LoggerInterface */
37
    private $logger;
38
39
    /** @var \fkooman\IO\IOInterface */
40
    private $io;
41
42
    public function __construct($configDir, LoggerInterface $logger, IOInterface $io)
43
    {
44
        $this->configDir = $configDir;
45
        $this->logger = $logger;
46
        $this->io = $io;
47
    }
48
49
    public function init(Service $service)
50
    {
51
        // GET /config/common_names               get all CNs
52
        // GET /config/common_names?user_id=foo   get all CNs, but only for one user
53
        $service->get(
54
            '/config/common_names',
55
            function (Request $request, TokenInfo $tokenInfo) {
56
                Utils::requireScope($tokenInfo, ['admin', 'portal']);
57
                $userId = $request->getUrl()->getQueryParameter('user_id');
58
                if (!is_null($userId)) {
59
                    InputValidation::userId($userId);
60
                    // filter the directory list if user_id was specified
61
                    $fileFilter = sprintf('%s_*', $userId);
62
                } else {
63
                    // only admin is allow to request all CNs for all users
64
                    Utils::requireScope($tokenInfo, ['admin']);
65
                    $fileFilter = '*';
66
                }
67
68
                $cnConfigArray = [];
69 View Code Duplication
                foreach ($this->io->readFolder($this->configDir, $fileFilter) as $configFile) {
0 ignored issues
show
Security File Exposure introduced by
$fileFilter can contain request data and is used in file inclusion context(s) leading to a potential security vulnerability.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
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...
70
                    $cnConfig = new CnConfig(
71
                        Json::decode(
72
                            $this->io->readFile($configFile)
73
                        )
74
                    );
75
                    $cnConfigArray[basename($configFile)] = $cnConfig->toArray();
76
                }
77
78
                $response = new JsonResponse();
79
                $response->setBody($cnConfigArray);
80
81
                return $response;
82
            }
83
        );
84
85
        // GET /config/common_names/:commonName   get a particular CN        
86
        $service->get(
87
            '/config/common_names/:commonName',
88 View Code Duplication
            function (Request $request, TokenInfo $tokenInfo, $commonName) {
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...
89
                Utils::requireScope($tokenInfo, ['admin']);
90
                InputValidation::commonName($commonName);
91
92
                $fileName = sprintf('%s/%s', $this->configDir, $commonName);
93
                if (!$this->io->isFile($fileName)) {
94
                    // if the file does not exist, use default values
95
                    $cnConfig = new CnConfig([]);
96
                } else {
97
                    $cnConfig = new CnConfig(
98
                        Json::decode(
99
                            $this->io->readFile(
100
                                sprintf('%s/%s', $this->configDir, $commonName)
101
                            )
102
                        )
103
                    );
104
                }
105
106
                $response = new JsonResponse();
107
                $response->setBody($cnConfig->toArray());
108
109
                return $response;
110
            }
111
        );
112
113
        // PUT /config/common_names/:commonName   set new config for CN
114
        $service->put(
115
            '/config/common_names/:commonName',
116 View Code Duplication
            function (Request $request, TokenInfo $tokenInfo, $commonName) {
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...
117
                Utils::requireScope($tokenInfo, ['admin']);
118
                InputValidation::commonName($commonName);
119
120
                // we wrap the request body in an CnConfig object to validate
121
                // whatever is there
122
                $requestCnConfig = new CnConfig(Json::decode($request->getBody()));
123
124
                $this->io->writeFile(
125
                    sprintf('%s/%s', $this->configDir, $commonName),
126
                    Json::encode($requestCnConfig->toArray())
127
                );
128
129
                $response = new JsonResponse();
130
                $response->setBody(['ok' => true]);
131
132
                return $response;
133
            }
134
        );
135
    }
136
}
137