Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sign/Verify of Long type claims #157

Merged
merged 4 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@ -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.
*
Expand Down Expand Up @@ -393,6 +408,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 @@ -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();
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 @@ -25,6 +25,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 @@ -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.
Expand Down
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 @@ -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";
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 @@ -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);
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