Skip to content

Commit 9d4f931

Browse files
authored
fix: updates access token type (#535)
* fixes access token type * updates CHANGELOG.md and adds test * adds additional test * updates build.gradle * adds more tests * adds more tests
1 parent 855f7bd commit 9d4f931

File tree

5 files changed

+124
-2
lines changed

5 files changed

+124
-2
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+
## [4.2.1] - 2022-11-24
11+
12+
- Updates the type of `access_token_validity` in the CoreConfig from `int` to `long`
13+
1014
## [4.2.0] - 2022-11-07
1115

1216
- Update dependencies for security updates: https://github.com/supertokens/supertokens-core/issues/525

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 = "4.2.0"
22+
version = "4.2.1"
2323

2424

2525
repositories {

src/main/java/io/supertokens/config/CoreConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class CoreConfig {
3636
private int core_config_version = -1;
3737

3838
@JsonProperty
39-
private int access_token_validity = 3600; // in seconds
39+
private long access_token_validity = 3600; // in seconds
4040

4141
@JsonProperty
4242
private boolean access_token_blacklisting = false;

src/test/java/io/supertokens/test/jwt/JWTCreateTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.Test;
3232
import org.junit.rules.TestRule;
3333

34+
import static org.junit.Assert.assertEquals;
3435
import static org.junit.Assert.assertNotNull;
3536
import static org.junit.Assert.fail;
3637

@@ -69,6 +70,35 @@ public void testNormalFunctioningOfCreateToken() throws Exception {
6970
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
7071
}
7172

73+
/**
74+
* Call JWTSigningFunctions.createJWTToken with valid params and long validity and ensure that it does not throw any
75+
* errors
76+
*/
77+
@Test
78+
public void testNormalFunctioningOfCreateTokenWithLongValidity() throws Exception {
79+
String[] args = { "../" };
80+
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
81+
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));
82+
83+
String algorithm = "RS256";
84+
JsonObject payload = new JsonObject();
85+
payload.addProperty("customClaim", "customValue");
86+
String jwksDomain = "http://localhost";
87+
long validity = 63072000;
88+
89+
String jwt = JWTSigningFunctions.createJWTToken(process.getProcess(), algorithm, payload, jwksDomain, validity);
90+
91+
DecodedJWT decodedJWT = JWT.decode(jwt);
92+
93+
// compares the (expiry time in seconds) - (issued at time in seconds) -1
94+
// 1 is added to expiry time to make sure expiry is atleast 1 second
95+
assertEquals((decodedJWT.getExpiresAt().getTime() / 1000 - decodedJWT.getIssuedAt().getTime() / 1000) - 1,
96+
validity);
97+
98+
process.kill();
99+
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
100+
}
101+
72102
/**
73103
* Trying to create a JWT with an unsupported algorithm should throw an error
74104
*/

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package io.supertokens.test.session;
1818

1919
import com.google.gson.JsonObject;
20+
import com.google.gson.JsonParser;
21+
2022
import io.supertokens.ProcessState;
2123
import io.supertokens.exceptions.TokenTheftDetectedException;
2224
import io.supertokens.exceptions.TryRefreshTokenException;
@@ -25,6 +27,7 @@
2527
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
2628
import io.supertokens.session.Session;
2729
import io.supertokens.session.accessToken.AccessTokenSigningKey;
30+
import io.supertokens.session.info.SessionInfo;
2831
import io.supertokens.session.info.SessionInformationHolder;
2932
import io.supertokens.storageLayer.StorageLayer;
3033
import io.supertokens.test.TestingProcessManager;
@@ -367,4 +370,89 @@ public void checkThatExpiredSessionIsNotReturnedForUserNorCanItBeUpdated() throw
367370
process.kill();
368371
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
369372
}
373+
374+
// session tests with long access and refresh token lifetimes
375+
@Test
376+
public void testCreatingSessionsWithLongAccessAndRefreshTokenLifeTimes() throws Exception {
377+
378+
Utils.setValueInConfig("access_token_validity", "63072000"); // 2 years in seconds
379+
Utils.setValueInConfig("refresh_token_validity", "1051200"); // 2 years in minutes
380+
381+
String[] args = { "../" };
382+
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
383+
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));
384+
385+
SessionInformationHolder sessionInfo = Session.createNewSession(process.getProcess(), "user", new JsonObject(),
386+
new JsonObject());
387+
long twoYearsInSeconds = 63072000;
388+
389+
assertEquals(sessionInfo.accessToken.expiry - sessionInfo.accessToken.createdTime, twoYearsInSeconds * 1000);
390+
assertEquals(sessionInfo.refreshToken.expiry - sessionInfo.refreshToken.createdTime, twoYearsInSeconds * 1000);
391+
392+
process.kill();
393+
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
394+
}
395+
396+
@Test
397+
public void testCreatingSessionsWithLongAccessAndRefreshTokenLifeTimesAndRefreshingTokens() throws Exception {
398+
399+
Utils.setValueInConfig("access_token_validity", "63072000"); // 2 years in seconds
400+
Utils.setValueInConfig("refresh_token_validity", "1051200"); // 2 years in minutes
401+
402+
String[] args = { "../" };
403+
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
404+
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));
405+
406+
SessionInformationHolder sessionInfo = Session.createNewSession(process.getProcess(), "user", new JsonObject(),
407+
new JsonObject());
408+
long twoYearsInSeconds = 63072000;
409+
410+
assertEquals(sessionInfo.accessToken.expiry - sessionInfo.accessToken.createdTime, twoYearsInSeconds * 1000);
411+
assertEquals(sessionInfo.refreshToken.expiry - sessionInfo.refreshToken.createdTime, twoYearsInSeconds * 1000);
412+
413+
SessionInformationHolder sessionInfo2 = Session.refreshSession(process.main, sessionInfo.refreshToken.token,
414+
null, false);
415+
416+
assertFalse(sessionInfo.accessToken.token.equals(sessionInfo2.accessToken.token));
417+
assertFalse(sessionInfo.refreshToken.token.equals(sessionInfo2.refreshToken.token));
418+
419+
assertEquals(sessionInfo2.accessToken.expiry - sessionInfo2.accessToken.createdTime, twoYearsInSeconds * 1000);
420+
assertEquals(sessionInfo2.refreshToken.expiry - sessionInfo2.refreshToken.createdTime,
421+
twoYearsInSeconds * 1000);
422+
423+
process.kill();
424+
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
425+
}
426+
427+
@Test
428+
public void createNewSessionAndUpdateSession() throws Exception {
429+
430+
Utils.setValueInConfig("access_token_validity", "63072000"); // 2 years in seconds
431+
Utils.setValueInConfig("refresh_token_validity", "1051200"); // 2 years in minutes
432+
String[] args = { "../" };
433+
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
434+
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));
435+
436+
SessionInformationHolder sessionInfo = Session.createNewSession(process.getProcess(), "user", new JsonObject(),
437+
new JsonObject());
438+
long twoYearsInSeconds = 63072000;
439+
440+
assertEquals(sessionInfo.accessToken.expiry - sessionInfo.accessToken.createdTime, twoYearsInSeconds * 1000);
441+
assertEquals(sessionInfo.refreshToken.expiry - sessionInfo.refreshToken.createdTime, twoYearsInSeconds * 1000);
442+
JsonObject sessionData = new JsonObject();
443+
sessionData.addProperty("test", "value");
444+
445+
JsonObject jwtData = new JsonObject();
446+
jwtData.addProperty("test", "value");
447+
448+
Session.updateSession(process.main, sessionInfo.session.handle, sessionData, jwtData, null);
449+
450+
io.supertokens.pluginInterface.session.SessionInfo sessionInfo2 = Session.getSession(process.main,
451+
sessionInfo.session.handle);
452+
453+
assertEquals(sessionInfo2.expiry - sessionInfo2.timeCreated, twoYearsInSeconds * 1000);
454+
455+
process.kill();
456+
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
457+
}
370458
}

0 commit comments

Comments
 (0)