Debian Codegraph @

lib/3rdParty/composer/facile-it/php-openid-client/src/Claims/AbstractClaims.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Facile\OpenIDClient\Claims;
6
7 use Facile\OpenIDClient\AlgorithmManagerBuilder;
8 use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient;
9 use Facile\OpenIDClient\Exception\InvalidArgumentException;
10 use Facile\OpenIDClient\Exception\RuntimeException;
11 use Facile\OpenIDClient\Issuer\IssuerBuilder;
12 use Facile\OpenIDClient\Issuer\IssuerBuilderInterface;
13 use Facile\OpenIDClient\Token\TokenSetInterface;
14 use Jose\Component\Core\AlgorithmManager;
15 use Jose\Component\Core\JWKSet;
16 use Jose\Component\Signature\JWSVerifier;
17 use Jose\Component\Signature\Serializer\CompactSerializer;
18 use Jose\Component\Signature\Serializer\JWSSerializer;
19 use JsonException;
20
21 use function array_diff_key;
22 use function array_flip;
23 use function array_key_exists;
24 use function count;
25 use function explode;
26 use function Facile\OpenIDClient\base64url_decode;
27 use function json_decode;
28 use function sprintf;
29
30 /**
31 * @psalm-import-type TokenSetClaimsType from TokenSetInterface
32 * @psalm-import-type ClaimSourceType from TokenSetInterface
33 * @psalm-import-type ClaimSourceAggregateType from TokenSetInterface
34 * @psalm-import-type ClaimSourceDistributedType from TokenSetInterface
35 */
36 abstract class AbstractClaims
37 {
38 protected IssuerBuilderInterface $issuerBuilder;
39
40 protected AlgorithmManager $algorithmManager;
41
42 protected JWSVerifier $JWSVerifier;
43
44 protected JWSSerializer $serializer;
45
46 public function __construct(
47 ?IssuerBuilderInterface $issuerBuilder = null,
48 ?AlgorithmManager $algorithmManager = null,
49 ?JWSVerifier $JWSVerifier = null,
50 ?JWSSerializer $serializer = null
51 ) {
52 $this->issuerBuilder = $issuerBuilder ?? new IssuerBuilder();
53 $this->algorithmManager = $algorithmManager ?? (new AlgorithmManagerBuilder())->build();
54 $this->JWSVerifier = $JWSVerifier ?? new JWSVerifier($this->algorithmManager);
55 $this->serializer = $serializer ?? new CompactSerializer();
56 }
57
58 /**
59 * @psalm-param array<string, mixed> $data
60 *
61 * @psalm-return bool
62 *
63 * @psalm-assert-if-true ClaimSourceAggregateType $data
64 */
65 protected function isAggregateSource(array $data): bool
66 {
67 return array_key_exists('JWT', $data);
68 }
69
70 /**
71 * @psalm-param array<string, mixed> $data
72 *
73 * @psalm-return bool
74 *
75 * @psalm-assert-if-true ClaimSourceDistributedType $data
76 */
77 protected function isDistributedSource(array $data): bool
78 {
79 return array_key_exists('endpoint', $data);
80 }
81
82 /**
83 * @return array<string, mixed>
84 */
85 protected function claimJWT(OpenIDClient $client, string $jwt): array
86 {
87 $issuer = $client->getIssuer();
88
89 try {
90 /** @var null|array<string, mixed> $header */
91 $header = json_decode(base64url_decode(explode('.', $jwt)[0] ?? '{}'), true, 512, JSON_THROW_ON_ERROR);
92 /** @var array<string, mixed> $payload */
93 $payload = json_decode(base64url_decode(explode('.', $jwt)[1] ?? '{}'), true, 512, JSON_THROW_ON_ERROR);
94 } catch (JsonException $e) {
95 throw new InvalidArgumentException('Invalid JWT content', 0, $e);
96 }
97
98 /** @var null|string $alg */
99 $alg = $header['alg'] ?? null;
100 /** @var null|string $kid */
101 $kid = $header['kid'] ?? null;
102
103 if (null === $alg) {
104 throw new InvalidArgumentException('Claim source is missing JWT header alg property');
105 }
106
107 if ('none' === $alg) {
108 return $payload;
109 }
110
111 /** @var null|string $iss */
112 $iss = $payload['iss'] ?? null;
113
114 if (null === $iss || $iss === $issuer->getMetadata()->getIssuer()) {
115 $jwks = JWKSet::createFromKeyData($issuer->getJwksProvider()->getJwks());
116 } else {
117 $discovered = $this->issuerBuilder->build($iss);
118 $jwks = JWKSet::createFromKeyData($discovered->getJwksProvider()->getJwks());
119 }
120
121 $jws = $this->serializer->unserialize($jwt);
122
123 $jwk = $jwks->selectKey('sig', $this->algorithmManager->get($alg), null !== $kid ? ['kid' => $kid] : []);
124
125 if (null === $jwk) {
126 throw new RuntimeException('Unable to get a key to verify claim source JWT');
127 }
128
129 if (false === $this->JWSVerifier->verifyWithKey($jws, $jwk, 0)) {
130 throw new InvalidArgumentException('Invalid claim source JWT signature');
131 }
132
133 return $payload;
134 }
135
136 /**
137 * @param array<string, mixed> $claims
138 * @param array<string, string> $sourceNames
139 * @param array<string, array<string, mixed>> $sources
140 *
141 * @psalm-param TokenSetClaimsType $claims
142 *
143 * @return array<string, mixed>
144 *
145 * @psalm-return TokenSetClaimsType
146 */
147 protected function assignClaims(array $claims, array $sourceNames, array $sources): array
148 {
149 foreach ($sourceNames as $claim => $inSource) {
150 if (! array_key_exists($inSource, $sources)) {
151 continue;
152 }
153
154 if (! array_key_exists($claim, $sources[$inSource])) {
155 throw new RuntimeException(sprintf('Unable to find claim "%s" in source "%s"', $claim, $inSource));
156 }
157
158 /** @psalm-var scalar $value */
159 $value = $sources[$inSource][$claim];
160 $claims[$claim] = $value;
161 /** @psalm-var TokenSetClaimsType $claims */
162 $claims['_claim_names'] = array_diff_key($claims['_claim_names'] ?? [], array_flip([$claim]));
163 }
164
165 /** @psalm-var TokenSetClaimsType $claims */
166 return $claims;
167 }
168
169 /**
170 * @param array<string, mixed> $claims
171 *
172 * @psalm-param TokenSetClaimsType $claims
173 *
174 * @return array<string, mixed>
175 *
176 * @psalm-return TokenSetClaimsType
177 */
178 protected function cleanClaims(array $claims): array
179 {
180 if (array_key_exists('_claim_names', $claims) && 0 === count($claims['_claim_names'] ?? [])) {
181 /** @var TokenSetClaimsType $claims */
182 $claims = array_diff_key($claims, array_flip(['_claim_names']));
183 }
184
185 if (array_key_exists('_claim_sources', $claims) && 0 === count($claims['_claim_sources'] ?? [])) {
186 /** @var TokenSetClaimsType $claims */
187 $claims = array_diff_key($claims, array_flip(['_claim_sources']));
188 }
189
190 return $claims;
191 }
192 }