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 ( aff5f5...d962fa )
by François
02:15
created

ConnectionsModule::verifyOtp()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 3
Ratio 11.11 %

Importance

Changes 0
Metric Value
dl 3
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 4
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 Base32\Base32;
22
use Otp\Otp;
23
use SURFnet\VPN\Common\Config;
24
use SURFnet\VPN\Common\Http\ApiErrorResponse;
25
use SURFnet\VPN\Common\Http\ApiResponse;
26
use SURFnet\VPN\Common\Http\AuthUtils;
27
use SURFnet\VPN\Common\Http\InputValidation;
28
use SURFnet\VPN\Common\Http\Request;
29
use SURFnet\VPN\Common\Http\Service;
30
use SURFnet\VPN\Common\Http\ServiceModuleInterface;
31
use SURFnet\VPN\Common\ProfileConfig;
32
use SURFnet\VPN\Server\Storage;
33
34
class ConnectionsModule implements ServiceModuleInterface
35
{
36
    /** @var \SURFnet\VPN\Common\Config */
37
    private $config;
38
39
    /** @var \SURFnet\VPN\Server\Storage */
40
    private $storage;
41
42
    /** @var array */
43
    private $groupProviders;
44
45
    public function __construct(Config $config, Storage $storage, array $groupProviders)
46
    {
47
        $this->config = $config;
48
        $this->storage = $storage;
49
        $this->groupProviders = $groupProviders;
50
    }
51
52
    public function init(Service $service)
53
    {
54
        $service->post(
55
            '/connect',
56
            function (Request $request, array $hookData) {
57
                AuthUtils::requireUser($hookData, ['vpn-server-node']);
58
59
                return $this->connect($request);
60
            }
61
        );
62
63
        $service->post(
64
            '/disconnect',
65
            function (Request $request, array $hookData) {
66
                AuthUtils::requireUser($hookData, ['vpn-server-node']);
67
68
                return $this->disconnect($request);
69
            }
70
        );
71
72
        $service->post(
73
            '/verify_otp',
74
            function (Request $request, array $hookData) {
75
                AuthUtils::requireUser($hookData, ['vpn-server-node']);
76
77
                return $this->verifyOtp($request);
78
            }
79
        );
80
    }
81
82
    public function connect(Request $request)
83
    {
84
        $profileId = InputValidation::profileId($request->getPostParameter('profile_id'));
85
        $commonName = InputValidation::commonName($request->getPostParameter('common_name'));
86
        $ip4 = InputValidation::ip4($request->getPostParameter('ip4'));
87
        $ip6 = InputValidation::ip6($request->getPostParameter('ip6'));
88
        $connectedAt = InputValidation::connectedAt($request->getPostParameter('connected_at'));
89
90
        if (true !== $response = $this->verifyConnection($profileId, $commonName)) {
91
            return $response;
92
        }
93
94
        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...
95
            return new ApiErrorResponse('connect', 'unable to write connect event to log');
96
        }
97
98
        return new ApiResponse('connect');
99
    }
100
101
    private function verifyConnection($profileId, $commonName)
102
    {
103
        // verify status of certificate/user
104
        if (false === $result = $this->storage->getUserCertificateInfo($commonName)) {
105
            return new ApiErrorResponse('connect', 'user or certificate does not exist');
106
        }
107
108
        if ($result['user_is_disabled']) {
109
            return new ApiErrorResponse('connect', 'user is disabled');
110
        }
111
112
        if ($result['certificate_is_disabled']) {
113
            return new ApiErrorResponse('connect', 'certificate is disabled');
114
        }
115
116
        return $this->verifyAcl($profileId, $result['user_id']);
117
    }
118
119
    private function verifyAcl($profileId, $externalUserId)
120
    {
121
        // verify ACL
122
        $profileConfig = new ProfileConfig($this->config->v('vpnProfiles', $profileId));
123
        if ($profileConfig->v('enableAcl')) {
124
            // ACL enabled
125
            $userGroups = [];
126
            foreach ($this->groupProviders as $groupProvider) {
127
                $userGroups = array_merge($userGroups, $groupProvider->getGroups($externalUserId));
128
            }
129
130
            if (false === self::isMember($userGroups, $profileConfig->v('aclGroupList'))) {
131
                return new ApiErrorResponse('connect', 'user not in ACL');
132
            }
133
        }
134
135
        return true;
136
    }
137
138
    private static function isMember(array $memberOf, array $aclGroupList)
139
    {
140
        // one of the groups must be listed in the profile ACL list
141
        foreach ($memberOf as $memberGroup) {
142
            if (in_array($memberGroup['id'], $aclGroupList)) {
143
                return true;
144
            }
145
        }
146
147
        return false;
148
    }
149
150
    public function disconnect(Request $request)
151
    {
152
        $profileId = InputValidation::profileId($request->getPostParameter('profile_id'));
153
        $commonName = InputValidation::commonName($request->getPostParameter('common_name'));
154
        $ip4 = InputValidation::ip4($request->getPostParameter('ip4'));
155
        $ip6 = InputValidation::ip6($request->getPostParameter('ip6'));
156
157
        $connectedAt = InputValidation::connectedAt($request->getPostParameter('connected_at'));
158
        $disconnectedAt = InputValidation::disconnectedAt($request->getPostParameter('disconnected_at'));
159
        $bytesTransferred = InputValidation::bytesTransferred($request->getPostParameter('bytes_transferred'));
160
161
        if (false === $this->storage->clientDisconnect($profileId, $commonName, $ip4, $ip6, $connectedAt, $disconnectedAt, $bytesTransferred)) {
162
            return new ApiErrorResponse('disconnect', 'unable to write disconnect event to log');
163
        }
164
165
        return new ApiResponse('disconnect');
166
    }
167
168
    public function verifyOtp(Request $request)
169
    {
170
        $commonName = InputValidation::commonName($request->getPostParameter('common_name'));
171
        // we do not need 'otp_type', as only 'totp' is supported at the moment
172
        InputValidation::otpType($request->getPostParameter('otp_type'));
173
        $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key'));
174
175
        $certInfo = $this->storage->getUserCertificateInfo($commonName);
176
        $userId = $certInfo['user_id'];
177
178
        if (!$this->storage->hasTotpSecret($userId)) {
179
            return new ApiErrorResponse('verify_otp', 'user has no OTP secret');
180
        }
181
182
        $totpSecret = $this->storage->getTotpSecret($userId);
183
184
        $otp = new Otp();
185
        if (!$otp->checkTotp(Base32::decode($totpSecret), $totpKey)) {
186
            return new ApiErrorResponse('verify_otp', 'invalid OTP key');
187
        }
188
189 View Code Duplication
        if (false === $this->storage->recordTotpKey($userId, $totpKey, time())) {
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...
190
            return new ApiErrorResponse('verify_otp', 'OTP key replay');
191
        }
192
193
        return new ApiResponse('verify_otp');
194
    }
195
}
196