|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server; |
|
19
|
|
|
|
|
20
|
|
|
use fkooman\Http\Exception\BadRequestException; |
|
21
|
|
|
|
|
22
|
|
|
class InputValidation |
|
23
|
|
|
{ |
|
24
|
|
|
const COMMON_NAME_PATTERN = '/^[a-zA-Z0-9-_.@]+$/'; |
|
25
|
|
|
const USER_ID_PATTERN = '/^[a-zA-Z0-9-_.@]+$/'; |
|
26
|
|
|
const OTP_KEY_PATTERN = '/^[0-9]{6}$/'; |
|
27
|
|
|
const OTP_SECRET_PATTERN = '/^[A-Z0-9]{16}$/'; |
|
28
|
|
|
const ACCESS_TOKEN_PATTERN = '/^[\x20-\x7E]+$/'; |
|
29
|
|
|
|
|
30
|
|
|
public static function commonName($commonName) |
|
31
|
|
|
{ |
|
32
|
|
|
if (0 === preg_match(self::COMMON_NAME_PATTERN, $commonName)) { |
|
33
|
|
|
throw new BadRequestException('invalid value for "common_name"'); |
|
34
|
|
|
} |
|
35
|
|
|
if ('..' === $commonName) { |
|
36
|
|
|
throw new BadRequestException('"common_name" cannot be ".."'); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function userId($userId) |
|
41
|
|
|
{ |
|
42
|
|
|
if (0 === preg_match(self::USER_ID_PATTERN, $userId)) { |
|
43
|
|
|
throw new BadRequestException('invalid value for "user_id"'); |
|
44
|
|
|
} |
|
45
|
|
|
if ('..' === $userId) { |
|
46
|
|
|
throw new BadRequestException('"user_id" cannot be ".."'); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function disable($disable) |
|
51
|
|
|
{ |
|
52
|
|
|
if (!is_bool($disable)) { |
|
53
|
|
|
throw new BadRequestException('"disable" must be boolean'); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function otpKey($otpKey) |
|
58
|
|
|
{ |
|
59
|
|
|
if (0 === preg_match(self::OTP_KEY_PATTERN, $otpKey)) { |
|
60
|
|
|
throw new BadRequestException('invalid OTP key format'); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public static function otpSecret($otpSecret) |
|
65
|
|
|
{ |
|
66
|
|
|
if (0 === preg_match(self::OTP_SECRET_PATTERN, $otpSecret)) { |
|
67
|
|
|
throw new BadRequestException('invalid OTP secret format'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public static function vootToken($vootToken) |
|
72
|
|
|
{ |
|
73
|
|
|
if (!is_string($vootToken) || 0 >= strlen($vootToken)) { |
|
74
|
|
|
throw new BadRequestException('voot token must be non-empty string'); |
|
75
|
|
|
} |
|
76
|
|
|
if (0 === preg_match(self::ACCESS_TOKEN_PATTERN, $vootToken)) { |
|
77
|
|
|
throw new BadRequestException('invalid value for "vootToken"'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function dateTime($dateTime) |
|
82
|
|
|
{ |
|
83
|
|
|
if (false === strtotime($dateTime)) { |
|
84
|
|
|
throw new BadRequestException('invalid date/time format'); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public static function ipAddress($ipAddress) |
|
89
|
|
|
{ |
|
90
|
|
|
if (false === filter_var($ipAddress, FILTER_VALIDATE_IP)) { |
|
91
|
|
|
throw new BadRequestException('invalid IP address'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|