Completed
Push — master ( 9c2b09...5e7564 )
by Francis
01:33
created
models/RESTModel.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -94,7 +94,7 @@  discard block
 block discarded – undo
94 94
    * @param  string $apiKey [description]
95 95
    * @return array          [description]
96 96
    */
97
-  public function getAPIKeyData(string $apiKey):?array {
97
+  public function getAPIKeyData(string $apiKey): ?array {
98 98
     // Preliminary Check.
99 99
     if ($this->api_key_table == null || $this->api_key_column == null) return null;
100 100
     // Query.
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
    * @param  string $group  [description]
121 121
    * @return [type]         [description]
122 122
    */
123
-  public function getLimitData(string $client, string $group):?array {
123
+  public function getLimitData(string $client, string $group): ?array {
124 124
     $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 = ?';
125 125
     $query = $this->db->query($sql, [$client, $group]);
126 126
     if (!is_scalar($query) && $query->num_rows() > 0) return $query->result_array()[0];
Please login to merge, or discard this patch.
Braces   +18 added lines, -6 removed lines patch added patch discarded remove patch
@@ -81,10 +81,14 @@  discard block
 block discarded – undo
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;
@@ -96,15 +100,21 @@  discard block
 block discarded – undo
96 100
    */
97 101
   public function getAPIKeyData(string $apiKey):?array {
98 102
     // Preliminary Check.
99
-    if ($this->api_key_table == null || $this->api_key_column == null) return null;
103
+    if ($this->api_key_table == null || $this->api_key_column == null) {
104
+     return null;
105
+    }
100 106
     // Query.
101 107
     $this->db->select($this->api_key_column);
102
-    if ($this->api_key_limit_column != null) $this->db->select($this->api_key_limit_column);
108
+    if ($this->api_key_limit_column != null) {
109
+     $this->db->select($this->api_key_limit_column);
110
+    }
103 111
     $this->db->from($this->api_key_table);
104 112
     $this->db->where($this->api_key_column, $apiKey);
105 113
     $query = $this->db->get();
106 114
     // Process Result.
107
-    if ($query->num_rows() > 0) return $query->result_array()[0];
115
+    if ($query->num_rows() > 0) {
116
+     return $query->result_array()[0];
117
+    }
108 118
     return null;
109 119
   }
110 120
   /**
@@ -123,7 +133,9 @@  discard block
 block discarded – undo
123 133
   public function getLimitData(string $client, string $group):?array {
124 134
     $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 = ?';
125 135
     $query = $this->db->query($sql, [$client, $group]);
126
-    if (!is_scalar($query) && $query->num_rows() > 0) return $query->result_array()[0];
136
+    if (!is_scalar($query) && $query->num_rows() > 0) {
137
+     return $query->result_array()[0];
138
+    }
127 139
     return null;
128 140
   }
129 141
   /**
Please login to merge, or discard this patch.
phpunit/BasicAuthTest.php 1 patch
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -22,10 +22,10 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
   /**
Please login to merge, or discard this patch.
phpunit/APIKeyAuthAuthTest.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -22,10 +22,10 @@
 block discarded – undo
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) {
Please login to merge, or discard this patch.
phpunit/config/rest.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -11,19 +11,19 @@  discard block
 block discarded – undo
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
 block discarded – undo
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 {
Please login to merge, or discard this patch.
libraries/RESTAuth.php 1 patch
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@  discard block
 block discarded – undo
6 6
    * [BasicAuth description]
7 7
    * @var string
8 8
    */
9
-  const BASIC   = "Basic";
9
+  const BASIC = "Basic";
10 10
   /**
11 11
    * [API_KEY description]
12 12
    * @var string
@@ -16,17 +16,17 @@  discard block
 block discarded – undo
16 16
    * [BEARER description]
17 17
    * @var string
18 18
    */
19
-  const BEARER  = "Bearer";
19
+  const BEARER = "Bearer";
20 20
   /**
21 21
    * [OAUTH2 description]
22 22
    * @var string
23 23
    */
24
-  const OAUTH2  = "OAUTH2";
24
+  const OAUTH2 = "OAUTH2";
25 25
   /**
26 26
    * [IP description]
27 27
    * @var string
28 28
    */
29
-  const IP      = "IP";
29
+  const IP = "IP";
30 30
   /**
31 31
    * [CUSTOM description]
32 32
    * @param  string $header [description]
Please login to merge, or discard this patch.
helpers/rest_helper.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -2,7 +2,7 @@
 block discarded – undo
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
   }
Please login to merge, or discard this patch.
libraries/RESTResponse.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -28,7 +28,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 
Please login to merge, or discard this patch.
Braces   +9 added lines, -3 removed lines patch added patch discarded remove patch
@@ -65,11 +65,17 @@
 block discarded – undo
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
 ?>
Please login to merge, or discard this patch.
libraries/REST.php 3 patches
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -201,7 +201,7 @@
 block discarded – undo
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)) {
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -111,9 +111,9 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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");
Please login to merge, or discard this patch.
Braces   +27 added lines, -8 removed lines patch added patch discarded remove patch
@@ -115,7 +115,9 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 }
Please login to merge, or discard this patch.