Skip to content

Commit 3eac127

Browse files
committed
refactor PayloadImpl
1 parent 9a825f8 commit 3eac127

3 files changed

Lines changed: 77 additions & 29 deletions

File tree

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import com.auth0.jwtdecodejava.exceptions.JWTException;
44
import com.auth0.jwtdecodejava.interfaces.Payload;
55
import com.fasterxml.jackson.core.JsonParser;
6+
import com.fasterxml.jackson.core.JsonProcessingException;
67
import com.fasterxml.jackson.core.type.TypeReference;
78
import com.fasterxml.jackson.databind.DeserializationContext;
89
import com.fasterxml.jackson.databind.JsonNode;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
911
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
1012

1113
import java.io.IOException;
14+
import java.util.Date;
1215
import java.util.Map;
1316

1417
public class PayloadDeserializer extends StdDeserializer<Payload> {
@@ -28,6 +31,53 @@ public Payload deserialize(JsonParser p, DeserializationContext ctxt) throws IOE
2831
if (tree == null) {
2932
throw new JWTException("Null map");
3033
}
31-
return new PayloadImpl(tree);
34+
35+
String issuer = removeString(tree, Claims.ISSUER);
36+
String subject = removeString(tree, Claims.SUBJECT);
37+
String[] audience = removeStringOrArray(tree, Claims.AUDIENCE);
38+
Date expiresAt = removeDate(tree, Claims.EXPIRES_AT);
39+
Date notBefore = removeDate(tree, Claims.NOT_BEFORE);
40+
Date issuedAt = removeDate(tree, Claims.ISSUED_AT);
41+
String jwtId = removeString(tree, Claims.JWT_ID);
42+
43+
return new PayloadImpl(issuer, subject, audience, expiresAt, notBefore, issuedAt, jwtId, tree);
44+
}
45+
46+
private String[] removeStringOrArray(Map<String, JsonNode> tree, String claimName) {
47+
JsonNode node = tree.remove(claimName);
48+
if (node == null || node.isNull() || !(node.isArray() || node.isTextual())) {
49+
return null;
50+
}
51+
if (node.isTextual() && !node.asText().isEmpty()) {
52+
return new String[]{node.asText()};
53+
}
54+
55+
ObjectMapper mapper = new ObjectMapper();
56+
String[] arr = new String[node.size()];
57+
for (int i = 0; i < node.size(); i++) {
58+
try {
59+
arr[i] = mapper.treeToValue(node.get(i), String.class);
60+
} catch (JsonProcessingException e) {
61+
throw new JWTException("Couldn't map the Claim's array contents to String", e);
62+
}
63+
}
64+
return arr;
65+
}
66+
67+
private Date removeDate(Map<String, JsonNode> tree, String claimName) {
68+
JsonNode node = tree.remove(claimName);
69+
if (node == null || node.isNull() || !node.canConvertToLong()) {
70+
return null;
71+
}
72+
final long ms = node.asLong() * 1000;
73+
return new Date(ms);
74+
}
75+
76+
private String removeString(Map<String, JsonNode> tree, String claimName) {
77+
JsonNode node = tree.remove(claimName);
78+
if (node == null || node.isNull()) {
79+
return null;
80+
}
81+
return node.asText(null);
3282
}
3383
}
Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.auth0.jwtdecodejava.impl;
22

3-
import com.auth0.jwtdecodejava.exceptions.JWTException;
43
import com.auth0.jwtdecodejava.interfaces.Claim;
54
import com.auth0.jwtdecodejava.interfaces.Payload;
65
import com.fasterxml.jackson.databind.JsonNode;
@@ -9,68 +8,67 @@
98
import java.util.Date;
109
import java.util.Map;
1110

12-
import static com.auth0.jwtdecodejava.impl.ClaimImpl.claimFromNode;
1311
import static com.auth0.jwtdecodejava.impl.ClaimImpl.extractClaim;
14-
import static com.auth0.jwtdecodejava.impl.Claims.*;
1512

1613
class PayloadImpl implements Payload {
17-
private Map<String, JsonNode> tree;
14+
private final String issuer;
15+
private final String subject;
16+
private final String[] audience;
17+
private final Date expiresAt;
18+
private final Date notBefore;
19+
private final Date issuedAt;
20+
private final String jwtId;
21+
private final Map<String, JsonNode> extras;
1822

19-
PayloadImpl(Map<String, JsonNode> tree) {
20-
this.tree = tree;
23+
PayloadImpl(String issuer, String subject, String[] audience, Date expiresAt, Date notBefore, Date issuedAt, String jwtId, Map<String, JsonNode> extras) {
24+
this.issuer = issuer;
25+
this.subject = subject;
26+
this.audience = audience;
27+
this.expiresAt = expiresAt;
28+
this.notBefore = notBefore;
29+
this.issuedAt = issuedAt;
30+
this.jwtId = jwtId;
31+
this.extras = extras;
2132
}
2233

2334
@Override
2435
public String getIssuer() {
25-
return extractClaim(ISSUER, tree).asString();
36+
return issuer;
2637
}
2738

2839
@Override
2940
public String getSubject() {
30-
return extractClaim(SUBJECT, tree).asString();
41+
return subject;
3142
}
3243

3344
@Override
3445
public String[] getAudience() {
35-
JsonNode audNode = tree.get(AUDIENCE);
36-
if (audNode == null || audNode.isNull()) {
37-
return new String[]{};
38-
}
39-
if (audNode.isTextual() && !audNode.asText().isEmpty()) {
40-
return new String[]{audNode.asText()};
41-
}
42-
Claim claim = claimFromNode(audNode);
43-
try {
44-
return claim.asArray(String.class);
45-
} catch (Exception e) {
46-
e.printStackTrace();
47-
throw new JWTException("The Audience contained invalid values.", e);
48-
}
46+
return audience;
4947
}
5048

5149
@Override
5250
public Date getExpiresAt() {
53-
return extractClaim(EXPIRES_AT, tree).asDate();
51+
return expiresAt;
5452
}
5553

5654
@Override
5755
public Date getNotBefore() {
58-
return extractClaim(NOT_BEFORE, tree).asDate();
56+
return notBefore;
5957
}
6058

6159
@Override
6260
public Date getIssuedAt() {
63-
return extractClaim(ISSUED_AT, tree).asDate();
61+
return issuedAt;
6462
}
6563

6664
@Override
6765
public String getId() {
68-
return extractClaim(JWT_ID, tree).asString();
66+
return jwtId;
6967
}
7068

7169
@Override
7270
public Claim getClaim(@NotNull String name) {
73-
return extractClaim(name, tree);
71+
return extractClaim(name, extras);
7472
}
7573

7674
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,5 @@ public interface Payload {
7171
* @return the Claim if found or null.
7272
*/
7373
@Nullable
74-
public Claim getClaim(@NotNull String name);
74+
Claim getClaim(@NotNull String name);
7575
}

0 commit comments

Comments
 (0)