Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added date validation dedicated exception #155

Merged
merged 5 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions lib/src/main/java/com/auth0/jwt/JWTVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.impl.PublicClaims;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.Clock;
Expand Down Expand Up @@ -417,23 +418,31 @@ private void assertValidStringClaim(String claimName, String value, String expec
}

private void assertValidDateClaim(Date date, long leeway, boolean shouldBeFuture) {
Date today = clock.getToday();
today.setTime((long) Math.floor((today.getTime() / 1000) * 1000)); //truncate millis
boolean isValid;
String errMessage;
if (shouldBeFuture) {
today.setTime(today.getTime() - leeway * 1000);
isValid = date == null || !today.after(date);
errMessage = String.format("The Token has expired on %s.", date);
} else {
today.setTime(today.getTime() + leeway * 1000);
isValid = date == null || !today.before(date);
errMessage = String.format("The Token can't be used before %s.", date);
}
if (!isValid) {
throw new InvalidClaimException(errMessage);
}
}
Date today = clock.getToday();
today.setTime((long) Math.floor((today.getTime() / 1000) * 1000)); // truncate
// millis
if (shouldBeFuture) {
assertDateIsFuture(date, leeway, today);
} else {
assertDateIsPast(date, leeway, today);
}
}

private void assertDateIsFuture(Date date, long leeway, Date today) {

today.setTime(today.getTime() - leeway * 1000);
if (date != null && today.after(date)) {
throw new TokenExpiredException(String.format("The Token has expired on %s.", date));
}
}

private void assertDateIsPast(Date date, long leeway, Date today) {
today.setTime(today.getTime() + leeway * 1000);
if(date!=null && today.before(date)) {
throw new InvalidClaimException(String.format("The Token can't be used before %s.", date));
}

}

private void assertValidAudienceClaim(List<String> audience, List<String> value) {
if (audience == null || !audience.containsAll(value)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.auth0.jwt.exceptions;

public class TokenExpiredException extends JWTVerificationException {

private static final long serialVersionUID = -7076928975713577708L;

public TokenExpiredException(String message) {
super(message);
}
}
3 changes: 2 additions & 1 deletion lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.Clock;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.junit.Rule;
Expand Down Expand Up @@ -392,7 +393,7 @@ public void shouldValidateExpiresAtIfPresent() throws Exception {

@Test
public void shouldThrowOnInvalidExpiresAtIfPresent() throws Exception {
exception.expect(InvalidClaimException.class);
exception.expect(TokenExpiredException.class);
exception.expectMessage(startsWith("The Token has expired on"));
Clock clock = mock(Clock.class);
when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE + 1000));
Expand Down