Passed
Pull Request — master (#1316)
by
unknown
31:50
created

IdTokenResponse::getExtraParams()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 3
b 0
f 0
nc 3
nop 1
dl 0
loc 32
rs 9.7333
1
<?php
2
3
namespace League\OAuth2\Server\ResponseTypes;
4
5
use Lcobucci\JWT\Signer\Key\InMemory;
6
use Lcobucci\JWT\Signer\Rsa\Sha256;
7
use League\Event\EmitterAwareTrait;
8
use League\OAuth2\Server\ClaimExtractor;
9
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
10
use League\OAuth2\Server\Entities\ClaimSetInterface;
11
use League\OAuth2\Server\Entities\ScopeEntityInterface;
12
use League\OAuth2\Server\IdTokenClaimsCreated;
13
use League\OAuth2\Server\IdTokenEvent;
14
use League\OAuth2\Server\IdTokenIssued;
15
use League\OAuth2\Server\Repositories\ClaimSetRepositoryInterface;
16
use League\OAuth2\Server\Repositories\IdTokenRepositoryInterface;
17
use League\OAuth2\Server\RequestEvent;
18
19
/**
20
 * OpenidConfigurationResponse
21
 *
22
 * @link https://github.com/steverhoades/oauth2-openid-connect-server
23
 *
24
 * @author Steve Rhoades <[email protected]>
25
 * @author Marc Riemer <[email protected]>
26
 */
27
class IdTokenResponse extends BearerTokenResponse
28
{
29
    use EmitterAwareTrait;
30
31
    public function __construct(
32
        protected IdTokenRepositoryInterface $builder,
33
        protected ClaimSetRepositoryInterface $claimRepository,
34
        protected ?ClaimExtractor $extractor = null
35
    ) {
36
        if (!$extractor) {
37
            $this->extractor = new ClaimExtractor();
38
        }
39
    }
40
41
    /**
42
     * @param AccessTokenEntityInterface $accessToken
43
     *
44
     * @return array
45
     */
46
    protected function getExtraParams(AccessTokenEntityInterface $accessToken): array
47
    {
48
        // Onyly add id_token to openid scopes
49
        if (!self::isOpenIDRequest($accessToken->getScopes())) {
50
            return [];
51
        }
52
53
        $claimSet = $this->claimRepository->getClaimSetEntry($accessToken);
54
55
        $builder = $this->builder->getBuilder($accessToken);
56
57
        if ($claimSet instanceof ClaimSetInterface) {
0 ignored issues
show
introduced by
$claimSet is always a sub-type of League\OAuth2\Server\Entities\ClaimSetInterface.
Loading history...
58
            foreach ($this->extractor->extract($accessToken->getScopes(), $claimSet->getClaims()) as $claimName => $claimValue) {
0 ignored issues
show
Bug introduced by
The method extract() does not exist on null. ( Ignorable by Annotation )

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

58
            foreach ($this->extractor->/** @scrutinizer ignore-call */ extract($accessToken->getScopes(), $claimSet->getClaims()) as $claimName => $claimValue) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
                $builder->withClaim($claimName, $claimValue);
60
            }
61
        }
62
63
        $this->getEmitter()->emit(
64
            new IdTokenClaimsCreated(IdTokenEvent::ID_TOKEN_CLAIMS_CREATED, $builder)
65
        );
66
67
        $token = $builder->getToken(
68
            new Sha256(),
69
            InMemory::file($this->privateKey->getKeyPath(), (string) $this->privateKey->getPassPhrase())
70
        );
71
72
        $this->getEmitter()->emit(
73
            new IdTokenIssued(IdTokenEvent::ID_TOKEN_ISSUED, $token)
74
        );
75
76
        return [
77
            'id_token' => $token->toString(),
78
        ];
79
    }
80
81
    /**
82
     * Return true If this is an OpenID request
83
     *
84
     * @param ScopeEntityInterface[] $scopes
85
     *
86
     * @return bool
87
     */
88
    private static function isOpenIDRequest($scopes): bool
89
    {
90
        foreach ($scopes as $scope) {
91
            if ($scope instanceof ScopeEntityInterface) {
92
                if ($scope->getIdentifier() === 'openid') {
93
                    return true;
94
                }
95
            }
96
        }
97
98
        return false;
99
    }
100
}
101