Skip to content

Commit e8c124d

Browse files
author
Zihlu Wang
committed
fix: Fix the issue that reports illegal base64 character when decoding a JWT Payload
1 parent e1f2b93 commit e8c124d

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

devkit-utils/src/main/java/cn/org/codecrafters/devkit/utils/Base64Util.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public final class Base64Util {
6060

6161
private static Base64.Decoder decoder;
6262

63+
private static Base64.Encoder urlEncoder;
64+
65+
private static Base64.Decoder urlDecoder;
66+
6367
/**
6468
* Ensure that there is only one Base64 Encoder.
6569
*
@@ -84,6 +88,20 @@ private static Base64.Decoder getDecoder() {
8488
return decoder;
8589
}
8690

91+
private static Base64.Encoder getUrlEncoder() {
92+
if (Objects.isNull(urlEncoder)) {
93+
urlEncoder = Base64.getUrlEncoder();
94+
}
95+
return urlEncoder;
96+
}
97+
98+
public static Base64.Decoder getUrlDecoder() {
99+
if (Objects.isNull(urlDecoder)) {
100+
urlDecoder = Base64.getUrlDecoder();
101+
}
102+
return urlDecoder;
103+
}
104+
87105
/**
88106
* Private constructor to prevent instantiation of the class.
89107
*/
@@ -136,4 +154,50 @@ public static String decode(String value) {
136154
return decode(value, StandardCharsets.UTF_8);
137155
}
138156

157+
/**
158+
* Encodes the given string using the specified charset.
159+
*
160+
* @param value the string to be encoded
161+
* @param charset the charset to be used for encoding
162+
* @return the Base64 encoded string
163+
*/
164+
public static String encodeUrlComponents(String value, Charset charset) {
165+
var encoded = getUrlEncoder().encode(value.getBytes(charset));
166+
167+
return new String(encoded);
168+
}
169+
170+
/**
171+
* Encodes the given string using the default UTF-8 charset.
172+
*
173+
* @param value the string to be encoded
174+
* @return the Base64 encoded string
175+
*/
176+
public static String encodeUrlComponents(String value) {
177+
return encodeUrlComponents(value, StandardCharsets.UTF_8);
178+
}
179+
180+
/**
181+
* Decodes the given Base64 encoded string using the specified charset.
182+
*
183+
* @param value the Base64 encoded string to be decoded
184+
* @param charset the charset to be used for decoding
185+
* @return the decoded string
186+
*/
187+
public static String decodeUrlComponents(String value, Charset charset) {
188+
var decoded = getUrlDecoder().decode(value.getBytes(charset));
189+
190+
return new String(decoded);
191+
}
192+
193+
/**
194+
* Decodes the given Base64 encoded string using the default UTF-8 charset.
195+
*
196+
* @param value the Base64 encoded string to be decoded
197+
* @return the decoded string
198+
*/
199+
public static String decodeUrlComponents(String value) {
200+
return decodeUrlComponents(value, StandardCharsets.UTF_8);
201+
}
202+
139203
}

simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/AuthzeroTokenResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ public DecodedJWT resolve(String token) {
429429
public <T extends TokenPayload> T extract(String token, Class<T> targetType) {
430430
try {
431431
// Get claims from token.
432-
var payloads = objectMapper.readValue(Base64Util.decode(resolve(token).getPayload()), new MapTypeReference());
432+
var payloads = objectMapper.readValue(Base64Util.decodeUrlComponents(resolve(token).getPayload()), new MapTypeReference());
433433
// Get the no-argument constructor to create an instance.
434434
var bean = targetType.getConstructor().newInstance();
435435

@@ -478,7 +478,7 @@ public String renew(String oldToken, Duration expireAfter) {
478478
var resolved = resolve(oldToken);
479479

480480
try {
481-
var payload = objectMapper.readValue(Base64Util.decode(resolved.getPayload()), ObjectNode.class);
481+
var payload = objectMapper.readValue(Base64Util.decodeUrlComponents(resolved.getPayload()), ObjectNode.class);
482482
payload.remove(PredefinedKeys.KEYS);
483483

484484
var payloadMap = objectMapper.convertValue(payload, new MapTypeReference());

0 commit comments

Comments
 (0)