Skip to content

Commit d0358a3

Browse files
committed
Update README, add examples of clock skew and time machine.
1 parent c19225e commit d0358a3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,35 @@ JWT jwt = JWT.getDecoder().decode(encodedJWT, verifier);
174174
assertEquals(jwt.subject, "f1e33ab3-027f-47c5-bb07-8dd8ab37a2d3");
175175
```
176176

177+
#### Verify a JWT adjusting for Clock Skew
178+
```java
179+
// Build an EC verifier using an EC Public Key
180+
Verifier verifier = ECVerifier.newVerifier(Paths.get("public_key.pem"));
181+
182+
// Verify and decode the encoded string JWT to a rich object and allow up to 60 seconds of clock skew when
183+
// asserting the 'exp' and 'nbf' claims if they exist.
184+
JWT jwt = JWT.getDecoder().withClockSkew(60).decode(encodedJWT, verifier);
185+
186+
// Assert the subject of the JWT is as expected
187+
assertEquals(jwt.subject, "f1e33ab3-027f-47c5-bb07-8dd8ab37a2d3");
188+
```
189+
190+
#### Verify an expired JWT in a test case
191+
In a scenario where you may have a hard coded JWT in a test case that you wish to validate, you may use the time machine JWT decoder. Ideally you would not hard code JWTs in your tests and instead generate a new one each time so that the JWT would pass the expiration check. If this is not possible, this option is provided.
192+
```java
193+
// Build an EC verifier using an EC Public Key
194+
Verifier verifier = ECVerifier.newVerifier(Paths.get("public_key.pem"));
195+
196+
// Using the time machine decoder, you may adjust 'now' to any point in the past, or future.
197+
// Note, this is only provided for testing, and should not be used in production.
198+
ZonedDateTime thePast = ZonedDateTime.of(2019, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)
199+
JWT jwt = JWT.getTimeMachineDecoder(thePast).decode(encodedJWT, verifier);
200+
201+
// Assert the subject of the JWT is as expected
202+
assertEquals(jwt.subject, "f1e33ab3-027f-47c5-bb07-8dd8ab37a2d3");
203+
```
204+
205+
177206
### Build a Signer, or a Verifier using a provided CryptoProvider
178207

179208
This pattern is available on the HMAC, RSA and EC verifier and signers.

0 commit comments

Comments
 (0)