Skip to content

Commit

Permalink
Add unit tests and handling of integer vs long datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber committed Jan 29, 2019
1 parent 07f8401 commit e9c3040
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/src/main/java/com/auth0/jwt/JWTVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,23 @@ private void assertValidClaim(Claim claim, String claimName, Object value) {
} else if (value instanceof Date) {
isValid = value.equals(claim.asDate());
} else if (value instanceof Object[]) {
List<Object> claimArr = Arrays.asList(claim.as(Object[].class));
List<Object> claimArr;
Object[] claimAsObject = claim.as(Object[].class);

// Jackson uses 'natural' mapping which uses Integer if value fits in 32 bits.
if(value instanceof Long[]) {
// convert Integers to Longs for comparison with equals
claimArr = new ArrayList<>(claimAsObject.length);
for(Object cao : claimAsObject) {
if(cao instanceof Integer) {
claimArr.add(((Integer)cao).longValue());
} else {
claimArr.add(cao);
}
}
} else {
claimArr = Arrays.asList(claim.as(Object[].class));
}
List<Object> valueArr = Arrays.asList((Object[]) value);
isValid = claimArr.containsAll(valueArr);
}
Expand Down
32 changes: 32 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,38 @@ public void shouldValidateCustomArrayClaimOfTypeInteger() throws Exception {
assertThat(jwt, is(notNullValue()));
}

@Test
public void shouldValidateCustomArrayClaimOfTypeLong() throws Exception {
String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbNTAwMDAwMDAwMDAxLDUwMDAwMDAwMDAwMiw1MDAwMDAwMDAwMDNdfQ.vzV7S0gbV9ZAVxChuIt4XZuSVTxMH536rFmoHzxmayM";
DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret"))
.withArrayClaim("name", 500000000001L, 500000000002L, 500000000003L)
.build()
.verify(token);

assertThat(jwt, is(notNullValue()));
}

@Test
public void shouldValidateCustomArrayClaimOfTypeLongWhenValueIsInteger() throws Exception {
String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbMSwyLDNdfQ.UEuMKRQYrzKAiPpPLhIVawWkKWA1zj0_GderrWUIyFE";
DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret"))
.withArrayClaim("name", 1L, 2L, 3L)
.build()
.verify(token);

assertThat(jwt, is(notNullValue()));
}

@Test
public void shouldValidateCustomArrayClaimOfTypeLongWhenValueIsIntegerAndLong() throws Exception {
String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbMSw1MDAwMDAwMDAwMDIsNTAwMDAwMDAwMDAzXX0.PQjb2rPPpYjM2sItZEzZcjS2YbfPCp6xksTSPjpjTQA";
DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret"))
.withArrayClaim("name", 1L, 500000000002L, 500000000003L)
.build()
.verify(token);

assertThat(jwt, is(notNullValue()));
}
// Generic Delta
@SuppressWarnings("RedundantCast")
@Test
Expand Down

0 comments on commit e9c3040

Please sign in to comment.