~fkooman/vpn-user-portal

ref: a305375f4d7c03df206fa5731d224f40d01dd592 vpn-user-portal/src/VpnApiModule.php -rw-r--r-- 17.8 KiB
a305375fFrançois Kooman fix response error for missing/unauthorized profile, add InputValidation 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2019, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace LC\Portal;

use DateInterval;
use DateTime;
use DateTimeZone;
use LC\Common\Config;
use LC\Common\Http\ApiErrorResponse;
use LC\Common\Http\ApiResponse;
use LC\Common\Http\Exception\InputValidationException;
use LC\Common\Http\InputValidation;
use LC\Common\Http\JsonResponse;
use LC\Common\Http\Request;
use LC\Common\Http\Response;
use LC\Common\Http\Service;
use LC\Common\Http\ServiceModuleInterface;
use LC\Common\HttpClient\ServerClient;
use LC\Common\ProfileConfig;
use LC\Portal\Exception\ClientConfigException;
use LC\Portal\OAuth\VpnAccessTokenInfo;

class VpnApiModule implements ServiceModuleInterface
{
    /** @var \LC\Common\Config */
    private $config;

    /** @var \LC\Common\HttpClient\ServerClient */
    private $serverClient;

    /** @var \DateInterval */
    private $sessionExpiry;

    /** @var \DateTime */
    private $dateTime;

    public function __construct(Config $config, ServerClient $serverClient, DateInterval $sessionExpiry)
    {
        $this->config = $config;
        $this->serverClient = $serverClient;
        $this->sessionExpiry = $sessionExpiry;
        $this->dateTime = new DateTime();
    }

