@@ -24,6 +24,8 @@ class JWK
24
24
* Parse a set of JWK keys
25
25
*
26
26
* @param array<mixed> $jwks The JSON Web Key Set as an associative array
27
+ * @param string $defaultAlg The algorithm for the Key object if "alg" is not set in the
28
+ * JSON Web Key Set
27
29
*
28
30
* @return array<string, Key> An associative array of key IDs (kid) to Key objects
29
31
*
@@ -33,7 +35,7 @@ class JWK
33
35
*
34
36
* @uses parseKey
35
37
*/
36
- public static function parseKeySet (array $ jwks ): array
38
+ public static function parseKeySet (array $ jwks, string $ defaultAlg = null ): array
37
39
{
38
40
$ keys = [];
39
41
@@ -47,7 +49,7 @@ public static function parseKeySet(array $jwks): array
47
49
48
50
foreach ($ jwks ['keys ' ] as $ k => $ v ) {
49
51
$ kid = isset ($ v ['kid ' ]) ? $ v ['kid ' ] : $ k ;
50
- if ($ key = self ::parseKey ($ v )) {
52
+ if ($ key = self ::parseKey ($ v, $ defaultAlg )) {
51
53
$ keys [(string ) $ kid ] = $ key ;
52
54
}
53
55
}
@@ -63,6 +65,8 @@ public static function parseKeySet(array $jwks): array
63
65
* Parse a JWK key
64
66
*
65
67
* @param array<mixed> $jwk An individual JWK
68
+ * @param string $defaultAlg The algorithm for the Key object if "alg" is not set in the
69
+ * JSON Web Key Set
66
70
*
67
71
* @return Key The key object for the JWK
68
72
*
@@ -72,7 +76,7 @@ public static function parseKeySet(array $jwks): array
72
76
*
73
77
* @uses createPemFromModulusAndExponent
74
78
*/
75
- public static function parseKey (array $ jwk ): ?Key
79
+ public static function parseKey (array $ jwk, string $ defaultAlg = null ): ?Key
76
80
{
77
81
if (empty ($ jwk )) {
78
82
throw new InvalidArgumentException ('JWK must not be empty ' );
@@ -83,10 +87,14 @@ public static function parseKey(array $jwk): ?Key
83
87
}
84
88
85
89
if (!isset ($ jwk ['alg ' ])) {
86
- // The "alg" parameter is optional in a KTY, but is required for parsing in
87
- // this library. Add it manually to your JWK array if it doesn't already exist.
88
- // @see https://datatracker.ietf.org/doc/html/rfc7517#section-4.4
89
- throw new UnexpectedValueException ('JWK must contain an "alg" parameter ' );
90
+ if (\is_null ($ defaultAlg )) {
91
+ // The "alg" parameter is optional in a KTY, but an algorithm is required
92
+ // for parsing in this library. Use the $defaultAlg parameter when parsing the
93
+ // key set in order to prevent this error.
94
+ // @see https://datatracker.ietf.org/doc/html/rfc7517#section-4.4
95
+ throw new UnexpectedValueException ('JWK must contain an "alg" parameter ' );
96
+ }
97
+ $ jwk ['alg ' ] = $ defaultAlg ;
90
98
}
91
99
92
100
switch ($ jwk ['kty ' ]) {
0 commit comments