Passed
Pull Request — master (#1316)
by
unknown
32:56
created

IdTokenResponse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

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

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\ClaimSetInterface;
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
    protected IdTokenRepositoryInterface $idTokenRepository;
38
39
    /**
40
     * ClaimSetRepositoryInterface
41
     */
42
    protected ClaimSetRepositoryInterface $claimRepository;
43
44
    /**
45
     * ClaimExtractorInterface
46
     */
47
    protected ClaimExtractorInterface $extractor;
48
49
    public function __construct(
50
        IdTokenRepositoryInterface $idTokenRepository,
51
        ClaimSetRepositoryInterface $claimRepository,
52
        EventEmitter $emitter,
53
        ?ClaimExtractorInterface $extractor = null
54
    ) {
55
        if (!$extractor) {
56
            $this->extractor = new ClaimExtractor();
57
        } else {
58
            $this->extractor = $extractor;
59
        }
60
        $this->idTokenRepository = $idTokenRepository;
61
        $this->claimRepository = $claimRepository;
62
        $this->setEmitter($emitter);
63
    }
64
65
    /**
66
     * Add custom fields to your Bearer Token response here, then override
67
     * AuthorizationServer::getResponseType() to pull in your version of
68
     * this class rather than the default.
69
     *
70
     * @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...
71
     */
72
    protected function getExtraParams(AccessTokenEntityInterface $accessToken): array
73
    {
74
        // Onyly add id_token to openid scopes
75
        if (!self::isOpenIDRequest($accessToken->getScopes())) {
76
            return [];
77
        }
78
79
        $claimSet = $this->claimRepository->getClaimSet($accessToken);
80
81
        $builder = $this->idTokenRepository->getBuilder($accessToken);
82
83
        if ($claimSet instanceof ClaimSetInterface) {
0 ignored issues
show
introduced by
$claimSet is always a sub-type of League\OAuth2\Server\Entities\ClaimSetInterface.
Loading history...
84
            foreach ($this->extractor->extract($accessToken->getScopes(), $claimSet->getClaims()) as $claimName => $claimValue) {
85
                $builder = $builder->withClaim($claimName, $claimValue);
86
            }
87
        }
88
89
        $this->getEmitter()->emit(
90
            new IdTokenClaimsCreatedEvent(IdTokenEvent::ID_TOKEN_CLAIMS_CREATED, $builder)
91
        );
92
93
        $token = $builder->getToken(
94
            new Sha256(),
95
            InMemory::file($this->privateKey->getKeyPath(), (string) $this->privateKey->getPassPhrase())
96
        );
97
98
        $this->getEmitter()->emit(
99
            new IdTokenIssuedEvent(IdTokenEvent::ID_TOKEN_ISSUED, $token)
100
        );
101
102
        return [
103
            'id_token' => $token->toString(),
104
        ];
105
    }
106
107
    /**
108
     * Return true If this is an OpenID request
109
     *
110
     * @param ScopeEntityInterface[] $scopes
111
     */
112
    private static function isOpenIDRequest(array $scopes): bool
113
    {
114
        foreach ($scopes as $scope) {
115
            if ($scope instanceof ScopeEntityInterface) {
116
                if ($scope->getIdentifier() === 'openid') {
117
                    return true;
118
                }
119
            }
120
        }
121
122
        return false;
123
    }
124
}
125