Skip to content

Commit c417d45

Browse files
authored
Phpdoc/incorrect header description (#496)
Fix PHPDoc for JWS header (not complete)
1 parent ab7c59e commit c417d45

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,16 +2045,6 @@ parameters:
20452045
count: 1
20462046
path: src/Component/NestedToken/NestedTokenBuilder.php
20472047

2048-
-
2049-
message: "#^Parameter \\#2 \\$protectedHeader of method Jose\\\\Component\\\\Signature\\\\JWSBuilder\\:\\:addSignature\\(\\) expects array\\{alg\\?\\: string, string\\?\\: mixed\\}, array\\<string, mixed\\> given\\.$#"
2050-
count: 1
2051-
path: src/Component/NestedToken/NestedTokenBuilder.php
2052-
2053-
-
2054-
message: "#^Parameter \\#3 \\$header of method Jose\\\\Component\\\\Signature\\\\JWSBuilder\\:\\:addSignature\\(\\) expects array\\{alg\\?\\: string, string\\?\\: mixed\\}, array\\<string, mixed\\> given\\.$#"
2055-
count: 1
2056-
path: src/Component/NestedToken/NestedTokenBuilder.php
2057-
20582048
-
20592049
message: "#^Result of \\|\\| is always false\\.$#"
20602050
count: 2

src/Component/Signature/JWSBuilder.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use function count;
2020
use function in_array;
2121
use function is_array;
22+
use function is_string;
2223

2324
class JWSBuilder
2425
{
@@ -79,8 +80,8 @@ public function withPayload(string $payload, bool $isPayloadDetached = false): s
7980
/**
8081
* Adds the information needed to compute the signature. This method will return a new JWSBuilder object.
8182
*
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
8485
*/
8586
public function addSignature(JWK $signatureKey, array $protectedHeader, array $header = []): self
8687
{
@@ -185,26 +186,25 @@ private function checkB64AndCriticalHeader(array $protectedHeader): void
185186
}
186187

187188
/**
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
190191
* @return MacAlgorithm|SignatureAlgorithm
191192
*/
192193
private function findSignatureAlgorithm(JWK $key, array $protectedHeader, array $header): Algorithm
193194
{
194195
$completeHeader = [...$header, ...$protectedHeader];
195-
if (! array_key_exists('alg', $completeHeader)) {
196+
$alg = $completeHeader['alg'] ?? null;
197+
if (! is_string($alg)) {
196198
throw new InvalidArgumentException('No "alg" parameter set in the header.');
197199
}
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));
203203
}
204204

205-
$algorithm = $this->signatureAlgorithmManager->get($completeHeader['alg']);
205+
$algorithm = $this->signatureAlgorithmManager->get($alg);
206206
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));
208208
}
209209

210210
return $algorithm;

0 commit comments

Comments
 (0)