Skip to content

Commit 08def5e

Browse files
authored
Merge pull request auth0#297 from complanboy2/issue254
Allow to skip "issued at" validation
2 parents f73f98d + 89400ad commit 08def5e

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

lib/src/main/java/com/auth0/jwt/JWTVerifier.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static class BaseVerification implements Verification {
4343
private final Algorithm algorithm;
4444
private final Map<String, Object> claims;
4545
private long defaultLeeway;
46+
private boolean ignoreIssuedAt;
4647

4748
BaseVerification(Algorithm algorithm) throws IllegalArgumentException {
4849
if (algorithm == null) {
@@ -150,6 +151,14 @@ public Verification acceptIssuedAt(long leeway) throws IllegalArgumentException
150151
return this;
151152
}
152153

154+
/**
155+
* Skip the Issued At ("iat") date verification. By default, the verification is performed.
156+
*/
157+
public Verification ignoreIssuedAt() {
158+
this.ignoreIssuedAt = true;
159+
return this;
160+
}
161+
153162
/**
154163
* Require a specific JWT Id ("jti") claim.
155164
*
@@ -323,6 +332,10 @@ private void addLeewayToDateClaims() {
323332
if (!claims.containsKey(PublicClaims.NOT_BEFORE)) {
324333
claims.put(PublicClaims.NOT_BEFORE, defaultLeeway);
325334
}
335+
if(ignoreIssuedAt) {
336+
claims.remove(PublicClaims.ISSUED_AT);
337+
return;
338+
}
326339
if (!claims.containsKey(PublicClaims.ISSUED_AT)) {
327340
claims.put(PublicClaims.ISSUED_AT, defaultLeeway);
328341
}

lib/src/main/java/com/auth0/jwt/interfaces/Verification.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ public interface Verification {
3737

3838
Verification withArrayClaim(String name, Integer... items) throws IllegalArgumentException;
3939

40+
Verification ignoreIssuedAt();
41+
4042
JWTVerifier build();
4143
}

lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,30 @@ public void shouldThrowOnNegativeNotBeforeLeeway() throws Exception {
478478
.acceptNotBefore(-1);
479479
}
480480

481-
// Issued At
481+
// Issued At with future date
482+
@Test (expected = InvalidClaimException.class)
483+
public void shouldThrowOnFutureIssuedAt() throws Exception {
484+
Clock clock = mock(Clock.class);
485+
when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 1000));
486+
487+
String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE0Nzc1OTJ9.CWq-6pUXl1bFg81vqOUZbZrheO2kUBd2Xr3FUZmvudE";
488+
JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"));
489+
490+
DecodedJWT jwt = verification.build(clock).verify(token);
491+
assertThat(jwt, is(notNullValue()));
492+
}
493+
494+
// Issued At with future date and ignore flag
482495
@Test
483-
public void shouldValidateIssuedAtWithLeeway() throws Exception {
496+
public void shouldSkipIssuedAtVerificationWhenFlagIsPassed() throws Exception {
484497
Clock clock = mock(Clock.class);
485498
when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 1000));
486499

487-
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0Nzc1OTJ9.0WJky9eLN7kuxLyZlmbcXRL3Wy8hLoNCEk5CCl2M4lo";
488-
JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"))
489-
.acceptIssuedAt(2);
490-
DecodedJWT jwt = verification
491-
.build(clock)
492-
.verify(token);
500+
String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE0Nzc1OTJ9.CWq-6pUXl1bFg81vqOUZbZrheO2kUBd2Xr3FUZmvudE";
501+
JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"));
502+
verification.ignoreIssuedAt();
493503

504+
DecodedJWT jwt = verification.build(clock).verify(token);
494505
assertThat(jwt, is(notNullValue()));
495506
}
496507

@@ -508,6 +519,20 @@ public void shouldThrowOnInvalidIssuedAtIfPresent() throws Exception {
508519
.verify(token);
509520
}
510521

522+
@Test
523+
public void shouldOverrideAcceptIssuedAtWhenIgnoreIssuedAtFlagPassedAndSkipTheVerification() throws Exception {
524+
Clock clock = mock(Clock.class);
525+
when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 1000));
526+
527+
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0Nzc1OTJ9.0WJky9eLN7kuxLyZlmbcXRL3Wy8hLoNCEk5CCl2M4lo";
528+
JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"));
529+
DecodedJWT jwt = verification.acceptIssuedAt(20).ignoreIssuedAt()
530+
.build()
531+
.verify(token);
532+
533+
assertThat(jwt, is(notNullValue()));
534+
}
535+
511536
@Test
512537
public void shouldValidateIssuedAtIfPresent() throws Exception {
513538
Clock clock = mock(Clock.class);

0 commit comments

Comments
 (0)