Passed
Push — master ( 2b2d6e...305862 )
by Rutger
13:36
created

Oauth2OidcBearerTokenResponse::getExtraParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 38
rs 9.568
cc 4
nc 4
nop 1
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\openidconnect\server\responses;
4
5
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
6
use rhertogh\Yii2Oauth2Server\components\server\responses\Oauth2BearerTokenResponse;
7
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\request\Oauth2OidcAuthenticationRequestInterface;
8
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeInterface;
9
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\server\responses\Oauth2OidcBearerTokenResponseInterface;
10
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2OidcUserInterface;
11
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AccessTokenInterface;
12
use Yii;
13
use yii\base\InvalidArgumentException;
14
use yii\base\InvalidConfigException;
15
use yii\helpers\ArrayHelper;
16
17
class Oauth2OidcBearerTokenResponse extends Oauth2BearerTokenResponse implements Oauth2OidcBearerTokenResponseInterface
18
{
19
    /**
20
     * @inheritDoc
21
     * @param Oauth2AccessTokenInterface $accessToken
22
     * @return array
23
     * @throws InvalidConfigException
24
     */
25
    protected function getExtraParams(AccessTokenEntityInterface $accessToken)
26
    {
27
        $extraParams = parent::getExtraParams($accessToken);
28
29
        $scopeIdentifiers = array_map(fn($scope) => $scope->getIdentifier(), $accessToken->getScopes());
30
31
        // Not a OpenId Connect request if OpenId scope is not present.
32
        if (!in_array(Oauth2OidcScopeInterface::OPENID_CONNECT_SCOPE_OPENID, $scopeIdentifiers)) {
33
            return $extraParams;
34
        }
35
36
        $module = $this->getModule();
37
38
        $user = $module->getUserRepository()->getUserEntityByIdentifier($accessToken->getUserIdentifier());
39
        if ($user === null) {
40
            throw new InvalidArgumentException(
41
                'No user with identifier "' . $accessToken->getUserIdentifier() . '" found.'
42
            );
43
        }
44
        if (!($user instanceof Oauth2OidcUserInterface)) {
45
            throw new InvalidConfigException(
46
                get_class($user) . ' must implement ' . Oauth2OidcUserInterface::class
47
            );
48
        }
49
50
        $nonce = Yii::$app->request->post(Oauth2OidcAuthenticationRequestInterface::REQUEST_PARAMETER_NONCE);
51
52
        $token = $module->generateOpenIdConnectUserClaimsToken(
53
            $user,
54
            $accessToken->getClient()->getIdentifier(),
55
            $this->privateKey,
56
            $scopeIdentifiers,
57
            $nonce,
0 ignored issues
show
Bug introduced by
It seems like $nonce can also be of type array; however, parameter $nonce of rhertogh\Yii2Oauth2Serve...onnectUserClaimsToken() does only seem to accept null|string, 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

57
            /** @scrutinizer ignore-type */ $nonce,
Loading history...
58
            $accessToken->getExpiryDateTime()
59
        );
60
61
        return ArrayHelper::merge($extraParams, [
62
            static::TOKEN_RESPONSE_ID_TOKEN => $token->toString()
63
        ]);
64
    }
65
}
66