From ce9a6120e3a6dabb9fc014c799e52894920e6f4e Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Wed, 14 Dec 2016 16:15:37 -0300 Subject: [PATCH] add getter for payload's Claims map --- README.md | 9 ++++++- .../main/java/com/auth0/jwt/JWTDecoder.java | 6 +++++ .../java/com/auth0/jwt/impl/PayloadImpl.java | 8 +++++++ .../com/auth0/jwt/interfaces/Payload.java | 10 +++++++- .../java/com/auth0/jwt/JWTDecoderTest.java | 17 +++++++++++++ .../com/auth0/jwt/impl/PayloadImplTest.java | 24 +++++++++++++++++++ 6 files changed, 72 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index af74bd18..f81d4c1b 100644 --- a/README.md +++ b/README.md @@ -256,7 +256,14 @@ String id = jwt.getId(); #### Private Claims -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()`. +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()`. + +```java +Map claims = jwt.getClaims(); //Key is the Claim name +Claim claim = claims.get("isAdmin"); +``` +al +or ```java Claim claim = jwt.getClaim("isAdmin"); diff --git a/lib/src/main/java/com/auth0/jwt/JWTDecoder.java b/lib/src/main/java/com/auth0/jwt/JWTDecoder.java index 1c0c0afc..b7540287 100644 --- a/lib/src/main/java/com/auth0/jwt/JWTDecoder.java +++ b/lib/src/main/java/com/auth0/jwt/JWTDecoder.java @@ -11,6 +11,7 @@ import java.util.Date; import java.util.List; +import java.util.Map; /** * 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) { return payload.getClaim(name); } + @Override + public Map getClaims() { + return payload.getClaims(); + } + @Override public String getSignature() { return signature; diff --git a/lib/src/main/java/com/auth0/jwt/impl/PayloadImpl.java b/lib/src/main/java/com/auth0/jwt/impl/PayloadImpl.java index a4998942..dced3062 100644 --- a/lib/src/main/java/com/auth0/jwt/impl/PayloadImpl.java +++ b/lib/src/main/java/com/auth0/jwt/impl/PayloadImpl.java @@ -76,4 +76,12 @@ public Claim getClaim(String name) { return extractClaim(name, tree); } + @Override + public Map getClaims() { + Map claims = new HashMap<>(); + for (String name : tree.keySet()) { + claims.put(name, extractClaim(name, tree)); + } + return Collections.unmodifiableMap(claims); + } } diff --git a/lib/src/main/java/com/auth0/jwt/interfaces/Payload.java b/lib/src/main/java/com/auth0/jwt/interfaces/Payload.java index 2fd93cc2..0f639ab9 100644 --- a/lib/src/main/java/com/auth0/jwt/interfaces/Payload.java +++ b/lib/src/main/java/com/auth0/jwt/interfaces/Payload.java @@ -2,6 +2,7 @@ import java.util.Date; import java.util.List; +import java.util.Map; /** * The Payload class represents the 2nd part of the JWT, where the Payload value is hold. @@ -58,10 +59,17 @@ public interface Payload { String getId(); /** - * Get a Private Claim given it's name. If the Claim wasn't specified in the Payload, a NullClaim will be returned. + * Get a Claim given it's name. If the Claim wasn't specified in the Payload, a NullClaim will be returned. * * @param name the name of the Claim to retrieve. * @return a non-null Claim. */ Claim getClaim(String name); + + /** + * Get the Claims defined in the Token. + * + * @return a non-null Map containing the Claims defined in the Token. + */ + Map getClaims(); } diff --git a/lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java b/lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java index b3e74f6e..a4ab4376 100644 --- a/lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java +++ b/lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java @@ -13,6 +13,7 @@ import java.nio.charset.StandardCharsets; import java.util.Date; +import java.util.Map; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -198,6 +199,22 @@ public void shouldGetNullClaimIfClaimValueIsNull() throws Exception { assertThat(jwt.getClaim("object").isNull(), is(true)); } + @Test + public void shouldGetAvailableClaims() throws Exception { + DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxMjM0NTY3ODkwIiwiaWF0IjoiMTIzNDU2Nzg5MCIsIm5iZiI6IjEyMzQ1Njc4OTAiLCJqdGkiOiJodHRwczovL2p3dC5pby8iLCJhdWQiOiJodHRwczovL2RvbWFpbi5hdXRoMC5jb20iLCJzdWIiOiJsb2dpbiIsImlzcyI6ImF1dGgwIiwiZXh0cmFDbGFpbSI6IkpvaG4gRG9lIn0.TX9Ct4feGp9YyeGK9Zl91tO0YBOrguJ4As9jeqgHdZQ"); + assertThat(jwt, is(notNullValue())); + assertThat(jwt.getClaims(), is(notNullValue())); + assertThat(jwt.getClaims(), is(instanceOf(Map.class))); + assertThat(jwt.getClaims().get("exp"), is(notNullValue())); + assertThat(jwt.getClaims().get("iat"), is(notNullValue())); + assertThat(jwt.getClaims().get("nbf"), is(notNullValue())); + assertThat(jwt.getClaims().get("jti"), is(notNullValue())); + assertThat(jwt.getClaims().get("aud"), is(notNullValue())); + assertThat(jwt.getClaims().get("sub"), is(notNullValue())); + assertThat(jwt.getClaims().get("iss"), is(notNullValue())); + assertThat(jwt.getClaims().get("extraClaim"), is(notNullValue())); + } + //Helper Methods private DecodedJWT customJWT(String jsonHeader, String jsonPayload, String signature) { diff --git a/lib/src/test/java/com/auth0/jwt/impl/PayloadImplTest.java b/lib/src/test/java/com/auth0/jwt/impl/PayloadImplTest.java index 6563f8e8..ca7562dc 100644 --- a/lib/src/test/java/com/auth0/jwt/impl/PayloadImplTest.java +++ b/lib/src/test/java/com/auth0/jwt/impl/PayloadImplTest.java @@ -1,5 +1,6 @@ package com.auth0.jwt.impl; +import com.auth0.jwt.interfaces.Claim; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.TextNode; import org.hamcrest.collection.IsCollectionWithSize; @@ -153,4 +154,27 @@ public void shouldGetNotNullExtraClaimIfMissing() throws Exception { assertThat(payload.getClaim("missing"), is(notNullValue())); assertThat(payload.getClaim("missing"), is(instanceOf(NullClaim.class))); } + + @Test + public void shouldGetClaims() throws Exception { + Map tree = new HashMap<>(); + tree.put("extraClaim", new TextNode("extraValue")); + tree.put("sub", new TextNode("auth0")); + PayloadImpl payload = new PayloadImpl(null, null, null, null, null, null, null, tree); + assertThat(payload, is(notNullValue())); + Map claims = payload.getClaims(); + assertThat(claims, is(notNullValue())); + + assertThat(claims.get("extraClaim"), is(notNullValue())); + assertThat(claims.get("sub"), is(notNullValue())); + } + + @Test + public void shouldNotAllowToModifyClaimsMap() throws Exception { + assertThat(payload, is(notNullValue())); + Map claims = payload.getClaims(); + assertThat(claims, is(notNullValue())); + exception.expect(UnsupportedOperationException.class); + claims.put("name", null); + } } \ No newline at end of file