    /**
     * @return void
     */
    public function init(Service $service)
    {
        // API 3
        if ($this->config->requireBool('enableApiThree', false)) {
            $service->get(
                '/v3/info',
                /**
                 * @return \LC\Common\Http\Response
                 */
                function (Request $request, array $hookData) {
                    /** @var \LC\Portal\OAuth\VpnAccessTokenInfo */
                    $accessTokenInfo = $hookData['auth'];

                    $profileList = $this->serverClient->getRequireArray('profile_list');
                    $userPermissions = $this->getPermissionList($accessTokenInfo);

                    $responseData = [
                        'info' => [
                            'profile_list' => [],
                        ],
                    ];

                    $userProfileList = [];
                    foreach ($profileList as $profileId => $profileData) {
                        $profileConfig = new ProfileConfig(new Config($profileData));
                        if ($profileConfig->hideProfile()) {
                            continue;
                        }
                        if ($profileConfig->enableAcl()) {
                            // is the user member of the aclPermissionList?
                            if (!VpnPortalModule::isMember($profileConfig->aclPermissionList(), $userPermissions)) {
                                continue;
                            }
                        }

                        $responseData['info']['profile_list'][] = [
                            'profile_id' => $profileId,
                            'display_name' => $profileConfig->displayName(),
                            'vpn_proto' => 'openvpn',
                        ];
                    }

                    return new JsonResponse($responseData, 200);
                }
            );

            $service->post(
                '/v3/connect',
                /**
                 * @return \LC\Common\Http\Response
                 */
                function (Request $request, array $hookData) {
                    /** @var \LC\Portal\OAuth\VpnAccessTokenInfo */
                    $accessTokenInfo = $hookData['auth'];
                    try {
                        $requestedProfileId = InputValidation::profileId($request->requirePostParameter('profile_id'));
                        $remoteStrategy = ClientConfig::STRATEGY_RANDOM;
                        $profileList = $this->serverClient->getRequireArray('profile_list');
                        $userPermissions = $this->getPermissionList($accessTokenInfo);
                        $availableProfiles = [];
                        foreach ($profileList as $profileId => $profileData) {
                            $profileConfig = new ProfileConfig(new Config($profileData));
                            if ($profileConfig->hideProfile()) {
                                continue;
                            }
                            if ($profileConfig->enableAcl()) {
                                // is the user member of the userPermissions?
                                if (!VpnPortalModule::isMember($profileConfig->aclPermissionList(), $userPermissions)) {
                                    continue;
                                }
                            }

                            $availableProfiles[] = $profileId;
                        }

                        if (!\in_array($requestedProfileId, $availableProfiles, true)) {
                            return new JsonResponse(['error' => 'profile not available, or no permission'], 400);
                        }

                        $tcpOnly = 'on' === InputValidation::tcpOnly($request->optionalPostParameter('tcp_only'));
                        $vpnConfig = $this->getConfigOnly($requestedProfileId, $remoteStrategy, $tcpOnly);
                        $clientCertificate = $this->getCertificate($accessTokenInfo);
                        $vpnConfig .= "\n<cert>\n".$clientCertificate['certificate']."\n</cert>\n<key>\n".$clientCertificate['private_key']."\n</key>";
                        $response = new Response(200, 'application/x-openvpn-profile');
                        $response->addHeader('Expires', $this->getExpiresAt($accessTokenInfo)->format('D, d M Y H:i:s \G\M\T'));
                        $response->setBody($vpnConfig);

                        return $response;
                    } catch (InputValidationException $e) {
                        return new JsonResponse(['error' => $e->getMessage()], 400);
                    } catch (ClientConfigException $e) {
                        return new JsonResponse(['error' => $e->getMessage()], 406);
                    }
                }
            );

            $service->post(
                '/v3/disconnect',
                /**
                 * @return \LC\Common\Http\Response
                 */
                function (Request $request, array $hookData) {
                    return new Response(204);
                }
            );
        }

        // API 1, 2
        $service->get(
            '/profile_list',
            /**
             * @return ApiResponse
             */
            function (Request $request, array $hookData) {
                /** @var \LC\Portal\OAuth\VpnAccessTokenInfo */
                $accessTokenInfo = $hookData['auth'];

                $profileList = $this->serverClient->getRequireArray('profile_list');
                $userPermissions = $this->getPermissionList($accessTokenInfo);
                $userProfileList = [];
                foreach ($profileList as $profileId => $profileData) {
                    $profileConfig = new ProfileConfig(new Config($profileData));
                    if ($profileConfig->hideProfile()) {
                        continue;
                    }
                    if ($profileConfig->enableAcl()) {
                        // is the user member of the aclPermissionList?
                        if (!VpnPortalModule::isMember($profileConfig->aclPermissionList(), $userPermissions)) {
                            continue;
                        }
                    }

                    $userProfileList[] = [
                        'profile_id' => $profileId,
                        'display_name' => $profileConfig->displayName(),
                        // 2FA is now decided by vpn-user-portal setting, so
                        // we "lie" here to the client
                        'two_factor' => false,
                        'default_gateway' => $profileConfig->defaultGateway(),
                    ];
                }

                return new ApiResponse('profile_list', $userProfileList);
            }
        );

        // API 2
        // DEPRECATED, this whole call is useless now!
        $service->get(
            '/user_info',
            /**
             * @return ApiResponse
             */
            function (Request $request, array $hookData) {
                /** @var \LC\Portal\OAuth\VpnAccessTokenInfo */
                $accessTokenInfo = $hookData['auth'];

                return new ApiResponse(
                    'user_info',
                    [
                        'user_id' => $accessTokenInfo->getUserId(),
                        // as 2FA works through the portal now, lie here to the
                        // clients so they won't try to enroll the user
                        'two_factor_enrolled' => false,
                        'two_factor_enrolled_with' => [],
                        'two_factor_supported_methods' => [],
                        // if the user is disabled, the access_token no longer
                        // works, so this is bogus
                        'is_disabled' => false,
                    ]
                );
            }
        );

        // API 2
        $service->post(
            '/create_keypair',
            /**
             * @return \LC\Common\Http\Response
             */
            function (Request $request, array $hookData) {
                /** @var \LC\Portal\OAuth\VpnAccessTokenInfo */
                $accessTokenInfo = $hookData['auth'];
                try {
                    $clientCertificate = $this->getCertificate($accessTokenInfo);

                    return new ApiResponse(
                        'create_keypair',
                        [
                            'certificate' => $clientCertificate['certificate'],
                            'private_key' => $clientCertificate['private_key'],
                        ]
                    );
                } catch (InputValidationException $e) {
                    return new ApiErrorResponse('create_keypair', $e->getMessage());
                }
            }
        );

        // API 2
        $service->get(
            '/check_certificate',
            /**
             * @return \LC\Common\Http\Response
             */
            function (Request $request, array $hookData) {
                $commonName = InputValidation::commonName($request->requireQueryParameter('common_name'));
                $clientCertificateInfo = $this->serverClient->getRequireArrayOrFalse('client_certificate_info', ['common_name' => $commonName]);
                $responseData = $this->validateCertificate($clientCertificateInfo);

                return new ApiResponse(
                    'check_certificate',
                    $responseData
                );
            }
        );

        // API 2
        $service->get(
            '/profile_config',
            /**
             * @return \LC\Common\Http\Response
             */
            function (Request $request, array $hookData) {
                /** @var \LC\Portal\OAuth\VpnAccessTokenInfo */
                $accessTokenInfo = $hookData['auth'];
                try {
                    $requestedProfileId = InputValidation::profileId($request->requireQueryParameter('profile_id'));

                    $remoteStrategy = $request->optionalQueryParameter('remote_strategy');
                    if (null === $remoteStrategy) {
                        $remoteStrategy = ClientConfig::STRATEGY_RANDOM;
                    }
                    $remoteStrategy = (int) $remoteStrategy;

                    $profileList = $this->serverClient->getRequireArray('profile_list');
                    $userPermissions = $this->getPermissionList($accessTokenInfo);

                    $availableProfiles = [];
                    foreach ($profileList as $profileId => $profileData) {
                        $profileConfig = new ProfileConfig(new Config($profileData));
                        if ($profileConfig->hideProfile()) {
                            continue;
                        }
                        if ($profileConfig->enableAcl()) {
                            // is the user member of the userPermissions?
                            if (!VpnPortalModule::isMember($profileConfig->aclPermissionList(), $userPermissions)) {
                                continue;
                            }
                        }

                        $availableProfiles[] = $profileId;
                    }

                    if (!\in_array($requestedProfileId, $availableProfiles, true)) {
                        return new ApiErrorResponse('profile_config', 'profile not available or no permission');
                    }

                    // APIv2 has no support for "tcpOnly" OpenVPN profiles, so
                    // always false...
                    $vpnConfig = $this->getConfigOnly($requestedProfileId, $remoteStrategy, false);
                    $response = new Response(200, 'application/x-openvpn-profile');
                    $response->setBody(str_replace("\n", "\r\n", $vpnConfig));

                    return $response;
                } catch (InputValidationException $e) {
                    return new ApiErrorResponse('profile_config', $e->getMessage());
                }
            }
        );

        // API 1, 2
        $service->get(
            '/user_messages',
            /**
             * @return ApiResponse
             */
            function (Request $request, array $hookData) {
                return new ApiResponse(
                    'user_messages',
                    []
                );
            }
        );

        // API 1, 2
        $service->get(
            '/system_messages',
            /**
             * @return ApiResponse
             */
            function (Request $request, array $hookData) {
                $msgList = [];

                $motdMessages = $this->serverClient->getRequireArray('system_messages', ['message_type' => 'motd']);
                foreach ($motdMessages as $motdMessage) {
                    $dateTime = new DateTime($motdMessage['date_time']);
                    $dateTime->setTimezone(new DateTimeZone('UTC'));

                    $msgList[] = [
                        // no support yet for 'motd' type in application API
                        'type' => 'notification',
                        'date_time' => $dateTime->format('Y-m-d\TH:i:s\Z'),
                        'message' => $motdMessage['message'],
                    ];
                }

                return new ApiResponse(
                    'system_messages',
                    $msgList
                );
            }
        );
    }

