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 ( 714f59...d0b1a3 )
by François
03:35
created

StaticConfig   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 149
Duplicated Lines 48.32 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 26
c 2
b 1
f 1
lcom 1
cbo 2
dl 72
loc 149
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parseConfig() 0 10 2
A disableCommonName() 15 15 3
A enableCommonName() 16 16 3
A isDisabled() 0 8 2
B getDisabledCommonNames() 22 22 5
A getAllStaticAddresses() 19 19 4
A getStaticAddresses() 0 20 3
A setStaticAddresses() 0 12 1
A writeFile() 0 8 2

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
 * 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
namespace fkooman\VPN\Server;
18
19
use fkooman\Json\Json;
20
use RuntimeException;
21
22
/**
23
 * Manage static configuration for configurations.
24
 * Features:
25
 * - disable a configuration based on CN
26
 * - set static IPv4/IPv6 address for a CN.
27
 */
28
class StaticConfig
29
{
30
    /** @var string */
31
    private $staticConfigDir;
32
33
    public function __construct($staticConfigDir)
34
    {
35
        $this->staticConfigDir = $staticConfigDir;
36
    }
37
38
    private function parseConfig($commonName)
39
    {
40
        $commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName);
41
        // XXX do something if file does not exist
42
        try {
43
            return Json::decodeFile($commonNamePath);
0 ignored issues
show
Security File Exposure introduced by
$commonNamePath 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...
44
        } catch (RuntimeException $e) {
45
            return [];
46
        }
47
    }
48
49 View Code Duplication
    public function disableCommonName($commonName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
    {
51
        Utils::validateCommonName($commonName);
52
53
        $clientConfig = $this->parseConfig($commonName);
54
        if (array_key_exists('disable', $clientConfig) && $clientConfig['disable']) {
55
            // already disabled
56
            return false;
57
        }
58
59
        $clientConfig['disable'] = true;
60
        $this->writeFile($commonName, $clientConfig);
61
62
        return true;
63
    }
64
65 View Code Duplication
    public function enableCommonName($commonName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
66
    {
67
        Utils::validateCommonName($commonName);
68
69
        $clientConfig = $this->parseConfig($commonName);
70
        if (array_key_exists('disable', $clientConfig) && $clientConfig['disable']) {
71
            // it is disabled, enable it
72
            $clientConfig['disable'] = false;
73
            $this->writeFile($commonName, $clientConfig);
74
75
            return true;
76
        }
77
78
        // it is not disabled
79
        return false;
80
    }
81
82
    public function isDisabled($commonName)
83
    {
84
        Utils::validateCommonName($commonName);
85
86
        $clientConfig = $this->parseConfig($commonName);
87
88
        return array_key_exists('disable', $clientConfig) && $clientConfig['disable'];
89
    }
90
91 View Code Duplication
    public function getDisabledCommonNames($userId = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
92
    {
93
        if (!is_null($userId)) {
94
            Utils::validateUserId($userId);
95
        }
96
97
        $disabledCommonNames = array();
98
        $pathFilter = sprintf('%s/*', $this->staticConfigDir);
99
        if (!is_null($userId)) {
100
            $pathFilter = sprintf('%s/%s_*', $this->staticConfigDir, $userId);
101
        }
102
103
        foreach (glob($pathFilter) as $commonNamePath) {
0 ignored issues
show
Security File Exposure introduced by
$pathFilter 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...
104
            $commonName = basename($commonNamePath);
105
106
            if ($this->isDisabled($commonName)) {
107
                $disabledCommonNames[] = $commonName;
108
            }
109
        }
110
111
        return $disabledCommonNames;
112
    }
113
114 View Code Duplication
    public function getAllStaticAddresses($userId = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
115
    {
116
        if (!is_null($userId)) {
117
            Utils::validateUserId($userId);
118
        }
119
120
        $staticAddresses = array();
121
        $pathFilter = sprintf('%s/*', $this->staticConfigDir);
122
        if (!is_null($userId)) {
123
            $pathFilter = sprintf('%s/%s_*', $this->staticConfigDir, $userId);
124
        }
125
126
        foreach (glob($pathFilter) as $commonNamePath) {
0 ignored issues
show
Security File Exposure introduced by
$pathFilter 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...
127
            $commonName = basename($commonNamePath);
128
            $staticAddresses[$commonName] = $this->getStaticAddresses($commonName);
129
        }
130
131
        return $staticAddresses;
132
    }
133
134
    public function getStaticAddresses($commonName)
135
    {
136
        Utils::validateCommonName($commonName);
137
138
        $clientConfig = $this->parseConfig($commonName);
139
140
        $v4 = null;
141
        if (array_key_exists('v4', $clientConfig)) {
142
            $v4 = $clientConfig['v4'];
143
        }
144
        $v6 = null;
145
        if (array_key_exists('v6', $clientConfig)) {
146
            $v6 = $clientConfig['v6'];
147
        }
148
149
        return array(
150
            'v4' => $v4,
151
            'v6' => $v6,
152
        );
153
    }
154
155
    public function setStaticAddresses($commonName, $v4, $v6)
156
    {
157
        Utils::validateCommonName($commonName);
158
159
        $clientConfig = $this->parseConfig($commonName);
160
161
        $clientConfig['v4'] = $v4;
162
        $clientConfig['v6'] = $v6;
163
        $this->writeFile($commonName, $clientConfig);
164
165
        return true;
166
    }
167
168
    private function writeFile($commonName, array $clientConfig)
169
    {
170
        $commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName);
171
172
        if (false === @file_put_contents($commonNamePath, Json::encode($clientConfig))) {
0 ignored issues
show
Security File Manipulation introduced by
$commonNamePath can contain request data and is used in file manipulation 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...
173
            throw new RuntimeException('unable to write to static configuration file');
174
        }
175
    }
176
}
177