GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f372ad...d5876c )
by François
02:01
created

InputValidation::languageCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
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\Common\Http;
20
21
use SURFnet\VPN\Common\Http\Exception\InputValidationException;
22
23
class InputValidation
24
{
25
    /**
26
     * @return string
27
     */
28
    public static function displayName($displayName)
29
    {
30
        $displayName = filter_var($displayName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
31
32
        if (0 === mb_strlen($displayName)) {
33
            throw new InputValidationException('invalid "display_name"');
34
        }
35
36
        return $displayName;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public static function commonName($commonName)
43
    {
44
        if (1 !== preg_match('/^[a-fA-F0-9]{32}$/', $commonName)) {
45
            throw new InputValidationException('invalid "common_name"');
46
        }
47
48
        return $commonName;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public static function serverCommonName($serverCommonName)
55
    {
56
        if (1 !== preg_match('/^[a-zA-Z0-9-.]+$/', $serverCommonName)) {
57
            throw new InputValidationException('invalid "server_common_name"');
58
        }
59
60
        return $serverCommonName;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public static function profileId($profileId)
67
    {
68
        if (1 !== preg_match('/^[a-zA-Z0-9]+$/', $profileId)) {
69
            throw new InputValidationException('invalid "profile_id"');
70
        }
71
72
        return $profileId;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public static function instanceId($instanceId)
79
    {
80
        if (1 !== preg_match('/^[a-zA-Z0-9-.]+$/', $instanceId)) {
81
            throw new InputValidationException('invalid "instance_id"');
82
        }
83
84
        return $instanceId;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public static function languageCode($languageCode)
91
    {
92
        $supportedLanguages = ['en_US', 'nl_NL', 'de_DE', 'fr_FR'];
93
        if (!in_array($languageCode, $supportedLanguages)) {
94
            throw new InputValidationException('invalid "language_code"');
95
        }
96
97
        return $languageCode;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public static function totpSecret($totpSecret)
104
    {
105
        if (1 !== preg_match('/^[A-Z0-9]{16}$/', $totpSecret)) {
106
            throw new InputValidationException('invalid "totp_secret"');
107
        }
108
109
        return $totpSecret;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public static function yubiId($yubiId)
116
    {
117
        if (1 !== preg_match('/^[a-z]{12}$/', $yubiId)) {
118
            throw new InputValidationException('invalid "yubi_id"');
119
        }
120
121
        return $yubiId;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public static function yubiOtp($yubiOtp)
128
    {
129
        if (1 !== preg_match('/^[a-z]{44}$/', $yubiOtp)) {
130
            throw new InputValidationException('invalid "yubi_otp"');
131
        }
132
133
        return $yubiOtp;
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public static function totpKey($totpKey)
140
    {
141
        if (1 !== preg_match('/^[0-9]{6}$/', $totpKey)) {
142
            throw new InputValidationException('invalid "totp_key"');
143
        }
144
145
        return $totpKey;
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public static function clientId($clientId)
152
    {
153
        if (1 !== preg_match('/^(?:[\x20-\x7E])+$/', $clientId)) {
154
            throw new InputValidationException('invalid "client_id"');
155
        }
156
157
        return $clientId;
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public static function userId($userId)
164
    {
165
        if (1 !== preg_match('/^[a-zA-Z0-9-.@]+$/', $userId)) {
166
            throw new InputValidationException('invalid "user_id"');
167
        }
168
169
        return $userId;
170
    }
171
172
    /**
173
     * @return int
174
     */
175
    public static function dateTime($dateTime)
176
    {
177
        // try to parse first
178
        if (false === $unixTime = strtotime($dateTime)) {
179
            // if that fails, check if it is already unixTime
180
            if (is_numeric($dateTime)) {
181
                $unixTime = (int) $dateTime;
182
                if (0 <= $unixTime) {
183
                    return $unixTime;
184
                }
185
            }
186
187
            throw new InputValidationException('invalid "date_time"');
188
        }
189
190
        return $unixTime;
191
    }
192
193
    /**
194
     * @return string
195
     */
196 View Code Duplication
    public static function ipAddress($ipAddress)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
197
    {
198
        if (false === filter_var($ipAddress, FILTER_VALIDATE_IP)) {
199
            throw new InputValidationException('invalid "ip_address"');
200
        }
201
202
        // normalize the IP address (only makes a difference for IPv6)
203
        return inet_ntop(inet_pton($ipAddress));
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public static function vootToken($vootToken)
210
    {
211
        if (1 !== preg_match('/^[a-zA-Z0-9-]+$/', $vootToken)) {
212
            throw new InputValidationException('invalid "voot_token"');
213
        }
214
215
        return $vootToken;
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    public static function ip4($ip4)
222
    {
223
        if (false === filter_var($ip4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
224
            throw new InputValidationException('invalid "ip4"');
225
        }
226
227
        return $ip4;
228
    }
229
230
    /**
231
     * @return string
232
     */
233 View Code Duplication
    public static function ip6($ip6)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
234
    {
235
        if (false === filter_var($ip6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
236
            throw new InputValidationException('invalid "ip6"');
237
        }
238
239
        // normalize the IPv6 address
240
        return inet_ntop(inet_pton($ip6));
241
    }
242
243
    /**
244
     * @return int
245
     */
246
    public static function connectedAt($connectedAt)
247
    {
248
        if (!is_numeric($connectedAt) || 0 > intval($connectedAt)) {
249
            throw new InputValidationException('invalid "connected_at"');
250
        }
251
252
        return intval($connectedAt);
253
    }
254
255
    /**
256
     * @return int
257
     */
258
    public static function disconnectedAt($disconnectedAt)
259
    {
260
        if (!is_numeric($disconnectedAt) || 0 > intval($disconnectedAt)) {
261
            throw new InputValidationException('invalid "disconnected_at"');
262
        }
263
264
        return intval($disconnectedAt);
265
    }
266
267
    /**
268
     * @return int
269
     */
270
    public static function bytesTransferred($bytesTransferred)
271
    {
272
        if (!is_numeric($bytesTransferred) || 0 > intval($bytesTransferred)) {
273
            throw new InputValidationException('invalid "bytes_transferred"');
274
        }
275
276
        return intval($bytesTransferred);
277
    }
278
279
    /**
280
     * @return string
281
     */
282
    public static function otpType($otpType)
283
    {
284
        if ('totp' !== $otpType && 'yubi' !== $otpType) {
285
            throw new InputValidationException('invalid "otp_type"');
286
        }
287
288
        return $otpType;
289
    }
290
291
    /**
292
     * @return int
293
     */
294
    public static function messageId($messageId)
295
    {
296
        if (!is_numeric($messageId) || 0 >= $messageId) {
297
            throw new InputValidationException('invalid "message_id"');
298
        }
299
300
        return (int) $messageId;
301
    }
302
303
    /**
304
     * @return string
305
     */
306
    public static function messageType($messageType)
307
    {
308
        if ('motd' !== $messageType && 'notification' !== $messageType && 'maintenance' !== $messageType) {
309
            throw new InputValidationException('invalid "message_type"');
310
        }
311
312
        return $messageType;
313
    }
314
}
315