Skip to content

Commit 199aca8

Browse files
authored
Merge pull request #157 from vrancic/missing-long
Add Sign/Verify of Long type claims
2 parents f5cf048 + 9193d5d commit 199aca8

10 files changed

Lines changed: 118 additions & 1 deletion

File tree

lib/src/main/java/com/auth0/jwt/JWTCreator.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.fasterxml.jackson.core.JsonProcessingException;
1010
import com.fasterxml.jackson.databind.MapperFeature;
1111
import com.fasterxml.jackson.databind.ObjectMapper;
12-
import com.fasterxml.jackson.databind.SerializationConfig;
1312
import com.fasterxml.jackson.databind.module.SimpleModule;
1413
import org.apache.commons.codec.binary.Base64;
1514

@@ -192,6 +191,20 @@ public Builder withClaim(String name, Integer value) throws IllegalArgumentExcep
192191
return this;
193192
}
194193

194+
/**
195+
* Add a custom Claim value.
196+
*
197+
* @param name the Claim's name.
198+
* @param value the Claim's value.
199+
* @return this same Builder instance.
200+
* @throws IllegalArgumentException if the name is null.
201+
*/
202+
public Builder withClaim(String name, Long value) throws IllegalArgumentException {
203+
assertNonNull(name);
204+
addClaim(name, value);
205+
return this;
206+
}
207+
195208
/**
196209
* Add a custom Claim value.
197210
*
@@ -262,6 +275,20 @@ public Builder withArrayClaim(String name, Integer[] items) throws IllegalArgume
262275
return this;
263276
}
264277

278+
/**
279+
* Add a custom Array Claim with the given items.
280+
*
281+
* @param name the Claim's name.
282+
* @param items the Claim's value.
283+
* @return this same Builder instance.
284+
* @throws IllegalArgumentException if the name is null.
285+
*/
286+
public Builder withArrayClaim(String name, Long[] items) throws IllegalArgumentException {
287+
assertNonNull(name);
288+
addClaim(name, items);
289+
return this;
290+
}
291+
265292
/**
266293
* Creates a new JWT and signs is with the given algorithm
267294
*

lib/src/main/java/com/auth0/jwt/JWTVerifier.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,21 @@ public Verification withClaim(String name, Integer value) throws IllegalArgument
198198
return this;
199199
}
200200

201+
/**
202+
* Require a specific Claim value.
203+
*
204+
* @param name the Claim's name.
205+
* @param value the Claim's value.
206+
* @return this same Verification instance.
207+
* @throws IllegalArgumentException if the name is null.
208+
*/
209+
@Override
210+
public Verification withClaim(String name, Long value) throws IllegalArgumentException {
211+
assertNonNull(name);
212+
requireClaim(name, value);
213+
return this;
214+
}
215+
201216
/**
202217
* Require a specific Claim value.
203218
*
@@ -394,6 +409,8 @@ private void assertValidClaim(Claim claim, String claimName, Object value) {
394409
isValid = value.equals(claim.asString());
395410
} else if (value instanceof Integer) {
396411
isValid = value.equals(claim.asInt());
412+
} else if (value instanceof Long) {
413+
isValid = value.equals(claim.asLong());
397414
} else if (value instanceof Boolean) {
398415
isValid = value.equals(claim.asBoolean());
399416
} else if (value instanceof Double) {

lib/src/main/java/com/auth0/jwt/impl/JsonNodeClaim.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public Integer asInt() {
3535
return !data.isNumber() ? null : data.asInt();
3636
}
3737

38+
@Override
39+
public Long asLong() { return !data.isNumber() ? null : data.asLong(); }
40+
3841
@Override
3942
public Double asDouble() {
4043
return !data.isNumber() ? null : data.asDouble();

lib/src/main/java/com/auth0/jwt/impl/NullClaim.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public Integer asInt() {
2626
return null;
2727
}
2828

29+
@Override
30+
public Long asLong() {
31+
return null;
32+
}
33+
2934
@Override
3035
public Double asDouble() {
3136
return null;

lib/src/main/java/com/auth0/jwt/interfaces/Claim.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public interface Claim {
3434
*/
3535
Integer asInt();
3636

