Oauth2OidcBearerTokenResponse   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
wmc 4
eloc 23
c 0
b 0
f 0
dl 0
loc 46
ccs 23
cts 26
cp 0.8846
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getExtraParams() 0 38 4
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\openidconnect\server\responses;
4
5
// phpcs:disable Generic.Files.LineLength.TooLong
6
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
7
use rhertogh\Yii2Oauth2Server\components\server\responses\Oauth2BearerTokenResponse;
8
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\request\Oauth2OidcAuthenticationRequestInterface;
9
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeInterface;
10
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\server\responses\Oauth2OidcBearerTokenResponseInterface;
11
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2OidcUserInterface;
12
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AccessTokenInterface;
13
use Yii;
14
use yii\base\InvalidArgumentException;
15
use yii\base\InvalidConfigException;
16
use yii\helpers\ArrayHelper;
0 ignored issues
show
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
17
// phpcs:enable Generic.Files.LineLength.TooLong
18
19
class Oauth2OidcBearerTokenResponse extends Oauth2BearerTokenResponse implements Oauth2OidcBearerTokenResponseInterface
20
{
21
    /**
22
     * @inheritDoc
23
     * @param Oauth2AccessTokenInterface $accessToken
24
     * @return array
25
     * @throws InvalidConfigException
26
     */
27 3
    protected function getExtraParams(AccessTokenEntityInterface $accessToken)
28
    {
29 3
        $extraParams = parent::getExtraParams($accessToken);
30
31 3
        $scopeIdentifiers = array_map(fn($scope) => $scope->getIdentifier(), $accessToken->getScopes());
32
33
        // Not a OpenId Connect request if OpenId scope is not present.
34 3
        if (!in_array(Oauth2OidcScopeInterface::OPENID_CONNECT_SCOPE_OPENID, $scopeIdentifiers)) {
35 1
            return $extraParams;
36
        }
37
38 2
        $module = $this->getModule();
39
40 2
        $user = $module->getUserRepository()->getUserEntityByIdentifier($accessToken->getUserIdentifier());
41 2
        if ($user === null) {
42 1
            throw new InvalidArgumentException(
43 1
                'No user with identifier "' . $accessToken->getUserIdentifier() . '" found.'
44 1
            );
45
        }
46 1
        if (!($user instanceof Oauth2OidcUserInterface)) {
47
            throw new InvalidConfigException(
48
                get_class($user) . ' must implement ' . Oauth2OidcUserInterface::class
49
            );
50
        }
51
52 1
        $nonce = Yii::$app->request->post(Oauth2OidcAuthenticationRequestInterface::REQUEST_PARAMETER_NONCE);
53
54 1
        $token = $module->generateOpenIdConnectUserClaimsToken(
55 1
            $user,
56 1
            $accessToken->getClient()->getIdentifier(),
57 1
            $this->privateKey,
58 1
            $scopeIdentifiers,
59 1
            $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

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