|
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
|
|
|
|
|
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) { |
|
|
|
|
|
|
57
|
|
|
foreach ($this->extractor->extract($accessToken->getScopes(), $claimSet->getClaims()) as $claimName => $claimValue) { |
|
|
|
|
|
|
58
|
|
|
$builder->withClaim($claimName, $claimValue); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->getEmitter()->emit( |
|
63
|
|
|
new IdTokenClaimsCreated(IdTokenEvent::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(IdTokenEvent::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
|
|
|
|