Total Complexity | 66 |
Total Lines | 427 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like REST often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use REST, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class REST |
||
10 | { |
||
11 | /** |
||
12 | * [private description] |
||
13 | * @var [type] |
||
|
|||
14 | */ |
||
15 | private $ci; |
||
16 | |||
17 | /** |
||
18 | * [private description] |
||
19 | * @var [type] |
||
20 | */ |
||
21 | private $api_key_limit_column; |
||
22 | |||
23 | /** |
||
24 | * [private description] |
||
25 | * @var [type] |
||
26 | */ |
||
27 | private $api_key_column; |
||
28 | |||
29 | /** |
||
30 | * [private description] |
||
31 | * @var [type] |
||
32 | */ |
||
33 | private $per_hour; |
||
34 | |||
35 | /** |
||
36 | * [private description] |
||
37 | * @var [type] |
||
38 | */ |
||
39 | private $ip_per_hour; |
||
40 | |||
41 | /** |
||
42 | * [private description] |
||
43 | * @var [type] |
||
44 | */ |
||
45 | private $show_header; |
||
46 | |||
47 | /** |
||
48 | * [private description] |
||
49 | * @var [type] |
||
50 | */ |
||
51 | private $whitelist; |
||
52 | |||
53 | /** |
||
54 | * [private description] |
||
55 | * @var [type] |
||
56 | */ |
||
57 | private $checked_rate_limit = false; |
||
58 | |||
59 | /** |
||
60 | * [private description] |
||
61 | * @var [type] |
||
62 | */ |
||
63 | private $header_prefix; |
||
64 | |||
65 | /** |
||
66 | * [private description] |
||
67 | * @var [type] |
||
68 | */ |
||
69 | private $limit_api; |
||
70 | |||
71 | /** |
||
72 | * [public description] |
||
73 | * @var [type] |
||
74 | */ |
||
75 | public $userId; |
||
76 | |||
77 | /** |
||
78 | * [public description] |
||
79 | * @var [type] |
||
80 | */ |
||
81 | public $apiKeyHeader; |
||
82 | |||
83 | /** |
||
84 | * [public description] |
||
85 | * @var [type] |
||
86 | */ |
||
87 | public $token; |
||
88 | |||
89 | /** |
||
90 | * [public description] |
||
91 | * @var [type] |
||
92 | */ |
||
93 | public $allowedIps; |
||
94 | |||
95 | /** |
||
96 | * [PACKAGE description] |
||
97 | * @var string |
||
98 | */ |
||
99 | const PACKAGE = "francis94c/ci-rest"; |
||
100 | |||
101 | /** |
||
102 | * [RATE_LIMIT description] |
||
103 | * @var string |
||
104 | */ |
||
105 | const RATE_LIMIT = "RateLimit"; |
||
106 | |||
107 | /** |
||
108 | * [__construct This is the part of the code that takes care of all |
||
109 | * authentiations. allowing you to focus on building wonderful things at REST. |
||
110 | * pun intended ;-)] |
||
111 | * @param array|null $params Initialization parameters from the Slint system. |
||
112 | * There's no use for this arg yet. |
||
113 | */ |
||
114 | function __construct(?array $params=null) |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * [authenticate description] |
||
180 | * @date 2020-01-30 |
||
181 | */ |
||
182 | private function authenticate():void |
||
233 | } |
||
234 | /** |
||
235 | * [process_auth description] |
||
236 | * @param string $auth [description] |
||
237 | * @return bool [description] |
||
238 | */ |
||
239 | private function process_auth(string &$auth):void { |
||
240 | switch ($auth) { |
||
241 | case RESTAuth::IP: $this->ip_auth(); break; |
||
242 | case RESTAuth::BASIC: $this->basic_auth(); break; |
||
243 | case RESTAuth::API_KEY: $this->api_key_auth(); break; |
||
244 | case RESTAuth::OAUTH2: $this->bearer_auth(RESTAuth::OAUTH2); break; |
||
245 | case RESTAuth::BEARER: $this->bearer_auth(); break; |
||
246 | default: $this->custom_auth($auth); |
||
247 | } |
||
248 | } |
||
249 | /** |
||
250 | * [ip_auth description] |
||
251 | */ |
||
252 | private function ip_auth():void { |
||
253 | if (!in_array($this->ci->input->ip_address(), $this->allowedIps)) { |
||
254 | $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::IP); // Exits. |
||
255 | } |
||
256 | } |
||
257 | /** |
||
258 | * [bearer_auth description] |
||
259 | */ |
||
260 | private function bearer_auth($auth=RESTAuth::BEARER):void { |
||
261 | $authorization = $this->get_authorization_header(); |
||
262 | if ($authorization == null || substr_count($authorization, " ") != 1) { |
||
263 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth); // Exits. |
||
264 | } |
||
265 | $token = explode(" ", $authorization); |
||
266 | if ($token[0] != "Bearer") { |
||
267 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth); // Exits. |
||
268 | } |
||
269 | $this->token = $token[1]; |
||
270 | // Call Up Custom Implemented Bearer/Token Authorization. |
||
271 | // Callback Check. |
||
272 | if (!isset($this->ci->config->item('rest')['auth_callbacks'][$auth])) { |
||
273 | $this->handle_response(RESTResponse::NOT_IMPLEMENTED, $auth); // Exits. |
||
274 | } |
||
275 | // Authorization. |
||
276 | if (!$this->ci->config->item('rest')['auth_callbacks'][$auth]($this, $this->token)) { |
||
277 | $this->handle_response(RESTResponse::UN_AUTHORIZED, $auth); // Exits. |
||
278 | } |
||
279 | } |
||
280 | /** |
||
281 | * [basic_auth description] |
||
282 | */ |
||
283 | private function basic_auth():void { |
||
284 | $username = $_SERVER['PHP_AUTH_USER'] ?? null; |
||
285 | $password = $_SERVER['PHP_AUTH_PW'] ?? null; |
||
286 | if (!$username || !$password) $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); // Exits. |
||
287 | if (!$this->rest_model->basicAuth($this, $username, $password)) $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); // Exits. |
||
288 | } |
||
289 | /** |
||
290 | * [api_key_auth description] |
||
291 | */ |
||
292 | private function api_key_auth():void |
||
293 | { |
||
294 | if (uri_string() == '') return; |
||
295 | |||
296 | if (!$this->ci->input->get_request_header($this->apiKeyHeader, true)) { |
||
297 | // if (!isset($_SERVER['HTTP_' . str_replace("-", "_", $this->apiKeyHeader)])) { |
||
298 | $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::API_KEY); // Exits. |
||
299 | } |
||
300 | |||
301 | $apiKey = $this->rest_model->getAPIKeyData( |
||
302 | $this->ci->input->get_request_header($this->apiKeyHeader, true) |
||
303 | ); |
||
304 | |||
305 | if ($apiKey == null) { |
||
306 | $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::API_KEY); // Exits. |
||
307 | } |
||
308 | |||
309 | // API KEY Auth Passed Above. |
||
310 | if ($this->limit_api && $this->api_key_limit_column != null && $apiKey[$this->api_key_limit_column] == 1) { |
||
311 | // Trunctate Rate Limit Data. |
||
312 | $this->rest_model->truncateRatelimitData(); |
||
313 | // Check Whitelist. |
||
314 | if (in_array($this->ci->input->ip_address(), $this->whitelist)) { |
||
315 | $this->checked_rate_limit = true; // Ignore Limit By IP. |
||
316 | return; |
||
317 | } |
||
318 | // Should we acyually Limit? |
||
319 | if ($this->per_hour > 0) { |
||
320 | $client = hash('md5', $this->ci->input->ip_address() . "%" . $apiKey[$this->api_key_column]); |
||
321 | $limitData = $this->rest_model->getLimitData($client, '_api_keyed_user'); |
||
322 | if ($limitData == null) { |
||
323 | $limitData = []; |
||
324 | $limitData['count'] = 0; |
||
325 | $limitData['reset_epoch'] = gmdate('d M Y H:i:s', time() + (60 * 60)); |
||
326 | $limitData['start'] = date('d M Y H:i:s'); |
||
327 | } |
||
328 | if ($this->per_hour - $limitData['count'] > 0) { |
||
329 | if (!$this->rest_model->insertLimitData($client, '_api_keyed_user')) { |
||
330 | $this->handle_response(RESTResponse::INTERNAL_SERVER_ERROR, self::RATE_LIMIT); // Exits. |
||
331 | } |
||
332 | ++$limitData['count']; |
||
333 | if ($this->show_header) { |
||
334 | header($this->header_prefix.'Limit: '.$this->per_hour); |
||
335 | header($this->header_prefix.'Remaining: '.($this->per_hour - $limitData['count'])); |
||
336 | header($this->header_prefix.'Reset: '.strtotime($limitData['reset_epoch'])); |
||
337 | } |
||
338 | } else { |
||
339 | header('Retry-After: '.(strtotime($limitData['reset_epoch']) - strtotime(gmdate('d M Y H:i:s')))); |
||
340 | $this->handle_response(RESTResponse::TOO_MANY_REQUESTS, self::RATE_LIMIT); // Exits. |
||
341 | } |
||
342 | } |
||
343 | } |
||
344 | $this->checked_rate_limit = true; // Ignore Limit By IP. |
||
345 | } |
||
346 | /** |
||
347 | * [api_rest_limit_by_ip_address description] |
||
348 | * TODO: Implement. |
||
349 | */ |
||
350 | private function api_rest_limit_by_ip_address():void { |
||
351 | // Trunctate Rate Limit Data. |
||
352 | $this->rest_model->truncateRatelimitData(); |
||
353 | // Check Whitelist. |
||
354 | if (in_array($this->ci->input->ip_address(), $this->whitelist)) return; |
||
355 | // Should we acyually Limit? |
||
356 | if ($this->ip_per_hour > 0) { |
||
357 | $client = hash('md5', $this->ci->input->ip_address()); |
||
358 | $limitData = $this->rest_model->getLimitData($client, '_ip_address'); |
||
359 | if ($limitData == null) { |
||
360 | $limitData = []; |
||
361 | $limitData['count'] = 0; |
||
362 | $limitData['reset_epoch'] = gmdate('d M Y H:i:s', time() + (60 * 60)); |
||
363 | $limitData['start'] = date('d M Y H:i:s'); |
||
364 | } |
||
365 | if ($this->ip_per_hour - $limitData['count'] > 0) { |
||
366 | if (!$this->rest_model->insertLimitData($client, '_ip_address')) { |
||
367 | $this->handle_response(RESTResponse::INTERNAL_SERVER_ERROR, self::RATE_LIMIT); // Exits. |
||
368 | } |
||
369 | ++$limitData['count']; |
||
370 | if ($this->show_header) { |
||
371 | header($this->header_prefix.'Limit: '.$this->ip_per_hour); |
||
372 | header($this->header_prefix.'Remaining: '.($this->ip_per_hour - $limitData['count'])); |
||
373 | header($this->header_prefix.'Reset: '.strtotime($limitData['reset_epoch'])); |
||
374 | } |
||
375 | } else { |
||
376 | header('Retry-After: '.(strtotime($limitData['reset_epoch']) - strtotime(gmdate('d M Y H:i:s')))); |
||
377 | $this->handle_response(RESTResponse::TOO_MANY_REQUESTS, self::RATE_LIMIT); // Exits. |
||
378 | } |
||
379 | } |
||
380 | } |
||
381 | /** |
||
382 | * [custom_auth description] |
||
383 | * @param string $auth [description] |
||
384 | */ |
||
385 | private function custom_auth(string &$auth):void |
||
398 | } |
||
399 | } |
||
400 | /** |
||
401 | * [get_authorization_header description] |
||
402 | * @return [type] [description] |
||
403 | */ |
||
404 | private function get_authorization_header():?string |
||
405 | { |
||
406 | if (isset($_SERVER['Authorization'])) { |
||
407 | return trim($_SERVER["Authorization"]); |
||
408 | } else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI |
||
409 | return trim($_SERVER["HTTP_AUTHORIZATION"]); |
||
410 | } elseif (function_exists('apache_request_headers')) { |
||
411 | $requestHeaders = apache_request_headers(); |
||
412 | |||
413 | // Avoid Surprises. |
||
414 | $requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders)); |
||
415 | |||
416 | if (isset($requestHeaders['Authorization'])) { |
||
417 | return trim($requestHeaders['Authorization']); |
||
418 | } |
||
419 | } |
||
420 | return null; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * [handle_response description] |
||
425 | * @param int $code [description] |
||
426 | */ |
||
427 | private function handle_response(int $code, $auth=null):void |
||
436 | } |
||
437 | } |
||
439 |