| Conditions | 28 |
| Paths | > 20000 |
| Total Lines | 131 |
| Code Lines | 74 |
| 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 |
||
| 356 | protected function validateAccess() |
||
| 357 | { |
||
| 358 | $this->dnsbl(); // always check if dns blacklisted |
||
| 359 | |||
| 360 | $f3 = \Base::instance(); |
||
| 361 | |||
| 362 | // return if forcing access to https and not https |
||
| 363 | if ('http' == $f3->get('SCHEME') && !empty($f3->get('api.https'))) { |
||
| 364 | $this->failure('api_connection_error', "Connection only allowed via HTTPS!", 400); |
||
| 365 | $this->setOAuthError('unauthorized_client'); |
||
| 366 | return; |
||
| 367 | } |
||
| 368 | |||
| 369 | $oAuth2Model = Models\OAuth2::instance(); |
||
| 370 | $tokensMapper = $oAuth2Model->getTokensMapper(); |
||
| 371 | |||
| 372 | // get token from request to set the user and app |
||
| 373 | // override if anything in basic auth or client_id/secret after |
||
| 374 | $appLogin = false; |
||
| 375 | $token = $f3->get('REQUEST.access_token'); |
||
| 376 | if (!empty($token)) { |
||
| 377 | $tokensMapper->load(['token = ?', $token]); |
||
| 378 | // token does not exist! |
||
| 379 | if (null == $tokensMapper->uuid) { |
||
| 380 | $this->failure('authentication_error', "The token does not exist!", 401); |
||
| 381 | $this->setOAuthError('invalid_grant'); |
||
| 382 | return false; |
||
| 383 | } |
||
| 384 | // check token is not out-of-date |
||
| 385 | if (time() > strtotime($tokensMapper->expires)) { |
||
| 386 | $this->failure('authentication_error', "The token expired!", 401); |
||
| 387 | $this->setOAuthError('invalid_grant'); |
||
| 388 | return false; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | // if token found load the user for the token |
||
| 393 | $usersModel = Models\Users::instance(); |
||
| 394 | if (null !== $tokensMapper->users_uuid) { |
||
| 395 | $usersModel->getUserByUUID($tokensMapper->users_uuid); |
||
| 396 | } |
||
| 397 | |||
| 398 | // login with client_id and client_secret in request |
||
| 399 | $clientId = $f3->get('REQUEST.client_id'); |
||
| 400 | $clientSecret = $f3->get('REQUEST.client_secret'); |
||
| 401 | |||
| 402 | $appLogin = (!empty($clientId) && !empty($clientSecret) |
||
| 403 | && $this->authenticateClientIdSecret($clientId, $clientSecret)); |
||
| 404 | |||
| 405 | // check if login via http basic auth |
||
| 406 | if (!empty($f3->get('REQUEST.PHP_AUTH_USER'))) { |
||
| 407 | // try to login as email:password |
||
| 408 | if ($this->basicAuthenticateLoginPassword()) { |
||
| 409 | $email = $f3->get('REQUEST.PHP_AUTH_USER'); |
||
| 410 | $usersModel->getUserByEmail($email); |
||
| 411 | } elseif ($this->basicAuthenticateClientIdSecret()) { |
||
| 412 | $appLogin = true; // client_id:client_secret |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | // login with app credentials: client_id/client_secret? |
||
| 417 | // if so fetch app and user information |
||
| 418 | $usersMapper = $usersModel->getMapper(); |
||
| 419 | $appsMapper = $oAuth2Model->getAppsMapper(); |
||
| 420 | if (!empty($appLogin)) { |
||
| 421 | // set app in f3 |
||
| 422 | $data = $appsMapper->cast(); |
||
| 423 | $f3->set('api_app', $data); |
||
| 424 | $usersMapper->load(['uuid = ?', $appsMapper->users_uuid]); |
||
| 425 | } |
||
| 426 | |||
| 427 | // check user has api access enabled |
||
| 428 | // has to have 'api' in group |
||
| 429 | $groups = empty($usersMapper->groups) ? [] : preg_split("/[\s,]+/", $usersMapper->groups); |
||
| 430 | $f3->set('is_admin', 0); |
||
| 431 | if (empty($token)) { |
||
| 432 | if (!in_array('api', $groups)) { |
||
| 433 | // clear authorized app as user doesn't have access |
||
| 434 | $usersMapper->reset(); |
||
| 435 | $f3->clear('api_app'); |
||
| 436 | } |
||
| 437 | if (in_array('admin', $groups)) { |
||
| 438 | $f3->set('is_admin', 1); |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | // fetch user information if available |
||
| 443 | if (null !== $usersMapper->uuid) { |
||
| 444 | $data = $usersMapper->cast(); |
||
| 445 | $f3->set('user', $data); |
||
| 446 | $f3->set('uuid', $f3->set('uuid', $usersMapper->uuid)); |
||
| 447 | } |
||
| 448 | |||
| 449 | $app = $f3->get('api_app'); // authenticated as a client app |
||
| 450 | $user = $f3->get('user'); // authenticated as a user |
||
| 451 | |||
| 452 | // fetch scope if available |
||
| 453 | if (!empty($app) && !empty($user)) { |
||
| 454 | $tokensMapper->load(['client_id = ? AND users_uuid = ?', $app['client_id'], $user['uuid']]); |
||
| 455 | } |
||
| 456 | |||
| 457 | // get the scopes, this might have come from the token auth |
||
| 458 | $scope = $f3->get('REQUEST.scope'); |
||
| 459 | $scopes = empty($scope) ? [] : preg_split("/[\s,]+/", $scope); |
||
| 460 | if (!empty($tokensMapper->users_uuid)) { |
||
| 461 | $f3->set('user_scopes', $scopes); |
||
| 462 | // also check the token is valid |
||
| 463 | if (!$appLogin && time() > strtotime($tokensMapper->expires)) { |
||
| 464 | $this->failure('authentication_error', "The token expired!", 401); |
||
| 465 | $this->setOAuthError('invalid_grant'); |
||
| 466 | return false; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | // set user groups |
||
| 471 | $f3->set('is_admin', in_array('admin', $groups)); |
||
| 472 | $groups = empty($usersMapper->groups) ? [] : preg_split("/[\s,]+/", $usersMapper->groups); |
||
| 473 | if (!empty($groups)) { |
||
| 474 | $f3->set('user_groups', $groups); |
||
| 475 | } |
||
| 476 | |||
| 477 | $userAuthenticated = (is_array($user) || is_array($app)); |
||
| 478 | if (!$userAuthenticated) { |
||
| 479 | $this->failure('authentication_error', "Not possible to authenticate the request.", 400); |
||
| 480 | $this->setOAuthError('invalid_credentials'); |
||
| 481 | |||
| 482 | return false; |
||
| 483 | } |
||
| 484 | |||
| 485 | return true; |
||
| 486 | } |
||
| 487 | |||
| 500 |
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..