@@ -50,11 +50,17 @@ public static function parseKeySet(array $jwks, string $defaultAlg = null): arra
50
50
$ keys = [];
51
51
52
52
if (!isset ($ jwks ['keys ' ])) {
53
- throw new UnexpectedValueException ('"keys" member must exist in the JWK Set ' );
53
+ throw new UnexpectedValueException (
54
+ '"keys" member must exist in the JWK Set ' ,
55
+ ExceptionCodes::JWK_MISSING_KEYS
56
+ );
54
57
}
55
58
56
59
if (empty ($ jwks ['keys ' ])) {
57
- throw new InvalidArgumentException ('JWK Set did not contain any keys ' );
60
+ throw new InvalidArgumentException (
61
+ 'JWK Set did not contain any keys ' ,
62
+ ExceptionCodes::JWT_KEYS_IS_EMPTY
63
+ );
58
64
}
59
65
60
66
foreach ($ jwks ['keys ' ] as $ k => $ v ) {
@@ -65,7 +71,11 @@ public static function parseKeySet(array $jwks, string $defaultAlg = null): arra
65
71
}
66
72
67
73
if (0 === \count ($ keys )) {
68
- throw new UnexpectedValueException ('No supported algorithms found in JWK Set ' );
74
+ throw new UnexpectedValueException (
75
+ 'No supported algorithms found in JWK Set ' ,
76
+ ExceptionCodes::JWT_ALGORITHM_NOT_SUPPORTED
77
+
78
+ );
69
79
}
70
80
71
81
return $ keys ;
@@ -89,11 +99,17 @@ public static function parseKeySet(array $jwks, string $defaultAlg = null): arra
89
99
public static function parseKey (array $ jwk , string $ defaultAlg = null ): ?Key
90
100
{
91
101
if (empty ($ jwk )) {
92
- throw new InvalidArgumentException ('JWK must not be empty ' );
102
+ throw new InvalidArgumentException (
103
+ 'JWK must not be empty ' ,
104
+ ExceptionCodes::JWK_IS_EMPTY
105
+ );
93
106
}
94
107
95
108
if (!isset ($ jwk ['kty ' ])) {
96
- throw new UnexpectedValueException ('JWK must contain a "kty" parameter ' );
109
+ throw new UnexpectedValueException (
110
+ 'JWK must contain a "kty" parameter ' ,
111
+ ExceptionCodes::JWT_MISSING_KTY_PARAMETER
112
+ );
97
113
}
98
114
99
115
if (!isset ($ jwk ['alg ' ])) {
@@ -102,44 +118,66 @@ public static function parseKey(array $jwk, string $defaultAlg = null): ?Key
102
118
// for parsing in this library. Use the $defaultAlg parameter when parsing the
103
119
// key set in order to prevent this error.
104
120
// @see https://datatracker.ietf.org/doc/html/rfc7517#section-4.4
105
- throw new UnexpectedValueException ('JWK must contain an "alg" parameter ' );
121
+ throw new UnexpectedValueException (
122
+ 'JWK must contain an "alg" parameter ' ,
123
+ ExceptionCodes::JWT_MISSING_ALG_PARAMETER
124
+ );
106
125
}
107
126
$ jwk ['alg ' ] = $ defaultAlg ;
108
127
}
109
128
110
129
switch ($ jwk ['kty ' ]) {
111
130
case 'RSA ' :
112
131
if (!empty ($ jwk ['d ' ])) {
113
- throw new UnexpectedValueException ('RSA private keys are not supported ' );
132
+ throw new UnexpectedValueException (
133
+ 'RSA private keys are not supported ' ,
134
+ ExceptionCodes::JWT_RSA_KEYS_NOT_SUPPORTED
135
+ );
114
136
}
115
137
if (!isset ($ jwk ['n ' ]) || !isset ($ jwk ['e ' ])) {
116
- throw new UnexpectedValueException ('RSA keys must contain values for both "n" and "e" ' );
138
+ throw new UnexpectedValueException (
139
+ 'RSA keys must contain values for both "n" and "e" ' ,
140
+ ExceptionCodes::JWT_RSA_KEYS_MISSING_N_AND_E
141
+ );
117
142
}
118
143
119
144
$ pem = self ::createPemFromModulusAndExponent ($ jwk ['n ' ], $ jwk ['e ' ]);
120
145
$ publicKey = \openssl_pkey_get_public ($ pem );
121
146
if (false === $ publicKey ) {
122
147
throw new DomainException (
123
- 'OpenSSL error: ' . \openssl_error_string ()
148
+ 'OpenSSL error: ' . \openssl_error_string (),
149
+ ExceptionCodes::JWT_OPEN_SSL_ERROR
124
150
);
125
151
}
126
152
return new Key ($ publicKey , $ jwk ['alg ' ]);
127
153
case 'EC ' :
128
154
if (isset ($ jwk ['d ' ])) {
129
155
// The key is actually a private key
130
- throw new UnexpectedValueException ('Key data must be for a public key ' );
156
+ throw new UnexpectedValueException (
157
+ 'Key data must be for a public key ' ,
158
+ ExceptionCodes::JWK_EC_D_IS_NOT_SET
159
+ );
131
160
}
132
161
133
162
if (empty ($ jwk ['crv ' ])) {
134
- throw new UnexpectedValueException ('crv not set ' );
163
+ throw new UnexpectedValueException (
164
+ 'crv not set ' ,
165
+ ExceptionCodes::JWT_EC_CRV_IS_EMPTY
166
+ );
135
167
}
136
168
137
169
if (!isset (self ::EC_CURVES [$ jwk ['crv ' ]])) {
138
- throw new DomainException ('Unrecognised or unsupported EC curve ' );
170
+ throw new DomainException (
171
+ 'Unrecognised or unsupported EC curve ' ,
172
+ ExceptionCodes::JWK_UNSUPPORTED_EC_CURVE
173
+ );
139
174
}
140
175
141
176
if (empty ($ jwk ['x ' ]) || empty ($ jwk ['y ' ])) {
142
- throw new UnexpectedValueException ('x and y not set ' );
177
+ throw new UnexpectedValueException (
178
+ 'x and y not set ' ,
179
+ ExceptionCodes::JWT_X_AND_Y_ARE_EMPTY
180
+ );
143
181
}
144
182
145
183
$ publicKey = self ::createPemFromCrvAndXYCoordinates ($ jwk ['crv ' ], $ jwk ['x ' ], $ jwk ['y ' ]);
0 commit comments