Skip to content

Commit 59ac5e4

Browse files
committed
allow to require a custom class Claim
1 parent 973f989 commit 59ac5e4

File tree

6 files changed

+55
-28
lines changed

6 files changed

+55
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ The Claim class is a wrapper for the Claim values. It allows you to get the Clai
300300
#### Custom Class and Collections
301301
To obtain a Claim as a Collection you'll need to provide the **Class Type** of the contents to convert from.
302302

303-
* **as(class)**: Returns the value parsed as **Class Type**.
303+
* **as(class)**: Returns the value parsed as **Class Type**. For collections you should use the `asArray` and `asList` methods.
304304
* **asArray(class)**: Returns the value parsed as an Array of elements of type **Class Type**, or null if the value isn't a JSON Array.
305305
* **asList(class)**: Returns the value parsed as a List of elements of type **Class Type**, or null if the value isn't a JSON Array.
306306

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,15 @@ public Verification withJWTId(String jwtId) {
168168
/**
169169
* Require a specific Claim value.
170170
*
171-
* @param name the Claim's name
172-
* @param value the Claim's value. Must be an instance of Integer, Double, Boolean, Date or String class.
171+
* @param name the Claim's name.
172+
* @param value the Claim's value.
173173
* @return this same Verification instance.
174-
* @throws IllegalArgumentException if the name is null or the value class is not allowed.
174+
* @throws IllegalArgumentException if the name is null.
175175
*/
176176
public Verification withClaim(String name, Object value) throws IllegalArgumentException {
177-
final boolean validValue = value instanceof Integer || value instanceof Double ||
178-
value instanceof Boolean || value instanceof Date || value instanceof String;
179177
if (name == null) {
180178
throw new IllegalArgumentException("The Custom Claim's name can't be null.");
181179
}
182-
if (!validValue) {
183-
throw new IllegalArgumentException("The Custom Claim's value class must be an instance of Integer, Double, Boolean, Date or String.");
184-
}
185180

186181
requireClaim(name, value);
187182
return this;
@@ -301,6 +296,8 @@ private void assertValidClaim(Claim claim, String claimName, Object value) {
301296
isValid = value.equals(claim.asDouble());
302297
} else if (value instanceof Date) {
303298
isValid = value.equals(claim.asDate());
299+
} else {
300+
isValid = Objects.deepEquals(value, claim.as(value.getClass()));
304301
}
305302

306303
if (!isValid) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.fasterxml.jackson.databind.ObjectMapper;
88

9+
import java.io.IOException;
910
import java.lang.reflect.Array;
1011
import java.util.ArrayList;
1112
import java.util.Date;
@@ -93,8 +94,8 @@ public <T> List<T> asList(Class<T> tClazz) throws JWTDecodeException {
9394
public <T> T as(Class<T> tClazz) throws JWTDecodeException {
9495
ObjectMapper mapper = new ObjectMapper();
9596
try {
96-
return mapper.treeToValue(data, tClazz);
97-
} catch (JsonProcessingException e) {
97+
return mapper.treeAsTokens(data).readValueAs(tClazz);
98+
} catch (IOException e) {
9899
throw new JWTDecodeException("Couldn't map the Claim value to " + tClazz.getSimpleName(), e);
99100
}
100101
}

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import org.junit.Test;
99
import org.junit.rules.ExpectedException;
1010

11-
import java.util.Date;
12-
import java.util.HashMap;
13-
import java.util.Map;
11+
import java.util.*;
1412

1513
import static org.hamcrest.Matchers.*;
1614
import static org.junit.Assert.assertThat;
@@ -132,14 +130,6 @@ public void shouldThrowOnNullCustomClaimName() throws Exception {
132130
.withClaim(null, "value");
133131
}
134132

135-
@Test
136-
public void shouldThrowOnIllegalCustomClaimValueClass() throws Exception {
137-
exception.expect(IllegalArgumentException.class);
138-
exception.expectMessage("The Custom Claim's value class must be an instance of Integer, Double, Boolean, Date or String.");
139-
JWTVerifier.init(Algorithm.HMAC256("secret"))
140-
.withClaim("name", new Object());
141-
}
142-
143133
@Test
144134
public void shouldThrowOnInvalidCustomClaimValueOfTypeString() throws Exception {
145135
exception.expect(InvalidClaimException.class);
@@ -263,6 +253,38 @@ public void shouldValidateCustomClaimOfTypeDate() throws Exception {
263253
assertThat(jwt, is(notNullValue()));
264254
}
265255

256+
@Test
257+
public void shouldValidateCustomClaimOfCustomType() throws Exception {
258+
String token = "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7Im5hbWUiOiJqb2huIiwiaWQiOjEyM319.j3e7IfnEchQEwgDs1icOyufhzAyNOYfX9fjJwV6uyZk";
259+
DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret"))
260+
.withClaim("user", new UserPojo("john", 123))
261+
.build()
262+
.verify(token);
263+
264+
assertThat(jwt, is(notNullValue()));
265+
}
266+
267+
@Test
268+
public void shouldValidateCustomClaimOfTypeArray() throws Exception {
269+
String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbInRleHQiLDEyMyx0cnVlXX0.uSulPFzLSbgfG8Lpr0jq0JDMhDlGGeQrx09PHEymu1E";
270+
DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret"))
271+
.withClaim("name", new Object[]{"text", 123, true})
272+
.build()
273+
.verify(token);
274+
275+
assertThat(jwt, is(notNullValue()));
276+
}
277+
278+
@Test
279+
public void shouldValidateCustomClaimOfTypeList() throws Exception {
280+
String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbInRleHQiLDEyMyx0cnVlXX0.uSulPFzLSbgfG8Lpr0jq0JDMhDlGGeQrx09PHEymu1E";
281+
DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret"))
282+
.withClaim("name", new ArrayList<>(Arrays.asList("text", 123, true)))
283+
.build()
284+
.verify(token);
285+
286+
assertThat(jwt, is(notNullValue()));
287+
}
266288

267289
// Generic Delta
268290
@SuppressWarnings("RedundantCast")

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.node.MissingNode;
99
import com.fasterxml.jackson.databind.node.NullNode;
10-
import org.hamcrest.collection.IsMapContaining;
1110
import org.junit.Before;
1211
import org.junit.Rule;
1312
import org.junit.Test;
1413
import org.junit.rules.ExpectedException;
1514

16-
import java.util.*;
15+
import java.util.Arrays;
16+
import java.util.Collections;
17+
import java.util.Date;
18+
import java.util.Map;
1719

1820
import static com.auth0.jwt.impl.JWTParser.getDefaultObjectMapper;
1921
import static com.auth0.jwt.impl.JsonNodeClaim.claimFromNode;
@@ -223,16 +225,16 @@ public void shouldThrowIfCustomClassMismatch() throws Exception {
223225
claim.as(String.class);
224226
}
225227

226-
@SuppressWarnings("unchecked")
228+
@SuppressWarnings({"unchecked", "RedundantCast"})
227229
@Test
228230
public void shouldGetAsMapValue() throws Exception {
229231
JsonNode value = mapper.valueToTree(Collections.singletonMap("key", new UserPojo("john", 123)));
230232
Claim claim = claimFromNode(value);
231233

232234
assertThat(claim, is(notNullValue()));
233235
Map map = claim.as(Map.class);
234-
assertThat(((HashMap<String, Object>) map.get("key")), IsMapContaining.hasEntry("name", "john"));
235-
assertThat(((HashMap<String, Object>) map.get("key")), IsMapContaining.hasEntry("id", 123));
236+
assertThat(((Map<String, Object>) map.get("key")), hasEntry("name", (Object) "john"));
237+
assertThat(((Map<String, Object>) map.get("key")), hasEntry("id", (Object) 123));
236238
}
237239

238240
@Test

lib/src/test/java/com/auth0/jwt/impl/BaseClaimTest.java renamed to lib/src/test/java/com/auth0/jwt/impl/NullClaimTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import static org.hamcrest.Matchers.nullValue;
99
import static org.junit.Assert.assertThat;
1010

11-
public class BaseClaimTest {
11+
public class NullClaimTest {
1212
private NullClaim claim;
1313

1414
@Before
@@ -56,4 +56,9 @@ public void shouldGetAsList() throws Exception {
5656
assertThat(claim.asList(Object.class), is(nullValue()));
5757
}
5858

59+
@Test
60+
public void shouldGetAsCustomClass() throws Exception {
61+
assertThat(claim.as(Object.class), is(nullValue()));
62+
}
63+
5964
}

0 commit comments

Comments
 (0)