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 ( 6c4153...d181b6 )
by François
03:10
created

ConnectionsModule::init()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 1
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 SURFnet\VPN\Server\Storage;
22
use SURFnet\VPN\Common\Http\AuthUtils;
23
use SURFnet\VPN\Common\Http\ServiceModuleInterface;
24
use SURFnet\VPN\Common\Http\Service;
25
use SURFnet\VPN\Common\Http\Request;
26
use SURFnet\VPN\Common\Config;
27
use SURFnet\VPN\Common\Http\ApiResponse;
28
use SURFnet\VPN\Common\ProfileConfig;
29
30
class ConnectionsModule implements ServiceModuleInterface
31
{
32
    /** @var \SURFnet\VPN\Common\Config */
33
    private $config;
34
35
    /** @var \SURFnet\VPN\Server\Storage */
36
    private $storage;
37
38
    /** @var array */
39
    private $groupProviders;
40
41
    public function __construct(Config $config, Storage $storage, array $groupProviders)
42
    {
43
        $this->config = $config;
44
        $this->storage = $storage;
45
        $this->groupProviders = $groupProviders;
46
    }
47
48
    public function init(Service $service)
49
    {
50
        $service->post(
51
            '/connect',
52
            function (Request $request, array $hookData) {
53
                AuthUtils::requireUser($hookData, ['vpn-server-node']);
54
55
                return $this->connect($request);
56
            }
57
        );
58
59
        $service->post(
60
            '/disconnect',
61
            function (Request $request, array $hookData) {
62
                AuthUtils::requireUser($hookData, ['vpn-server-node']);
63
64
                return $this->disconnect($request);
65
            }
66
        );
67
68
        $service->post(
69
            '/verify_otp',
70
            function (Request $request, array $hookData) {
71
                AuthUtils::requireUser($hookData, ['vpn-server-node']);
72
73
                return $this->verifyOtp($request);
74
            }
75
        );
76
    }
77
78
    public function connect(Request $request)
79
    {
80
        $profileId = $request->getPostParameter('profile_id');
81
        InputValidation::profileId($profileId);
82
        $commonName = $request->getPostParameter('common_name');
83
        InputValidation::commonName($commonName);
84
        $ip4 = $request->getPostParameter('ip4');
85
        InputValidation::ip4($ip4);
86
        $ip6 = $request->getPostParameter('ip6');
87
        InputValidation::ip6($ip6);
88
89
        // normalize the IPv6 address
90
        $ip6 = inet_ntop(inet_pton($ip6));
91
92
        $connectedAt = $request->getPostParameter('connected_at');
93
        InputValidation::connectedAt($connectedAt);
94
95
        if (true !== $response = $this->verifyConnection($profileId, $commonName)) {
96
            return $response;
97
        }
98
99 View Code Duplication
        if (false == $this->storage->clientConnect($profileId, $commonName, $ip4, $ip6, $connectedAt)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

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...
100
            return new ApiResponse('connect', ['ok' => false, 'error' => 'unable to write connect event to log']);
101
        }
102
103
        return new ApiResponse('connect', ['ok' => true]);
104
    }
105
106
    private function verifyConnection($profileId, $commonName)
107
    {
108
        // verify status of certificate/user
109
        if (false === $result = $this->storage->getUserCertificateStatus($commonName)) {
110
            return new ApiResponse('connect', ['ok' => false, 'error' => 'user or certificate does not exist']);
111
        }
112
113 View Code Duplication
        if ($result['user_is_disabled']) {
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
            return new ApiResponse('connect', ['ok' => false, 'error' => 'user is disabled']);
115
        }
116
117 View Code Duplication
        if ($result['certificate_is_disabled']) {
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...
118
            return new ApiResponse('connect', ['ok' => false, 'error' => 'certificate is disabled']);
119
        }
120
121
        return $this->verifyAcl($profileId, $result['external_user_id']);
122
    }
123
124
    private function verifyAcl($profileId, $externalUserId)
125
    {
126
        // verify ACL
127
        $profileConfig = new ProfileConfig($this->config->v('vpnProfiles', $profileId));
128
        if ($profileConfig->v('enableAcl')) {
129
            // ACL enabled
130
            $userGroups = [];
131
            foreach ($this->groupProviders as $groupProvider) {
132
                $userGroups = array_merge($userGroups, $groupProvider->getGroups($externalUserId));
133
            }
134
135
            if (false === self::isMember($userGroups, $profileConfig->v('aclGroupList'))) {
136
                return new ApiResponse('connect', ['ok' => false, 'error' => 'user not in ACL']);
137
            }
138
        }
139
140
        return true;
141
    }
142
143
    private static function isMember(array $memberOf, array $aclGroupList)
144
    {
145
        // one of the groups must be listed in the profile ACL list
146
        foreach ($memberOf as $memberGroup) {
147
            if (in_array($memberGroup['id'], $aclGroupList)) {
148
                return true;
149
            }
150
        }
151
152
        return false;
153
    }
154
155
    public function disconnect(Request $request)
156
    {
157
        $profileId = $request->getPostParameter('profile_id');
158
        InputValidation::profileId($profileId);
159
        $commonName = $request->getPostParameter('common_name');
160
        InputValidation::commonName($commonName);
161
        $ip4 = $request->getPostParameter('ip4');
162
        InputValidation::ip4($ip4);
163
        $ip6 = $request->getPostParameter('ip6');
164
        InputValidation::ip6($ip6);
165
166
        // normalize the IPv6 address
167
        $ip6 = inet_ntop(inet_pton($ip6));
168
169
        $connectedAt = $request->getPostParameter('connected_at');
170
        InputValidation::connectedAt($connectedAt);
171
172
        $disconnectedAt = $request->getPostParameter('disconnected_at');
173
        InputValidation::disconnectedAt($disconnectedAt);
174
175
        $bytesTransferred = $request->getPostParameter('bytes_transferred');
176
        InputValidation::bytesTransferred($bytesTransferred);
177
178 View Code Duplication
        if (false === $this->storage->clientDisconnect($profileId, $commonName, $ip4, $ip6, $connectedAt, $disconnectedAt, $bytesTransferred)) {
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...
179
            return new ApiResponse('disconnect', ['ok' => false, 'error' => 'unable to write disconnect event to log']);
180
        }
181
182
        return new ApiResponse('disconnect', ['ok' => true]);
183
    }
184
185
    public function verifyOtp(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
    {
187
    }
188
}
189