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 ( 9650d4...b69b8b )
by François
02:36
created

ConnectionsModule::connect()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 3
Ratio 16.67 %

Importance

Changes 0
Metric Value
dl 3
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
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\Common\Config;
22
use SURFnet\VPN\Common\Http\ApiResponse;
23
use SURFnet\VPN\Common\Http\AuthUtils;
24
use SURFnet\VPN\Common\Http\InputValidation;
25
use SURFnet\VPN\Common\Http\Request;
26
use SURFnet\VPN\Common\Http\Service;
27
use SURFnet\VPN\Common\Http\ServiceModuleInterface;
28
use SURFnet\VPN\Common\ProfileConfig;
29
use SURFnet\VPN\Server\Storage;
30
31
class ConnectionsModule implements ServiceModuleInterface
32
{
33
    /** @var \SURFnet\VPN\Common\Config */
34
    private $config;
35
36
    /** @var \SURFnet\VPN\Server\Storage */
37
    private $storage;
38
39
    /** @var array */
40
    private $groupProviders;
41
42
    public function __construct(Config $config, Storage $storage, array $groupProviders)
43
    {
44
        $this->config = $config;
45
        $this->storage = $storage;
46
        $this->groupProviders = $groupProviders;
47
    }
48
49
    public function init(Service $service)
50
    {
51
        $service->post(
52
            '/connect',
53
            function (Request $request, array $hookData) {
54
                AuthUtils::requireUser($hookData, ['vpn-server-node']);
55
56
                return $this->connect($request);
57
            }
58
        );
59
60
        $service->post(
61
            '/disconnect',
62
            function (Request $request, array $hookData) {
63
                AuthUtils::requireUser($hookData, ['vpn-server-node']);
64
65
                return $this->disconnect($request);
66
            }
67
        );
68
69
        $service->post(
70
            '/verify_otp',
71
            function (Request $request, array $hookData) {
72
                AuthUtils::requireUser($hookData, ['vpn-server-node']);
73
74
                return $this->verifyOtp($request);
75
            }
76
        );
77
    }
78
79
    public function connect(Request $request)
80
    {
81
        $profileId = InputValidation::profileId($request->getPostParameter('profile_id'));
82
        $commonName = InputValidation::commonName($request->getPostParameter('common_name'));
83
        $ip4 = InputValidation::ip4($request->getPostParameter('ip4'));
84
        $ip6 = InputValidation::ip6($request->getPostParameter('ip6'));
85
        $connectedAt = InputValidation::connectedAt($request->getPostParameter('connected_at'));
86
87
        if (true !== $response = $this->verifyConnection($profileId, $commonName)) {
88
            return $response;
89
        }
90
91 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...
92
            return new ApiResponse('connect', ['ok' => false, 'error' => 'unable to write connect event to log']);
93
        }
94
95
        return new ApiResponse('connect', ['ok' => true]);
96
    }
97
98
    private function verifyConnection($profileId, $commonName)
99
    {
100
        // verify status of certificate/user
101
        if (false === $result = $this->storage->getUserCertificateStatus($commonName)) {
102
            return new ApiResponse('connect', ['ok' => false, 'error' => 'user or certificate does not exist']);
103
        }
104
105 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...
106
            return new ApiResponse('connect', ['ok' => false, 'error' => 'user is disabled']);
107
        }
108
109 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...
110
            return new ApiResponse('connect', ['ok' => false, 'error' => 'certificate is disabled']);
111
        }
112
113
        return $this->verifyAcl($profileId, $result['external_user_id']);
114
    }
115
116
    private function verifyAcl($profileId, $externalUserId)
117
    {
118
        // verify ACL
119
        $profileConfig = new ProfileConfig($this->config->v('vpnProfiles', $profileId));
120
        if ($profileConfig->v('enableAcl')) {
121
            // ACL enabled
122
            $userGroups = [];
123
            foreach ($this->groupProviders as $groupProvider) {
124
                $userGroups = array_merge($userGroups, $groupProvider->getGroups($externalUserId));
125
            }
126
127
            if (false === self::isMember($userGroups, $profileConfig->v('aclGroupList'))) {
128
                return new ApiResponse('connect', ['ok' => false, 'error' => 'user not in ACL']);
129
            }
130
        }
131
132
        return true;
133
    }
134
135
    private static function isMember(array $memberOf, array $aclGroupList)
136
    {
137
        // one of the groups must be listed in the profile ACL list
138
        foreach ($memberOf as $memberGroup) {
139
            if (in_array($memberGroup['id'], $aclGroupList)) {
140
                return true;
141
            }
142
        }
143
144
        return false;
145
    }
146
147
    public function disconnect(Request $request)
148
    {
149
        $profileId = InputValidation::profileId($request->getPostParameter('profile_id'));
150
        $commonName = InputValidation::commonName($request->getPostParameter('common_name'));
151
        $ip4 = InputValidation::ip4($request->getPostParameter('ip4'));
152
        $ip6 = InputValidation::ip6($request->getPostParameter('ip6'));
153
154
        $connectedAt = InputValidation::connectedAt($request->getPostParameter('connected_at'));
155
        $disconnectedAt = InputValidation::disconnectedAt($request->getPostParameter('disconnected_at'));
156
        $bytesTransferred = InputValidation::bytesTransferred($request->getPostParameter('bytes_transferred'));
157
158 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...
159
            return new ApiResponse('disconnect', ['ok' => false, 'error' => 'unable to write disconnect event to log']);
160
        }
161
162
        return new ApiResponse('disconnect', ['ok' => true]);
163
    }
164
165
    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...
166
    {
167
    }
168
}
169