Passed
Push — master ( e9591a...c0a685 )
by Rutger
13:20
created

Oauth2OidcBearerTokenResponse::getExtraParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4.0275

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 36
ccs 22
cts 25
cp 0.88
rs 9.584
cc 4
nc 4
nop 1
crap 4.0275
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\openidconnect\server;
4
5
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
6
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
7
use rhertogh\Yii2Oauth2Server\helpers\OpenIdConnectHelper;
0 ignored issues
show
Bug introduced by
The type rhertogh\Yii2Oauth2Serve...ers\OpenIdConnectHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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\Oauth2OidcBearerTokenResponseInterface;
11
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2OidcUserInterface;
12
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AccessTokenInterface;
13
use rhertogh\Yii2Oauth2Server\Oauth2Module;
14
use Yii;
15
use yii\base\InvalidArgumentException;
16
use yii\base\InvalidConfigException;
17
18
class Oauth2OidcBearerTokenResponse extends BearerTokenResponse implements Oauth2OidcBearerTokenResponseInterface
19
{
20
    /**
21
     * @var Oauth2Module
22
     */
23
    protected $_module;
24
25
    /**
26
     * @inheritDoc
27
     */
28 7
    public function __construct(Oauth2Module $module)
29
    {
30 7
        $this->_module = $module;
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36 3
    public function getModule()
37
    {
38 3
        return $this->_module;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     * @param Oauth2AccessTokenInterface $accessToken
44
     * @return array
45
     * @throws InvalidConfigException
46
     */
47 3
    protected function getExtraParams(AccessTokenEntityInterface $accessToken)
48
    {
49 3
        $scopeIdentifiers = array_map(fn($scope) => $scope->getIdentifier(), $accessToken->getScopes());
50
51
        // Not a OpenId Connect request if OpenId scope is not present.
52 3
        if (!in_array(Oauth2OidcScopeInterface::OPENID_CONNECT_SCOPE_OPENID, $scopeIdentifiers)) {
53 1
            return [];
54
        }
55
56 2
        $module = $this->getModule();
57
58 2
        $user = $module->getUserRepository()->getUserEntityByIdentifier($accessToken->getUserIdentifier());
59 2
        if ($user === null) {
60 1
            throw new InvalidArgumentException(
61 1
                'No user with identifier "' . $accessToken->getUserIdentifier() . '" found.'
62 1
            );
63
        }
64 1
        if (!($user instanceof Oauth2OidcUserInterface)) {
65
            throw new InvalidConfigException(
66
                get_class($user) . ' must implement ' . Oauth2OidcUserInterface::class
67
            );
68
        }
69
70 1
        $nonce = Yii::$app->request->post(Oauth2OidcAuthenticationRequestInterface::REQUEST_PARAMETER_NONCE);
71
72 1
        $token = $module->generateOpenIdConnectUserClaimsToken(
73 1
            $user,
74 1
            $accessToken->getClient()->getIdentifier(),
75 1
            $this->privateKey,
76 1
            $scopeIdentifiers,
0 ignored issues
show
Bug introduced by
$scopeIdentifiers of type array is incompatible with the type string expected by parameter $scopeIdentifiers of rhertogh\Yii2Oauth2Serve...onnectUserClaimsToken(). ( Ignorable by Annotation )

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

76
            /** @scrutinizer ignore-type */ $scopeIdentifiers,
Loading history...
77 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

77
            /** @scrutinizer ignore-type */ $nonce,
Loading history...
78 1
            $accessToken->getExpiryDateTime()
79 1
        );
80
81 1
        return [
82 1
            static::TOKEN_RESPONSE_ID_TOKEN => $token->toString()
83 1
        ];
84
    }
85
}
86