|
5 | 5 | import com.auth0.jwt.exceptions.InvalidClaimException; |
6 | 6 | import com.auth0.jwt.exceptions.JWTVerificationException; |
7 | 7 | import com.auth0.jwt.exceptions.SignatureVerificationException; |
| 8 | +import com.auth0.jwt.exceptions.TokenExpiredException; |
8 | 9 | import com.auth0.jwt.impl.PublicClaims; |
9 | 10 | import com.auth0.jwt.interfaces.Claim; |
10 | 11 | import com.auth0.jwt.interfaces.Clock; |
@@ -417,23 +418,31 @@ private void assertValidStringClaim(String claimName, String value, String expec |
417 | 418 | } |
418 | 419 |
|
419 | 420 | 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 | + } |
437 | 446 |
|
438 | 447 | private void assertValidAudienceClaim(List<String> audience, List<String> value) { |
439 | 448 | if (audience == null || !audience.containsAll(value)) { |
|
0 commit comments