From d27b1215ef1652f5563f47243f4b093615c187de Mon Sep 17 00:00:00 2001 From: vrancic Date: Fri, 31 Mar 2017 00:43:51 +0200 Subject: [PATCH 1/3] Long type is missing for claims. I added Long type for claims and updated tests. --- .../main/java/com/auth0/jwt/JWTVerifier.java | 2 ++ .../java/com/auth0/jwt/impl/JsonNodeClaim.java | 3 +++ .../main/java/com/auth0/jwt/impl/NullClaim.java | 5 +++++ .../java/com/auth0/jwt/interfaces/Claim.java | 8 ++++++++ .../com/auth0/jwt/impl/JsonNodeClaimTest.java | 17 +++++++++++++++++ 5 files changed, 35 insertions(+) diff --git a/lib/src/main/java/com/auth0/jwt/JWTVerifier.java b/lib/src/main/java/com/auth0/jwt/JWTVerifier.java index 0ee93f76..46f26e98 100644 --- a/lib/src/main/java/com/auth0/jwt/JWTVerifier.java +++ b/lib/src/main/java/com/auth0/jwt/JWTVerifier.java @@ -393,6 +393,8 @@ private void assertValidClaim(Claim claim, String claimName, Object value) { isValid = value.equals(claim.asString()); } else if (value instanceof Integer) { isValid = value.equals(claim.asInt()); + } else if (value instanceof Long) { + isValid = value.equals(claim.asLong()); } else if (value instanceof Boolean) { isValid = value.equals(claim.asBoolean()); } else if (value instanceof Double) { diff --git a/lib/src/main/java/com/auth0/jwt/impl/JsonNodeClaim.java b/lib/src/main/java/com/auth0/jwt/impl/JsonNodeClaim.java index 317b4946..67355331 100644 --- a/lib/src/main/java/com/auth0/jwt/impl/JsonNodeClaim.java +++ b/lib/src/main/java/com/auth0/jwt/impl/JsonNodeClaim.java @@ -34,6 +34,9 @@ public Integer asInt() { return !data.isNumber() ? null : data.asInt(); } + @Override + public Long asLong() { return !data.isNumber() ? null : data.asLong(); } + @Override public Double asDouble() { return !data.isNumber() ? null : data.asDouble(); diff --git a/lib/src/main/java/com/auth0/jwt/impl/NullClaim.java b/lib/src/main/java/com/auth0/jwt/impl/NullClaim.java index 755ef64d..e403100e 100644 --- a/lib/src/main/java/com/auth0/jwt/impl/NullClaim.java +++ b/lib/src/main/java/com/auth0/jwt/impl/NullClaim.java @@ -25,6 +25,11 @@ public Integer asInt() { return null; } + @Override + public Long asLong() { + return null; + } + @Override public Double asDouble() { return null; diff --git a/lib/src/main/java/com/auth0/jwt/interfaces/Claim.java b/lib/src/main/java/com/auth0/jwt/interfaces/Claim.java index 28c158f1..1b58f9ca 100644 --- a/lib/src/main/java/com/auth0/jwt/interfaces/Claim.java +++ b/lib/src/main/java/com/auth0/jwt/interfaces/Claim.java @@ -28,6 +28,14 @@ public interface Claim { */ Integer asInt(); + /** + * Get this Claim as an Long. + * If the value isn't of type Long or it can't be converted to an Long, null will be returned. + * + * @return the value as an Long or null. + */ + Long asLong(); + /** * Get this Claim as a Double. * If the value isn't of type Double or it can't be converted to a Double, null will be returned. diff --git a/lib/src/test/java/com/auth0/jwt/impl/JsonNodeClaimTest.java b/lib/src/test/java/com/auth0/jwt/impl/JsonNodeClaimTest.java index 7a5dbc35..0ee0c71b 100644 --- a/lib/src/test/java/com/auth0/jwt/impl/JsonNodeClaimTest.java +++ b/lib/src/test/java/com/auth0/jwt/impl/JsonNodeClaimTest.java @@ -69,6 +69,23 @@ public void shouldGetNullIntIfNotIntValue() throws Exception { assertThat(claimFromNode(stringValue).asInt(), is(nullValue())); } + @Test + public void shouldGetLongValue() throws Exception { + JsonNode value = mapper.valueToTree(Long.MAX_VALUE); + Claim claim = claimFromNode(value); + + assertThat(claim.asLong(), is(notNullValue())); + assertThat(claim.asLong(), is(Long.MAX_VALUE)); + } + + @Test + public void shouldGetNullLongIfNotIntValue() throws Exception { + JsonNode objectValue = mapper.valueToTree(new Object()); + assertThat(claimFromNode(objectValue).asLong(), is(nullValue())); + JsonNode stringValue = mapper.valueToTree("" + Long.MAX_VALUE); + assertThat(claimFromNode(stringValue).asLong(), is(nullValue())); + } + @Test public void shouldGetDoubleValue() throws Exception { JsonNode value = mapper.valueToTree(1.5); From 6825cd3b549ddf249d3458494c4e47827c2d1f56 Mon Sep 17 00:00:00 2001 From: vrancic Date: Tue, 4 Apr 2017 00:07:49 +0300 Subject: [PATCH 2/3] Tests for Long type updated. --- lib/src/main/java/com/auth0/jwt/JWTVerifier.java | 15 +++++++++++++++ .../com/auth0/jwt/interfaces/Verification.java | 2 ++ .../test/java/com/auth0/jwt/JWTVerifierTest.java | 11 +++++++++++ .../java/com/auth0/jwt/impl/NullClaimTest.java | 5 +++++ 4 files changed, 33 insertions(+) diff --git a/lib/src/main/java/com/auth0/jwt/JWTVerifier.java b/lib/src/main/java/com/auth0/jwt/JWTVerifier.java index 46f26e98..3d00ba59 100644 --- a/lib/src/main/java/com/auth0/jwt/JWTVerifier.java +++ b/lib/src/main/java/com/auth0/jwt/JWTVerifier.java @@ -197,6 +197,21 @@ public Verification withClaim(String name, Integer value) throws IllegalArgument return this; } + /** + * Require a specific Claim value. + * + * @param name the Claim's name. + * @param value the Claim's value. + * @return this same Verification instance. + * @throws IllegalArgumentException if the name is null. + */ + @Override + public Verification withClaim(String name, Long value) throws IllegalArgumentException { + assertNonNull(name); + requireClaim(name, value); + return this; + } + /** * Require a specific Claim value. * diff --git a/lib/src/main/java/com/auth0/jwt/interfaces/Verification.java b/lib/src/main/java/com/auth0/jwt/interfaces/Verification.java index 3d2ac60f..465ebe5d 100644 --- a/lib/src/main/java/com/auth0/jwt/interfaces/Verification.java +++ b/lib/src/main/java/com/auth0/jwt/interfaces/Verification.java @@ -25,6 +25,8 @@ public interface Verification { Verification withClaim(String name, Integer value) throws IllegalArgumentException; + Verification withClaim(String name, Long value) throws IllegalArgumentException; + Verification withClaim(String name, Double value) throws IllegalArgumentException; Verification withClaim(String name, String value) throws IllegalArgumentException; diff --git a/lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java b/lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java index b7db94da..4b55d2a8 100644 --- a/lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java +++ b/lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java @@ -222,6 +222,17 @@ public void shouldValidateCustomClaimOfTypeInteger() throws Exception { assertThat(jwt, is(notNullValue())); } + @Test + public void shouldValidateCustomClaimOfTypeLong() throws Exception { + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjo5MjIzMzcyMDM2ODU0Nzc2MDB9.km-IwQ5IDnTZFmuJzhSgvjTzGkn_Z5X29g4nAuVC56I"; + DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret")) + .withClaim("name", 922337203685477600L) + .build() + .verify(token); + + assertThat(jwt, is(notNullValue())); + } + @Test public void shouldValidateCustomClaimOfTypeDouble() throws Exception { String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoyMy40NX0.7pyX2OmEGaU9q15T8bGFqRm-d3RVTYnqmZNZtxMKSlA"; diff --git a/lib/src/test/java/com/auth0/jwt/impl/NullClaimTest.java b/lib/src/test/java/com/auth0/jwt/impl/NullClaimTest.java index 9796c322..94b1d4cd 100644 --- a/lib/src/test/java/com/auth0/jwt/impl/NullClaimTest.java +++ b/lib/src/test/java/com/auth0/jwt/impl/NullClaimTest.java @@ -31,6 +31,11 @@ public void shouldGetAsInt() throws Exception { assertThat(claim.asInt(), is(nullValue())); } + @Test + public void shouldGetAsLong() throws Exception { + assertThat(claim.asLong(), is(nullValue())); + } + @Test public void shouldGetAsDouble() throws Exception { assertThat(claim.asDouble(), is(nullValue())); From c60687cd4a018c6b87c72b52c3c0419aa30d8014 Mon Sep 17 00:00:00 2001 From: vrancic Date: Thu, 6 Apr 2017 23:37:34 +0300 Subject: [PATCH 3/3] JWTCreator extended with Long type. --- .../main/java/com/auth0/jwt/JWTCreator.java | 29 ++++++++++++++++++- .../java/com/auth0/jwt/JWTCreatorTest.java | 22 ++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/src/main/java/com/auth0/jwt/JWTCreator.java b/lib/src/main/java/com/auth0/jwt/JWTCreator.java index 23d87dc4..fa93be95 100644 --- a/lib/src/main/java/com/auth0/jwt/JWTCreator.java +++ b/lib/src/main/java/com/auth0/jwt/JWTCreator.java @@ -9,7 +9,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationConfig; import com.fasterxml.jackson.databind.module.SimpleModule; import org.apache.commons.codec.binary.Base64; @@ -192,6 +191,20 @@ public Builder withClaim(String name, Integer value) throws IllegalArgumentExcep return this; } + /** + * Add a custom Claim value. + * + * @param name the Claim's name. + * @param value the Claim's value. + * @return this same Builder instance. + * @throws IllegalArgumentException if the name is null. + */ + public Builder withClaim(String name, Long value) throws IllegalArgumentException { + assertNonNull(name); + addClaim(name, value); + return this; + } + /** * Add a custom Claim value. * @@ -262,6 +275,20 @@ public Builder withArrayClaim(String name, Integer[] items) throws IllegalArgume return this; } + /** + * Add a custom Array Claim with the given items. + * + * @param name the Claim's name. + * @param items the Claim's value. + * @return this same Builder instance. + * @throws IllegalArgumentException if the name is null. + */ + public Builder withArrayClaim(String name, Long[] items) throws IllegalArgumentException { + assertNonNull(name); + addClaim(name, items); + return this; + } + /** * Creates a new JWT and signs is with the given algorithm * diff --git a/lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java b/lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java index 43d336bb..a4d5204c 100644 --- a/lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java +++ b/lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java @@ -204,6 +204,17 @@ public void shouldAcceptCustomClaimOfTypeInteger() throws Exception { assertThat(parts[1], is("eyJuYW1lIjoxMjN9")); } + @Test + public void shouldAcceptCustomClaimOfTypeLong() throws Exception { + String jwt = JWTCreator.init() + .withClaim("name", Long.MAX_VALUE) + .sign(Algorithm.HMAC256("secret")); + + assertThat(jwt, is(notNullValue())); + String[] parts = jwt.split("\\."); + assertThat(parts[1], is("eyJuYW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3fQ")); + } + @Test public void shouldAcceptCustomClaimOfTypeDouble() throws Exception { String jwt = JWTCreator.init() @@ -259,4 +270,15 @@ public void shouldAcceptCustomArrayClaimOfTypeInteger() throws Exception { String[] parts = jwt.split("\\."); assertThat(parts[1], is("eyJuYW1lIjpbMSwyLDNdfQ")); } + + @Test + public void shouldAcceptCustomArrayClaimOfTypeLong() throws Exception { + String jwt = JWTCreator.init() + .withArrayClaim("name", new Long[]{1L, 2L, 3L}) + .sign(Algorithm.HMAC256("secret")); + + assertThat(jwt, is(notNullValue())); + String[] parts = jwt.split("\\."); + assertThat(parts[1], is("eyJuYW1lIjpbMSwyLDNdfQ")); + } } \ No newline at end of file