@@ -245,6 +245,56 @@ $decoded = JWT::decode($jwt, $keySet);
245
245
Miscellaneous
246
246
-------------
247
247
248
+ #### Exception Handling
249
+
250
+ When a call to ` JWT::decode ` is invalid, it will throw one of the following exceptions:
251
+
252
+ ``` php
253
+ use Firebase\JWT\JWT;
254
+ use Firebase\JWT\SignatureInvalidException;
255
+ use Firebase\JWT\BeforeValidException;
256
+ use Firebase\JWT\ExpiredException;
257
+ use DomainException;
258
+ use InvalidArgumentException;
259
+ use UnexpectedValueException;
260
+
261
+ try {
262
+ $decoded = JWT::decode($payload, $keys);
263
+ } catch (InvalidArgumentException $e) {
264
+ // provided key/key-array is empty or malformed.
265
+ } catch (DomainException $e) {
266
+ // provided algorithm is unsupported OR
267
+ // provided key is invalid OR
268
+ // unknown error thrown in openSSL or libsodium OR
269
+ // libsodium is required but not available.
270
+ } catch (SignatureInvalidException $e) {
271
+ // provided JWT signature verification failed.
272
+ } catch (BeforeValidException $e) {
273
+ // provided JWT is trying to be used before "nbf" claim OR
274
+ // provided JWT is trying to be used before "iat" claim.
275
+ } catch (ExpiredException $e) {
276
+ // provided JWT is trying to be used after "exp" claim.
277
+ } catch (UnexpectedValueException $e) {
278
+ // provided JWT is malformed OR
279
+ // provided JWT is missing an algorithm / using an unsupported algorithm OR
280
+ // provided JWT algorithm does not match provided key OR
281
+ // provided key ID in key/key-array is empty or invalid.
282
+ }
283
+ ```
284
+
285
+ All exceptions in the ` Firebase\JWT ` namespace extend ` UnexpectedValueException ` , and can be simplified
286
+ like this:
287
+
288
+ ``` php
289
+ try {
290
+ $decoded = JWT::decode($payload, $keys);
291
+ } catch (LogicException $e) {
292
+ // errors having to do with environmental setup or malformed JWT Keys
293
+ } catch (UnexpectedValueException $e) {
294
+ // errors having to do with JWT signature and claims
295
+ }
296
+ ```
297
+
248
298
#### Casting to array
249
299
250
300
The return value of ` JWT::decode ` is the generic PHP object ` stdClass ` . If you'd like to handle with arrays
@@ -269,7 +319,7 @@ Changelog
269
319
#### 6.2.0 / 2022-05-14
270
320
271
321
- Added ` CachedKeySet ` ([ #397 ] ( https://github.com/firebase/php-jwt/pull/397 ) )
272
- - Added ` $defaultAlg ` parameter to ` JWT::parseKey ` and ` JWT::parseKeySet ` ([ #426 ] ( https://github.com/firebase/php-jwt/pull/426 ) ).
322
+ - Added ` $defaultAlg ` parameter to ` JWT::parseKey ` and ` JWT::parseKeySet ` ([ #426 ] ( https://github.com/firebase/php-jwt/pull/426 ) ).
273
323
274
324
#### 6.1.0 / 2022-03-23
275
325
0 commit comments