37+
/**
38+
* Get this Claim as an Long.
39+
* If the value isn't of type Long or it can't be converted to an Long, null will be returned.
40+
*
41+
* @return the value as an Long or null.
42+
*/
43+
Long asLong();
44+
3745
/**
3846
* Get this Claim as a Double.
3947
* If the value isn't of type Double or it can't be converted to a Double, null will be returned.

lib/src/main/java/com/auth0/jwt/interfaces/Verification.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public interface Verification {
2525

2626
Verification withClaim(String name, Integer value) throws IllegalArgumentException;
2727

28+
Verification withClaim(String name, Long value) throws IllegalArgumentException;
29+
2830
Verification withClaim(String name, Double value) throws IllegalArgumentException;
2931

3032
Verification withClaim(String name, String value) throws IllegalArgumentException;

lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ public void shouldAcceptCustomClaimOfTypeInteger() throws Exception {
204204
assertThat(parts[1], is("eyJuYW1lIjoxMjN9"));
205205
}
206206

207+
@Test
208+
public void shouldAcceptCustomClaimOfTypeLong() throws Exception {
209+
String jwt = JWTCreator.init()
210+
.withClaim("name", Long.MAX_VALUE)
211+
.sign(Algorithm.HMAC256("secret"));
212+
213+
assertThat(jwt, is(notNullValue()));
214+
String[] parts = jwt.split("\\.");
215+
assertThat(parts[1], is("eyJuYW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3fQ"));
216+
}
217+
207218
@Test
208219
public void shouldAcceptCustomClaimOfTypeDouble() throws Exception {
209220
String jwt = JWTCreator.init()
@@ -259,4 +270,15 @@ public void shouldAcceptCustomArrayClaimOfTypeInteger() throws Exception {
259270
String[] parts = jwt.split("\\.");
260271
assertThat(parts[1], is("eyJuYW1lIjpbMSwyLDNdfQ"));
261272
}
273+
274+
@Test
275+
public void shouldAcceptCustomArrayClaimOfTypeLong() throws Exception {
276+
String jwt = JWTCreator.init()
277+
.withArrayClaim("name", new Long[]{1L, 2L, 3L})
278+
.sign(Algorithm.HMAC256("secret"));
279+
280+
assertThat(jwt, is(notNullValue()));
281+
String[] parts = jwt.split("\\.");
282+
assertThat(parts[1], is("eyJuYW1lIjpbMSwyLDNdfQ"));
283+
}
262284
}

lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ public void shouldValidateCustomClaimOfTypeInteger() throws Exception {
223223
assertThat(jwt, is(notNullValue()));
224224
}
225225

226+
@Test
227+
public void shouldValidateCustomClaimOfTypeLong() throws Exception {
228+
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjo5MjIzMzcyMDM2ODU0Nzc2MDB9.km-IwQ5IDnTZFmuJzhSgvjTzGkn_Z5X29g4nAuVC56I";
229+
DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret"))
230+
.withClaim("name", 922337203685477600L)
231+
.build()
232+
.verify(token);
233+
234+
assertThat(jwt, is(notNullValue()));
235+
}
236+
226237
@Test
227238
public void shouldValidateCustomClaimOfTypeDouble() throws Exception {
228239
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoyMy40NX0.7pyX2OmEGaU9q15T8bGFqRm-d3RVTYnqmZNZtxMKSlA";

lib/src/test/java/com/auth0/jwt/impl/JsonNodeClaimTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ public void shouldGetNullIntIfNotIntValue() throws Exception {
7272
assertThat(claimFromNode(stringValue).asInt(), is(nullValue()));
7373
}
7474

75+
@Test
76+
public void shouldGetLongValue() throws Exception {
77+
JsonNode value = mapper.valueToTree(Long.MAX_VALUE);
78+
Claim claim = claimFromNode(value);
79+
80+
assertThat(claim.asLong(), is(notNullValue()));
81+
assertThat(claim.asLong(), is(Long.MAX_VALUE));
82+
}
83+
84+
@Test
85+
public void shouldGetNullLongIfNotIntValue() throws Exception {
86+
JsonNode objectValue = mapper.valueToTree(new Object());
87+
assertThat(claimFromNode(objectValue).asLong(), is(nullValue()));
88+
JsonNode stringValue = mapper.valueToTree("" + Long.MAX_VALUE);
89+
assertThat(claimFromNode(stringValue).asLong(), is(nullValue()));
90+
}
91+
7592
@Test
7693
public void shouldGetDoubleValue() throws Exception {
7794
JsonNode value = mapper.valueToTree(1.5);

lib/src/test/java/com/auth0/jwt/impl/NullClaimTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public void shouldGetAsInt() throws Exception {
3131
assertThat(claim.asInt(), is(nullValue()));
3232
}
3333

34+
@Test
35+
public void shouldGetAsLong() throws Exception {
36+
assertThat(claim.asLong(), is(nullValue()));
37+
}
38+
3439
@Test
3540
public void shouldGetAsDouble() throws Exception {
3641
assertThat(claim.asDouble(), is(nullValue()));

0 commit comments

Comments
 (0)