|
13 | 13 | use League\OAuth2\Server\Entities\UserEntityInterface; |
14 | 14 | use RuntimeException; |
15 | 15 | use SimpleSAML\Module\oidc\Entities\AccessTokenEntity; |
| 16 | +use SimpleSAML\Module\oidc\Entities\ClientEntity; |
16 | 17 | use SimpleSAML\Module\oidc\Entities\Interfaces\ClaimSetInterface; |
17 | 18 | use SimpleSAML\Module\oidc\Entities\Interfaces\EntityStringRepresentationInterface; |
| 19 | +use SimpleSAML\Module\oidc\ModuleConfig; |
18 | 20 | use SimpleSAML\Module\oidc\Utils\ClaimTranslatorExtractor; |
| 21 | +use SimpleSAML\OpenID\Algorithms\SignatureAlgorithmEnum; |
| 22 | +use SimpleSAML\OpenID\Codebooks\ClaimsEnum; |
| 23 | +use SimpleSAML\OpenID\Core; |
| 24 | +use SimpleSAML\OpenID\Core\IdToken; |
19 | 25 |
|
20 | 26 | class IdTokenBuilder |
21 | 27 | { |
22 | 28 | public function __construct( |
23 | | - private readonly JsonWebTokenBuilderService $jsonWebTokenBuilderService, |
24 | | - private readonly ClaimTranslatorExtractor $claimExtractor, |
| 29 | + protected readonly JsonWebTokenBuilderService $jsonWebTokenBuilderService, |
| 30 | + protected readonly ClaimTranslatorExtractor $claimExtractor, |
| 31 | + protected readonly Core $core, |
| 32 | + protected readonly ModuleConfig $moduleConfig, |
25 | 33 | ) { |
26 | 34 | } |
27 | 35 |
|
| 36 | + /** |
| 37 | + * @psalm-suppress MixedAssignment |
| 38 | + */ |
| 39 | + public function buildFor( |
| 40 | + UserEntityInterface $userEntity, |
| 41 | + AccessTokenEntity $accessToken, |
| 42 | + bool $addClaimsFromScopes, |
| 43 | + bool $addAccessTokenHash, |
| 44 | + ?string $nonce, |
| 45 | + ?int $authTime, |
| 46 | + ?string $acr, |
| 47 | + ?string $sessionId, |
| 48 | + ): IdToken { |
| 49 | + if (!is_a($userEntity, ClaimSetInterface::class)) { |
| 50 | + throw new RuntimeException('UserEntity must implement ClaimSetInterface'); |
| 51 | + } |
| 52 | + |
| 53 | + $client = $accessToken->getClient(); |
| 54 | + if (! $client instanceof ClientEntity) { |
| 55 | + throw new RuntimeException('Client is expected to be instance of ' . ClientEntity::class); |
| 56 | + } |
| 57 | + |
| 58 | + $protocolSignatureKeyPairBag = $this->moduleConfig->getProtocolSignatureKeyPairBag(); |
| 59 | + $protocolSignatureKeyPair = $protocolSignatureKeyPairBag->getFirstOrFail(); |
| 60 | + |
| 61 | + // ID Token signing algorithm that the client wants. |
| 62 | + $clientIdTokenSignedResponseAlg = $client->getIdTokenSignedResponseAlg(); |
| 63 | + |
| 64 | + if (is_string($clientIdTokenSignedResponseAlg)) { |
| 65 | + $protocolSignatureKeyPair = $protocolSignatureKeyPairBag->getFirstByAlgorithmOrFail( |
| 66 | + SignatureAlgorithmEnum::from($clientIdTokenSignedResponseAlg), |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + $currentTimestamp = $this->core->helpers()->dateTime()->getUtc()->getTimestamp(); |
| 71 | + |
| 72 | + $payload = array_filter([ |
| 73 | + ClaimsEnum::Iss->value => $this->moduleConfig->getIssuer(), |
| 74 | + ClaimsEnum::Iat->value => $currentTimestamp, |
| 75 | + ClaimsEnum::Jti->value => $this->core->helpers()->random()->string(), |
| 76 | + ClaimsEnum::Aud->value => $client->getIdentifier(), |
| 77 | + ClaimsEnum::Nbf->value => $currentTimestamp, |
| 78 | + ClaimsEnum::Exp->value => $accessToken->getExpiryDateTime()->getTimestamp(), |
| 79 | + ClaimsEnum::Sub->value => $this->core->helpers()->type()->ensureNonEmptyString( |
| 80 | + $userEntity->getIdentifier(), |
| 81 | + ), |
| 82 | + ClaimsEnum::Nonce->value => $nonce, |
| 83 | + ClaimsEnum::AuthTime->value => $authTime, |
| 84 | + ClaimsEnum::ATHash->value => $addAccessTokenHash ? |
| 85 | + $this->generateAccessTokenHash( |
| 86 | + $accessToken, |
| 87 | + $protocolSignatureKeyPair->getSignatureAlgorithm()->value, |
| 88 | + ) : |
| 89 | + null, |
| 90 | + ClaimsEnum::Acr->value => $acr, |
| 91 | + ClaimsEnum::Sid->value => $sessionId, |
| 92 | + ]); |
| 93 | + |
| 94 | + // Reduce the number of claims by provided scope. |
| 95 | + $claims = $this->claimExtractor->extract( |
| 96 | + $accessToken->getScopes(), |
| 97 | + $userEntity->getClaims(), |
| 98 | + ); |
| 99 | + $requestedClaims = $accessToken->getRequestedClaims(); |
| 100 | + $additionalClaims = $this->claimExtractor->extractAdditionalIdTokenClaims( |
| 101 | + $requestedClaims, |
| 102 | + $userEntity->getClaims(), |
| 103 | + ); |
| 104 | + $claims = array_merge($additionalClaims, $claims); |
| 105 | + |
| 106 | + foreach ($claims as $claimName => $claimValue) { |
| 107 | + if ( |
| 108 | + is_string($claimName) && |
| 109 | + $claimName !== '' && |
| 110 | + ($addClaimsFromScopes || array_key_exists($claimName, $additionalClaims)) |
| 111 | + ) { |
| 112 | + $payload[$claimName] = $claimValue; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + $header = [ |
| 117 | + ClaimsEnum::Kid->value => $protocolSignatureKeyPair->getKeyPair()->getKeyId(), |
| 118 | + ]; |
| 119 | + |
| 120 | + return $this->core->idTokenFactory()->fromData( |
| 121 | + $protocolSignatureKeyPair->getKeyPair()->getPrivateKey(), |
| 122 | + $protocolSignatureKeyPair->getSignatureAlgorithm(), |
| 123 | + $payload, |
| 124 | + $header, |
| 125 | + ); |
| 126 | + } |
| 127 | + |
28 | 128 | /** |
29 | 129 | * @throws \Exception |
30 | 130 | * @psalm-suppress ArgumentTypeCoercion |
| 131 | + * @deprecated Since v7 |
| 132 | + * @see self::buildFor() |
31 | 133 | */ |
32 | 134 | public function build( |
33 | 135 | UserEntityInterface $userEntity, |
|
0 commit comments