Passed
Push — master ( a721e7...5904cd )
by Rutger
13:40
created

Oauth2OidcEndSessionAction::run()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 60
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 11.4242

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 60
ccs 25
cts 33
cp 0.7576
rs 7.6666
cc 10
nc 10
nop 1
crap 11.4242

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\web\openidconnect;
4
5
// phpcs:disable Generic.Files.LineLength.TooLong
6
use rhertogh\Yii2Oauth2Server\controllers\web\base\Oauth2BaseWebAction;
7
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2OidcController;
8
use rhertogh\Yii2Oauth2Server\interfaces\components\authorization\EndSession\Oauth2EndSessionAuthorizationRequestInterface;
9
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\openidconnect\Oauth2OidcEndSessionActionInterface;
10
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2OidcUserInterface;
11
use Yii;
12
use yii\base\InvalidConfigException;
13
use yii\web\BadRequestHttpException;
14
use yii\web\ForbiddenHttpException;
15
use yii\web\NotFoundHttpException;
16
use yii\web\Response;
17
use yii\web\UnauthorizedHttpException;
0 ignored issues
show
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
18
// phpcs:enable Generic.Files.LineLength.TooLong
19
20
/**
21
 * @property Oauth2OidcController $controller
22
 */
23
class Oauth2OidcEndSessionAction extends Oauth2BaseWebAction implements Oauth2OidcEndSessionActionInterface
24
{
25
    /**
26
     * @see https://openid.net/specs/openid-connect-rpinitiated-1_0.html
27
     * @return Response
28
     * @throws InvalidConfigException
29
     */
30 1
    public function run($endSessionAuthorizationRequestId = null)
31
    {
32 1
        $module = $this->controller->module;
33 1
        $request = Yii::$app->request;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->request can also be of type yii\web\Request. However, the property $request is declared as type yii\console\Request. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
34 1
        $identity = $module->getUserIdentity();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $identity is correct as $module->getUserIdentity() targeting rhertogh\Yii2Oauth2Serve...dule::getUserIdentity() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
35
36 1
        if (!$module->enableOpenIdConnect) {
37
            throw new ForbiddenHttpException('OpenID Connect is disabled.');
38
        }
39
40 1
        if ($identity && !($identity instanceof Oauth2OidcUserInterface)) {
0 ignored issues
show
introduced by
$identity is of type null, thus it always evaluated to false.
Loading history...
41
            throw new InvalidConfigException('In order to support OpenID Connect '
42
                . get_class($identity) . ' must implement ' . Oauth2OidcUserInterface::class);
43
        }
44
45 1
        if (empty($endSessionAuthorizationRequestId)) {
46
            /** @var Oauth2EndSessionAuthorizationRequestInterface $endSessionAuthorizationRequest */
47 1
            $endSessionAuthorizationRequest = Yii::createObject([
48 1
                'class' => Oauth2EndSessionAuthorizationRequestInterface::class,
49 1
                'module' => $module,
50 1
                'idTokenHint' => $this->getRequestParam($request, 'id_token_hint'),
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type yii\console\Request; however, parameter $request of rhertogh\Yii2Oauth2Serve...tion::getRequestParam() does only seem to accept yii\web\Request, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
                'idTokenHint' => $this->getRequestParam(/** @scrutinizer ignore-type */ $request, 'id_token_hint'),
Loading history...
51 1
                'clientIdentifier' => $this->getRequestParam($request, 'client_id'),
52 1
                'endSessionUrl' => $request->absoluteUrl,
53 1
                'redirectUri' => $this->getRequestParam($request, 'post_logout_redirect_uri'),
54 1
                'state' => $this->getRequestParam($request, 'state'),
55 1
            ]);
56
57 1
            $endSessionAuthorizationRequest->validateRequest();
58
59 1
            if (!$identity) {
0 ignored issues
show
introduced by
$identity is of type null, thus it always evaluated to false.
Loading history...
60
                /**
61
                 * Specified in https://openid.net/specs/openid-connect-rpinitiated-1_0.html#ValidationAndErrorHandling
62
                 * "Note that because RP-Initiated Logout Requests are intended to be idempotent,
63
                 *  it is explicitly not an error for an RP to request that a logout be performed when the OP does not
64
                 *  consider that the End-User is logged in with the OP at the requesting RP."
65
                 */
66
                return $this->controller->redirect($endSessionAuthorizationRequest->getRequestCompletedRedirectUrl(true));
67
            }
68
69 1
            if ($endSessionAuthorizationRequest->getEndUserAuthorizationRequired()) {
70 1
                if ($endSessionAuthorizationRequest->isAuthorizationAllowed()) {
71 1
                    return $module->generateEndSessionAuthReqRedirectResponse($endSessionAuthorizationRequest);
72
                } else {
73
                    throw new UnauthorizedHttpException(Yii::t('oauth2', 'You are not allowed to authorize logging out.'));
74
                }
75
            } else {
76
                $endSessionAuthorizationRequest->autoApproveAndProcess();
77
            }
78
        } else {
79 1
            $endSessionAuthorizationRequest = $module->getEndSessionAuthReqSession($endSessionAuthorizationRequestId);
80 1
            if (empty($endSessionAuthorizationRequest)) {
81
                throw new NotFoundHttpException('End Session authorization request not found.');
82
            }
83
        }
84
85 1
        if (!$endSessionAuthorizationRequest->isCompleted()) {
86
            throw new BadRequestHttpException('End Session authorization is not completed.');
87
        }
88
89 1
        return $this->controller->redirect($endSessionAuthorizationRequest->getRequestCompletedRedirectUrl());
90
    }
91
}
92