Passed
Pull Request — master (#1316)
by
unknown
30:49
created

IdTokenResponse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
eloc 33
c 8
b 0
f 1
dl 0
loc 97
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isOpenIDRequest() 0 11 4
A getExtraParams() 0 32 4
A __construct() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\OAuth2\Server\ResponseTypes;
6
7
use Lcobucci\JWT\Signer\Key\InMemory;
8
use Lcobucci\JWT\Signer\Rsa\Sha256;
9
use League\OAuth2\Server\ClaimExtractor;
10
use League\OAuth2\Server\ClaimExtractorInterface;
11
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
12
use League\OAuth2\Server\Entities\ClaimSetEntryInterface;
13
use League\OAuth2\Server\Entities\ScopeEntityInterface;
14
use League\OAuth2\Server\EventEmitting\EmitterAwarePolyfill;
15
use League\OAuth2\Server\EventEmitting\EventEmitter;
16
use League\OAuth2\Server\IdTokenClaimsCreatedEvent;
17
use League\OAuth2\Server\IdTokenEvent;
18
use League\OAuth2\Server\IdTokenIssuedEvent;
19
use League\OAuth2\Server\Repositories\ClaimSetRepositoryInterface;
20
use League\OAuth2\Server\Repositories\IdTokenRepositoryInterface;
21
22
/**
23
 * OpenidConfigurationResponse
24
 *
25
 * @link https://github.com/steverhoades/oauth2-openid-connect-server
26
 *
27
 * @author Steve Rhoades <[email protected]>
28
 * @author Marc Riemer <[email protected]>
29
 */
30
class IdTokenResponse extends BearerTokenResponse
31
{
32
    use EmitterAwarePolyfill;
33
34
    /**
35
     * IdTokenRepositoryInterface
36
     *
37
     */
38
    protected IdTokenRepositoryInterface $idTokenRepository;
39
40
    /**
41
     * ClaimSetRepositoryInterface
42
     *
43
     */
44
    protected ClaimSetRepositoryInterface $claimRepository;
45
46
    /**
47
     * ClaimExtractorInterface
48
     *
49
     */
50
    protected ClaimExtractorInterface $extractor;
51
52
    public function __construct(
53
        IdTokenRepositoryInterface $idTokenRepository,
54
        ClaimSetRepositoryInterface $claimRepository,
55
        EventEmitter $emitter,
56
        ?ClaimExtractorInterface $extractor = null
57
    ) {
58
        if (!$extractor) {
59
            $this->extractor = new ClaimExtractor();
60
        } else {
61
            $this->extractor = $extractor;
62
        }
63
        $this->idTokenRepository = $idTokenRepository;
64
        $this->claimRepository = $claimRepository;
65
        $this->setEmitter($emitter);
66
    }
67
68
    /**
69
     * Add custom fields to your Bearer Token response here, then override
70
     * AuthorizationServer::getResponseType() to pull in your version of
71
     * this class rather than the default.
72
     *
73
     * @return array<array-key,mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key,mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key,mixed>.
Loading history...
74
     */
75
    protected function getExtraParams(AccessTokenEntityInterface $accessToken): array
76
    {
77
        // Onyly add id_token to openid scopes
78
        if (!self::isOpenIDRequest($accessToken->getScopes())) {
79
            return [];
80
        }
81
82
        $claimSet = $this->claimRepository->getClaimSetEntry($accessToken);
83
84
        $builder = $this->idTokenRepository->getBuilder($accessToken);
85
86
        if ($claimSet instanceof ClaimSetEntryInterface) {
0 ignored issues
show
introduced by
$claimSet is always a sub-type of League\OAuth2\Server\Ent...\ClaimSetEntryInterface.
Loading history...
87
            foreach ($this->extractor->extract($accessToken->getScopes(), $claimSet->getClaims()) as $claimName => $claimValue) {
88
                $builder = $builder->withClaim($claimName, $claimValue);
89
            }
90
        }
91
92
        $this->getEmitter()->emit(
93
            new IdTokenClaimsCreatedEvent(IdTokenEvent::ID_TOKEN_CLAIMS_CREATED, $builder)
94
        );
95
96
        $token = $builder->getToken(
97
            new Sha256(),
98
            InMemory::file($this->privateKey->getKeyPath(), (string) $this->privateKey->getPassPhrase())
99
        );
100
101
        $this->getEmitter()->emit(
102
            new IdTokenIssuedEvent(IdTokenEvent::ID_TOKEN_ISSUED, $token)
103
        );
104
105
        return [
106
            'id_token' => $token->toString(),
107
        ];
108
    }
109
110
    /**
111
     * Return true If this is an OpenID request
112
     *
113
     * @param ScopeEntityInterface[] $scopes
114
     *
115
     */
116
    private static function isOpenIDRequest(array $scopes): bool
117
    {
118
        foreach ($scopes as $scope) {
119
            if ($scope instanceof ScopeEntityInterface) {
120
                if ($scope->getIdentifier() === 'openid') {
121
                    return true;
122
                }
123
            }
124
        }
125
126
        return false;
127
    }
128
}
129