|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Server\Api; |
|
11
|
|
|
|
|
12
|
|
|
use DateTime; |
|
13
|
|
|
use SURFnet\VPN\Common\Http\ApiResponse; |
|
14
|
|
|
use SURFnet\VPN\Common\Http\AuthUtils; |
|
15
|
|
|
use SURFnet\VPN\Common\Http\InputValidation; |
|
16
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
17
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
18
|
|
|
use SURFnet\VPN\Common\Http\ServiceModuleInterface; |
|
19
|
|
|
use SURFnet\VPN\Common\RandomInterface; |
|
20
|
|
|
use SURFnet\VPN\Server\CA\CaInterface; |
|
21
|
|
|
use SURFnet\VPN\Server\Storage; |
|
22
|
|
|
use SURFnet\VPN\Server\TlsAuth; |
|
23
|
|
|
|
|
24
|
|
|
class CertificatesModule implements ServiceModuleInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var \SURFnet\VPN\Server\CA\CaInterface */ |
|
27
|
|
|
private $ca; |
|
28
|
|
|
|
|
29
|
|
|
/** @var \SURFnet\VPN\Server\Storage */ |
|
30
|
|
|
private $storage; |
|
31
|
|
|
|
|
32
|
|
|
/** @var \SURFnet\VPN\Server\TlsAuth */ |
|
33
|
|
|
private $tlsAuth; |
|
34
|
|
|
|
|
35
|
|
|
/** @var \SURFnet\VPN\Common\RandomInterface */ |
|
36
|
|
|
private $random; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(CaInterface $ca, Storage $storage, TlsAuth $tlsAuth, RandomInterface $random) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->ca = $ca; |
|
41
|
|
|
$this->storage = $storage; |
|
42
|
|
|
$this->tlsAuth = $tlsAuth; |
|
43
|
|
|
$this->random = $random; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function init(Service $service) |
|
47
|
|
|
{ |
|
48
|
|
|
/* CERTIFICATES */ |
|
49
|
|
|
$service->post( |
|
50
|
|
|
'/add_client_certificate', |
|
51
|
|
|
function (Request $request, array $hookData) { |
|
52
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
|
53
|
|
|
|
|
54
|
|
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
55
|
|
|
$displayName = InputValidation::displayName($request->getPostParameter('display_name')); |
|
56
|
|
|
|
|
57
|
|
|
// generate a random string as the certificate's CN |
|
58
|
|
|
$commonName = $this->random->get(16); |
|
59
|
|
|
$certInfo = $this->ca->clientCert($commonName); |
|
60
|
|
|
|
|
61
|
|
|
$this->storage->addCertificate( |
|
62
|
|
|
$userId, |
|
63
|
|
|
$commonName, |
|
64
|
|
|
$displayName, |
|
65
|
|
|
new DateTime(sprintf('@%d', $certInfo['valid_from'])), |
|
66
|
|
|
new DateTime(sprintf('@%d', $certInfo['valid_to'])) |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$this->storage->addUserMessage( |
|
70
|
|
|
$userId, |
|
71
|
|
|
'notification', |
|
72
|
|
|
sprintf('new certificate "%s" generated by user', $displayName) |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
return new ApiResponse('add_client_certificate', $certInfo, 201); |
|
76
|
|
|
} |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
/* |
|
80
|
|
|
* This provides the CA (public) certificate and the "tls-auth" key |
|
81
|
|
|
* for this instance. The API call has a terrible name... |
|
82
|
|
|
*/ |
|
83
|
|
|
$service->get( |
|
84
|
|
|
'/server_info', |
|
85
|
|
|
function (Request $request, array $hookData) { |
|
86
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
|
87
|
|
|
|
|
88
|
|
|
$serverInfo = [ |
|
89
|
|
|
'ta' => $this->tlsAuth->get(), |
|
90
|
|
|
'ca' => $this->ca->caCert(), |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
return new ApiResponse('server_info', $serverInfo); |
|
94
|
|
|
} |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$service->post( |
|
98
|
|
|
'/add_server_certificate', |
|
99
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
100
|
|
|
AuthUtils::requireUser($hookData, ['vpn-server-node']); |
|
101
|
|
|
|
|
102
|
|
|
$commonName = InputValidation::serverCommonName($request->getPostParameter('common_name')); |
|
103
|
|
|
|
|
104
|
|
|
$certInfo = $this->ca->serverCert($commonName); |
|
105
|
|
|
// add TLS Auth |
|
106
|
|
|
$certInfo['ta'] = $this->tlsAuth->get(); |
|
107
|
|
|
$certInfo['ca'] = $this->ca->caCert(); |
|
108
|
|
|
|
|
109
|
|
|
return new ApiResponse('add_server_certificate', $certInfo, 201); |
|
110
|
|
|
} |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
$service->post( |
|
114
|
|
|
'/delete_client_certificate', |
|
115
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
116
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
|
117
|
|
|
|
|
118
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
119
|
|
|
$certInfo = $this->storage->getUserCertificateInfo($commonName); |
|
120
|
|
|
|
|
121
|
|
|
$this->storage->addUserMessage( |
|
122
|
|
|
$certInfo['user_id'], |
|
123
|
|
|
'notification', |
|
124
|
|
|
sprintf('certificate "%s" deleted by user', $certInfo['display_name']) |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
return new ApiResponse('delete_client_certificate', $this->storage->deleteCertificate($commonName)); |
|
128
|
|
|
} |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
$service->post( |
|
132
|
|
|
'/disable_client_certificate', |
|
133
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
134
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
135
|
|
|
|
|
136
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
137
|
|
|
$certInfo = $this->storage->getUserCertificateInfo($commonName); |
|
138
|
|
|
|
|
139
|
|
|
$this->storage->addUserMessage( |
|
140
|
|
|
$certInfo['user_id'], |
|
141
|
|
|
'notification', |
|
142
|
|
|
sprintf('certificate "%s" disabled by an administrator', $certInfo['display_name']) |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
return new ApiResponse('disable_client_certificate', $this->storage->disableCertificate($commonName)); |
|
146
|
|
|
} |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
$service->post( |
|
150
|
|
|
'/enable_client_certificate', |
|
151
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
152
|
|
|
AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
|
153
|
|
|
|
|
154
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
155
|
|
|
$certInfo = $this->storage->getUserCertificateInfo($commonName); |
|
156
|
|
|
|
|
157
|
|
|
$this->storage->addUserMessage( |
|
158
|
|
|
$certInfo['user_id'], |
|
159
|
|
|
'notification', |
|
160
|
|
|
sprintf('certificate "%s" enabled by an administrator', $certInfo['display_name']) |
|
161
|
|
|
); |
|
162
|
|
|
|
|
163
|
|
|
return new ApiResponse('enable_client_certificate', $this->storage->enableCertificate($commonName)); |
|
164
|
|
|
} |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
$service->get( |
|
168
|
|
|
'/client_certificate_list', |
|
169
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
170
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
171
|
|
|
|
|
172
|
|
|
$userId = InputValidation::userId($request->getQueryParameter('user_id')); |
|
173
|
|
|
|
|
174
|
|
|
return new ApiResponse('client_certificate_list', $this->storage->getCertificates($userId)); |
|
175
|
|
|
} |
|
176
|
|
|
); |
|
177
|
|
|
|
|
178
|
|
|
$service->get( |
|
179
|
|
|
'/client_certificate_info', |
|
180
|
|
View Code Duplication |
function (Request $request, array $hookData) { |
|
|
|
|
|
|
181
|
|
|
AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
|
182
|
|
|
|
|
183
|
|
|
$commonName = InputValidation::commonName($request->getQueryParameter('common_name')); |
|
184
|
|
|
|
|
185
|
|
|
return new ApiResponse('client_certificate_info', $this->storage->getUserCertificateInfo($commonName)); |
|
186
|
|
|
} |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
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.