forked from auth0/java-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJWTDecoder.java
More file actions
136 lines (111 loc) · 3.17 KB
/
JWTDecoder.java
File metadata and controls
136 lines (111 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.auth0.jwt;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.impl.JWTParser;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.Header;
import com.auth0.jwt.interfaces.Payload;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import java.io.Serializable;
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.
*/
@SuppressWarnings("WeakerAccess")
final class JWTDecoder implements DecodedJWT, Serializable {
private static final long serialVersionUID = 1873362438023312895L;
private final String[] parts;
private final Header header;
private final Payload payload;
JWTDecoder(String jwt) throws JWTDecodeException {
this(new JWTParser(), jwt);
}
JWTDecoder(JWTParser converter, String jwt) throws JWTDecodeException {
parts = TokenUtils.splitToken(jwt);
String headerJson;
String payloadJson;
try {
headerJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[0]));
payloadJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[1]));
} catch (NullPointerException e) {
throw new JWTDecodeException("The UTF-8 Charset isn't initialized.", e);
}
header = converter.parseHeader(headerJson);
payload = converter.parsePayload(payloadJson);
}
@Override
public String getAlgorithm() {
return header.getAlgorithm();
}
@Override
public String getType() {
return header.getType();
}
@Override
public String getContentType() {
return header.getContentType();
}
@Override
public String getKeyId() {
return header.getKeyId();
}
@Override
public Claim getHeaderClaim(String name) {
return header.getHeaderClaim(name);
}
@Override
public String getIssuer() {
return payload.getIssuer();
}
@Override
public String getSubject() {
return payload.getSubject();
}
@Override
public List<String> getAudience() {
return payload.getAudience();
}
@Override
public Date getExpiresAt() {
return payload.getExpiresAt();
}
@Override
public Date getNotBefore() {
return payload.getNotBefore();
}
@Override
public Date getIssuedAt() {
return payload.getIssuedAt();
}
@Override
public String getId() {
return payload.getId();
}
@Override
public Claim getClaim(String name) {
return payload.getClaim(name);
}
@Override
public Map<String, Claim> getClaims() {
return payload.getClaims();
}
@Override
public String getHeader() {
return parts[0];
}
@Override
public String getPayload() {
return parts[1];
}
@Override
public String getSignature() {
return parts[2];
}
@Override
public String getToken() {
return String.format("%s.%s.%s", parts[0], parts[1], parts[2]);
}
}