Passed
Pull Request — master (#1316)
by
unknown
34:03
created

IdTokenResponse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 26
dl 0
loc 74
rs 10
c 4
b 0
f 1
wmc 10

3 Methods

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

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