Skip to content

Commit a2dc736

Browse files
committed
keep public claims on claims tree
1 parent a007aaa commit a2dc736

File tree

5 files changed

+19
-20
lines changed

5 files changed

+19
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ String keyId = jwt.getKeyId();
189189

190190
#### Private Claims
191191

192-
Additional Claims defined in the token's Header can be obtained by calling `getHeaderClaim()` and passing the Claim name. A Claim will always be returned, even if it can't be found. You should always check for null values.
192+
Additional Claims defined in the token's Header can be obtained by calling `getHeaderClaim()` 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()`.
193193

194194
```java
195195
Claim claim = jwt.getHeaderClaim("owner");
@@ -256,7 +256,7 @@ 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 should always check for null values.
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()`.
260260

261261
```java
262262
Claim claim = jwt.getClaim("isAdmin");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public BasicHeader deserialize(JsonParser p, DeserializationContext ctxt) throws
3636
}
3737

3838
String getString(Map<String, JsonNode> tree, String claimName) {
39-
JsonNode node = tree.remove(claimName);
39+
JsonNode node = tree.get(claimName);
4040
if (node == null || node.isNull()) {
4141
return null;
4242
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Payload deserialize(JsonParser p, DeserializationContext ctxt) throws IOE
4343
}
4444

4545
List<String> getStringOrArray(Map<String, JsonNode> tree, String claimName) throws JWTDecodeException {
46-
JsonNode node = tree.remove(claimName);
46+
JsonNode node = tree.get(claimName);
4747
if (node == null || node.isNull() || !(node.isArray() || node.isTextual())) {
4848
return null;
4949
}
@@ -64,7 +64,7 @@ List<String> getStringOrArray(Map<String, JsonNode> tree, String claimName) thro
6464
}
6565

6666
Date getDateFromSeconds(Map<String, JsonNode> tree, String claimName) {
67-
JsonNode node = tree.remove(claimName);
67+
JsonNode node = tree.get(claimName);
6868
if (node == null || node.isNull() || !node.canConvertToLong()) {
6969
return null;
7070
}
@@ -73,7 +73,7 @@ Date getDateFromSeconds(Map<String, JsonNode> tree, String claimName) {
7373
}
7474

7575
String getString(Map<String, JsonNode> tree, String claimName) {
76-
JsonNode node = tree.remove(claimName);
76+
JsonNode node = tree.get(claimName);
7777
if (node == null || node.isNull()) {
7878
return null;
7979
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void shouldThrowOnNullTree() throws Exception {
5858

5959

6060
@Test
61-
public void shouldRemoveKnownPublicClaimsFromTree() throws Exception {
61+
public void shouldNotRemoveKnownPublicClaimsFromTree() throws Exception {
6262
String headerJSON = "{\n" +
6363
" \"alg\": \"HS256\",\n" +
6464
" \"typ\": \"jws\",\n" +
@@ -80,10 +80,10 @@ public void shouldRemoveKnownPublicClaimsFromTree() throws Exception {
8080
assertThat(header.getKeyId(), is("key"));
8181

8282
assertThat(header.getHeaderClaim("roles").asString(), is("admin"));
83-
assertThat(header.getHeaderClaim("alg").isNull(), is(true));
84-
assertThat(header.getHeaderClaim("typ").isNull(), is(true));
85-
assertThat(header.getHeaderClaim("cty").isNull(), is(true));
86-
assertThat(header.getHeaderClaim("kid").isNull(), is(true));
83+
assertThat(header.getHeaderClaim("alg").asString(), is("HS256"));
84+
assertThat(header.getHeaderClaim("typ").asString(), is("jws"));
85+
assertThat(header.getHeaderClaim("cty").asString(), is("content"));
86+
assertThat(header.getHeaderClaim("kid").asString(), is("key"));
8787
}
8888

8989
@Test

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void shouldThrowWhenParsingArrayWithObjectValue() throws Exception {
7171
}
7272

7373
@Test
74-
public void shouldRemoveKnownPublicClaimsFromTree() throws Exception {
74+
public void shouldNotRemoveKnownPublicClaimsFromTree() throws Exception {
7575
String payloadJSON = "{\n" +
7676
" \"iss\": \"auth0\",\n" +
7777
" \"sub\": \"emails\",\n" +
@@ -99,14 +99,13 @@ public void shouldRemoveKnownPublicClaimsFromTree() throws Exception {
9999
assertThat(payload.getId(), is("idid"));
100100

101101
assertThat(payload.getClaim("roles").asString(), is("admin"));
102-
assertThat(payload.getClaim("iss").isNull(), is(true));
103-
assertThat(payload.getClaim("sub").isNull(), is(true));
104-
assertThat(payload.getClaim("aud").isNull(), is(true));
105-
assertThat(payload.getClaim("iat").isNull(), is(true));
106-
assertThat(payload.getClaim("exp").isNull(), is(true));
107-
assertThat(payload.getClaim("nbf").isNull(), is(true));
108-
assertThat(payload.getClaim("jti").isNull(), is(true));
109-
102+
assertThat(payload.getClaim("iss").asString(), is("auth0"));
103+
assertThat(payload.getClaim("sub").asString(), is("emails"));
104+
assertThat(payload.getClaim("aud").asString(), is("users"));
105+
assertThat(payload.getClaim("iat").asDouble(), is(10101010D));
106+
assertThat(payload.getClaim("exp").asDouble(), is(11111111D));
107+
assertThat(payload.getClaim("nbf").asDouble(), is(10101011D));
108+
assertThat(payload.getClaim("jti").asString(), is("idid"));
110109
}
111110

112111
@Test

0 commit comments

Comments
 (0)