| Conditions | 30 |
| Paths | > 20000 |
| Total Lines | 136 |
| Code Lines | 80 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 365 | protected function validateAccess() |
||
| 366 | { |
||
| 367 | $this->dnsbl(); |
||
| 368 | |||
| 369 | $f3 = \Base::instance(); |
||
| 370 | |||
| 371 | // if forcing access to https die |
||
| 372 | if ('http' == $f3->get('SCHEME') && !empty($f3->get('api.https'))) { |
||
| 373 | $this->failure('api_connection_error', "Connection only allowed via HTTPS!", 400); |
||
| 374 | $this->setOAuthError('unauthorized_client'); |
||
| 375 | return; |
||
| 376 | } |
||
| 377 | |||
| 378 | $usersModel = Models\Users::instance(); |
||
| 379 | $usersMapper = $usersModel->getMapper(); |
||
| 380 | |||
| 381 | $oAuth2Model = Models\OAuth2::instance(); |
||
| 382 | $appsMapper = $oAuth2Model->getAppsMapper(); |
||
| 383 | $tokensMapper = $oAuth2Model->getTokensMapper(); |
||
| 384 | |||
| 385 | // get token from request to set the user and app |
||
| 386 | // override if anything in basic auth or client_id/secret after |
||
| 387 | $appLogin = false; |
||
| 388 | $token = $f3->get('REQUEST.access_token'); |
||
| 389 | if (!empty($token)) { |
||
| 390 | $tokensMapper->load(['token = ?', $token]); |
||
| 391 | // check token is not out-of-date |
||
| 392 | if (null == $tokensMapper->uuid) { |
||
| 393 | $this->failure('authentication_error', "The token does not exist!", 401); |
||
| 394 | $this->setOAuthError('invalid_grant'); |
||
| 395 | return false; |
||
| 396 | } |
||
| 397 | if (time() > strtotime($tokensMapper->expires)) { |
||
| 398 | $this->failure('authentication_error', "The token expired!", 401); |
||
| 399 | $this->setOAuthError('invalid_grant'); |
||
| 400 | return false; |
||
| 401 | } |
||
| 402 | if (null !== $tokensMapper->users_uuid) { |
||
| 403 | $usersModel->getUserByUUID($tokensMapper->users_uuid); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | // login with client_id and client_secret in request |
||
| 408 | $clientId = $f3->get('REQUEST.client_id'); |
||
| 409 | $clientSecret = $f3->get('REQUEST.client_secret'); |
||
| 410 | if (!empty($clientId) && !empty($clientSecret) |
||
| 411 | && $this->authenticateClientIdSecret($clientId, $clientSecret)) { |
||
| 412 | $appLogin = true; |
||
| 413 | } |
||
| 414 | |||
| 415 | // check if login via http basic auth |
||
| 416 | $phpAuthUser = $f3->get('REQUEST.PHP_AUTH_USER'); |
||
| 417 | if (!empty($phpAuthUser)) { |
||
| 418 | // try to login as email:password |
||
| 419 | if ($this->basicAuthenticateLoginPassword()) { |
||
| 420 | $email = $f3->get('REQUEST.PHP_AUTH_USER'); |
||
| 421 | $usersModel->getUserByEmail($email); |
||
| 422 | } elseif ($this->basicAuthenticateClientIdSecret()) { |
||
| 423 | $appLogin = true; // client_id:client_secret |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | // login with app credentials? client_id/client_secret? |
||
| 428 | // if so fetch app information |
||
| 429 | if (!empty($appLogin)) { |
||
| 430 | // set app in f3 |
||
| 431 | $data = $appsMapper->cast(); |
||
| 432 | unset($data['id']); |
||
| 433 | $f3->set('api_app', $data); |
||
| 434 | // load the user by app user uuid |
||
| 435 | $usersMapper->load(['uuid = ?', $appsMapper->users_uuid]); |
||
| 436 | } |
||
| 437 | |||
| 438 | // check user has api access enabled |
||
| 439 | // has to have 'api' in group |
||
| 440 | $f3->set('is_admin', 0); |
||
| 441 | if (empty($token)) { |
||
| 442 | $groups = empty($usersMapper->groups) ? [] : preg_split("/[\s,]+/", $usersMapper->groups); |
||
| 443 | if (!in_array('api', $groups)) { |
||
| 444 | $usersMapper->reset(); |
||
| 445 | $f3->clear('api_app'); // clear authorized app as user doesn't have access |
||
| 446 | } |
||
| 447 | if (in_array('admin', $groups)) { |
||
| 448 | $f3->set('is_admin', 1); |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | // fetch user information if available |
||
| 453 | if (null !== $usersMapper->uuid) { |
||
| 454 | $data = $usersMapper->cast(); |
||
| 455 | unset($data['id']); |
||
| 456 | unset($data['password']); |
||
| 457 | $f3->set('user', $data); |
||
| 458 | $f3->set('uuid', $f3->set('uuid', $usersMapper->uuid)); |
||
| 459 | } |
||
| 460 | |||
| 461 | $app = $f3->get('api_app'); // authenticated as a client app |
||
| 462 | $user = $f3->get('user'); // authenticated as a user |
||
| 463 | |||
| 464 | // fetch scope if available |
||
| 465 | if (!empty($app) && !empty($user)) { |
||
| 466 | $tokensMapper->load(['client_id = ? AND users_uuid = ?', $app['client_id'], $user['uuid']]); |
||
| 467 | } |
||
| 468 | |||
| 469 | // get the scopes, this might have come from the token auth |
||
| 470 | $scopes = []; |
||
| 471 | if (!empty($tokensMapper->users_uuid)) { |
||
| 472 | $scopes = empty($request['scope']) ? [] : preg_split("/[\s,]+/", $request['scope']); |
||
| 473 | $f3->set('user_scopes', $scopes); |
||
| 474 | // also check the token is valid |
||
| 475 | if (!$appLogin && time() > strtotime($tokensMapper->expires)) { |
||
| 476 | $this->failure('authentication_error', "The token expired!", 401); |
||
| 477 | $this->setOAuthError('invalid_grant'); |
||
| 478 | return false; |
||
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | // set user groups |
||
| 483 | $groups = empty($usersMapper->groups) ? [] : preg_split("/[\s,]+/", $usersMapper->groups); |
||
| 484 | if (!empty($groups)) { |
||
| 485 | $f3->set('user_groups', $groups); |
||
| 486 | } |
||
| 487 | if (in_array('admin', $groups)) { |
||
| 488 | $f3->set('is_admin', 1); |
||
| 489 | } |
||
| 490 | |||
| 491 | $userAuthenticated = (is_array($user) || is_array($app)); |
||
| 492 | if (!$userAuthenticated) { |
||
| 493 | $this->failure('authentication_error', "Not possible to authenticate the request.", 400); |
||
| 494 | $this->setOAuthError('invalid_credentials'); |
||
| 495 | |||
| 496 | return false; |
||
| 497 | } |
||
| 498 | |||
| 499 | return true; |
||
| 500 | } |
||
| 501 | |||
| 514 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..