| Total Complexity | 61 |
| Total Lines | 353 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 | * [private description] |
||
| 18 | * @var [type] |
||
| 19 | */ |
||
| 20 | private $api_key_limit_column; |
||
| 21 | /** |
||
| 22 | * [private description] |
||
| 23 | * @var [type] |
||
| 24 | */ |
||
| 25 | private $api_key_column; |
||
| 26 | /** |
||
| 27 | * [private description] |
||
| 28 | * @var [type] |
||
| 29 | */ |
||
| 30 | private $per_hour; |
||
| 31 | /** |
||
| 32 | * [private description] |
||
| 33 | * @var [type] |
||
| 34 | */ |
||
| 35 | private $ip_per_hour; |
||
| 36 | /** |
||
| 37 | * [private description] |
||
| 38 | * @var [type] |
||
| 39 | */ |
||
| 40 | private $show_header; |
||
| 41 | /** |
||
| 42 | * [private description] |
||
| 43 | * @var [type] |
||
| 44 | */ |
||
| 45 | private $whitelist; |
||
| 46 | /** |
||
| 47 | * [private description] |
||
| 48 | * @var [type] |
||
| 49 | */ |
||
| 50 | private $checked_rate_limit = false; |
||
| 51 | /** |
||
| 52 | * [private description] |
||
| 53 | * @var [type] |
||
| 54 | */ |
||
| 55 | private $header_prefix; |
||
| 56 | /** |
||
| 57 | * [private description] |
||
| 58 | * @var [type] |
||
| 59 | */ |
||
| 60 | private $limit_api; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * [public description] |
||
| 64 | * @var [type] |
||
| 65 | */ |
||
| 66 | public $userId; |
||
| 67 | /** |
||
| 68 | * [public description] |
||
| 69 | * @var [type] |
||
| 70 | */ |
||
| 71 | public $apiKeyHeader; |
||
| 72 | /** |
||
| 73 | * [public description] |
||
| 74 | * @var [type] |
||
| 75 | */ |
||
| 76 | public $token; |
||
| 77 | /** |
||
| 78 | * [public description] |
||
| 79 | * @var [type] |
||
| 80 | */ |
||
| 81 | public $allowedIPs; |
||
| 82 | /** |
||
| 83 | * [PACKAGE description] |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | const PACKAGE = "francis94c/ci-rest"; |
||
| 87 | /** |
||
| 88 | * [RATE_LIMIT description] |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | const RATE_LIMIT = "RateLimit"; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * [__construct This is the part of the code that takes care of all |
||
| 95 | * authentiations. allowijg you to focus on build wonderfult things at REST. |
||
| 96 | * pun intended ;-)] |
||
| 97 | * @param array|null $params Initialization parameters from the Slint system. |
||
| 98 | * There's no use for this arg yet. |
||
| 99 | */ |
||
| 100 | function __construct($params=null) { |
||
| 141 | } |
||
| 142 | /** |
||
| 143 | * [authenticate description] |
||
| 144 | */ |
||
| 145 | private function authenticate():void { |
||
| 170 | } |
||
| 171 | /** |
||
| 172 | * [process_auth description] |
||
| 173 | * @param string $auth [description] |
||
| 174 | * @return bool [description] |
||
| 175 | */ |
||
| 176 | private function process_auth(string &$auth):void { |
||
| 177 | switch ($auth) { |
||
| 178 | case RESTAuth::IP: $this->ip_auth(); break; |
||
| 179 | case RESTAuth::BASIC: $this->basic_auth(); break; |
||
| 180 | case RESTAuth::API_KEY: $this->api_key_auth(); break; |
||
| 181 | case RESTAuth::OAUTH2: $this->bearer_auth(RESTAuth::OAUTH2); break; |
||
| 182 | case RESTAuth::BEARER: $this->bearer_auth(); break; |
||
| 183 | default: $this->custom_auth($auth); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | /** |
||
| 187 | * [ip_auth description] |
||
| 188 | */ |
||
| 189 | private function ip_auth():void { |
||
| 190 | if (!in_array($this->ci->input->ip_address(), $this->allowedIPs)) { |
||
| 191 | $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::IP); // Exits. |
||
| 192 | } |
||
| 193 | } |
||
| 194 | /** |
||
| 195 | * [bearer_auth description] |
||
| 196 | */ |
||
| 197 | private function bearer_auth($auth=RESTAuth::BEARER):void { |
||
| 198 | $authorization = $this->get_authorization_header(); |
||
| 199 | if ($authorization == null || substr_count($authorization, " ") != 1) { |
||
| 200 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth); // Exits. |
||
| 201 | } |
||
| 202 | $token = explode(" ", $authorization); |
||
| 203 | if ($token[0] != "Bearer") { |
||
| 204 | $this->handle_response(RESTResponse::BAD_REQUEST, $auth); // Exits. |
||
| 205 | } |
||
| 206 | $this->token = $token[1]; |
||
| 207 | // Call Up Custom Implemented Bearer/Token Authorization. |
||
| 208 | // Callback Check. |
||
| 209 | if (!isset($this->ci->config->item('rest')['auth_callbacks'][$auth])) { |
||
| 210 | $this->handle_response(RESTResponse::NOT_IMPLEMENTED, $auth); // Exits. |
||
| 211 | } |
||
| 212 | // Authorization. |
||
| 213 | if (!$this->ci->config->item('rest')['auth_callbacks'][$auth]($this, $this->token)) { |
||
| 214 | $this->handle_response(RESTResponse::UN_AUTHORIZED, $auth); // Exits. |
||
| 215 | } |
||
| 216 | } |
||
| 217 | /** |
||
| 218 | * [basic_auth description] |
||
| 219 | */ |
||
| 220 | private function basic_auth():void { |
||
| 221 | $username = $_SERVER['PHP_AUTH_USER'] ?? null; |
||
| 222 | $password = $_SERVER['PHP_AUTH_PW'] ?? null; |
||
| 223 | if (!$username || !$password) $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::BASIC); // Exits. |
||
| 224 | if (!$this->rest_model->basicAuth($this, $username, $password)) $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::BASIC); // Exits. |
||
| 225 | } |
||
| 226 | /** |
||
| 227 | * [api_key_auth description] |
||
| 228 | */ |
||
| 229 | private function api_key_auth():void { |
||
| 230 | if (!isset($_SERVER['HTTP_' . str_replace("-", "_", $this->apiKeyHeader)])) { |
||
| 231 | $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::API_KEY); // Exits. |
||
| 232 | } |
||
| 233 | $apiKey = $this->rest_model->getAPIKeyData( |
||
| 234 | $_SERVER['HTTP_' . str_replace("-", "_", $this->apiKeyHeader)] |
||
| 235 | ); |
||
| 236 | if ($apiKey == null) { |
||
| 237 | $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::API_KEY); // Exits. |
||
| 238 | } |
||
| 239 | // API KEY Auth Passed Above. |
||
| 240 | if ($this->limit_api && $this->api_key_limit_column != null && $apiKey[$this->api_key_limit_column] == 1) { |
||
| 241 | // Trunctate Rate Limit Data. |
||
| 242 | $this->rest_model->truncateRatelimitData(); |
||
| 243 | // Check Whitelist. |
||
| 244 | if (in_array($this->ci->input->ip_address(), $this->whitelist)) { |
||
| 245 | $this->checked_rate_limit = true; // Ignore Limit By IP. |
||
| 246 | return; |
||
| 247 | } |
||
| 248 | // Should we acyually Limit? |
||
| 249 | if ($this->per_hour > 0) { |
||
| 250 | $client = hash('md5', $this->ci->input->ip_address() . "%" . $apiKey[$this->api_key_column]); |
||
| 251 | $limitData = $this->rest_model->getLimitData($client, '_api_keyed_user'); |
||
| 252 | if ($limitData == null) { |
||
| 253 | $limitData = []; |
||
| 254 | $limitData['count'] = 0; |
||
| 255 | $limitData['reset_epoch'] = gmdate('d M Y H:i:s', time() + (60 * 60)); |
||
| 256 | $limitData['start'] = date('d M Y H:i:s'); |
||
| 257 | } |
||
| 258 | if ($this->per_hour - $limitData['count'] > 0) { |
||
| 259 | if (!$this->rest_model->insertLimitData($client, '_api_keyed_user')) { |
||
| 260 | $this->handle_response(RESTResponse::INTERNAL_SERVER_ERROR, self::RATE_LIMIT); // Exits. |
||
| 261 | } |
||
| 262 | ++$limitData['count']; |
||
| 263 | if ($this->show_header) { |
||
| 264 | header($this->header_prefix.'Limit: '.$this->per_hour); |
||
| 265 | header($this->header_prefix.'Remaining: '.($this->per_hour - $limitData['count'])); |
||
| 266 | header($this->header_prefix.'Reset: '.strtotime($limitData['reset_epoch'])); |
||
| 267 | } |
||
| 268 | } else { |
||
| 269 | header('Retry-After: '.(strtotime($limitData['reset_epoch']) - strtotime(gmdate('d M Y H:i:s')))); |
||
| 270 | $this->handle_response(RESTResponse::TOO_MANY_REQUESTS, self::RATE_LIMIT); // Exits. |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | $this->checked_rate_limit = true; // Ignore Limit By IP. |
||
| 275 | } |
||
| 276 | /** |
||
| 277 | * [api_rest_limit_by_ip_address description] |
||
| 278 | * TODO: Implement. |
||
| 279 | */ |
||
| 280 | private function api_rest_limit_by_ip_address():void { |
||
| 281 | // Trunctate Rate Limit Data. |
||
| 282 | $this->rest_model->truncateRatelimitData(); |
||
| 283 | // Check Whitelist. |
||
| 284 | if (in_array($this->ci->input->ip_address(), $this->whitelist)) return; |
||
| 285 | // Should we acyually Limit? |
||
| 286 | if ($this->ip_per_hour > 0) { |
||
| 287 | $client = hash('md5', $this->ci->input->ip_address()); |
||
| 288 | $limitData = $this->rest_model->getLimitData($client, '_ip_address'); |
||
| 289 | if ($limitData == null) { |
||
| 290 | $limitData = []; |
||
| 291 | $limitData['count'] = 0; |
||
| 292 | $limitData['reset_epoch'] = gmdate('d M Y H:i:s', time() + (60 * 60)); |
||
| 293 | $limitData['start'] = date('d M Y H:i:s'); |
||
| 294 | } |
||
| 295 | if ($this->ip_per_hour - $limitData['count'] > 0) { |
||
| 296 | if (!$this->rest_model->insertLimitData($client, '_ip_address')) { |
||
| 297 | $this->handle_response(RESTResponse::INTERNAL_SERVER_ERROR, self::RATE_LIMIT); // Exits. |
||
| 298 | } |
||
| 299 | ++$limitData['count']; |
||
| 300 | if ($this->show_header) { |
||
| 301 | header($this->header_prefix.'Limit: '.$this->ip_per_hour); |
||
| 302 | header($this->header_prefix.'Remaining: '.($this->ip_per_hour - $limitData['count'])); |
||
| 303 | header($this->header_prefix.'Reset: '.strtotime($limitData['reset_epoch'])); |
||
| 304 | } |
||
| 305 | } else { |
||
| 306 | header('Retry-After: '.(strtotime($limitData['reset_epoch']) - strtotime(gmdate('d M Y H:i:s')))); |
||
| 307 | $this->handle_response(RESTResponse::TOO_MANY_REQUESTS, self::RATE_LIMIT); // Exits. |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | /** |
||
| 312 | * [custom_auth description] |
||
| 313 | * @param string $auth [description] |
||
| 314 | */ |
||
| 315 | private function custom_auth(string &$auth):void { |
||
| 327 | } |
||
| 328 | } |
||
| 329 | /** |
||
| 330 | * [get_authorization_header description] |
||
| 331 | * @return [type] [description] |
||
| 332 | */ |
||
| 333 | private function get_authorization_header():?string { |
||
| 334 | if (isset($_SERVER['Authorization'])) { |
||
| 335 | return trim($_SERVER["Authorization"]); |
||
| 336 | } else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI |
||
| 337 | return trim($_SERVER["HTTP_AUTHORIZATION"]); |
||
| 338 | } elseif (function_exists('apache_request_headers')) { |
||
| 339 | $requestHeaders = apache_request_headers(); |
||
| 340 | |||
| 341 | // Avoid Surprises. |
||
| 342 | $requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders)); |
||
| 343 | |||
| 344 | if (isset($requestHeaders['Authorization'])) { |
||
| 345 | return trim($requestHeaders['Authorization']); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | return null; |
||
| 349 | } |
||
| 350 | /** |
||
| 351 | * [handle_response description] |
||
| 352 | * @param int $code [description] |
||
| 353 | */ |
||
| 354 | private function handle_response(int $code, $auth=null):void { |
||
| 362 | } |
||
| 363 | } |
||
| 365 |