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
4 changes: 2 additions & 2 deletions lib/src/main/java/com/auth0/jwt/impl/JsonNodeClaim.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public Boolean asBoolean() {

@Override
public Integer asInt() {
return isMissing() || isNull() || !data.isNumber() ? null : data.asInt();
return isMissing() || isNull() || !data.canConvertToInt() ? null : data.asInt();
}

@Override
public Long asLong() {
return isMissing() || isNull() || !data.isNumber() ? null : data.asLong();
return isMissing() || isNull() || !data.canConvertToLong() ? null : data.asLong();
}

@Override
Expand Down
24 changes: 24 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,30 @@ public void shouldValidateCustomClaimOfTypeLong() {
assertThat(jwt, is(notNullValue()));
}

@Test
public void shouldThrowOnInvalidCustomClaimValueOfTypeIntegerOutOfRange() {
// {"name": 4294967419} is 2^32 + 123 and must not be narrowed to 123
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjo0Mjk0OTY3NDE5fQ.GhKOUxKAss6pfnzjz8RNA_RXl2TuMDbYA9EL6WD5Nxo";
IncorrectClaimException e = assertThrows(null, IncorrectClaimException.class, () ->
JWTVerifier.init(Algorithm.HMAC256("secret"))
.withClaim("name", 123)
.build()
.verify(token));
assertThat(e.getClaimName(), is("name"));
}

@Test
public void shouldThrowOnInvalidCustomClaimValueOfTypeLongOutOfRange() {
// {"name": 18446744073709551739} is 2^64 + 123 and must not be narrowed to 123L
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoxODQ0Njc0NDA3MzcwOTU1MTczOX0.JlkpbTCD4Po-jQAEJcdYwJLjqNw8uQz8XcEj5OBLgZY";
IncorrectClaimException e = assertThrows(null, IncorrectClaimException.class, () ->
JWTVerifier.init(Algorithm.HMAC256("secret"))
.withClaim("name", 123L)
.build()
.verify(token));
assertThat(e.getClaimName(), is("name"));
}

@Test
public void shouldValidateCustomClaimOfTypeDouble() {
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoyMy40NX0.7pyX2OmEGaU9q15T8bGFqRm-d3RVTYnqmZNZtxMKSlA";
Expand Down
17 changes: 17 additions & 0 deletions lib/src/test/java/com/auth0/jwt/impl/JsonNodeClaimTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.mockito.ArgumentMatchers;

import java.io.IOException;
import java.math.BigInteger;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -92,6 +93,14 @@ public void shouldGetNullIntIfNotIntValue() {
assertThat(claimFromNode(stringValue).asInt(), is(nullValue()));
}

@Test
public void shouldGetNullIntIfValueOutOfIntRange() {
JsonNode longValue = mapper.valueToTree(Integer.MAX_VALUE + 124L);
assertThat(claimFromNode(longValue).asInt(), is(nullValue()));
JsonNode bigValue = mapper.valueToTree(BigInteger.ONE.shiftLeft(64).add(BigInteger.valueOf(123)));
assertThat(claimFromNode(bigValue).asInt(), is(nullValue()));
}

@Test
public void shouldGetLongValue() {
JsonNode value = mapper.valueToTree(Long.MAX_VALUE);
Expand All @@ -101,6 +110,14 @@ public void shouldGetLongValue() {
assertThat(claim.asLong(), is(Long.MAX_VALUE));
}

@Test
public void shouldGetNullLongIfValueOutOfLongRange() {
JsonNode bigValue = mapper.valueToTree(BigInteger.ONE.shiftLeft(64).add(BigInteger.valueOf(123)));
assertThat(claimFromNode(bigValue).asLong(), is(nullValue()));
JsonNode doubleValue = mapper.valueToTree(1e300);
assertThat(claimFromNode(doubleValue).asLong(), is(nullValue()));
}

@Test
public void shouldGetNullLongIfNotIntValue() {
JsonNode objectValue = mapper.valueToTree(new Object());
Expand Down