Skip to content

Commit e53d31d

Browse files
committed
allow to add custom class claims
1 parent 1b9cc7a commit e53d31d

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,15 @@ public Builder withJWTId(String jwtId) {
153153
/**
154154
* Add a custom Claim value.
155155
*
156-
* @param name the Claim's name
157-
* @param value the Claim's value. Must be an instance of Integer, Double, Boolean, Date or String class.
156+
* @param name the Claim's name.
157+
* @param value the Claim's value.
158158
* @return this same Builder instance.
159-
* @throws IllegalArgumentException if the name is null or the value class is not allowed.
159+
* @throws IllegalArgumentException if the name is null.
160160
*/
161161
public Builder withClaim(String name, Object value) throws IllegalArgumentException {
162-
final boolean validValue = value instanceof Integer || value instanceof Double ||
163-
value instanceof Boolean || value instanceof Date || value instanceof String;
164162
if (name == null) {
165163
throw new IllegalArgumentException("The Custom Claim's name can't be null.");
166164
}
167-
if (!validValue) {
168-
throw new IllegalArgumentException("The Custom Claim's value class must be an instance of Integer, Double, Boolean, Date or String.");
169-
}
170165

171166
addClaim(name, value);
172167
return this;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ public interface Claim {
5454

5555
/**
5656
* Get this Claim as an Array of type T.
57-
* If the value isn't an Array, an empty Array will be returned.
57+
* If the value isn't an Array, null will be returned.
5858
*
59-
* @return the value as an Array or an empty Array.
59+
* @return the value as an Array or null.
6060
* @throws JWTDecodeException if the values inside the Array can't be converted to a class T.
6161
*/
6262
<T> T[] asArray(Class<T> tClazz) throws JWTDecodeException;
6363

6464
/**
6565
* Get this Claim as a List of type T.
66-
* If the value isn't an Array, an empty List will be returned.
66+
* If the value isn't an Array, null will be returned.
6767
*
68-
* @return the value as a List or an empty List.
68+
* @return the value as a List or null.
6969
* @throws JWTDecodeException if the values inside the List can't be converted to a class T.
7070
*/
7171
<T> List<T> asList(Class<T> tClazz) throws JWTDecodeException;

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

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import org.junit.Test;
66
import org.junit.rules.ExpectedException;
77

8-
import java.util.Date;
9-
import java.util.HashMap;
10-
import java.util.Map;
8+
import java.util.*;
119

1210
import static org.hamcrest.Matchers.is;
1311
import static org.hamcrest.Matchers.notNullValue;
@@ -153,14 +151,6 @@ public void shouldThrowOnNullCustomClaimName() throws Exception {
153151
.withClaim(null, "value");
154152
}
155153

156-
@Test
157-
public void shouldThrowOnIllegalCustomClaimValueClass() throws Exception {
158-
exception.expect(IllegalArgumentException.class);
159-
exception.expectMessage("The Custom Claim's value class must be an instance of Integer, Double, Boolean, Date or String.");
160-
JWTCreator.init()
161-
.withClaim("name", new Object());
162-
}
163-
164154
@Test
165155
public void shouldAcceptCustomClaimOfTypeString() throws Exception {
166156
String jwt = JWTCreator.init()
@@ -217,4 +207,47 @@ public void shouldAcceptCustomClaimOfTypeDate() throws Exception {
217207
assertThat(jwt, is(token));
218208
}
219209

210+
@Test
211+
public void shouldAcceptCustomClaimOfTypeArray() throws Exception {
212+
String jwt = JWTCreator.init()
213+
.withClaim("name", new Object[]{"text", 123, true})
214+
.sign(Algorithm.HMAC256("secret"));
215+
String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbInRleHQiLDEyMyx0cnVlXX0.uSulPFzLSbgfG8Lpr0jq0JDMhDlGGeQrx09PHEymu1E";
216+
217+
assertThat(jwt, is(notNullValue()));
218+
assertThat(jwt, is(token));
219+
}
220+
221+
@Test
222+
public void shouldAcceptCustomClaimOfTypeList() throws Exception {
223+
String jwt = JWTCreator.init()
224+
.withClaim("name", Arrays.asList("text", 123, true))
225+
.sign(Algorithm.HMAC256("secret"));
226+
String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbInRleHQiLDEyMyx0cnVlXX0.uSulPFzLSbgfG8Lpr0jq0JDMhDlGGeQrx09PHEymu1E";
227+
228+
assertThat(jwt, is(notNullValue()));
229+
assertThat(jwt, is(token));
230+
}
231+
232+
@Test
233+
public void shouldAcceptCustomClaimOfTypeMap() throws Exception {
234+
String jwt = JWTCreator.init()
235+
.withClaim("name", Collections.singletonMap("value", new Object[]{"text", 123, true}))
236+
.sign(Algorithm.HMAC256("secret"));
237+
String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjp7InZhbHVlIjpbInRleHQiLDEyMyx0cnVlXX19.CtZqZMoG__8yJQisT__pcv3NlynrkDl6qvq4sERx6D0";
238+
239+
assertThat(jwt, is(notNullValue()));
240+
assertThat(jwt, is(token));
241+
}
242+
243+
@Test
244+
public void shouldAcceptCustomClaimOfTypeObject() throws Exception {
245+
String jwt = JWTCreator.init()
246+
.withClaim("name", new UserPojo("john", 123))
247+
.sign(Algorithm.HMAC256("secret"));
248+
String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjp7Im5hbWUiOiJqb2huIiwiaWQiOjEyM319.4ar5Q2vy8h7mw-FjFp1XRoiiKQrrPqdrSqEfATCGmNM";
249+
250+
assertThat(jwt, is(notNullValue()));
251+
assertThat(jwt, is(token));
252+
}
220253
}

0 commit comments

Comments
 (0)