@@ -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 ;
0 commit comments