|
19 | 19 | use function count;
|
20 | 20 | use function in_array;
|
21 | 21 | use function is_array;
|
| 22 | +use function is_string; |
22 | 23 |
|
23 | 24 | class JWSBuilder
|
24 | 25 | {
|
@@ -79,8 +80,8 @@ public function withPayload(string $payload, bool $isPayloadDetached = false): s
|
79 | 80 | /**
|
80 | 81 | * Adds the information needed to compute the signature. This method will return a new JWSBuilder object.
|
81 | 82 | *
|
82 |
| - * @param array{alg?: string, string?: mixed} $protectedHeader |
83 |
| - * @param array{alg?: string, string?: mixed} $header |
| 83 | + * @param array<string, mixed> $protectedHeader |
| 84 | + * @param array<string, mixed> $header |
84 | 85 | */
|
85 | 86 | public function addSignature(JWK $signatureKey, array $protectedHeader, array $header = []): self
|
86 | 87 | {
|
@@ -185,26 +186,25 @@ private function checkB64AndCriticalHeader(array $protectedHeader): void
|
185 | 186 | }
|
186 | 187 |
|
187 | 188 | /**
|
188 |
| - * @param array{alg?: string, string?: mixed} $protectedHeader |
189 |
| - * @param array{alg?: string, string?: mixed} $header |
| 189 | + * @param array<string, mixed> $protectedHeader |
| 190 | + * @param array<string, mixed> $header |
190 | 191 | * @return MacAlgorithm|SignatureAlgorithm
|
191 | 192 | */
|
192 | 193 | private function findSignatureAlgorithm(JWK $key, array $protectedHeader, array $header): Algorithm
|
193 | 194 | {
|
194 | 195 | $completeHeader = [...$header, ...$protectedHeader];
|
195 |
| - if (! array_key_exists('alg', $completeHeader)) { |
| 196 | + $alg = $completeHeader['alg'] ?? null; |
| 197 | + if (! is_string($alg)) { |
196 | 198 | throw new InvalidArgumentException('No "alg" parameter set in the header.');
|
197 | 199 | }
|
198 |
| - if ($key->has('alg') && $key->get('alg') !== $completeHeader['alg']) { |
199 |
| - throw new InvalidArgumentException(sprintf( |
200 |
| - 'The algorithm "%s" is not allowed with this key.', |
201 |
| - $completeHeader['alg'] |
202 |
| - )); |
| 200 | + $keyAlg = $key->has('alg') ? $key->get('alg') : null; |
| 201 | + if (is_string($keyAlg) && $keyAlg !== $alg) { |
| 202 | + throw new InvalidArgumentException(sprintf('The algorithm "%s" is not allowed with this key.', $alg)); |
203 | 203 | }
|
204 | 204 |
|
205 |
| - $algorithm = $this->signatureAlgorithmManager->get($completeHeader['alg']); |
| 205 | + $algorithm = $this->signatureAlgorithmManager->get($alg); |
206 | 206 | if (! $algorithm instanceof SignatureAlgorithm && ! $algorithm instanceof MacAlgorithm) {
|
207 |
| - throw new InvalidArgumentException(sprintf('The algorithm "%s" is not supported.', $completeHeader['alg'])); |
| 207 | + throw new InvalidArgumentException(sprintf('The algorithm "%s" is not supported.', $alg)); |
208 | 208 | }
|
209 | 209 |
|
210 | 210 | return $algorithm;
|
|
0 commit comments