Skip to content

Commit f3a6ea1

Browse files
authored
fix: fix handling of b64 and b64url encoded access tokens (#767)
1 parent 6aac895 commit f3a6ea1

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [unreleased]
99

10+
## [6.0.10] - 2023-08-16
11+
12+
- Fixed an encoding/decoding issue for certain access token payloads
13+
1014
## [6.0.9] - 2023-08-14
1115

1216
- Now using decimal notation to add numbers into the access token payload (instead of scientific notation)

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ compileTestJava { options.encoding = "UTF-8" }
1919
// }
2020
//}
2121

22-
version = "6.0.9"
22+
version = "6.0.10"
2323

2424

2525
repositories {

src/main/java/io/supertokens/utils/Utils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ public static String convertToBase64(String str) {
7777
return new String(Base64.getEncoder().encode(stringToBytes(str)), StandardCharsets.UTF_8);
7878
}
7979

80+
// This function deserializes both B64 and B64URL encodings
8081
public static String convertFromBase64(String str) {
81-
return new String(Base64.getDecoder().decode(stringToBytes(str)), StandardCharsets.UTF_8);
82+
return new String(Base64.getDecoder().decode(stringToBytes(str.replace("-", "+").replace("_", "/"))), StandardCharsets.UTF_8);
8283
}
8384

8485
public static String throwableStacktraceToString(Throwable e) {

src/test/java/io/supertokens/test/session/AccessTokenTest.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ public void inputOutputTest() throws Exception {
257257
EventAndException e = process.checkOrWaitForEvent(PROCESS_STATE.STARTED);
258258
assertNotNull(e);
259259
JsonObject jsonObj = new JsonObject();
260-
jsonObj.addProperty("key", "value");
260+
String testValue = "asdf???123";
261+
jsonObj.addProperty("key", testValue);
261262

262263
// db key
263264
long expiryTime = System.currentTimeMillis() + 1000;
@@ -269,7 +270,7 @@ public void inputOutputTest() throws Exception {
269270
assertEquals("userId", info.userId);
270271
assertEquals("refreshTokenHash1", info.refreshTokenHash1);
271272
assertEquals("parentRefreshTokenHash1", info.parentRefreshTokenHash1);
272-
assertEquals("value", info.userData.get("key").getAsString());
273+
assertEquals(testValue, info.userData.get("key").getAsString());
273274
assertEquals("antiCsrfToken", info.antiCsrfToken);
274275
assertEquals(expiryTime / 1000 * 1000, info.expiryTime);
275276

@@ -292,19 +293,21 @@ public void inputOutputTestStatic() throws Exception {
292293
EventAndException e = process.checkOrWaitForEvent(PROCESS_STATE.STARTED);
293294
assertNotNull(e);
294295
JsonObject jsonObj = new JsonObject();
295-
jsonObj.addProperty("key", "value");
296+
String testValue = "asdf???123";
297+
jsonObj.addProperty("key", testValue);
296298

297299
// db key
298300
long expiryTime = System.currentTimeMillis() + 1000;
299301
TokenInfo newToken = AccessToken.createNewAccessToken(process.getProcess(), "sessionHandle", "userId",
300302
"refreshTokenHash1", "parentRefreshTokenHash1", jsonObj, "antiCsrfToken", expiryTime,
301303
AccessToken.getLatestVersion(), true);
304+
System.out.println(newToken.token);
302305
AccessTokenInfo info = AccessToken.getInfoFromAccessToken(process.getProcess(), newToken.token, true);
303306
assertEquals("sessionHandle", info.sessionHandle);
304307
assertEquals("userId", info.userId);
305308
assertEquals("refreshTokenHash1", info.refreshTokenHash1);
306309
assertEquals("parentRefreshTokenHash1", info.parentRefreshTokenHash1);
307-
assertEquals("value", info.userData.get("key").getAsString());
310+
assertEquals(testValue, info.userData.get("key").getAsString());
308311
assertEquals("antiCsrfToken", info.antiCsrfToken);
309312
assertEquals(expiryTime / 1000 * 1000, info.expiryTime);
310313

@@ -326,7 +329,8 @@ public void inputOutputTestV2() throws Exception {
326329
EventAndException e = process.checkOrWaitForEvent(PROCESS_STATE.STARTED);
327330
assertNotNull(e);
328331
JsonObject jsonObj = new JsonObject();
329-
jsonObj.addProperty("key", "value");
332+
String testValue = "asdf???123";
333+
jsonObj.addProperty("key", testValue);
330334

331335
// db key
332336
long expiryTime = System.currentTimeMillis() + 1000;
@@ -338,7 +342,7 @@ public void inputOutputTestV2() throws Exception {
338342
assertEquals("userId", info.userId);
339343
assertEquals("refreshTokenHash1", info.refreshTokenHash1);
340344
assertEquals("parentRefreshTokenHash1", info.parentRefreshTokenHash1);
341-
assertEquals("value", info.userData.get("key").getAsString());
345+
assertEquals(testValue, info.userData.get("key").getAsString());
342346
assertEquals("antiCsrfToken", info.antiCsrfToken);
343347
assertEquals(expiryTime, info.expiryTime);
344348

@@ -360,7 +364,8 @@ public void inputOutputTestv1() throws InterruptedException, InvalidKeyException
360364
EventAndException e = process.checkOrWaitForEvent(PROCESS_STATE.STARTED);
361365
assertNotNull(e);
362366
JsonObject jsonObj = new JsonObject();
363-
jsonObj.addProperty("key", "value");
367+
String testValue = "asdf???123";
368+
jsonObj.addProperty("key", testValue);
364369

365370
// db key
366371
TokenInfo newToken = AccessToken.createNewAccessTokenV1(process.getProcess(), "sessionHandle", "userId",
@@ -370,7 +375,7 @@ public void inputOutputTestv1() throws InterruptedException, InvalidKeyException
370375
assertEquals("userId", info.userId);
371376
assertEquals("refreshTokenHash1", info.refreshTokenHash1);
372377
assertEquals("parentRefreshTokenHash1", info.parentRefreshTokenHash1);
373-
assertEquals("value", info.userData.get("key").getAsString());
378+
assertEquals(testValue, info.userData.get("key").getAsString());
374379
assertEquals("antiCsrfToken", info.antiCsrfToken);
375380

376381
JsonObject payload = (JsonObject) new JsonParser()

0 commit comments

Comments
 (0)