Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ private static long instantToSeconds(Instant instant) {
}

private static long dateToSeconds(Date date) {
return date.getTime() / 1000;
// Floor (not truncate) so dates before the Unix epoch round toward
// negative infinity, matching Instant#getEpochSecond. (GH-806)
return Math.floorDiv(date.getTime(), 1000L);
}
}
30 changes: 30 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,36 @@ public void shouldAddExpiresAtInstant() {
assertThat(TokenUtils.splitToken(signed)[1], is("eyJleHAiOjE0Nzc1OTJ9"));
}

@Test
public void shouldAddExpiresAtBeforeEpochConsistentlyWithInstant() {
// Dates before the Unix epoch must floor to the previous second,
// matching the Instant serialization (GH-806).
Date date = new Date(-500);
Instant instant = date.toInstant();

String signedDate = JWTCreator.init()
.withExpiresAt(date)
.sign(Algorithm.HMAC256("secret"));
String signedInstant = JWTCreator.init()
.withExpiresAt(instant)
.sign(Algorithm.HMAC256("secret"));

assertThat(signedDate, is(notNullValue()));
assertThat(signedInstant, is(notNullValue()));
assertThat(TokenUtils.splitToken(signedDate)[1], is("eyJleHAiOi0xfQ"));
assertThat(TokenUtils.splitToken(signedDate)[1], is(TokenUtils.splitToken(signedInstant)[1]));
}

@Test
public void shouldAddNotBeforeBeforeEpoch() {
String signed = JWTCreator.init()
.withNotBefore(new Date(-500))
.sign(Algorithm.HMAC256("secret"));

assertThat(signed, is(notNullValue()));
assertThat(TokenUtils.splitToken(signed)[1], is("eyJuYmYiOi0xfQ"));
}

@Test
public void shouldAddNotBefore() {
String signed = JWTCreator.init()
Expand Down