forked from GJWT/javaOIDCMsg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJWTDecoder.java
More file actions
164 lines (140 loc) · 4.82 KB
/
JWTDecoder.java
File metadata and controls
164 lines (140 loc) · 4.82 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2017 The Authors of 'JWTS for Java'
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package com.auth0.jwt;
import com.auth0.jwt.creators.EncodeType;
import com.auth0.jwt.creators.JWTCreator;
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.Base32;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.binary.StringUtils;
import java.net.URLDecoder;
import java.net.URLEncoder;
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")
public final class JWTDecoder implements DecodedJWT {
private final String[] parts;
private final Header header;
private final Payload payload;
public JWTDecoder(String jwt, EncodeType encodeType) throws Exception {
parts = TokenUtils.splitToken(jwt);
final JWTParser converter = new JWTParser();
String headerJson = null;
String payloadJson = null;
switch (encodeType) {
case Base16:
headerJson = URLDecoder.decode(new String(Hex.decodeHex(parts[0])), "UTF-8");
payloadJson = URLDecoder.decode(new String(Hex.decodeHex(parts[1])), "UTF-8");
break;
case Base32:
Base32 base32 = new Base32();
headerJson = URLDecoder.decode(new String(base32.decode(parts[0]), "UTF-8"));
payloadJson = URLDecoder.decode(new String(base32.decode(parts[1]), "UTF-8"));
break;
case Base64:
headerJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[0]));
payloadJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[1]));
break;
}
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 List<String> getIssuer() {
return payload.getIssuer();
}
@Override
public List<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]);
}
}