Skip to content

Commit

Permalink
jwtk#60 Add validation tests for JWT expiration
Browse files Browse the repository at this point in the history
Two new tests have been added to DefaultJwtParserTest to validate JWT expiration behavior. The tests ensure that for the 'expireAfter()' method, duration must be more than 0 and timeUnit cannot be null. The error messages for these validation checks have also been modified for clarity.
  • Loading branch information
pveeckhout committed Dec 25, 2023
1 parent ff6e2df commit e91b042
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ public JwtBuilder id(String jti) {

@Override
public JwtBuilder expireAfter(long duration, TimeUnit timeUnit) { // TODO: use java.time for version 1.0?
Assert.state(duration > 0, "duration must be a positive value.");
Assert.stateNotNull(timeUnit, "timeUnit is required.");
Assert.gt(duration, 0L, "duration must be > 0.");
Assert.notNull(timeUnit, "timeUnit cannot be null.");

Date exp = Optional.ofNullable(this.claimsBuilder.get(DefaultClaims.ISSUED_AT))
.map(Date::getTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,31 @@ class DefaultJwtParserTest {
}
}

@Test
void testExpiredAfterDurationValidationMessage() {
def duration = -1L
def timeUnit = TimeUnit.MINUTES
try {
Jwts.builder().expireAfter(duration, timeUnit).compact()
} catch (IllegalArgumentException expected) {
String msg = "duration must be > 0."
assertEquals msg, expected.message
}
}

@Test
void testExpiredAfterTimeUnitValidationMessage() {
def duration = 15L
def timeUnit = null
try {
Jwts.builder().expireAfter(duration, timeUnit).compact()
} catch (IllegalArgumentException expected) {
String msg = "timeUnit cannot be null."
assertEquals msg, expected.message
}
}


@Test
void testExpiredAfterExceptionMessage() {
long differenceMillis = 781 // arbitrary, anything > 0 is fine
Expand All @@ -293,7 +318,7 @@ class DefaultJwtParserTest {
def exp8601 = DateFormats.formatIso8601(expectedExpiry, true)
def later8601 = DateFormats.formatIso8601(later, true)
String msg = "JWT expired ${differenceMillis} milliseconds ago at ${exp8601}. " +
"Current time: ${later8601}. Allowed clock skew: 0 milliseconds.";
"Current time: ${later8601}. Allowed clock skew: 0 milliseconds."
assertEquals msg, expected.message
}
}
Expand All @@ -317,7 +342,7 @@ class DefaultJwtParserTest {
def exp8601 = DateFormats.formatIso8601(expectedExpiry, true)
def later8601 = DateFormats.formatIso8601(later, true)
String msg = "JWT expired ${differenceMillis} milliseconds ago at ${exp8601}. " +
"Current time: ${later8601}. Allowed clock skew: 0 milliseconds.";
"Current time: ${later8601}. Allowed clock skew: 0 milliseconds."
assertEquals msg, expected.message
}
}
Expand All @@ -336,7 +361,7 @@ class DefaultJwtParserTest {
def nbf8601 = DateFormats.formatIso8601(nbf, true)
def earlier8601 = DateFormats.formatIso8601(earlier, true)
String msg = "JWT early by ${differenceMillis} milliseconds before ${nbf8601}. " +
"Current time: ${earlier8601}. Allowed clock skew: 0 milliseconds.";
"Current time: ${earlier8601}. Allowed clock skew: 0 milliseconds."
assertEquals msg, expected.message
}
}
Expand Down

0 comments on commit e91b042

Please sign in to comment.