Passed
Pull Request — master (#1316)
by
unknown
62:15 queued 27:15
created

IdTokenResponse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
c 2
b 0
f 0
dl 0
loc 72
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isOpenIDRequest() 0 11 4
A getExtraParams() 0 32 4
A __construct() 0 7 2
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\IdTokenIssued;
14
use League\OAuth2\Server\Repositories\ClaimSetRepositoryInterface;
15
use League\OAuth2\Server\Repositories\IdTokenRepositoryInterface;
16
use League\OAuth2\Server\RequestEvent;
17
18
/**
19
 * OpenidConfigurationResponse
20
 *
21
 * @link https://github.com/steverhoades/oauth2-openid-connect-server
22
 *
23
 * @author Steve Rhoades <[email protected]>
24
 * @author Marc Riemer <[email protected]>
25
 */
26
class IdTokenResponse extends BearerTokenResponse
27
{
28
    use EmitterAwareTrait;
29
30
    public function __construct(
31
        protected IdTokenRepositoryInterface $builder,
32
        protected ClaimSetRepositoryInterface $claimRepository,
33
        protected ?ClaimExtractor $extractor = null
34
    ) {
35
        if (!$extractor) {
36
            $this->extractor = new ClaimExtractor();
37
        }
38
    }
39
40
    /**
41
     * @param AccessTokenEntityInterface $accessToken
42
     *
43
     * @return array
44
     */
45
    protected function getExtraParams(AccessTokenEntityInterface $accessToken): array
46
    {
47
        // Onyly add id_token to openid scopes
48
        if (!self::isOpenIDRequest($accessToken->getScopes())) {
49
            return [];
50
        }
51
52
        $claimSet = $this->claimRepository->getClaimSetEntry($accessToken);
53
54
        $builder = $this->builder->getBuilder($accessToken);
55
56
        if ($claimSet instanceof ClaimSetInterface) {
0 ignored issues
show
introduced by
$claimSet is always a sub-type of League\OAuth2\Server\Entities\ClaimSetInterface.
Loading history...
57
            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

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