@@ -22,10 +22,10 @@ discard block |
||
| 22 | 22 | * @covers JWT::__construct |
| 23 | 23 | */ |
| 24 | 24 | public static function setUpBeforeClass(): void { |
| 25 | - self::$ci =& get_instance(); |
|
| 25 | + self::$ci = & get_instance(); |
|
| 26 | 26 | self::$ci->load->database('mysqli://root@localhost/test_db'); |
| 27 | 27 | self::$ci->load->helper("url"); |
| 28 | - $queries = explode("#@@@", file_get_contents(FCPATH . 'application/splints/' . self::PACKAGE . '/phpunit/database.sql')); |
|
| 28 | + $queries = explode("#@@@", file_get_contents(FCPATH.'application/splints/'.self::PACKAGE.'/phpunit/database.sql')); |
|
| 29 | 29 | self::assertTrue(count($queries) > 0); |
| 30 | 30 | self::$ci->load->database(); |
| 31 | 31 | foreach ($queries as $query) { |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | // However, for the purpose of this test, we are going to Hack Code CodeIgniter |
| 42 | 42 | // with a Splint Config variable to allow us load config files from where |
| 43 | 43 | // ever we want. This happens below. |
| 44 | - self::$ci->load->add_package_path(APPPATH . 'splints/' . self::PACKAGE . "/phpunit/"); |
|
| 44 | + self::$ci->load->add_package_path(APPPATH.'splints/'.self::PACKAGE."/phpunit/"); |
|
| 45 | 45 | //self::$ci->config->set_item('st_config_path_prefix', '../splints/' . self::PACKAGE . "/phpunit/config/"); |
| 46 | 46 | } |
| 47 | 47 | /** |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | $_SERVER['PHP_AUTH_PW'] = "012345678901234567890"; |
| 59 | 59 | self::$ci->config->set_item('expected_uri', 'basic/auth'); |
| 60 | 60 | self::$ci->config->set_item('expected_auth', RESTAuth::BASIC); |
| 61 | - $this->expectExceptionMessage('Error ' . RESTResponse::UN_AUTHORIZED . ' in ' . RESTAuth::BASIC); |
|
| 61 | + $this->expectExceptionMessage('Error '.RESTResponse::UN_AUTHORIZED.' in '.RESTAuth::BASIC); |
|
| 62 | 62 | self::$ci->load->splint(self::PACKAGE, '+REST', null, 'basic_rest_2'); |
| 63 | 63 | } |
| 64 | 64 | /** |
@@ -22,10 +22,10 @@ |
||
| 22 | 22 | * @covers JWT::__construct |
| 23 | 23 | */ |
| 24 | 24 | public static function setUpBeforeClass(): void { |
| 25 | - self::$ci =& get_instance(); |
|
| 25 | + self::$ci = & get_instance(); |
|
| 26 | 26 | self::$ci->load->database('mysqli://root@localhost/test_db'); |
| 27 | 27 | self::$ci->load->helper("url"); |
| 28 | - $queries = explode("#@@@", file_get_contents(FCPATH . 'application/splints/' . self::PACKAGE . '/phpunit/database.sql')); |
|
| 28 | + $queries = explode("#@@@", file_get_contents(FCPATH.'application/splints/'.self::PACKAGE.'/phpunit/database.sql')); |
|
| 29 | 29 | self::assertTrue(count($queries) > 0); |
| 30 | 30 | self::$ci->load->database(); |
| 31 | 31 | foreach ($queries as $query) { |
@@ -11,19 +11,19 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | $config['auth_callbacks'] = [ |
| 13 | 13 | |
| 14 | - RESTAuth::CUSTOM('X-APP-ID') => function (&$context, $value):bool { |
|
| 14 | + RESTAuth::CUSTOM('X-APP-ID') => function(&$context, $value):bool { |
|
| 15 | 15 | return true; |
| 16 | 16 | }, |
| 17 | 17 | |
| 18 | - RESTAuth::CUSTOM('X-DEVICE-ID') => function (&$context, $value):bool { |
|
| 18 | + RESTAuth::CUSTOM('X-DEVICE-ID') => function(&$context, $value):bool { |
|
| 19 | 19 | return true; |
| 20 | 20 | }, |
| 21 | 21 | |
| 22 | - RESTAuth::BEARER => function (&$context, $token):bool { |
|
| 22 | + RESTAuth::BEARER => function(&$context, $token):bool { |
|
| 23 | 23 | return true; |
| 24 | 24 | }, |
| 25 | 25 | |
| 26 | - RESTAuth::OAUTH2 => function (&$context, $token):bool { |
|
| 26 | + RESTAuth::OAUTH2 => function(&$context, $token):bool { |
|
| 27 | 27 | return true; |
| 28 | 28 | } |
| 29 | 29 | |
@@ -38,8 +38,8 @@ discard block |
||
| 38 | 38 | }, |
| 39 | 39 | |
| 40 | 40 | RESTResponse::UN_AUTHORIZED => function(&$auth):void { |
| 41 | - $ci =& get_instance(); |
|
| 42 | - Assert::assertTrue(uri_string() == $ci->config->item('expected_uri')&& $auth == $ci->config->item('expected_auth')); |
|
| 41 | + $ci = & get_instance(); |
|
| 42 | + Assert::assertTrue(uri_string() == $ci->config->item('expected_uri') && $auth == $ci->config->item('expected_auth')); |
|
| 43 | 43 | }, |
| 44 | 44 | |
| 45 | 45 | RESTResponse::NOT_ACCEPTABLE => function(&$auth):void { |
@@ -2,7 +2,7 @@ |
||
| 2 | 2 | declare(strict_types=1); |
| 3 | 3 | |
| 4 | 4 | if (!function_exists('response')) { |
| 5 | - function response($data=null, int $code=null):RESTResponse |
|
| 5 | + function response($data = null, int $code = null):RESTResponse |
|
| 6 | 6 | { |
| 7 | 7 | return new RESTResponse($data, $code); |
| 8 | 8 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | * @var bool |
| 29 | 29 | */ |
| 30 | 30 | protected $json; |
| 31 | - function __construct($data=null, int $code=null) |
|
| 31 | + function __construct($data = null, int $code = null) |
|
| 32 | 32 | { |
| 33 | 33 | $this->data = $data; |
| 34 | 34 | $this->code = $code; |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * @date 2019-11-11 |
| 62 | 62 | * @param boolean $exit [description] |
| 63 | 63 | */ |
| 64 | - public function send(bool $exit=false):void |
|
| 64 | + public function send(bool $exit = false):void |
|
| 65 | 65 | { |
| 66 | 66 | http_response_code($this->code ?? 200); |
| 67 | 67 | |
@@ -65,11 +65,17 @@ |
||
| 65 | 65 | { |
| 66 | 66 | http_response_code($this->code ?? 200); |
| 67 | 67 | |
| 68 | - if ($this->json) header('Content-Type: application/json'); |
|
| 68 | + if ($this->json) { |
|
| 69 | + header('Content-Type: application/json'); |
|
| 70 | + } |
|
| 69 | 71 | |
| 70 | - if ($this->data !== null) echo !$this->json ? $this->data : json_encode($this->data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); |
|
| 72 | + if ($this->data !== null) { |
|
| 73 | + echo !$this->json ? $this->data : json_encode($this->data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); |
|
| 74 | + } |
|
| 71 | 75 | |
| 72 | - if ($exit) exit(EXIT_SUCCESS); |
|
| 76 | + if ($exit) { |
|
| 77 | + exit(EXIT_SUCCESS); |
|
| 78 | + } |
|
| 73 | 79 | } |
| 74 | 80 | } |
| 75 | 81 | ?> |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | * @param string $apiKey [description] |
| 96 | 96 | * @return array [description] |
| 97 | 97 | */ |
| 98 | - public function getAPIKeyData(string $apiKey):?object { |
|
| 98 | + public function getAPIKeyData(string $apiKey): ?object { |
|
| 99 | 99 | // Preliminary Check. |
| 100 | 100 | if ($this->api_key_table == null || $this->api_key_column == null) return null; |
| 101 | 101 | // Query. |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @param string $group [description] |
| 120 | 120 | * @return [type] [description] |
| 121 | 121 | */ |
| 122 | - public function getLimitData(string $client, string $group):?array { |
|
| 122 | + public function getLimitData(string $client, string $group): ?array { |
|
| 123 | 123 | $sql = 'SELECT count, start, (`start` + INTERVAL (1 - TIMESTAMPDIFF(HOUR, UTC_TIMESTAMP(), NOW())) HOUR) AS reset_epoch FROM rest_api_rate_limit WHERE client = ? AND _group = ?'; |
| 124 | 124 | $query = $this->db->query($sql, [$client, $group]); |
| 125 | 125 | if (!is_scalar($query) && $query->num_rows() > 0) return $query->result_array()[0]; |
@@ -81,10 +81,14 @@ discard block |
||
| 81 | 81 | $this->db->or_where($this->users_username_column, $username); |
| 82 | 82 | } |
| 83 | 83 | $query = $this->db->get(); |
| 84 | - if ($query->num_rows() == 0) return false; |
|
| 84 | + if ($query->num_rows() == 0) { |
|
| 85 | + return false; |
|
| 86 | + } |
|
| 85 | 87 | // Authenticate. |
| 86 | 88 | if (password_verify($password, $query->result()[0]->{$this->users_password_column})) { |
| 87 | - if ($this->users_id_column != null) $context->userId = $query->result()[0]->{$this->users_id_column}; |
|
| 89 | + if ($this->users_id_column != null) { |
|
| 90 | + $context->userId = $query->result()[0]->{$this->users_id_column}; |
|
| 91 | + } |
|
| 88 | 92 | return true; |
| 89 | 93 | } |
| 90 | 94 | return false; |
@@ -97,7 +101,9 @@ discard block |
||
| 97 | 101 | */ |
| 98 | 102 | public function getAPIKeyData(string $apiKey):?object { |
| 99 | 103 | // Preliminary Check. |
| 100 | - if ($this->api_key_table == null || $this->api_key_column == null) return null; |
|
| 104 | + if ($this->api_key_table == null || $this->api_key_column == null) { |
|
| 105 | + return null; |
|
| 106 | + } |
|
| 101 | 107 | // Query. |
| 102 | 108 | $this->db->from($this->api_key_table); |
| 103 | 109 | $this->db->where($this->api_key_column, $apiKey); |
@@ -122,7 +128,9 @@ discard block |
||
| 122 | 128 | public function getLimitData(string $client, string $group):?array { |
| 123 | 129 | $sql = 'SELECT count, start, (`start` + INTERVAL (1 - TIMESTAMPDIFF(HOUR, UTC_TIMESTAMP(), NOW())) HOUR) AS reset_epoch FROM rest_api_rate_limit WHERE client = ? AND _group = ?'; |
| 124 | 130 | $query = $this->db->query($sql, [$client, $group]); |
| 125 | - if (!is_scalar($query) && $query->num_rows() > 0) return $query->result_array()[0]; |
|
| 131 | + if (!is_scalar($query) && $query->num_rows() > 0) { |
|
| 132 | + return $query->result_array()[0]; |
|
| 133 | + } |
|
| 126 | 134 | return null; |
| 127 | 135 | } |
| 128 | 136 | /** |
@@ -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] |
@@ -141,7 +141,9 @@ discard block |
||
| 141 | 141 | { |
| 142 | 142 | $this->ci =& get_instance(); |
| 143 | 143 | |
| 144 | - if ($this->ci->input->is_cli_request()) return; |
|
| 144 | + if ($this->ci->input->is_cli_request()) { |
|
| 145 | + return; |
|
| 146 | + } |
|
| 145 | 147 | |
| 146 | 148 | // Load Config If Exists. |
| 147 | 149 | //$this->ci->config->load('rest', true, true); |
@@ -214,9 +216,14 @@ discard block |
||
| 214 | 216 | { |
| 215 | 217 | $auths = null; |
| 216 | 218 | $auths = $this->config['auth'] ?? null; |
| 217 | - if ($auths) $auths = is_array($auths) ? $auths : [$auths]; |
|
| 219 | + if ($auths) { |
|
| 220 | + $auths = is_array($auths) ? $auths : [$auths]; |
|
| 221 | + } |
|
| 218 | 222 | |
| 219 | - if (!$auths) return; // No authentication(s) to carry out. |
|
| 223 | + if (!$auths) { |
|
| 224 | + return; |
|
| 225 | + } |
|
| 226 | + // No authentication(s) to carry out. |
|
| 220 | 227 | |
| 221 | 228 | /** |
| 222 | 229 | * $this->process_auth() terminates the script if authentication fails |
@@ -227,7 +234,9 @@ discard block |
||
| 227 | 234 | */ |
| 228 | 235 | |
| 229 | 236 | foreach ($auths as $key => $auth) { |
| 230 | - if ($this->authPreempted) break; |
|
| 237 | + if ($this->authPreempted) { |
|
| 238 | + break; |
|
| 239 | + } |
|
| 231 | 240 | if (is_numeric($key)) { |
| 232 | 241 | $this->process_auth($auth, self::AUTH_GRAVITY); |
| 233 | 242 | } else { |
@@ -264,7 +273,9 @@ discard block |
||
| 264 | 273 | */ |
| 265 | 274 | private function auth_proceed(bool $success, int $flags):bool |
| 266 | 275 | { |
| 267 | - if ($flags & self::AUTH_GRAVITY) return $success; |
|
| 276 | + if ($flags & self::AUTH_GRAVITY) { |
|
| 277 | + return $success; |
|
| 278 | + } |
|
| 268 | 279 | if ($success) { |
| 269 | 280 | if ($flags & self::AUTH_FINAL) { |
| 270 | 281 | $this->authPreempted = true; |
@@ -298,12 +309,16 @@ discard block |
||
| 298 | 309 | $authorization = $this->get_authorization_header(); |
| 299 | 310 | $shouldProceed = $this->auth_proceed(false, $flags); |
| 300 | 311 | if ($authorization == null || substr_count($authorization, ' ') != 1) { |
| 301 | - if ($shouldProceed) return; |
|
| 312 | + if ($shouldProceed) { |
|
| 313 | + return; |
|
| 314 | + } |
|
| 302 | 315 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth, 'Bad Request'); // Exits. |
| 303 | 316 | } |
| 304 | 317 | $token = explode(" ", $authorization); |
| 305 | 318 | if ($token[0] != $auth) { |
| 306 | - if ($shouldProceed) return; |
|
| 319 | + if ($shouldProceed) { |
|
| 320 | + return; |
|
| 321 | + } |
|
| 307 | 322 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth, 'Bad Request'); // Exits. |
| 308 | 323 | } |
| 309 | 324 | $this->token = $token[1]; |
@@ -327,15 +342,23 @@ discard block |
||
| 327 | 342 | { |
| 328 | 343 | $username = $_SERVER['PHP_AUTH_USER'] ?? null; |
| 329 | 344 | $password = $_SERVER['PHP_AUTH_PW'] ?? null; |
| 330 | - if (!$this->auth_proceed(!$username || !$password, $flags)) $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); // Exits. |
|
| 331 | - if (!$this->auth_proceed($this->rest_model->basicAuth($this, $username, $password), $flags)) $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); // Exits. |
|
| 345 | + if (!$this->auth_proceed(!$username || !$password, $flags)) { |
|
| 346 | + $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); |
|
| 347 | + } |
|
| 348 | + // Exits. |
|
| 349 | + if (!$this->auth_proceed($this->rest_model->basicAuth($this, $username, $password), $flags)) { |
|
| 350 | + $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); |
|
| 351 | + } |
|
| 352 | + // Exits. |
|
| 332 | 353 | } |
| 333 | 354 | /** |
| 334 | 355 | * [api_key_auth description] |
| 335 | 356 | */ |
| 336 | 357 | private function api_key_auth(int $flags=self::AUTH_GRAVITY):void |
| 337 | 358 | { |
| 338 | - if (uri_string() == '') return; |
|
| 359 | + if (uri_string() == '') { |
|
| 360 | + return; |
|
| 361 | + } |
|
| 339 | 362 | $shouldProceed = $this->auth_proceed(false, $flags); |
| 340 | 363 | |
| 341 | 364 | if (!$this->ci->input->get_request_header($this->apiKeyHeader, true) && !$shouldProceed) { |
@@ -353,7 +376,9 @@ discard block |
||
| 353 | 376 | |
| 354 | 377 | $this->apiKey = $apiKey; |
| 355 | 378 | |
| 356 | - if (!$this->auth_proceed(true, $flags)) return; |
|
| 379 | + if (!$this->auth_proceed(true, $flags)) { |
|
| 380 | + return; |
|
| 381 | + } |
|
| 357 | 382 | |
| 358 | 383 | // ==== API KEY Auth Passed ==== // |
| 359 | 384 | |
@@ -414,7 +439,9 @@ discard block |
||
| 414 | 439 | // Trunctate Rate Limit Data. |
| 415 | 440 | $this->rest_model->truncateRatelimitData(); |
| 416 | 441 | // Check Whitelist. |
| 417 | - if (in_array($this->ci->input->ip_address(), $this->whitelist)) return; |
|
| 442 | + if (in_array($this->ci->input->ip_address(), $this->whitelist)) { |
|
| 443 | + return; |
|
| 444 | + } |
|
| 418 | 445 | // Should we acyually Limit? |
| 419 | 446 | if ($this->ip_per_hour > 0) { |
| 420 | 447 | $client = hash('md5', $this->ci->input->ip_address()); |
@@ -494,7 +521,9 @@ discard block |
||
| 494 | 521 | if (isset($this->config['response_callbacks'][$code])) { |
| 495 | 522 | $this->config['response_callbacks'][$code]($auth, $errorReason); |
| 496 | 523 | } |
| 497 | - if (ENVIRONMENT != 'testing') exit($code); |
|
| 524 | + if (ENVIRONMENT != 'testing') { |
|
| 525 | + exit($code); |
|
| 526 | + } |
|
| 498 | 527 | throw new Exception("Error $code in $auth", $code); |
| 499 | 528 | } |
| 500 | 529 | } |
@@ -137,16 +137,16 @@ discard block |
||
| 137 | 137 | * @param array|null $params Initialization parameters from the Slint system. |
| 138 | 138 | * There's no use for this arg yet. |
| 139 | 139 | */ |
| 140 | - function __construct(?array $params=null) |
|
| 140 | + function __construct(?array $params = null) |
|
| 141 | 141 | { |
| 142 | - $this->ci =& get_instance(); |
|
| 142 | + $this->ci = & get_instance(); |
|
| 143 | 143 | |
| 144 | 144 | if ($this->ci->input->is_cli_request()) return; |
| 145 | 145 | |
| 146 | 146 | // Load Config If Exists. |
| 147 | 147 | //$this->ci->config->load('rest', true, true); |
| 148 | - if (is_file(APPPATH . 'config/rest.php')) { |
|
| 149 | - include APPPATH . 'config/rest.php'; |
|
| 148 | + if (is_file(APPPATH.'config/rest.php')) { |
|
| 149 | + include APPPATH.'config/rest.php'; |
|
| 150 | 150 | } else { |
| 151 | 151 | $config = []; |
| 152 | 152 | } |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | |
| 165 | 165 | // Load Model. |
| 166 | 166 | $this->ci->load->splint(self::PACKAGE, '*RESTModel', 'rest_model'); |
| 167 | - $this->rest_model =& $this->ci->rest_model; |
|
| 167 | + $this->rest_model = & $this->ci->rest_model; |
|
| 168 | 168 | |
| 169 | 169 | $this->rest_model->init([ |
| 170 | 170 | 'users_table' => $config['basic_auth']['users_table'] ?? null, |
@@ -266,14 +266,14 @@ discard block |
||
| 266 | 266 | */ |
| 267 | 267 | private function auth_proceed(bool $success, int $flags):bool |
| 268 | 268 | { |
| 269 | - if ($flags & self::AUTH_GRAVITY) return $success; |
|
| 269 | + if ($flags&self::AUTH_GRAVITY) return $success; |
|
| 270 | 270 | if ($success) { |
| 271 | - if ($flags & self::AUTH_FINAL) { |
|
| 271 | + if ($flags&self::AUTH_FINAL) { |
|
| 272 | 272 | $this->authPreempted = true; |
| 273 | 273 | return true; |
| 274 | 274 | } |
| 275 | 275 | } else { |
| 276 | - return $flags & self::AUTH_PASSIVE ? true : false; |
|
| 276 | + return $flags&self::AUTH_PASSIVE ? true : false; |
|
| 277 | 277 | } |
| 278 | 278 | } |
| 279 | 279 | |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | /** |
| 336 | 336 | * [api_key_auth description] |
| 337 | 337 | */ |
| 338 | - private function api_key_auth(int $flags=self::AUTH_GRAVITY):void |
|
| 338 | + private function api_key_auth(int $flags = self::AUTH_GRAVITY):void |
|
| 339 | 339 | { |
| 340 | 340 | if (uri_string() == '') return; |
| 341 | 341 | $shouldProceed = $this->auth_proceed(false, $flags); |
@@ -382,7 +382,7 @@ discard block |
||
| 382 | 382 | } |
| 383 | 383 | // Should we acyually Limit? |
| 384 | 384 | if ($this->per_hour > 0) { |
| 385 | - $client = hash('md5', $this->ci->input->ip_address() . "%" . $apiKey); |
|
| 385 | + $client = hash('md5', $this->ci->input->ip_address()."%".$apiKey); |
|
| 386 | 386 | $limitData = $this->rest_model->getLimitData($client, '_api_keyed_user'); |
| 387 | 387 | if ($limitData == null) { |
| 388 | 388 | $limitData = []; |
@@ -466,7 +466,7 @@ discard block |
||
| 466 | 466 | * [get_authorization_header description] |
| 467 | 467 | * @return [type] [description] |
| 468 | 468 | */ |
| 469 | - private function get_authorization_header():?string |
|
| 469 | + private function get_authorization_header(): ?string |
|
| 470 | 470 | { |
| 471 | 471 | if (isset($_SERVER['Authorization'])) { |
| 472 | 472 | return trim($_SERVER["Authorization"]); |
@@ -489,7 +489,7 @@ discard block |
||
| 489 | 489 | * [handle_response description] |
| 490 | 490 | * @param int $code [description] |
| 491 | 491 | */ |
| 492 | - private function handle_response(int $code, $auth=null, ?string $errorReason=null):void |
|
| 492 | + private function handle_response(int $code, $auth = null, ?string $errorReason = null):void |
|
| 493 | 493 | { |
| 494 | 494 | http_response_code($code); |
| 495 | 495 | header("Content-Type: application/json"); |