Skip to content

Commit aa037e1

Browse files
committed
Change base64 padding logics
1 parent 72776e4 commit aa037e1

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/main/java/fi/hsl/common/redis/RedisUtils.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,23 +249,24 @@ public static Jedis createJedisClient(String cacheHostname, int port, String use
249249
public static String extractUsernameFromToken(String token) {
250250
String[] parts = token.split("\\.");
251251
String base64 = parts[1];
252-
253-
switch (base64.length() % 4) {
254-
case 2:
255-
base64 += "==";
256-
break;
257-
case 3:
258-
base64 += "=";
259-
break;
260-
}
261-
252+
253+
base64 = addPaddingToBase64String(base64);
254+
262255
byte[] jsonBytes = Base64.getDecoder().decode(base64);
263256
String json = new String(jsonBytes, StandardCharsets.UTF_8);
264257
JsonObject jwt = JsonParser.parseString(json).getAsJsonObject();
265258

266259
return jwt.get("oid").getAsString();
267260
}
268-
261+
262+
private static String addPaddingToBase64String(String input) {
263+
if (input != null && !input.isEmpty()) {
264+
int paddingLength = (4 - input.length() % 4) % 4;
265+
input += "=".repeat(paddingLength);
266+
}
267+
return input;
268+
}
269+
269270
/**
270271
* The token cache to store and proactively refresh the access token.
271272
*/

0 commit comments

Comments
 (0)