Passed
Push — master ( 745cb0...e178e9 )
by Rutger
03:00
created

Oauth2OidcBearerTokenResponse   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 25
c 1
b 0
f 0
dl 0
loc 65
ccs 20
cts 22
cp 0.9091
rs 10

3 Methods

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

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

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