Skip to content

Commit

Permalink
Merge pull request #157 from vrancic/missing-long
Browse files Browse the repository at this point in the history
Add Sign/Verify of Long type claims
  • Loading branch information
lbalmaceda authored Apr 12, 2017
2 parents f5cf048 + 9193d5d commit 199aca8
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/src/main/java/com/auth0/jwt/JWTCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
*
Expand Down
17 changes: 17 additions & 0 deletions lib/src/main/java/com/auth0/jwt/JWTVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,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.
*
Expand Down Expand Up @@ -394,6 +409,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) {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/main/java/com/auth0/jwt/impl/JsonNodeClaim.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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();
Expand Down
5 changes: 5 additions & 0 deletions lib/src/main/java/com/auth0/jwt/impl/NullClaim.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public Integer asInt() {
return null;
}

@Override
public Long asLong() {
return null;
}

@Override
public Double asDouble() {
return null;
Expand Down
8 changes: 8 additions & 0 deletions lib/src/main/java/com/auth0/jwt/interfaces/Claim.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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.
Expand Down
2 changes: 2 additions & 0 deletions lib/src/main/java/com/auth0/jwt/interfaces/Verification.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"));
}
}
11 changes: 11 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,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";
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 @@ -72,6 +72,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);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/test/java/com/auth0/jwt/impl/NullClaimTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit 199aca8

Please sign in to comment.