@@ -201,7 +201,7 @@ |
||
| 201 | 201 | if ($uri_auths != null || is_array($uri_auths)) { |
| 202 | 202 | foreach ($uri_auths as $uri => $auth_array) { |
| 203 | 203 | // Convert wildcards to RegEx. |
| 204 | - $uri = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $uri); |
|
| 204 | + $uri = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $uri); |
|
| 205 | 205 | if (preg_match('#^'.$uri.'$#', uri_string())) { |
| 206 | 206 | // Assign Authentication Steps. |
| 207 | 207 | if (is_array($auth_array)) { |
@@ -111,9 +111,9 @@ discard block |
||
| 111 | 111 | * @param array|null $params Initialization parameters from the Slint system. |
| 112 | 112 | * There's no use for this arg yet. |
| 113 | 113 | */ |
| 114 | - function __construct(?array $params=null) |
|
| 114 | + function __construct(?array $params = null) |
|
| 115 | 115 | { |
| 116 | - $this->ci =& get_instance(); |
|
| 116 | + $this->ci = & get_instance(); |
|
| 117 | 117 | |
| 118 | 118 | if ($this->ci->input->is_cli_request()) return; |
| 119 | 119 | |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | |
| 132 | 132 | // Load Model. |
| 133 | 133 | $this->ci->load->splint(self::PACKAGE, '*RESTModel', 'rest_model'); |
| 134 | - $this->rest_model =& $this->ci->rest_model; |
|
| 134 | + $this->rest_model = & $this->ci->rest_model; |
|
| 135 | 135 | |
| 136 | 136 | $config = [ |
| 137 | 137 | 'users_table' => $this->ci->config->item('rest')['basic_auth']['users_table'] ?? null, |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | /** |
| 258 | 258 | * [bearer_auth description] |
| 259 | 259 | */ |
| 260 | - private function bearer_auth($auth=RESTAuth::BEARER):void { |
|
| 260 | + private function bearer_auth($auth = RESTAuth::BEARER):void { |
|
| 261 | 261 | $authorization = $this->get_authorization_header(); |
| 262 | 262 | if ($authorization == null || substr_count($authorization, " ") != 1) { |
| 263 | 263 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth); // Exits. |
@@ -317,7 +317,7 @@ discard block |
||
| 317 | 317 | } |
| 318 | 318 | // Should we acyually Limit? |
| 319 | 319 | if ($this->per_hour > 0) { |
| 320 | - $client = hash('md5', $this->ci->input->ip_address() . "%" . $apiKey[$this->api_key_column]); |
|
| 320 | + $client = hash('md5', $this->ci->input->ip_address()."%".$apiKey[$this->api_key_column]); |
|
| 321 | 321 | $limitData = $this->rest_model->getLimitData($client, '_api_keyed_user'); |
| 322 | 322 | if ($limitData == null) { |
| 323 | 323 | $limitData = []; |
@@ -401,7 +401,7 @@ discard block |
||
| 401 | 401 | * [get_authorization_header description] |
| 402 | 402 | * @return [type] [description] |
| 403 | 403 | */ |
| 404 | - private function get_authorization_header():?string |
|
| 404 | + private function get_authorization_header(): ?string |
|
| 405 | 405 | { |
| 406 | 406 | if (isset($_SERVER['Authorization'])) { |
| 407 | 407 | return trim($_SERVER["Authorization"]); |
@@ -424,7 +424,7 @@ discard block |
||
| 424 | 424 | * [handle_response description] |
| 425 | 425 | * @param int $code [description] |
| 426 | 426 | */ |
| 427 | - private function handle_response(int $code, $auth=null):void |
|
| 427 | + private function handle_response(int $code, $auth = null):void |
|
| 428 | 428 | { |
| 429 | 429 | http_response_code($code); |
| 430 | 430 | header("Content-Type: application/json"); |
@@ -115,7 +115,9 @@ discard block |
||
| 115 | 115 | { |
| 116 | 116 | $this->ci =& get_instance(); |
| 117 | 117 | |
| 118 | - if ($this->ci->input->is_cli_request()) return; |
|
| 118 | + if ($this->ci->input->is_cli_request()) { |
|
| 119 | + return; |
|
| 120 | + } |
|
| 119 | 121 | |
| 120 | 122 | // Load Config If Exists. |
| 121 | 123 | $this->ci->config->load('rest', true, true); |
@@ -217,7 +219,10 @@ discard block |
||
| 217 | 219 | } |
| 218 | 220 | |
| 219 | 221 | //$auths = $this->ci->config->item('rest')['uri_auth'][uri_string()] ?? null; |
| 220 | - if ($auths == null) return; // No authentication(s) to carry out. |
|
| 222 | + if ($auths == null) { |
|
| 223 | + return; |
|
| 224 | + } |
|
| 225 | + // No authentication(s) to carry out. |
|
| 221 | 226 | |
| 222 | 227 | // $this->process_auth() terminates the script if authentication fails |
| 223 | 228 | // It will call the callable in the rest.php config file under |
@@ -229,7 +234,9 @@ discard block |
||
| 229 | 234 | //return; |
| 230 | 235 | //} |
| 231 | 236 | |
| 232 | - foreach ($auths as $auth) $this->process_auth($auth); |
|
| 237 | + foreach ($auths as $auth) { |
|
| 238 | + $this->process_auth($auth); |
|
| 239 | + } |
|
| 233 | 240 | } |
| 234 | 241 | /** |
| 235 | 242 | * [process_auth description] |
@@ -283,15 +290,23 @@ discard block |
||
| 283 | 290 | private function basic_auth():void { |
| 284 | 291 | $username = $_SERVER['PHP_AUTH_USER'] ?? null; |
| 285 | 292 | $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. |
|
| 293 | + if (!$username || !$password) { |
|
| 294 | + $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); |
|
| 295 | + } |
|
| 296 | + // Exits. |
|
| 297 | + if (!$this->rest_model->basicAuth($this, $username, $password)) { |
|
| 298 | + $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); |
|
| 299 | + } |
|
| 300 | + // Exits. |
|
| 288 | 301 | } |
| 289 | 302 | /** |
| 290 | 303 | * [api_key_auth description] |
| 291 | 304 | */ |
| 292 | 305 | private function api_key_auth():void |
| 293 | 306 | { |
| 294 | - if (uri_string() == '') return; |
|
| 307 | + if (uri_string() == '') { |
|
| 308 | + return; |
|
| 309 | + } |
|
| 295 | 310 | |
| 296 | 311 | if (!$this->ci->input->get_request_header($this->apiKeyHeader, true)) { |
| 297 | 312 | // if (!isset($_SERVER['HTTP_' . str_replace("-", "_", $this->apiKeyHeader)])) { |
@@ -351,7 +366,9 @@ discard block |
||
| 351 | 366 | // Trunctate Rate Limit Data. |
| 352 | 367 | $this->rest_model->truncateRatelimitData(); |
| 353 | 368 | // Check Whitelist. |
| 354 | - if (in_array($this->ci->input->ip_address(), $this->whitelist)) return; |
|
| 369 | + if (in_array($this->ci->input->ip_address(), $this->whitelist)) { |
|
| 370 | + return; |
|
| 371 | + } |
|
| 355 | 372 | // Should we acyually Limit? |
| 356 | 373 | if ($this->ip_per_hour > 0) { |
| 357 | 374 | $client = hash('md5', $this->ci->input->ip_address()); |
@@ -431,7 +448,9 @@ discard block |
||
| 431 | 448 | if (isset($this->ci->config->item('rest')['response_callbacks'][$code])) { |
| 432 | 449 | $this->ci->config->item('rest')['response_callbacks'][$code]($auth); |
| 433 | 450 | } |
| 434 | - if (ENVIRONMENT != 'testing') exit($code); |
|
| 451 | + if (ENVIRONMENT != 'testing') { |
|
| 452 | + exit($code); |
|
| 453 | + } |
|
| 435 | 454 | throw new Exception("Error $code in $auth", $code); |
| 436 | 455 | } |
| 437 | 456 | } |