|
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\Common\Http; |
|
11
|
|
|
|
|
12
|
|
|
use DateTime; |
|
13
|
|
|
use SURFnet\VPN\Common\Http\Exception\InputValidationException; |
|
14
|
|
|
|
|
15
|
|
|
class InputValidation |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @return string |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function displayName($displayName) |
|
21
|
|
|
{ |
|
22
|
|
|
$displayName = filter_var($displayName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
|
23
|
|
|
|
|
24
|
|
|
if (0 === mb_strlen($displayName)) { |
|
25
|
|
|
throw new InputValidationException('invalid "display_name"'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return $displayName; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function commonName($commonName) |
|
35
|
|
|
{ |
|
36
|
|
|
if (1 !== preg_match('/^[a-fA-F0-9]{32}$/', $commonName)) { |
|
37
|
|
|
throw new InputValidationException('invalid "common_name"'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $commonName; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function serverCommonName($serverCommonName) |
|
47
|
|
|
{ |
|
48
|
|
|
if (1 !== preg_match('/^[a-zA-Z0-9-.]+$/', $serverCommonName)) { |
|
49
|
|
|
throw new InputValidationException('invalid "server_common_name"'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $serverCommonName; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function profileId($profileId) |
|
59
|
|
|
{ |
|
60
|
|
|
if (1 !== preg_match('/^[a-zA-Z0-9-.]+$/', $profileId)) { |
|
61
|
|
|
throw new InputValidationException('invalid "profile_id"'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $profileId; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function instanceId($instanceId) |
|
71
|
|
|
{ |
|
72
|
|
|
if (1 !== preg_match('/^[a-zA-Z0-9-.]+$/', $instanceId)) { |
|
73
|
|
|
throw new InputValidationException('invalid "instance_id"'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $instanceId; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function languageCode($languageCode) |
|
83
|
|
|
{ |
|
84
|
|
|
$supportedLanguages = ['en_US', 'nl_NL', 'de_DE', 'fr_FR']; |
|
85
|
|
|
if (!in_array($languageCode, $supportedLanguages)) { |
|
86
|
|
|
throw new InputValidationException('invalid "language_code"'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $languageCode; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function totpSecret($totpSecret) |
|
96
|
|
|
{ |
|
97
|
|
|
if (1 !== preg_match('/^[A-Z0-9]{16}$/', $totpSecret)) { |
|
98
|
|
|
throw new InputValidationException('invalid "totp_secret"'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $totpSecret; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function yubiKeyOtp($yubiKeyOtp) |
|
108
|
|
|
{ |
|
109
|
|
|
if (1 !== preg_match('/^[a-z]{44}$/', $yubiKeyOtp)) { |
|
110
|
|
|
throw new InputValidationException('invalid "yubi_key_otp"'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $yubiKeyOtp; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function totpKey($totpKey) |
|
120
|
|
|
{ |
|
121
|
|
|
if (1 !== preg_match('/^[0-9]{6}$/', $totpKey)) { |
|
122
|
|
|
throw new InputValidationException('invalid "totp_key"'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $totpKey; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
public static function clientId($clientId) |
|
132
|
|
|
{ |
|
133
|
|
|
if (1 !== preg_match('/^(?:[\x20-\x7E])+$/', $clientId)) { |
|
134
|
|
|
throw new InputValidationException('invalid "client_id"'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $clientId; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function userId($userId) |
|
144
|
|
|
{ |
|
145
|
|
|
if (1 !== preg_match('/^[a-zA-Z0-9-_.|@]+$/', $userId)) { |
|
146
|
|
|
throw new InputValidationException('invalid "user_id"'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $userId; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string $dateTime |
|
154
|
|
|
* |
|
155
|
|
|
* @return DateTime |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function dateTime($dateTime) |
|
158
|
|
|
{ |
|
159
|
|
|
if (false === $dateTimeObj = DateTime::createFromFormat('Y-m-d H:i:s', $dateTime)) { |
|
160
|
|
|
throw new InputValidationException('invalid "date_time"'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $dateTimeObj; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
View Code Duplication |
public static function ipAddress($ipAddress) |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
if (false === filter_var($ipAddress, FILTER_VALIDATE_IP)) { |
|
172
|
|
|
throw new InputValidationException('invalid "ip_address"'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// normalize the IP address (only makes a difference for IPv6) |
|
176
|
|
|
return inet_ntop(inet_pton($ipAddress)); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
|
|
public static function ip4($ip4) |
|
183
|
|
|
{ |
|
184
|
|
|
if (false === filter_var($ip4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
185
|
|
|
throw new InputValidationException('invalid "ip4"'); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $ip4; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @return string |
|
193
|
|
|
*/ |
|
194
|
|
View Code Duplication |
public static function ip6($ip6) |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
|
|
if (false === filter_var($ip6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
197
|
|
|
throw new InputValidationException('invalid "ip6"'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
// normalize the IPv6 address |
|
201
|
|
|
return inet_ntop(inet_pton($ip6)); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return int |
|
206
|
|
|
*/ |
|
207
|
|
|
public static function connectedAt($connectedAt) |
|
208
|
|
|
{ |
|
209
|
|
|
if (!is_numeric($connectedAt) || 0 > intval($connectedAt)) { |
|
210
|
|
|
throw new InputValidationException('invalid "connected_at"'); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return intval($connectedAt); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @return int |
|
218
|
|
|
*/ |
|
219
|
|
|
public static function disconnectedAt($disconnectedAt) |
|
220
|
|
|
{ |
|
221
|
|
|
if (!is_numeric($disconnectedAt) || 0 > intval($disconnectedAt)) { |
|
222
|
|
|
throw new InputValidationException('invalid "disconnected_at"'); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return intval($disconnectedAt); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return int |
|
230
|
|
|
*/ |
|
231
|
|
|
public static function bytesTransferred($bytesTransferred) |
|
232
|
|
|
{ |
|
233
|
|
|
if (!is_numeric($bytesTransferred) || 0 > intval($bytesTransferred)) { |
|
234
|
|
|
throw new InputValidationException('invalid "bytes_transferred"'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return intval($bytesTransferred); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @return string |
|
242
|
|
|
*/ |
|
243
|
|
|
public static function twoFactorType($twoFactorType) |
|
244
|
|
|
{ |
|
245
|
|
|
if ('totp' !== $twoFactorType && 'yubi' !== $twoFactorType) { |
|
246
|
|
|
throw new InputValidationException('invalid "two_factor_type"'); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return $twoFactorType; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @return string |
|
254
|
|
|
*/ |
|
255
|
|
|
public static function twoFactorValue($twoFactorValue) |
|
256
|
|
|
{ |
|
257
|
|
|
if (!is_string($twoFactorValue) || 0 >= strlen($twoFactorValue)) { |
|
258
|
|
|
throw new InputValidationException('invalid "two_factor_value"'); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return $twoFactorValue; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @return int |
|
266
|
|
|
*/ |
|
267
|
|
|
public static function messageId($messageId) |
|
268
|
|
|
{ |
|
269
|
|
|
if (!is_numeric($messageId) || 0 >= $messageId) { |
|
270
|
|
|
throw new InputValidationException('invalid "message_id"'); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
return (int) $messageId; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @return string |
|
278
|
|
|
*/ |
|
279
|
|
|
public static function messageType($messageType) |
|
280
|
|
|
{ |
|
281
|
|
|
if ('motd' !== $messageType && 'notification' !== $messageType && 'maintenance' !== $messageType) { |
|
282
|
|
|
throw new InputValidationException('invalid "message_type"'); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
return $messageType; |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
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.