Skip to content

Commit c60687c

Browse files
committed
JWTCreator extended with Long type.
1 parent 6825cd3 commit c60687c

2 files changed

Lines changed: 50 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/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
}

0 commit comments

Comments
 (0)