| Total Complexity | 74 |
| Total Lines | 474 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| 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 $apiKey; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * [public description] |
||
| 85 | * @var [type] |
||
| 86 | */ |
||
| 87 | public $apiKeyHeader; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * [public description] |
||
| 91 | * @var [type] |
||
| 92 | */ |
||
| 93 | public $token; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * [public description] |
||
| 97 | * @var [type] |
||
| 98 | */ |
||
| 99 | public $allowedIps; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * [public description] |
||
| 103 | * @var [type] |
||
| 104 | */ |
||
| 105 | public $config; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * [public description] |
||
| 109 | * @var [type] |
||
| 110 | */ |
||
| 111 | public $authPreempted = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * [PACKAGE description] |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | const PACKAGE = "francis94c/ci-rest"; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * [RATE_LIMIT description] |
||
| 121 | * @var string |
||
| 122 | */ |
||
| 123 | const RATE_LIMIT = "RateLimit"; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * [AUTH_GRAVITY description] |
||
| 127 | * @var integer |
||
| 128 | */ |
||
| 129 | const AUTH_GRAVITY = 0b100; |
||
| 130 | const AUTH_PASSIVE = 0b010; |
||
| 131 | const AUTH_FINAL = 0b001; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * [__construct This is the part of the code that takes care of all |
||
| 135 | * authentiations. allowing you to focus on building wonderful things at REST. |
||
| 136 | * pun intended ;-)] |
||
| 137 | * @param array|null $params Initialization parameters from the Slint system. |
||
| 138 | * There's no use for this arg yet. |
||
| 139 | */ |
||
| 140 | function __construct(?array $params=null) |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * [authenticate description] |
||
| 209 | * @date 2020-01-30 |
||
| 210 | */ |
||
| 211 | private function authenticate():void |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * [process_auth description] |
||
| 239 | * @date 2020-04-07 |
||
| 240 | * @param string $auth [description] |
||
| 241 | * @param int $flags [description] |
||
| 242 | */ |
||
| 243 | private function process_auth(string &$auth, int $flags):void |
||
| 244 | { |
||
| 245 | switch ($auth) { |
||
| 246 | case RESTAuth::IP: $this->ip_auth($flags); break; |
||
| 247 | case RESTAuth::BASIC: $this->basic_auth($flags); break; |
||
| 248 | case RESTAuth::API_KEY: $this->api_key_auth($flags); break; |
||
| 249 | case RESTAuth::OAUTH2: $this->bearer_auth(RESTAuth::OAUTH2, $flags); break; |
||
| 250 | case RESTAuth::BEARER: $this->bearer_auth(RESTAuth::BEARER, $flags); break; |
||
| 251 | case RESTAuth::SECRET: $this->bearer_auth(RESTAuth::SECRET, $flags); break; |
||
| 252 | default: $this->custom_auth($auth, $flags); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * [auth_proceed description] |
||
| 258 | * @date 2020-04-07 |
||
| 259 | * @param bool $success [description] |
||
| 260 | * @param int $flags [description] |
||
| 261 | * @return bool [description] |
||
| 262 | */ |
||
| 263 | private function auth_proceed(bool $success, int $flags):bool |
||
| 264 | { |
||
| 265 | if ($flags & self::AUTH_GRAVITY) return $success; |
||
| 266 | if ($success) { |
||
| 267 | if ($flags & self::AUTH_FINAL) { |
||
| 268 | $this->authPreempted = true; |
||
| 269 | return true; |
||
| 270 | } |
||
| 271 | } else { |
||
| 272 | return $flags & self::AUTH_PASSIVE ? true : false; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * [ip_auth description] |
||
| 278 | * @date 2020-04-07 |
||
| 279 | * @param int $flags [description] |
||
| 280 | */ |
||
| 281 | private function ip_auth(int $flags):void |
||
| 282 | { |
||
| 283 | if (!$this->auth_proceed(in_array($this->ci->input->ip_address(), $this->allowedIps), $flags)) { |
||
| 284 | $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::IP); // Exits. |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * [bearer_auth description] |
||
| 290 | * @date 2020-04-07 |
||
| 291 | * @param string $auth [description] |
||
| 292 | * @param int $flags [description] |
||
| 293 | */ |
||
| 294 | private function bearer_auth(string $auth, int $flags):void |
||
| 295 | { |
||
| 296 | $authorization = $this->get_authorization_header(); |
||
| 297 | $shouldProceed = $this->auth_proceed(false, $flags); |
||
| 298 | if ($authorization == null || substr_count($authorization, ' ') != 1 && !$shouldProceed) { |
||
| 299 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth, 'Bad Request'); // Exits. |
||
| 300 | } |
||
| 301 | $token = explode(" ", $authorization); |
||
| 302 | if ($token[0] != $auth && !$shouldProceed) { |
||
| 303 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth, 'Bad Request'); // Exits. |
||
| 304 | } |
||
| 305 | $this->token = $token[1]; |
||
| 306 | // Call Up Custom Implemented Bearer/Token Authorization. |
||
| 307 | // Callback Check. |
||
| 308 | if (!isset($this->config['auth_callbacks'][$auth])) { |
||
| 309 | $this->handle_response(RESTResponse::NOT_IMPLEMENTED, $auth); // Exits. |
||
| 310 | } |
||
| 311 | // Authorization. |
||
| 312 | if (!$this->auth_proceed($this->config['auth_callbacks'][$auth]($this, $this->token), $flags)) { |
||
| 313 | $this->handle_response(RESTResponse::UN_AUTHORIZED, $auth); // Exits. |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * [basic_auth description] |
||
| 319 | * @date 2020-04-07 |
||
| 320 | * @param int $flags [description] |
||
| 321 | */ |
||
| 322 | private function basic_auth(int $flags):void |
||
| 323 | { |
||
| 324 | $username = $_SERVER['PHP_AUTH_USER'] ?? null; |
||
| 325 | $password = $_SERVER['PHP_AUTH_PW'] ?? null; |
||
| 326 | if (!$this->auth_proceed(!$username || !$password, $flags)) $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); // Exits. |
||
| 327 | if (!$this->auth_proceed($this->rest_model->basicAuth($this, $username, $password), $flags)) $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); // Exits. |
||
| 328 | } |
||
| 329 | /** |
||
| 330 | * [api_key_auth description] |
||
| 331 | */ |
||
| 332 | private function api_key_auth(int $flags=self::AUTH_GRAVITY):void |
||
| 333 | { |
||
| 334 | if (uri_string() == '') return; |
||
| 335 | $shouldProceed = $this->auth_proceed(false, $flags); |
||
| 336 | |||
| 337 | if (!$this->ci->input->get_request_header($this->apiKeyHeader, true) && !$shouldProceed) { |
||
| 338 | // if (!isset($_SERVER['HTTP_' . str_replace("-", "_", $this->apiKeyHeader)])) { |
||
| 339 | $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::API_KEY); // Exits. |
||
| 340 | } |
||
| 341 | |||
| 342 | $apiKey = $this->rest_model->getAPIKeyData( |
||
| 343 | $this->ci->input->get_request_header($this->apiKeyHeader, true) |
||
| 344 | ); |
||
| 345 | |||
| 346 | if ($apiKey == null && !$shouldProceed) { |
||
| 347 | $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::API_KEY); // Exits. |
||
| 348 | } |
||
| 349 | |||
| 350 | $this->apiKey = $apiKey; |
||
| 351 | |||
| 352 | if (!$this->auth_proceed(true, $flags)) return; |
||
| 353 | |||
| 354 | // API KEY Auth Passed Above. |
||
| 355 | if ($this->limit_api && $this->api_key_limit_column != null && $apiKey->{$this->api_key_limit_column} == 1) { |
||
| 356 | // Trunctate Rate Limit Data. |
||
| 357 | $this->rest_model->truncateRatelimitData(); |
||
| 358 | // Check Whitelist. |
||
| 359 | if (in_array($this->ci->input->ip_address(), $this->whitelist)) { |
||
| 360 | $this->checked_rate_limit = true; // Ignore Limit By IP. |
||
| 361 | return; |
||
| 362 | } |
||
| 363 | // Should we acyually Limit? |
||
| 364 | if ($this->per_hour > 0) { |
||
| 365 | $client = hash('md5', $this->ci->input->ip_address() . "%" . $apiKey->{$this->api_key_column}); |
||
| 366 | $limitData = $this->rest_model->getLimitData($client, '_api_keyed_user'); |
||
| 367 | if ($limitData == null) { |
||
| 368 | $limitData = []; |
||
| 369 | $limitData['count'] = 0; |
||
| 370 | $limitData['reset_epoch'] = gmdate('d M Y H:i:s', time() + (60 * 60)); |
||
| 371 | $limitData['start'] = date('d M Y H:i:s'); |
||
| 372 | } |
||
| 373 | if ($this->per_hour - $limitData['count'] > 0) { |
||
| 374 | if (!$this->rest_model->insertLimitData($client, '_api_keyed_user')) { |
||
| 375 | $this->handle_response(RESTResponse::INTERNAL_SERVER_ERROR, self::RATE_LIMIT); // Exits. |
||
| 376 | } |
||
| 377 | ++$limitData['count']; |
||
| 378 | if ($this->show_header) { |
||
| 379 | header($this->header_prefix.'Limit: '.$this->per_hour); |
||
| 380 | header($this->header_prefix.'Remaining: '.($this->per_hour - $limitData['count'])); |
||
| 381 | header($this->header_prefix.'Reset: '.strtotime($limitData['reset_epoch'])); |
||
| 382 | } |
||
| 383 | } else { |
||
| 384 | header('Retry-After: '.(strtotime($limitData['reset_epoch']) - strtotime(gmdate('d M Y H:i:s')))); |
||
| 385 | $this->handle_response(RESTResponse::TOO_MANY_REQUESTS, self::RATE_LIMIT); // Exits. |
||
| 386 | } |
||
| 387 | } |
||
| 388 | } |
||
| 389 | $this->checked_rate_limit = true; // Ignore Limit By IP. |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * [api_rest_limit_by_ip_address description] |
||
| 394 | * TODO: Implement. |
||
| 395 | */ |
||
| 396 | private function api_rest_limit_by_ip_address():void |
||
| 425 | } |
||
| 426 | } |
||
| 427 | } |
||
| 428 | /** |
||
| 429 | * [custom_auth description] |
||
| 430 | * @param string $auth [description] |
||
| 431 | */ |
||
| 432 | private function custom_auth(string &$auth):void |
||
| 445 | } |
||
| 446 | } |
||
| 447 | /** |
||
| 448 | * [get_authorization_header description] |
||
| 449 | * @return [type] [description] |
||
| 450 | */ |
||
| 451 | private function get_authorization_header():?string |
||
| 452 | { |
||
| 453 | if (isset($_SERVER['Authorization'])) { |
||
| 454 | return trim($_SERVER["Authorization"]); |
||
| 455 | } else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI |
||
| 456 | return trim($_SERVER["HTTP_AUTHORIZATION"]); |
||
| 457 | } elseif (function_exists('apache_request_headers')) { |
||
| 458 | $requestHeaders = apache_request_headers(); |
||
| 459 | |||
| 460 | // Avoid Surprises. |
||
| 461 | $requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders)); |
||
| 462 | |||
| 463 | if (isset($requestHeaders['Authorization'])) { |
||
| 464 | return trim($requestHeaders['Authorization']); |
||
| 465 | } |
||
| 466 | } |
||
| 467 | return null; |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * [handle_response description] |
||
| 472 | * @param int $code [description] |
||
| 473 | */ |
||
| 474 | private function handle_response(int $code, $auth=null, ?string $errorReason=null):void |
||
| 483 | } |
||
| 484 | } |
||
| 485 | ?> |
||
| 486 |