Add getter for all the Payload's Claims by lbalmaceda · Pull Request #124 · auth0/java-jwt · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
6 changes: 6 additions & 0 deletions lib/src/main/java/com/auth0/jwt/JWTDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -109,6 +110,11 @@ public Claim getClaim(String name) {
return payload.getClaim(name);
}

@Override
public Map<String, Claim> getClaims() {
return payload.getClaims();
}

@Override
public String getSignature() {
return signature;
Expand Down
8 changes: 8 additions & 0 deletions lib/src/main/java/com/auth0/jwt/impl/PayloadImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ public Claim getClaim(String name) {
return extractClaim(name, tree);
}

@Override
public Map<String, Claim> getClaims() {
Map<String, Claim> claims = new HashMap<>();
for (String name : tree.keySet()) {
claims.put(name, extractClaim(name, tree));
}
return Collections.unmodifiableMap(claims);
}
}
10 changes: 9 additions & 1 deletion lib/src/main/java/com/auth0/jwt/interfaces/Payload.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<String, Claim> getClaims();
}
17 changes: 17 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions lib/src/test/java/com/auth0/jwt/impl/PayloadImplTest.java