Skip to content

Commit 0541cba

Browse files
authored
feat!: update return type for JWK methods (#392)
1 parent 8699eb9 commit 0541cba

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ use Firebase\JWT\JWT;
198198
// this endpoint: https://www.gstatic.com/iap/verify/public_key-jwk
199199
$jwks = ['keys' => []];
200200

201-
// JWK::parseKeySet($jwks) returns an associative array of **kid** to private
202-
// key. Pass this as the second parameter to JWT::decode.
201+
// JWK::parseKeySet($jwks) returns an associative array of **kid** to Firebase\JWT\Key
202+
// objects. Pass this as the second parameter to JWT::decode.
203203
JWT::decode($payload, JWK::parseKeySet($jwks));
204204
```
205205

@@ -208,6 +208,7 @@ Changelog
208208

209209
#### 6.0.0 / 2022-01-24
210210

211+
- **Backwards-Compatibility Breaking Changes**: See the [Release Notes](https://github.com/firebase/php-jwt/releases/tag/v5.5.1) for more information.
211212
- New Key object to prevent key/algorithm type confusion (#365)
212213
- Add JWK support (#273)
213214
- Add ES256 support (#256)

src/JWK.php

+10-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class JWK
2525
*
2626
* @param array $jwks The JSON Web Key Set as an associative array
2727
*
28-
* @return array An associative array that represents the set of keys
28+
* @return array<string, Key> An associative array of key IDs (kid) to Key objects
2929
*
3030
* @throws InvalidArgumentException Provided JWK Set is empty
3131
* @throws UnexpectedValueException Provided JWK Set was invalid
@@ -47,15 +47,7 @@ public static function parseKeySet(array $jwks)
4747
foreach ($jwks['keys'] as $k => $v) {
4848
$kid = isset($v['kid']) ? $v['kid'] : $k;
4949
if ($key = self::parseKey($v)) {
50-
if (isset($v['alg'])) {
51-
$keys[$kid] = new Key($key, $v['alg']);
52-
} else {
53-
// The "alg" parameter is optional in a KTY, but is required
54-
// for parsing in this library. Add it manually to your JWK
55-
// array if it doesn't already exist.
56-
// @see https://datatracker.ietf.org/doc/html/rfc7517#section-4.4
57-
throw new InvalidArgumentException('JWK key is missing "alg"');
58-
}
50+
$keys[$kid] = $key;
5951
}
6052
}
6153

@@ -71,7 +63,7 @@ public static function parseKeySet(array $jwks)
7163
*
7264
* @param array $jwk An individual JWK
7365
*
74-
* @return resource|array An associative array that represents the key
66+
* @return Key The key object for the JWK
7567
*
7668
* @throws InvalidArgumentException Provided JWK is empty
7769
* @throws UnexpectedValueException Provided JWK was invalid
@@ -87,6 +79,12 @@ public static function parseKey(array $jwk)
8779
if (!isset($jwk['kty'])) {
8880
throw new UnexpectedValueException('JWK must contain a "kty" parameter');
8981
}
82+
if (!isset($jwk['alg'])) {
83+
// The "alg" parameter is optional in a KTY, but is required for parsing in
84+
// this library. Add it manually to your JWK array if it doesn't already exist.
85+
// @see https://datatracker.ietf.org/doc/html/rfc7517#section-4.4
86+
throw new UnexpectedValueException('JWK must contain an "alg" parameter');
87+
}
9088

9189
switch ($jwk['kty']) {
9290
case 'RSA':
@@ -104,7 +102,7 @@ public static function parseKey(array $jwk)
104102
'OpenSSL error: ' . \openssl_error_string()
105103
);
106104
}
107-
return $publicKey;
105+
return new Key($publicKey, $jwk['alg']);
108106
default:
109107
// Currently only RSA is supported
110108
break;

src/JWT.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class JWT
6363
* Decodes a JWT string into a PHP object.
6464
*
6565
* @param string $jwt The JWT
66-
* @param Key|array<Key> $keyOrKeyArray The Key or array of Key objects.
66+
* @param Key|array<string, Key> $keyOrKeyArray The Key or associative array of key IDs (kid) to Key objects.
6767
* If the algorithm used is asymmetric, this is the public key
6868
* Each Key object contains an algorithm and matching key.
6969
* Supported algorithms are 'ES384','ES256', 'HS256', 'HS384',
@@ -381,8 +381,8 @@ public static function urlsafeB64Encode($input)
381381
/**
382382
* Determine if an algorithm has been provided for each Key
383383
*
384-
* @param Key|array<Key>|mixed $keyOrKeyArray
385-
* @param string|null $kid
384+
* @param Key|array<string, Key> $keyOrKeyArray
385+
* @param string|null $kid
386386
*
387387
* @throws UnexpectedValueException
388388
*

tests/JWKTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testInvalidAlgorithm()
2828
'No supported algorithms found in JWK Set'
2929
);
3030

31-
$badJwk = array('kty' => 'BADALG');
31+
$badJwk = array('kty' => 'BADALG', 'alg' => 'RSA256');
3232
$keys = JWK::parseKeySet(array('keys' => array($badJwk)));
3333
}
3434

@@ -51,8 +51,8 @@ public function testParsePrivateKey()
5151
public function testParsePrivateKeyWithoutAlg()
5252
{
5353
$this->setExpectedException(
54-
'InvalidArgumentException',
55-
'JWK key is missing "alg"'
54+
'UnexpectedValueException',
55+
'JWK must contain an "alg" parameter'
5656
);
5757

5858
$jwkSet = json_decode(

0 commit comments

Comments
 (0)