Skip to content

Commit 1cd2137

Browse files
authored
chore(docs): add examples for exception handling (#463)
1 parent 2e07d8a commit 1cd2137

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,56 @@ $decoded = JWT::decode($jwt, $keySet);
245245
Miscellaneous
246246
-------------
247247

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+
248298
#### Casting to array
249299

250300
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
269319
#### 6.2.0 / 2022-05-14
270320

271321
- 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)).
273323

274324
#### 6.1.0 / 2022-03-23
275325

src/JWT.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class JWT
7676
*
7777
* @return stdClass The JWT's payload as a PHP object
7878
*
79-
* @throws InvalidArgumentException Provided key/key-array was empty
79+
* @throws InvalidArgumentException Provided key/key-array was empty or malformed
8080
* @throws DomainException Provided JWT is malformed
8181
* @throws UnexpectedValueException Provided JWT was invalid
8282
* @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed

0 commit comments

Comments
 (0)