Skip to content

Commit f5cf048

Browse files
authored
Merge pull request auth0#155 from Spyna/master
Add token expiration dedicated exception
2 parents bf0a030 + e5141f3 commit f5cf048

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.auth0.jwt.exceptions.InvalidClaimException;
66
import com.auth0.jwt.exceptions.JWTVerificationException;
77
import com.auth0.jwt.exceptions.SignatureVerificationException;
8+
import com.auth0.jwt.exceptions.TokenExpiredException;
89
import com.auth0.jwt.impl.PublicClaims;
910
import com.auth0.jwt.interfaces.Claim;
1011
import com.auth0.jwt.interfaces.Clock;
@@ -417,23 +418,31 @@ private void assertValidStringClaim(String claimName, String value, String expec
417418
}
418419

419420
private void assertValidDateClaim(Date date, long leeway, boolean shouldBeFuture) {
420-
Date today = clock.getToday();
421-
today.setTime((long) Math.floor((today.getTime() / 1000) * 1000)); //truncate millis
422-
boolean isValid;
423-
String errMessage;
424-
if (shouldBeFuture) {
425-
today.setTime(today.getTime() - leeway * 1000);
426-
isValid = date == null || !today.after(date);
427-
errMessage = String.format("The Token has expired on %s.", date);
428-
} else {
429-
today.setTime(today.getTime() + leeway * 1000);
430-
isValid = date == null || !today.before(date);
431-
errMessage = String.format("The Token can't be used before %s.", date);
432-
}
433-
if (!isValid) {
434-
throw new InvalidClaimException(errMessage);
435-
}
436-
}
421+
Date today = clock.getToday();
422+
today.setTime((long) Math.floor((today.getTime() / 1000) * 1000)); // truncate
423+
// millis
424+
if (shouldBeFuture) {
425+
assertDateIsFuture(date, leeway, today);
426+
} else {
427+
assertDateIsPast(date, leeway, today);
428+
}
429+
}
430+
431+
private void assertDateIsFuture(Date date, long leeway, Date today) {
432+
433+
today.setTime(today.getTime() - leeway * 1000);
434+
if (date != null && today.after(date)) {
435+
throw new TokenExpiredException(String.format("The Token has expired on %s.", date));
436+
}
437+
}
438+
439+
private void assertDateIsPast(Date date, long leeway, Date today) {
440+
today.setTime(today.getTime() + leeway * 1000);
441+
if(date!=null && today.before(date)) {
442+
throw new InvalidClaimException(String.format("The Token can't be used before %s.", date));
443+
}
444+
445+
}
437446

438447
private void assertValidAudienceClaim(List<String> audience, List<String> value) {
439448
if (audience == null || !audience.containsAll(value)) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.auth0.jwt.exceptions;
2+
3+
public class TokenExpiredException extends JWTVerificationException {
4+
5+
private static final long serialVersionUID = -7076928975713577708L;
6+
7+
public TokenExpiredException(String message) {
8+
super(message);
9+
}
10+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.auth0.jwt.algorithms.Algorithm;
44
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
55
import com.auth0.jwt.exceptions.InvalidClaimException;
6+
import com.auth0.jwt.exceptions.TokenExpiredException;
67
import com.auth0.jwt.interfaces.Clock;
78
import com.auth0.jwt.interfaces.DecodedJWT;
89
import org.junit.Rule;
@@ -392,7 +393,7 @@ public void shouldValidateExpiresAtIfPresent() throws Exception {
392393

393394
@Test
394395
public void shouldThrowOnInvalidExpiresAtIfPresent() throws Exception {
395-
exception.expect(InvalidClaimException.class);
396+
exception.expect(TokenExpiredException.class);
396397
exception.expectMessage(startsWith("The Token has expired on"));
397398
Clock clock = mock(Clock.class);
398399
when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE + 1000));

0 commit comments

Comments
 (0)