| Total Complexity | 56 |
| Total Lines | 554 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like OAuth2 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 OAuth2, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class OAuth2 { |
||
| 24 | use |
||
| 25 | Accessor, |
||
| 26 | Singleton; |
||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $automatic_prolongation; |
||
| 31 | /** |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $expiration; |
||
| 35 | /** |
||
| 36 | * @var Prefix |
||
| 37 | */ |
||
| 38 | protected $cache; |
||
| 39 | public function construct () { |
||
| 40 | $this->cache = new Prefix('OAuth2'); |
||
| 41 | $module_data = Config::instance()->module('OAuth2'); |
||
| 42 | $this->automatic_prolongation = $module_data->automatic_prolongation; |
||
|
|
|||
| 43 | $this->expiration = $module_data->expiration; |
||
| 44 | } |
||
| 45 | /** |
||
| 46 | * Returns database index |
||
| 47 | * |
||
| 48 | * @return int |
||
| 49 | */ |
||
| 50 | protected function cdb () { |
||
| 51 | return Config::instance()->module('OAuth2')->db('oauth2'); |
||
| 52 | } |
||
| 53 | /** |
||
| 54 | * Add new client |
||
| 55 | * |
||
| 56 | * @param string $name |
||
| 57 | * @param string $domain |
||
| 58 | * @param int $active |
||
| 59 | * |
||
| 60 | * @return false|string <i>false</i> on failure, id of created client otherwise |
||
| 61 | */ |
||
| 62 | public function add_client ($name, $domain, $active) { |
||
| 63 | if ( |
||
| 64 | !$domain || |
||
| 65 | strpos($domain, '/') !== false |
||
| 66 | ) { |
||
| 67 | return false; |
||
| 68 | } |
||
| 69 | /** |
||
| 70 | * Generate hash in cycle, to obtain unique value |
||
| 71 | */ |
||
| 72 | /** @noinspection LoopWhichDoesNotLoopInspection */ |
||
| 73 | while (true) { |
||
| 74 | $id = md5(random_bytes(1000)); |
||
| 75 | if ($this->db_prime()->qf( |
||
| 76 | "SELECT `id` |
||
| 77 | FROM `[prefix]oauth2_clients` |
||
| 78 | WHERE `id` = '$id' |
||
| 79 | LIMIT 1" |
||
| 80 | ) |
||
| 81 | ) { |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | $this->db_prime()->q( |
||
| 85 | "INSERT INTO `[prefix]oauth2_clients` |
||
| 86 | ( |
||
| 87 | `id`, |
||
| 88 | `secret`, |
||
| 89 | `name`, |
||
| 90 | `domain`, |
||
| 91 | `active` |
||
| 92 | ) VALUES ( |
||
| 93 | '%s', |
||
| 94 | '%s', |
||
| 95 | '%s', |
||
| 96 | '%s', |
||
| 97 | '%s' |
||
| 98 | )", |
||
| 99 | $id, |
||
| 100 | md5(random_bytes(1000)), |
||
| 101 | xap($name), |
||
| 102 | xap($domain), |
||
| 103 | (int)(bool)$active |
||
| 104 | ); |
||
| 105 | unset($this->cache->$id); |
||
| 106 | return $id; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | /** |
||
| 110 | * Get client data |
||
| 111 | * |
||
| 112 | * @param string $id |
||
| 113 | * |
||
| 114 | * @return array|false |
||
| 115 | */ |
||
| 116 | public function get_client ($id) { |
||
| 117 | if (!is_md5($id)) { |
||
| 118 | return false; |
||
| 119 | } |
||
| 120 | return $this->cache->get( |
||
| 121 | $id, |
||
| 122 | function () use ($id) { |
||
| 123 | return $this->db()->qf( |
||
| 124 | "SELECT * |
||
| 125 | FROM `[prefix]oauth2_clients` |
||
| 126 | WHERE `id` = '%s' |
||
| 127 | LIMIT 1", |
||
| 128 | $id |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | /** |
||
| 134 | * Set client data |
||
| 135 | * |
||
| 136 | * @param string $id |
||
| 137 | * @param string $secret |
||
| 138 | * @param string $name |
||
| 139 | * @param string $domain |
||
| 140 | * @param int $active |
||
| 141 | * |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public function set_client ($id, $secret, $name, $domain, $active) { |
||
| 145 | if ( |
||
| 146 | !is_md5($id) || |
||
| 147 | !is_md5($secret) || |
||
| 148 | !$domain || |
||
| 149 | strpos($domain, '/') !== false |
||
| 150 | ) { |
||
| 151 | return false; |
||
| 152 | } |
||
| 153 | $result = $this->db_prime()->q( |
||
| 154 | "UPDATE `[prefix]oauth2_clients` |
||
| 155 | SET |
||
| 156 | `secret` = '%s', |
||
| 157 | `name` = '%s', |
||
| 158 | `domain` = '%s', |
||
| 159 | `active` = '%s' |
||
| 160 | WHERE `id` = '%s'", |
||
| 161 | $secret, |
||
| 162 | xap($name), |
||
| 163 | xap($domain), |
||
| 164 | (int)(bool)$active, |
||
| 165 | $id |
||
| 166 | ); |
||
| 167 | unset($this->cache->$id); |
||
| 168 | return !!$result; |
||
| 169 | } |
||
| 170 | /** |
||
| 171 | * Delete client |
||
| 172 | * |
||
| 173 | * @param string $id |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public function del_client ($id) { |
||
| 191 | } |
||
| 192 | /** |
||
| 193 | * Get clients list in form of associative array |
||
| 194 | * |
||
| 195 | * @return array|false |
||
| 196 | */ |
||
| 197 | public function clients_list () { |
||
| 198 | return $this->db()->qfa( |
||
| 199 | 'SELECT * |
||
| 200 | FROM `[prefix]oauth2_clients`' |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | /** |
||
| 204 | * Grant access for specified client |
||
| 205 | * |
||
| 206 | * @param string $client |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function add_access ($client) { |
||
| 211 | $User = User::instance(); |
||
| 212 | if (!$User->user() || !$this->get_client($client)) { |
||
| 213 | return false; |
||
| 214 | } |
||
| 215 | $result = $this->db_prime()->q( |
||
| 216 | "INSERT IGNORE INTO `[prefix]oauth2_clients_grant_access` |
||
| 217 | ( |
||
| 218 | `id`, |
||
| 219 | `user` |
||
| 220 | ) VALUES ( |
||
| 221 | '%s', |
||
| 222 | '%s' |
||
| 223 | )", |
||
| 224 | $client, |
||
| 225 | $User->id |
||
| 226 | ); |
||
| 227 | unset($this->cache->{"grant_access/$User->id"}); |
||
| 228 | return $result; |
||
| 229 | } |
||
| 230 | /** |
||
| 231 | * Check granted access for specified client |
||
| 232 | * |
||
| 233 | * @param string $client |
||
| 234 | * @param bool|int $user If not specified - current user assumed |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function get_access ($client, $user = false) { |
||
| 239 | $user = (int)$user ?: User::instance()->id; |
||
| 240 | if ($user == User::GUEST_ID) { |
||
| 241 | return false; |
||
| 242 | } |
||
| 243 | $clients = $this->cache->get( |
||
| 244 | "grant_access/$user", |
||
| 245 | function () use ($user) { |
||
| 246 | return $this->db()->qfas( |
||
| 247 | "SELECT `id` |
||
| 248 | FROM `[prefix]oauth2_clients_grant_access` |
||
| 249 | WHERE `user` = '%s'", |
||
| 250 | $user |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | ); |
||
| 254 | return $clients ? in_array($client, $clients) : false; |
||
| 255 | } |
||
| 256 | /** |
||
| 257 | * Deny access for specified client/ |
||
| 258 | * |
||
| 259 | * @param string $client If '' - access for all clients will be denied |
||
| 260 | * @param bool|int $user If not specified - current user assumed |
||
| 261 | * |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | public function del_access ($client = '', $user = false) { |
||
| 265 | $user = (int)$user ?: User::instance()->id; |
||
| 266 | if ($user == User::GUEST_ID) { |
||
| 267 | return false; |
||
| 268 | } |
||
| 269 | $result = $client ? $this->db_prime()->q( |
||
| 270 | [ |
||
| 271 | "DELETE FROM `[prefix]oauth2_clients_grant_access` |
||
| 272 | WHERE |
||
| 273 | `user` = $user AND |
||
| 274 | `id` = '%s'", |
||
| 275 | "DELETE FROM `[prefix]oauth2_clients_sessions` |
||
| 276 | WHERE |
||
| 277 | `user` = $user AND |
||
| 278 | `id` = '%s'" |
||
| 279 | ], |
||
| 280 | $client |
||
| 281 | ) : $this->db_prime()->q( |
||
| 282 | [ |
||
| 283 | "DELETE FROM `[prefix]oauth2_clients_grant_access` |
||
| 284 | WHERE |
||
| 285 | `user` = $user", |
||
| 286 | "DELETE FROM `[prefix]oauth2_clients_sessions` |
||
| 287 | WHERE |
||
| 288 | `user` = $user" |
||
| 289 | ] |
||
| 290 | ); |
||
| 291 | unset($this->cache->{"grant_access/$user"}); |
||
| 292 | return $result; |
||
| 293 | } |
||
| 294 | /** |
||
| 295 | * Adds new code for specified client, code is used to obtain token |
||
| 296 | * |
||
| 297 | * @param string $client |
||
| 298 | * @param string $response_type 'code' or 'token' |
||
| 299 | * @param string $redirect_uri |
||
| 300 | * |
||
| 301 | * @return false|string <i>false</i> on failure or code for token access otherwise |
||
| 302 | */ |
||
| 303 | public function add_code ($client, $response_type, $redirect_uri = '') { |
||
| 304 | $Session = Session::instance(); |
||
| 305 | $client = $this->get_client($client); |
||
| 306 | if ( |
||
| 307 | !$client || |
||
| 308 | !$Session->user() || |
||
| 309 | !$this->get_access($client['id']) |
||
| 310 | ) { |
||
| 311 | return false; |
||
| 312 | } |
||
| 313 | $Request = Request::instance(); |
||
| 314 | $user_agent = $Request->header('user-agent'); |
||
| 315 | $current_session = $Session->get_id(); |
||
| 316 | $Request->headers['user-agent'] = "OAuth2-$client[name]-$client[id]"; |
||
| 317 | $Session->add($Session->get_user(), false); |
||
| 318 | $new_session = $Session->get_id(); |
||
| 319 | $Request->headers['user-agent'] = $user_agent; |
||
| 320 | $Session->load($current_session); |
||
| 321 | unset($user_agent, $current_session); |
||
| 322 | /** @noinspection LoopWhichDoesNotLoopInspection */ |
||
| 323 | while (true) { |
||
| 324 | $access_token = md5(random_bytes(1000)); |
||
| 325 | $refresh_token = md5($access_token.random_bytes(1000)); |
||
| 326 | $code = md5($refresh_token.random_bytes(1000)); |
||
| 327 | if ($this->db_prime()->qf( |
||
| 328 | "SELECT `id` |
||
| 329 | FROM `[prefix]oauth2_clients_sessions` |
||
| 330 | WHERE |
||
| 331 | `access_token` = '$access_token' OR |
||
| 332 | `refresh_token` = '$refresh_token' OR |
||
| 333 | `code` = '$code' |
||
| 334 | LIMIT 1" |
||
| 335 | ) |
||
| 336 | ) { |
||
| 337 | continue; |
||
| 338 | } |
||
| 339 | $result = $this->db_prime()->q( |
||
| 340 | "INSERT INTO `[prefix]oauth2_clients_sessions` |
||
| 341 | ( |
||
| 342 | `id`, |
||
| 343 | `user`, |
||
| 344 | `session`, |
||
| 345 | `created`, |
||
| 346 | `expire`, |
||
| 347 | `access_token`, |
||
| 348 | `refresh_token`, |
||
| 349 | `code`, |
||
| 350 | `type`, |
||
| 351 | `redirect_uri` |
||
| 352 | ) VALUES ( |
||
| 353 | '%s', |
||
| 354 | '%s', |
||
| 355 | '%s', |
||
| 356 | '%s', |
||
| 357 | '%s', |
||
| 358 | '%s', |
||
| 359 | '%s', |
||
| 360 | '%s', |
||
| 361 | '%s', |
||
| 362 | '%s' |
||
| 363 | )", |
||
| 364 | $client['id'], |
||
| 365 | $Session->get_user(), |
||
| 366 | $new_session, |
||
| 367 | time(), |
||
| 368 | time() + $this->expiration, |
||
| 369 | $access_token, |
||
| 370 | $refresh_token, |
||
| 371 | $code, |
||
| 372 | $response_type, |
||
| 373 | md5($redirect_uri) |
||
| 374 | ); |
||
| 375 | return $result ? $code : false; |
||
| 376 | } |
||
| 377 | return false; |
||
| 378 | } |
||
| 379 | /** |
||
| 380 | * Get code data (tokens) |
||
| 381 | * |
||
| 382 | * @param string $code |
||
| 383 | * @param string $client Client id |
||
| 384 | * @param string $secret Client secret |
||
| 385 | * @param string $redirect_uri |
||
| 386 | * |
||
| 387 | * @return array|false <i>false</i> on failure, otherwise array |
||
| 388 | * ['access_token' => md5, 'refresh_token' => md5, 'expires_in' => seconds, 'token_type' => 'bearer']<br> |
||
| 389 | * <i>expires_in</i> may be negative |
||
| 390 | */ |
||
| 391 | public function get_code ($code, $client, $secret, $redirect_uri = '') { |
||
| 392 | $client = $this->get_client($client); |
||
| 393 | if (!is_md5($code) || !$client || $client['secret'] != $secret) { |
||
| 394 | return false; |
||
| 395 | } |
||
| 396 | $data = $this->db_prime()->qf( |
||
| 397 | "SELECT |
||
| 398 | `access_token`, |
||
| 399 | `refresh_token`, |
||
| 400 | `expire`, |
||
| 401 | `user` |
||
| 402 | FROM `[prefix]oauth2_clients_sessions` |
||
| 403 | WHERE |
||
| 404 | `id` = '%s' AND |
||
| 405 | `code` = '%s' AND |
||
| 406 | `redirect_uri` = '%s' |
||
| 407 | LIMIT 1", |
||
| 408 | $client['id'], |
||
| 409 | $code, |
||
| 410 | md5($redirect_uri) |
||
| 411 | ); |
||
| 412 | if (!$data) { |
||
| 413 | return false; |
||
| 414 | } |
||
| 415 | $this->db_prime()->q( |
||
| 416 | "UPDATE `[prefix]oauth2_clients_sessions` |
||
| 417 | SET `code` = '' |
||
| 418 | WHERE |
||
| 419 | `id` = '%s' AND |
||
| 420 | `code` = '%s' AND |
||
| 421 | `redirect_uri` = '%s'", |
||
| 422 | $client['id'], |
||
| 423 | $code, |
||
| 424 | md5($redirect_uri) |
||
| 425 | ); |
||
| 426 | return [ |
||
| 427 | 'access_token' => $data['access_token'], |
||
| 428 | 'refresh_token' => $data['refresh_token'], |
||
| 429 | 'expires_in' => $data['expire'] - time(), |
||
| 430 | 'token_type' => 'bearer', |
||
| 431 | 'user_id' => $data['user'] |
||
| 432 | ]; |
||
| 433 | } |
||
| 434 | /** |
||
| 435 | * Get token data |
||
| 436 | * |
||
| 437 | * @param string $access_token |
||
| 438 | * |
||
| 439 | * @return array|false <i>false</i> on failure, array ['user' => id, 'session' => id, 'expire' => unix time, 'type' => 'code'|'token'] |
||
| 440 | */ |
||
| 441 | public function get_token ($access_token) { |
||
| 492 | } |
||
| 493 | /** |
||
| 494 | * Del token data (invalidate token) |
||
| 495 | * |
||
| 496 | * @param string $access_token |
||
| 497 | * |
||
| 498 | * @return bool |
||
| 499 | */ |
||
| 500 | public function del_token ($access_token) { |
||
| 523 | } |
||
| 524 | /** |
||
| 525 | * Get new access_token with refresh_token |
||
| 526 | * |
||
| 527 | * @param string $refresh_token |
||
| 528 | * @param string $client Client id |
||
| 529 | * @param string $secret Client secret |
||
| 530 | * |
||
| 531 | * @return array|false <i>false</i> on failure, otherwise array ['access_token' => md5, 'refresh_token' => md5, 'expires_in' => seconds, 'token_type' => |
||
| 532 | * 'bearer'] |
||
| 533 | */ |
||
| 534 | public function refresh_token ($refresh_token, $client, $secret) { |
||
| 577 | } |
||
| 578 | } |
||
| 579 |