Skip to content

Commit 522f279

Browse files
committed
allow to get a Claim as Map<String, Object>
1 parent 3193e62 commit 522f279

File tree

7 files changed

+65
-1
lines changed

7 files changed

+65
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ The Claim class is a wrapper for the Claim values. It allows you to get the Clai
338338
To obtain a Claim as a Collection you'll need to provide the **Class Type** of the contents to convert from.
339339

340340
* **as(class)**: Returns the value parsed as **Class Type**. For collections you should use the `asArray` and `asList` methods.
341+
* **asMap()**: Returns the value parsed as **Map<String, Object>**.
341342
* **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.
342343
* **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.
343344

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.auth0.jwt.exceptions.JWTDecodeException;
44
import com.auth0.jwt.impl.JWTParser;
55
import com.auth0.jwt.interfaces.Claim;
6-
import com.auth0.jwt.interfaces.DecodedJWT;
76
import com.auth0.jwt.interfaces.Header;
87
import com.auth0.jwt.interfaces.Payload;
98
import org.apache.commons.codec.binary.Base64;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.auth0.jwt.exceptions.JWTDecodeException;
44
import com.auth0.jwt.interfaces.Claim;
55
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import com.fasterxml.jackson.core.type.TypeReference;
67
import com.fasterxml.jackson.databind.JsonNode;
78
import com.fasterxml.jackson.databind.ObjectMapper;
89

@@ -90,6 +91,18 @@ public <T> List<T> asList(Class<T> tClazz) throws JWTDecodeException {
9091
return list;
9192
}
9293

94+
@Override
95+
public Map<String, Object> asMap() throws JWTDecodeException {
96+
ObjectMapper mapper = new ObjectMapper();
97+
try {
98+
TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {
99+
};
100+
return mapper.treeAsTokens(data).readValueAs(mapType);
101+
} catch (IOException e) {
102+
throw new JWTDecodeException("Couldn't map the Claim value to Map", e);
103+
}
104+
}
105+
93106
@Override
94107
public <T> T as(Class<T> tClazz) throws JWTDecodeException {
95108
ObjectMapper mapper = new ObjectMapper();

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.Date;
77
import java.util.List;
8+
import java.util.Map;
89

910
/**
1011
* The {@link NullClaim} class is a Claim implementation that returns null when any of it's methods it's called.
@@ -50,6 +51,11 @@ public <T> List<T> asList(Class<T> tClazz) throws JWTDecodeException {
5051
return null;
5152
}
5253

54+
@Override
55+
public Map<String, Object> asMap() throws JWTDecodeException {
56+
return null;
57+
}
58+
5359
@Override
5460
public <T> T as(Class<T> tClazz) throws JWTDecodeException {
5561
return null;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Date;
66
import java.util.List;
7+
import java.util.Map;
78

89
/**
910
* The Claim class holds the value in a generic way so that it can be recovered in many representations.
@@ -75,6 +76,14 @@ public interface Claim {
7576
*/
7677
<T> List<T> asList(Class<T> tClazz) throws JWTDecodeException;
7778

79+
/**
80+
* Get this Claim as a generic Map of values.
81+
*
82+
* @return the value as instance of Map.
83+
* @throws JWTDecodeException if the value can't be converted to a Map.
84+
*/
85+
Map<String, Object> asMap() throws JWTDecodeException;
86+
7887
/**
7988
* Get this Claim as a custom type T.
8089
*

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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;
1011
import org.junit.Before;
1112
import org.junit.Rule;
1213
import org.junit.Test;
@@ -203,6 +204,36 @@ public void shouldThrowIfListClassMismatch() throws Exception {
203204
claim.asList(UserPojo.class);
204205
}
205206

207+
@Test
208+
public void shouldGetMapValue() throws Exception {
209+
Map<String, Object> map = new HashMap<>();
210+
map.put("text", "extraValue");
211+
map.put("number", 12);
212+
map.put("boolean", true);
213+
map.put("object", Collections.singletonMap("something", "else"));
214+
215+
JsonNode value = mapper.valueToTree(map);
216+
Claim claim = claimFromNode(value);
217+
218+
assertThat(claim, is(notNullValue()));
219+
Map<String, Object> backMap = claim.asMap();
220+
assertThat(backMap, is(notNullValue()));
221+
assertThat(backMap, hasEntry("text", (Object) "extraValue"));
222+
assertThat(backMap, hasEntry("number", (Object) 12));
223+
assertThat(backMap, hasEntry("boolean", (Object) true));
224+
assertThat(backMap, hasKey("object"));
225+
assertThat((Map<String, Object>) backMap.get("object"), IsMapContaining.hasEntry("something", (Object) "else"));
226+
}
227+
228+
@Test
229+
public void shouldThrowIfMapClassMismatch() throws Exception {
230+
JsonNode value = mapper.valueToTree("text node");
231+
Claim claim = claimFromNode(value);
232+
233+
exception.expect(JWTDecodeException.class);
234+
claim.asMap();
235+
}
236+
206237
@Test
207238
public void shouldGetCustomClassValue() throws Exception {
208239
JsonNode value = mapper.valueToTree(new UserPojo("john", 123));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public void shouldGetAsList() throws Exception {
5656
assertThat(claim.asList(Object.class), is(nullValue()));
5757
}
5858

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

0 commit comments

Comments
 (0)