Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getter for all the Payload's Claims #124

Merged
merged 1 commit into from
Dec 14, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Claim> claims = jwt.getClaims(); //Key is the Claim name
Claim claim = claims.get("isAdmin");
```
al
or

```java
Claim claim = jwt.getClaim("isAdmin");
Expand Down
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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String, JsonNode> 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<String, Claim> 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<String, Claim> claims = payload.getClaims();
assertThat(claims, is(notNullValue()));
exception.expect(UnsupportedOperationException.class);
claims.put("name", null);
}
}