|
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\Http\ApiResponse; |
|
22
|
|
|
use SURFnet\VPN\Common\Http\AuthUtils; |
|
23
|
|
|
use SURFnet\VPN\Common\Http\InputValidation; |
|
24
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
25
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
26
|
|
|
use SURFnet\VPN\Common\Http\ServiceModuleInterface; |
|
27
|
|
|
use SURFnet\VPN\Common\RandomInterface; |
|
28
|
|
|
use SURFnet\VPN\Server\CA\CaInterface; |
|
29
|
|
|
use SURFnet\VPN\Server\Storage; |
|
30
|
|
|
use SURFnet\VPN\Server\TlsAuth; |
|
31
|
|
|
|
|
32
|
|
|
class CertificatesModule implements ServiceModuleInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var \SURFnet\VPN\Server\CA\CaInterface */ |
|
35
|
|
|
private $ca; |
|
36
|
|
|
|
|
37
|
|
|
/** @var \SURFnet\VPN\Server\Storage */ |
|
38
|
|
|
private $storage; |
|
39
|
|
|
|
|
40
|
|
|
/** @var \SURFnet\VPN\Server\TlsAuth */ |
|
41
|
|
|
private $tlsAuth; |
|
42
|
|
|
|
|
43
|
|
|
/** @var \SURFnet\VPN\Common\RandomInterface */ |
|
44
|
|
|
private $random; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct(CaInterface $ca, Storage $storage, TlsAuth $tlsAuth, RandomInterface $random) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->ca = $ca; |
|
49
|
|
|
$this->storage = $storage; |
|
50
|
|
|
$this->tlsAuth = $tlsAuth; |
|
51
|
|
|
$this->random = $random; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function init(Service $service) |
|
55
|
|
|
{ |
|
56
|
|
|
/* CERTIFICATES */ |
|
57
|
|
|
$service->post( |
|
58
|
|
|
'/add_client_certificate', |
|
59
|
|
|
function (Request $request, array $hookData) { |
|
60
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
|
61
|
|
|
|
|
62
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
63
|
|
|
$displayName = InputValidation::displayName($request->getPostParameter('display_name')); |
|
64
|
|
|
|
|
65
|
|
|
// generate a random string as the certificate's CN |
|
66
|
|
|
$commonName = $this->random->get(16); |
|
67
|
|
|
$certInfo = $this->ca->clientCert($commonName); |
|
68
|
|
|
// add TLS Auth |
|
69
|
|
|
$certInfo['ta'] = $this->tlsAuth->get(); |
|
70
|
|
|
$certInfo['ca'] = $this->ca->caCert(); |
|
71
|
|
|
|
|
72
|
|
|
$this->storage->addCertificate($userId, $commonName, $displayName, $certInfo['valid_from'], $certInfo['valid_to']); |
|
73
|
|
|
|
|
74
|
|
|
return new ApiResponse('add_client_certificate', $certInfo, 201); |
|
75
|
|
|
} |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
$service->post( |
|
79
|
|
|
'/add_server_certificate', |
|
80
|
|
|
function (Request $request, array $hookData) { |
|
81
|
|
|
AuthUtils::requireUser($hookData, ['vpn-server-node']); |
|
82
|
|
|
|
|
83
|
|
|
$commonName = InputValidation::serverCommonName($request->getPostParameter('common_name')); |
|
84
|
|
|
|
|
85
|
|
|
$certInfo = $this->ca->serverCert($commonName); |
|
86
|
|
|
// add TLS Auth |
|
87
|
|
|
$certInfo['ta'] = $this->tlsAuth->get(); |
|
88
|
|
|
$certInfo['ca'] = $this->ca->caCert(); |
|
89
|
|
|
|
|
90
|
|
|
return new ApiResponse('add_server_certificate', $certInfo, 201); |
|
91
|
|
|
} |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
$service->post( |
|
95
|
|
|
'/disable_client_certificate', |
|
96
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
97
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
98
|
|
|
|
|
99
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
100
|
|
|
|
|
101
|
|
|
return new ApiResponse('disable_client_certificate', $this->storage->disableCertificate($commonName)); |
|
102
|
|
|
} |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
$service->post( |
|
106
|
|
|
'/enable_client_certificate', |
|
107
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
108
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
109
|
|
|
|
|
110
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
111
|
|
|
|
|
112
|
|
|
return new ApiResponse('enable_client_certificate', $this->storage->enableCertificate($commonName)); |
|
113
|
|
|
} |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$service->get( |
|
117
|
|
|
'/list_client_certificates', |
|
118
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
119
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
120
|
|
|
|
|
121
|
|
|
$userId = InputValidation::userId($request->getQueryParameter('user_id')); |
|
122
|
|
|
|
|
123
|
|
|
return new ApiResponse('list_client_certificates', $this->storage->getCertificates($userId)); |
|
124
|
|
|
} |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
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.