@@ -50,11 +50,17 @@ public static function parseKeySet(array $jwks, string $defaultAlg = null): arra
5050 $ keys = [];
5151
5252 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+ );
5457 }
5558
5659 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+ );
5864 }
5965
6066 foreach ($ jwks ['keys ' ] as $ k => $ v ) {
@@ -65,7 +71,11 @@ public static function parseKeySet(array $jwks, string $defaultAlg = null): arra
6571 }
6672
6773 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+ );
6979 }
7080
7181 return $ keys ;
@@ -89,11 +99,17 @@ public static function parseKeySet(array $jwks, string $defaultAlg = null): arra
8999 public static function parseKey (array $ jwk , string $ defaultAlg = null ): ?Key
90100 {
91101 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+ );
93106 }
94107
95108 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+ );
97113 }
98114
99115 if (!isset ($ jwk ['alg ' ])) {
@@ -102,44 +118,66 @@ public static function parseKey(array $jwk, string $defaultAlg = null): ?Key
102118 // for parsing in this library. Use the $defaultAlg parameter when parsing the
103119 // key set in order to prevent this error.
104120 // @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+ );
106125 }
107126 $ jwk ['alg ' ] = $ defaultAlg ;
108127 }
109128
110129 switch ($ jwk ['kty ' ]) {
111130 case 'RSA ' :
112131 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+ );
114136 }
115137 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+ );
117142 }
118143
119144 $ pem = self ::createPemFromModulusAndExponent ($ jwk ['n ' ], $ jwk ['e ' ]);
120145 $ publicKey = \openssl_pkey_get_public ($ pem );
121146 if (false === $ publicKey ) {
122147 throw new DomainException (
123- 'OpenSSL error: ' . \openssl_error_string ()
148+ 'OpenSSL error: ' . \openssl_error_string (),
149+ ExceptionCodes::JWT_OPEN_SSL_ERROR
124150 );
125151 }
126152 return new Key ($ publicKey , $ jwk ['alg ' ]);
127153 case 'EC ' :
128154 if (isset ($ jwk ['d ' ])) {
129155 // 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+ );
131160 }
132161
133162 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+ );
135167 }
136168
137169 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+ );
139174 }
140175
141176 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+ );
143181 }
144182
145183 $ publicKey = self ::createPemFromCrvAndXYCoordinates ($ jwk ['crv ' ], $ jwk ['x ' ], $ jwk ['y ' ]);
0 commit comments