@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | * [BasicAuth description] |
7 | 7 | * @var string |
8 | 8 | */ |
9 | - const BASIC = "Basic"; |
|
9 | + const BASIC = "Basic"; |
|
10 | 10 | |
11 | 11 | /** |
12 | 12 | * [API_KEY description] |
@@ -30,13 +30,13 @@ discard block |
||
30 | 30 | * [OAUTH2 description] |
31 | 31 | * @var string |
32 | 32 | */ |
33 | - const OAUTH2 = "OAUTH2"; |
|
33 | + const OAUTH2 = "OAUTH2"; |
|
34 | 34 | |
35 | 35 | /** |
36 | 36 | * [IP description] |
37 | 37 | * @var string |
38 | 38 | */ |
39 | - const IP = "IP"; |
|
39 | + const IP = "IP"; |
|
40 | 40 | /** |
41 | 41 | * [CUSTOM description] |
42 | 42 | * @param string $header [description] |
@@ -123,16 +123,16 @@ discard block |
||
123 | 123 | * @param array|null $params Initialization parameters from the Slint system. |
124 | 124 | * There's no use for this arg yet. |
125 | 125 | */ |
126 | - function __construct(?array $params=null) |
|
126 | + function __construct(?array $params = null) |
|
127 | 127 | { |
128 | - $this->ci =& get_instance(); |
|
128 | + $this->ci = & get_instance(); |
|
129 | 129 | |
130 | 130 | if ($this->ci->input->is_cli_request()) return; |
131 | 131 | |
132 | 132 | // Load Config If Exists. |
133 | 133 | //$this->ci->config->load('rest', true, true); |
134 | - if (is_file(APPPATH . 'config/rest.php')) { |
|
135 | - include APPPATH . 'config/rest.php'; |
|
134 | + if (is_file(APPPATH.'config/rest.php')) { |
|
135 | + include APPPATH.'config/rest.php'; |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | $this->config = $config; |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | |
149 | 149 | // Load Model. |
150 | 150 | $this->ci->load->splint(self::PACKAGE, '*RESTModel', 'rest_model'); |
151 | - $this->rest_model =& $this->ci->rest_model; |
|
151 | + $this->rest_model = & $this->ci->rest_model; |
|
152 | 152 | |
153 | 153 | $this->rest_model->init([ |
154 | 154 | 'users_table' => $config['basic_auth']['users_table'] ?? null, |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | /** |
268 | 268 | * [bearer_auth description] |
269 | 269 | */ |
270 | - private function bearer_auth($auth=RESTAuth::BEARER):void |
|
270 | + private function bearer_auth($auth = RESTAuth::BEARER):void |
|
271 | 271 | { |
272 | 272 | $authorization = $this->get_authorization_header(); |
273 | 273 | if ($authorization == null || substr_count($authorization, ' ') != 1) { |
@@ -330,7 +330,7 @@ discard block |
||
330 | 330 | } |
331 | 331 | // Should we acyually Limit? |
332 | 332 | if ($this->per_hour > 0) { |
333 | - $client = hash('md5', $this->ci->input->ip_address() . "%" . $apiKey->{$this->api_key_column}); |
|
333 | + $client = hash('md5', $this->ci->input->ip_address()."%".$apiKey->{$this->api_key_column}); |
|
334 | 334 | $limitData = $this->rest_model->getLimitData($client, '_api_keyed_user'); |
335 | 335 | if ($limitData == null) { |
336 | 336 | $limitData = []; |
@@ -414,7 +414,7 @@ discard block |
||
414 | 414 | * [get_authorization_header description] |
415 | 415 | * @return [type] [description] |
416 | 416 | */ |
417 | - private function get_authorization_header():?string |
|
417 | + private function get_authorization_header(): ?string |
|
418 | 418 | { |
419 | 419 | if (isset($_SERVER['Authorization'])) { |
420 | 420 | return trim($_SERVER["Authorization"]); |
@@ -437,7 +437,7 @@ discard block |
||
437 | 437 | * [handle_response description] |
438 | 438 | * @param int $code [description] |
439 | 439 | */ |
440 | - private function handle_response(int $code, $auth=null, ?string $errorReason=null):void |
|
440 | + private function handle_response(int $code, $auth = null, ?string $errorReason = null):void |
|
441 | 441 | { |
442 | 442 | http_response_code($code); |
443 | 443 | header("Content-Type: application/json"); |
@@ -127,7 +127,9 @@ discard block |
||
127 | 127 | { |
128 | 128 | $this->ci =& get_instance(); |
129 | 129 | |
130 | - if ($this->ci->input->is_cli_request()) return; |
|
130 | + if ($this->ci->input->is_cli_request()) { |
|
131 | + return; |
|
132 | + } |
|
131 | 133 | |
132 | 134 | // Load Config If Exists. |
133 | 135 | //$this->ci->config->load('rest', true, true); |
@@ -200,7 +202,9 @@ discard block |
||
200 | 202 | |
201 | 203 | $globalAuths = $this->config['global_auth'] ?? null; |
202 | 204 | |
203 | - if ($globalAuths) $auths = is_array($globalAuths) ? $globalAuths : [$globalAuths]; |
|
205 | + if ($globalAuths) { |
|
206 | + $auths = is_array($globalAuths) ? $globalAuths : [$globalAuths]; |
|
207 | + } |
|
204 | 208 | |
205 | 209 | $uri_auths = $this->config['uri_auth'] ?? null; |
206 | 210 | |
@@ -226,7 +230,10 @@ discard block |
||
226 | 230 | } |
227 | 231 | |
228 | 232 | //$auths = $this->config['uri_auth'][uri_string()] ?? null; |
229 | - if (!$auths) return; // No authentication(s) to carry out. |
|
233 | + if (!$auths) { |
|
234 | + return; |
|
235 | + } |
|
236 | + // No authentication(s) to carry out. |
|
230 | 237 | |
231 | 238 | // $this->process_auth() terminates the script if authentication fails |
232 | 239 | // It will call the callable in the rest.php config file under |
@@ -238,7 +245,9 @@ discard block |
||
238 | 245 | //return; |
239 | 246 | //} |
240 | 247 | |
241 | - foreach ($auths as $auth) $this->process_auth($auth); |
|
248 | + foreach ($auths as $auth) { |
|
249 | + $this->process_auth($auth); |
|
250 | + } |
|
242 | 251 | } |
243 | 252 | /** |
244 | 253 | * [process_auth description] |
@@ -294,15 +303,23 @@ discard block |
||
294 | 303 | private function basic_auth():void { |
295 | 304 | $username = $_SERVER['PHP_AUTH_USER'] ?? null; |
296 | 305 | $password = $_SERVER['PHP_AUTH_PW'] ?? null; |
297 | - if (!$username || !$password) $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); // Exits. |
|
298 | - if (!$this->rest_model->basicAuth($this, $username, $password)) $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); // Exits. |
|
306 | + if (!$username || !$password) { |
|
307 | + $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); |
|
308 | + } |
|
309 | + // Exits. |
|
310 | + if (!$this->rest_model->basicAuth($this, $username, $password)) { |
|
311 | + $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); |
|
312 | + } |
|
313 | + // Exits. |
|
299 | 314 | } |
300 | 315 | /** |
301 | 316 | * [api_key_auth description] |
302 | 317 | */ |
303 | 318 | private function api_key_auth():void |
304 | 319 | { |
305 | - if (uri_string() == '') return; |
|
320 | + if (uri_string() == '') { |
|
321 | + return; |
|
322 | + } |
|
306 | 323 | |
307 | 324 | if (!$this->ci->input->get_request_header($this->apiKeyHeader, true)) { |
308 | 325 | // if (!isset($_SERVER['HTTP_' . str_replace("-", "_", $this->apiKeyHeader)])) { |
@@ -364,7 +381,9 @@ discard block |
||
364 | 381 | // Trunctate Rate Limit Data. |
365 | 382 | $this->rest_model->truncateRatelimitData(); |
366 | 383 | // Check Whitelist. |
367 | - if (in_array($this->ci->input->ip_address(), $this->whitelist)) return; |
|
384 | + if (in_array($this->ci->input->ip_address(), $this->whitelist)) { |
|
385 | + return; |
|
386 | + } |
|
368 | 387 | // Should we acyually Limit? |
369 | 388 | if ($this->ip_per_hour > 0) { |
370 | 389 | $client = hash('md5', $this->ci->input->ip_address()); |
@@ -444,7 +463,9 @@ discard block |
||
444 | 463 | if (isset($this->config['response_callbacks'][$code])) { |
445 | 464 | $this->config['response_callbacks'][$code]($auth, $errorReason); |
446 | 465 | } |
447 | - if (ENVIRONMENT != 'testing') exit($code); |
|
466 | + if (ENVIRONMENT != 'testing') { |
|
467 | + exit($code); |
|
468 | + } |
|
448 | 469 | throw new Exception("Error $code in $auth", $code); |
449 | 470 | } |
450 | 471 | } |