Passed
Push — master ( fb60c6...a721e7 )
by Rutger
03:14
created

Oauth2OidcEndSessionAction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 35
c 1
b 0
f 0
dl 0
loc 67
ccs 0
cts 33
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 60 10
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\web\openidconnect;
4
5
use Lcobucci\JWT\Encoding\JoseEncoder;
6
use Lcobucci\JWT\Signer\Key\InMemory;
7
use Lcobucci\JWT\Signer\Rsa\Sha256;
8
use Lcobucci\JWT\Token\Parser;
9
use Lcobucci\JWT\Validation\Constraint\PermittedFor;
10
use Lcobucci\JWT\Validation\Constraint\RelatedTo;
11
use Lcobucci\JWT\Validation\Constraint\SignedWith;
12
use Lcobucci\JWT\Validation\Validator;
13
use League\OAuth2\Server\RedirectUriValidators\RedirectUriValidator;
14
use rhertogh\Yii2Oauth2Server\controllers\web\base\Oauth2BaseWebAction;
15
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2OidcController;
16
use rhertogh\Yii2Oauth2Server\helpers\UrlHelper;
17
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\openidconnect\Oauth2OidcEndSessionActionInterface;
18
use rhertogh\Yii2Oauth2Server\interfaces\components\authorization\client\Oauth2ClientAuthorizationRequestInterface;
19
use rhertogh\Yii2Oauth2Server\interfaces\components\authorization\EndSession\Oauth2EndSessionAuthorizationRequestInterface;
20
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2OidcUserInterface;
21
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface;
22
use Yii;
23
use yii\base\InvalidConfigException;
24
use yii\web\BadRequestHttpException;
25
use yii\web\ForbiddenHttpException;
26
use yii\web\NotFoundHttpException;
27
use yii\web\Response;
28
use yii\web\UnauthorizedHttpException;
29
30
/**
31
 * @property Oauth2OidcController $controller
32
 */
33
class Oauth2OidcEndSessionAction extends Oauth2BaseWebAction implements Oauth2OidcEndSessionActionInterface
34
{
35
    /**
36
     * @see https://openid.net/specs/openid-connect-rpinitiated-1_0.html
37
     * @return Response
38
     * @throws InvalidConfigException
39
     */
40
    public function run($endSessionAuthorizationRequestId = null)
41
    {
42
        $module = $this->controller->module;
43
        $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...
44
        $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...
45
46
        if (!$module->enableOpenIdConnect) {
47
            throw new ForbiddenHttpException('OpenID Connect is disabled.');
48
        }
49
50
        if ($identity && !($identity instanceof Oauth2OidcUserInterface)) {
0 ignored issues
show
introduced by
$identity is of type null, thus it always evaluated to false.
Loading history...
51
            throw new InvalidConfigException('In order to support OpenID Connect '
52
                . get_class($identity) . ' must implement ' . Oauth2OidcUserInterface::class);
53
        }
54
55
        if (empty($endSessionAuthorizationRequestId)) {
56
            /** @var Oauth2EndSessionAuthorizationRequestInterface $endSessionAuthorizationRequest */
57
            $endSessionAuthorizationRequest = Yii::createObject([
58
                'class' => Oauth2EndSessionAuthorizationRequestInterface::class,
59
                'module' => $module,
60
                '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

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