| Total Complexity | 68 |
| Total Lines | 440 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| 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 | * [PACKAGE description] |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | const PACKAGE = "francis94c/ci-rest"; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * [RATE_LIMIT description] |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | const RATE_LIMIT = "RateLimit"; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * [__construct This is the part of the code that takes care of all |
||
| 121 | * authentiations. allowing you to focus on building wonderful things at REST. |
||
| 122 | * pun intended ;-)] |
||
| 123 | * @param array|null $params Initialization parameters from the Slint system. |
||
| 124 | * There's no use for this arg yet. |
||
| 125 | */ |
||
| 126 | function __construct(?array $params=null) |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * [authenticate description] |
||
| 195 | * @date 2020-01-30 |
||
| 196 | */ |
||
| 197 | private function authenticate():void |
||
| 242 | } |
||
| 243 | /** |
||
| 244 | * [process_auth description] |
||
| 245 | * @param string $auth [description] |
||
| 246 | * @return bool [description] |
||
| 247 | */ |
||
| 248 | private function process_auth(string &$auth):void { |
||
| 249 | switch ($auth) { |
||
| 250 | case RESTAuth::IP: $this->ip_auth(); break; |
||
| 251 | case RESTAuth::BASIC: $this->basic_auth(); break; |
||
| 252 | case RESTAuth::API_KEY: $this->api_key_auth(); break; |
||
| 253 | case RESTAuth::OAUTH2: $this->bearer_auth(RESTAuth::OAUTH2); break; |
||
| 254 | case RESTAuth::BEARER: $this->bearer_auth(); break; |
||
| 255 | case RESTAuth::SECRET: $this->bearer_auth(RESTAuth::SECRET); break; |
||
| 256 | default: $this->custom_auth($auth); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | /** |
||
| 260 | * [ip_auth description] |
||
| 261 | */ |
||
| 262 | private function ip_auth():void { |
||
| 263 | if (!in_array($this->ci->input->ip_address(), $this->allowedIps)) { |
||
| 264 | $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::IP); // Exits. |
||
| 265 | } |
||
| 266 | } |
||
| 267 | /** |
||
| 268 | * [bearer_auth description] |
||
| 269 | */ |
||
| 270 | private function bearer_auth($auth=RESTAuth::BEARER):void |
||
| 271 | { |
||
| 272 | $authorization = $this->get_authorization_header(); |
||
| 273 | if ($authorization == null || substr_count($authorization, ' ') != 1) { |
||
| 274 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth, 'Bad Request'); // Exits. |
||
| 275 | } |
||
| 276 | $token = explode(" ", $authorization); |
||
| 277 | if ($token[0] != $auth) { |
||
| 278 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth, 'Invalid Authorization'); // Exits. |
||
| 279 | } |
||
| 280 | $this->token = $token[1]; |
||
| 281 | // Call Up Custom Implemented Bearer/Token Authorization. |
||
| 282 | // Callback Check. |
||
| 283 | if (!isset($this->config['auth_callbacks'][$auth])) { |
||
| 284 | $this->handle_response(RESTResponse::NOT_IMPLEMENTED, $auth); // Exits. |
||
| 285 | } |
||
| 286 | // Authorization. |
||
| 287 | if (!$this->config['auth_callbacks'][$auth]($this, $this->token)) { |
||
| 288 | $this->handle_response(RESTResponse::UN_AUTHORIZED, $auth); // Exits. |
||
| 289 | } |
||
| 290 | } |
||
| 291 | /** |
||
| 292 | * [basic_auth description] |
||
| 293 | */ |
||
| 294 | private function basic_auth():void { |
||
| 295 | $username = $_SERVER['PHP_AUTH_USER'] ?? null; |
||
| 296 | $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. |
||
| 299 | } |
||
| 300 | /** |
||
| 301 | * [api_key_auth description] |
||
| 302 | */ |
||
| 303 | private function api_key_auth():void |
||
| 304 | { |
||
| 305 | if (uri_string() == '') return; |
||
| 306 | |||
| 307 | if (!$this->ci->input->get_request_header($this->apiKeyHeader, true)) { |
||
| 308 | // if (!isset($_SERVER['HTTP_' . str_replace("-", "_", $this->apiKeyHeader)])) { |
||
| 309 | $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::API_KEY); // Exits. |
||
| 310 | } |
||
| 311 | |||
| 312 | $apiKey = $this->rest_model->getAPIKeyData( |
||
| 313 | $this->ci->input->get_request_header($this->apiKeyHeader, true) |
||
| 314 | ); |
||
| 315 | |||
| 316 | if ($apiKey == null) { |
||
| 317 | $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::API_KEY); // Exits. |
||
| 318 | } |
||
| 319 | |||
| 320 | $this->apiKey = $apiKey; |
||
| 321 | |||
| 322 | // API KEY Auth Passed Above. |
||
| 323 | if ($this->limit_api && $this->api_key_limit_column != null && $apiKey->{$this->api_key_limit_column} == 1) { |
||
| 324 | // Trunctate Rate Limit Data. |
||
| 325 | $this->rest_model->truncateRatelimitData(); |
||
| 326 | // Check Whitelist. |
||
| 327 | if (in_array($this->ci->input->ip_address(), $this->whitelist)) { |
||
| 328 | $this->checked_rate_limit = true; // Ignore Limit By IP. |
||
| 329 | return; |
||
| 330 | } |
||
| 331 | // Should we acyually Limit? |
||
| 332 | if ($this->per_hour > 0) { |
||
| 333 | $client = hash('md5', $this->ci->input->ip_address() . "%" . $apiKey->{$this->api_key_column}); |
||
| 334 | $limitData = $this->rest_model->getLimitData($client, '_api_keyed_user'); |
||
| 335 | if ($limitData == null) { |
||
| 336 | $limitData = []; |
||
| 337 | $limitData['count'] = 0; |
||
| 338 | $limitData['reset_epoch'] = gmdate('d M Y H:i:s', time() + (60 * 60)); |
||
| 339 | $limitData['start'] = date('d M Y H:i:s'); |
||
| 340 | } |
||
| 341 | if ($this->per_hour - $limitData['count'] > 0) { |
||
| 342 | if (!$this->rest_model->insertLimitData($client, '_api_keyed_user')) { |
||
| 343 | $this->handle_response(RESTResponse::INTERNAL_SERVER_ERROR, self::RATE_LIMIT); // Exits. |
||
| 344 | } |
||
| 345 | ++$limitData['count']; |
||
| 346 | if ($this->show_header) { |
||
| 347 | header($this->header_prefix.'Limit: '.$this->per_hour); |
||
| 348 | header($this->header_prefix.'Remaining: '.($this->per_hour - $limitData['count'])); |
||
| 349 | header($this->header_prefix.'Reset: '.strtotime($limitData['reset_epoch'])); |
||
| 350 | } |
||
| 351 | } else { |
||
| 352 | header('Retry-After: '.(strtotime($limitData['reset_epoch']) - strtotime(gmdate('d M Y H:i:s')))); |
||
| 353 | $this->handle_response(RESTResponse::TOO_MANY_REQUESTS, self::RATE_LIMIT); // Exits. |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | $this->checked_rate_limit = true; // Ignore Limit By IP. |
||
| 358 | } |
||
| 359 | /** |
||
| 360 | * [api_rest_limit_by_ip_address description] |
||
| 361 | * TODO: Implement. |
||
| 362 | */ |
||
| 363 | private function api_rest_limit_by_ip_address():void { |
||
| 364 | // Trunctate Rate Limit Data. |
||
| 365 | $this->rest_model->truncateRatelimitData(); |
||
| 366 | // Check Whitelist. |
||
| 367 | if (in_array($this->ci->input->ip_address(), $this->whitelist)) return; |
||
| 368 | // Should we acyually Limit? |
||
| 369 | if ($this->ip_per_hour > 0) { |
||
| 370 | $client = hash('md5', $this->ci->input->ip_address()); |
||
| 371 | $limitData = $this->rest_model->getLimitData($client, '_ip_address'); |
||
| 372 | if ($limitData == null) { |
||
| 373 | $limitData = []; |
||
| 374 | $limitData['count'] = 0; |
||
| 375 | $limitData['reset_epoch'] = gmdate('d M Y H:i:s', time() + (60 * 60)); |
||
| 376 | $limitData['start'] = date('d M Y H:i:s'); |
||
| 377 | } |
||
| 378 | if ($this->ip_per_hour - $limitData['count'] > 0) { |
||
| 379 | if (!$this->rest_model->insertLimitData($client, '_ip_address')) { |
||
| 380 | $this->handle_response(RESTResponse::INTERNAL_SERVER_ERROR, self::RATE_LIMIT); // Exits. |
||
| 381 | } |
||
| 382 | ++$limitData['count']; |
||
| 383 | if ($this->show_header) { |
||
| 384 | header($this->header_prefix.'Limit: '.$this->ip_per_hour); |
||
| 385 | header($this->header_prefix.'Remaining: '.($this->ip_per_hour - $limitData['count'])); |
||
| 386 | header($this->header_prefix.'Reset: '.strtotime($limitData['reset_epoch'])); |
||
| 387 | } |
||
| 388 | } else { |
||
| 389 | header('Retry-After: '.(strtotime($limitData['reset_epoch']) - strtotime(gmdate('d M Y H:i:s')))); |
||
| 390 | $this->handle_response(RESTResponse::TOO_MANY_REQUESTS, self::RATE_LIMIT); // Exits. |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } |
||
| 394 | /** |
||
| 395 | * [custom_auth description] |
||
| 396 | * @param string $auth [description] |
||
| 397 | */ |
||
| 398 | private function custom_auth(string &$auth):void |
||
| 411 | } |
||
| 412 | } |
||
| 413 | /** |
||
| 414 | * [get_authorization_header description] |
||
| 415 | * @return [type] [description] |
||
| 416 | */ |
||
| 417 | private function get_authorization_header():?string |
||
| 418 | { |
||
| 419 | if (isset($_SERVER['Authorization'])) { |
||
| 420 | return trim($_SERVER["Authorization"]); |
||
| 421 | } else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI |
||
| 422 | return trim($_SERVER["HTTP_AUTHORIZATION"]); |
||
| 423 | } elseif (function_exists('apache_request_headers')) { |
||
| 424 | $requestHeaders = apache_request_headers(); |
||
| 425 | |||
| 426 | // Avoid Surprises. |
||
| 427 | $requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders)); |
||
| 428 | |||
| 429 | if (isset($requestHeaders['Authorization'])) { |
||
| 430 | return trim($requestHeaders['Authorization']); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | return null; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * [handle_response description] |
||
| 438 | * @param int $code [description] |
||
| 439 | */ |
||
| 440 | private function handle_response(int $code, $auth=null, ?string $errorReason=null):void |
||
| 449 | } |
||
| 450 | } |
||
| 452 |