    /**
     * @param string $profileId
     * @param int    $remoteStrategy
     * @param bool   $tcpOnly
     *
     * @return string
     */
    private function getConfigOnly($profileId, $remoteStrategy, $tcpOnly)
    {
        // obtain information about this profile to be able to construct
        // a client configuration file
        $profileList = $this->serverClient->getRequireArray('profile_list');
        $profileConfig = new ProfileConfig(new Config($profileList[$profileId]));

        // get the CA & tls-auth
        $serverInfo = $this->serverClient->getRequireArray('server_info', ['profile_id' => $profileId]);

        return ClientConfig::get($profileConfig, $serverInfo, [], $remoteStrategy, $tcpOnly);
    }

    /**
     * @return array
     */
    private function getCertificate(VpnAccessTokenInfo $accessTokenInfo)
    {
        // create a certificate
        return $this->serverClient->postRequireArray(
            'add_client_certificate',
            [
                'user_id' => $accessTokenInfo->getUserId(),
                // we won't show the Certificate entry anyway on the
                // "Certificates" page for certificates downloaded through the
                // API
                'display_name' => $accessTokenInfo->getClientId(),
                'client_id' => $accessTokenInfo->getClientId(),
                'expires_at' => $this->getExpiresAt($accessTokenInfo)->format(DateTime::ATOM),
            ]
        );
    }

    /**
     * @param false|array $clientCertificateInfo
     *
     * @return array<string, bool|string>
     */
    private function validateCertificate($clientCertificateInfo)
    {
        $reason = '';
        if (false === $clientCertificateInfo) {
            // certificate with this CN does not exist, was deleted by
            // user, or complete new installation of service with new
            // CA
            $isValid = false;
            $reason = 'certificate_missing';
        } elseif (new DateTime($clientCertificateInfo['valid_from']) > $this->dateTime) {
            // certificate not yet valid
            $isValid = false;
            $reason = 'certificate_not_yet_valid';
        } elseif (new DateTime($clientCertificateInfo['valid_to']) < $this->dateTime) {
            // certificate not valid anymore
            $isValid = false;
            $reason = 'certificate_expired';
        } else {
            $isValid = true;
        }

        $responseData = [
            'is_valid' => $isValid,
        ];

        if (!$isValid) {
            $responseData['reason'] = $reason;
        }

        return $responseData;
    }

    /**
     * @return array<string>
     */
    private function getPermissionList(VpnAccessTokenInfo $accessTokenInfo)
    {
        if (!$accessTokenInfo->getIsLocal()) {
            return [];
        }

        return $this->serverClient->getRequireArray('user_permission_list', ['user_id' => $accessTokenInfo->getUserId()]);
    }

    /**
     * @return \DateTime
     */
    private function getExpiresAt(VpnAccessTokenInfo $accessTokenInfo)
    {
        if (!$accessTokenInfo->getIsLocal()) {
            return date_add(clone $this->dateTime, $this->sessionExpiry);
        }

        return new DateTime($this->serverClient->getRequireString('user_session_expires_at', ['user_id' => $accessTokenInfo->getUserId()]));
    }
}