Skip to content

Commit ce9a612

Browse files
committed
add getter for payload's Claims map
1 parent 5754aaf commit ce9a612

File tree

6 files changed

+72
-2
lines changed

6 files changed

+72
-2
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,14 @@ String id = jwt.getId();
256256

257257
#### Private Claims
258258

259-
Additional Claims defined in the token's Payload can be obtained by calling `getClaim()` and passing the Claim name. A Claim will always be returned, even if it can't be found. You can check if a Claim's value is null by calling `claim.isNull()`.
259+
Additional Claims defined in the token's Payload can be obtained by calling `getClaims()` or `getClaim()` and passing the Claim name. A Claim will always be returned, even if it can't be found. You can check if a Claim's value is null by calling `claim.isNull()`.
260+
261+
```java
262+
Map<String, Claim> claims = jwt.getClaims(); //Key is the Claim name
263+
Claim claim = claims.get("isAdmin");
264+
```
265+
al
266+
or
260267

261268
```java
262269
Claim claim = jwt.getClaim("isAdmin");

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

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

1212
import java.util.Date;
1313
import java.util.List;
14+
import java.util.Map;
1415

1516
/**
1617
* The JWTDecoder class holds the decode method to parse a given JWT token into it's JWT representation.
@@ -109,6 +110,11 @@ public Claim getClaim(String name) {
109110
return payload.getClaim(name);
110111
}
111112

113+
@Override
114+
public Map<String, Claim> getClaims() {
115+
return payload.getClaims();
116+
}
117+
112118
@Override
113119
public String getSignature() {
114120
return signature;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,12 @@ public Claim getClaim(String name) {
7676
return extractClaim(name, tree);
7777
}
7878

79+
@Override
80+
public Map<String, Claim> getClaims() {
81+
Map<String, Claim> claims = new HashMap<>();
82+
for (String name : tree.keySet()) {
83+
claims.put(name, extractClaim(name, tree));
84+
}
85+
return Collections.unmodifiableMap(claims);
86+
}
7987
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Date;
44
import java.util.List;
5+
import java.util.Map;
56

67
/**
78
* The Payload class represents the 2nd part of the JWT, where the Payload value is hold.
@@ -58,10 +59,17 @@ public interface Payload {
5859
String getId();
5960

6061
/**
61-
* Get a Private Claim given it's name. If the Claim wasn't specified in the Payload, a NullClaim will be returned.
62+
* Get a Claim given it's name. If the Claim wasn't specified in the Payload, a NullClaim will be returned.
6263
*
6364
* @param name the name of the Claim to retrieve.
6465
* @return a non-null Claim.
6566
*/
6667
Claim getClaim(String name);
68+
69+
/**
70+
* Get the Claims defined in the Token.
71+
*
72+
* @return a non-null Map containing the Claims defined in the Token.
73+
*/
74+
Map<String, Claim> getClaims();
6775
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.nio.charset.StandardCharsets;
1515
import java.util.Date;
16+
import java.util.Map;
1617

1718
import static org.hamcrest.MatcherAssert.assertThat;
1819
import static org.hamcrest.Matchers.*;
@@ -198,6 +199,22 @@ public void shouldGetNullClaimIfClaimValueIsNull() throws Exception {
198199
assertThat(jwt.getClaim("object").isNull(), is(true));
199200
}
200201

202+
@Test
203+
public void shouldGetAvailableClaims() throws Exception {
204+
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxMjM0NTY3ODkwIiwiaWF0IjoiMTIzNDU2Nzg5MCIsIm5iZiI6IjEyMzQ1Njc4OTAiLCJqdGkiOiJodHRwczovL2p3dC5pby8iLCJhdWQiOiJodHRwczovL2RvbWFpbi5hdXRoMC5jb20iLCJzdWIiOiJsb2dpbiIsImlzcyI6ImF1dGgwIiwiZXh0cmFDbGFpbSI6IkpvaG4gRG9lIn0.TX9Ct4feGp9YyeGK9Zl91tO0YBOrguJ4As9jeqgHdZQ");
205+
assertThat(jwt, is(notNullValue()));
206+
assertThat(jwt.getClaims(), is(notNullValue()));
207+
assertThat(jwt.getClaims(), is(instanceOf(Map.class)));
208+
assertThat(jwt.getClaims().get("exp"), is(notNullValue()));
209+
assertThat(jwt.getClaims().get("iat"), is(notNullValue()));
210+
assertThat(jwt.getClaims().get("nbf"), is(notNullValue()));
211+
assertThat(jwt.getClaims().get("jti"), is(notNullValue()));
212+
assertThat(jwt.getClaims().get("aud"), is(notNullValue()));
213+
assertThat(jwt.getClaims().get("sub"), is(notNullValue()));
214+
assertThat(jwt.getClaims().get("iss"), is(notNullValue()));
215+
assertThat(jwt.getClaims().get("extraClaim"), is(notNullValue()));
216+
}
217+
201218
//Helper Methods
202219

203220
private DecodedJWT customJWT(String jsonHeader, String jsonPayload, String signature) {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.auth0.jwt.impl;
22

3+
import com.auth0.jwt.interfaces.Claim;
34
import com.fasterxml.jackson.databind.JsonNode;
45
import com.fasterxml.jackson.databind.node.TextNode;
56
import org.hamcrest.collection.IsCollectionWithSize;
@@ -153,4 +154,27 @@ public void shouldGetNotNullExtraClaimIfMissing() throws Exception {
153154
assertThat(payload.getClaim("missing"), is(notNullValue()));
154155
assertThat(payload.getClaim("missing"), is(instanceOf(NullClaim.class)));
155156
}
157+
158+
@Test
159+
public void shouldGetClaims() throws Exception {
160+
Map<String, JsonNode> tree = new HashMap<>();
161+
tree.put("extraClaim", new TextNode("extraValue"));
162+
tree.put("sub", new TextNode("auth0"));
163+
PayloadImpl payload = new PayloadImpl(null, null, null, null, null, null, null, tree);
164+
assertThat(payload, is(notNullValue()));
165+
Map<String, Claim> claims = payload.getClaims();
166+
assertThat(claims, is(notNullValue()));
167+
168+
assertThat(claims.get("extraClaim"), is(notNullValue()));
169+
assertThat(claims.get("sub"), is(notNullValue()));
170+
}
171+
172+
@Test
173+
public void shouldNotAllowToModifyClaimsMap() throws Exception {
174+
assertThat(payload, is(notNullValue()));
175+
Map<String, Claim> claims = payload.getClaims();
176+
assertThat(claims, is(notNullValue()));
177+
exception.expect(UnsupportedOperationException.class);
178+
claims.put("name", null);
179+
}
156180
}

0 commit comments

Comments
 (0)