Skip to content

Commit 665c4ab

Browse files
feat: returns only non expired session handles for user (#48)
1 parent 2bc7307 commit 665c4ab

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
## [1.17.0] - 2022-06-07
11+
12+
- Compatibility with plugin interface 2.15 - returns only non expired session handles for a user
13+
1014
## [1.16.0] - 2022-05-05
15+
1116
- Adds support for UserRoles recipe
1217

1318
## [1.15.0] - 2022-03-04

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
id 'java-library'
33
}
44

5-
version = "1.16.0"
5+
version = "1.17.0"
66

77
repositories {
88
mavenCentral()

pluginInterfaceSupported.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"_comment": "contains a list of plugin interfaces branch names that this core supports",
33
"versions": [
4-
"2.14"
4+
"2.15"
55
]
66
}

src/main/java/io/supertokens/storage/postgresql/Start.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,9 @@ public int deleteSession(String[] sessionHandles) throws StorageQueryException {
407407
}
408408

409409
@Override
410-
public String[] getAllSessionHandlesForUser(String userId) throws StorageQueryException {
410+
public String[] getAllNonExpiredSessionHandlesForUser(String userId) throws StorageQueryException {
411411
try {
412-
return SessionQueries.getAllSessionHandlesForUser(this, userId);
412+
return SessionQueries.getAllNonExpiredSessionHandlesForUser(this, userId);
413413
} catch (SQLException e) {
414414
throw new StorageQueryException(e);
415415
}

src/main/java/io/supertokens/storage/postgresql/queries/SessionQueries.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ public static String getQueryToCreateSessionInfoTable(Start start) {
4848
return "CREATE TABLE IF NOT EXISTS " + sessionInfoTable + " ("
4949
+ "session_handle VARCHAR(255) NOT NULL,"
5050
+ "user_id VARCHAR(128) NOT NULL,"
51-
+ "refresh_token_hash_2 VARCHAR(128) NOT NULL,"
52-
+ "session_data TEXT,"
51+
+ "refresh_token_hash_2 VARCHAR(128) NOT NULL,"
52+
+ "session_data TEXT,"
5353
+ "expires_at BIGINT NOT NULL,"
54-
+ "created_at_time BIGINT NOT NULL,"
55-
+ "jwt_user_payload TEXT,"
56-
+ "CONSTRAINT " + Utils.getConstraintName(schema, sessionInfoTable, null, "pkey") + " PRIMARY KEY(session_handle)" + " );";
54+
+ "created_at_time BIGINT NOT NULL,"
55+
+ "jwt_user_payload TEXT,"
56+
+ "CONSTRAINT " + Utils.getConstraintName(schema, sessionInfoTable, null, "pkey") +
57+
" PRIMARY KEY(session_handle)" + " );";
5758
// @formatter:on
5859

5960
}
@@ -63,9 +64,10 @@ static String getQueryToCreateAccessTokenSigningKeysTable(Start start) {
6364
String accessTokenSigningKeysTable = Config.getConfig(start).getAccessTokenSigningKeysTable();
6465
// @formatter:off
6566
return "CREATE TABLE IF NOT EXISTS " + accessTokenSigningKeysTable + " ("
66-
+ "created_at_time BIGINT NOT NULL,"
67-
+ "value TEXT,"
68-
+ "CONSTRAINT " + Utils.getConstraintName(schema, accessTokenSigningKeysTable, null, "pkey") + " PRIMARY KEY(created_at_time)" + " );";
67+
+ "created_at_time BIGINT NOT NULL,"
68+
+ "value TEXT,"
69+
+ "CONSTRAINT " + Utils.getConstraintName(schema, accessTokenSigningKeysTable, null, "pkey") +
70+
" PRIMARY KEY(created_at_time)" + " );";
6971
// @formatter:on
7072
}
7173

@@ -157,11 +159,15 @@ public static void deleteSessionsOfUser(Start start, String userId) throws SQLEx
157159
update(start, QUERY.toString(), pst -> pst.setString(1, userId));
158160
}
159161

160-
public static String[] getAllSessionHandlesForUser(Start start, String userId)
162+
public static String[] getAllNonExpiredSessionHandlesForUser(Start start, String userId)
161163
throws SQLException, StorageQueryException {
162-
String QUERY = "SELECT session_handle FROM " + getConfig(start).getSessionInfoTable() + " WHERE user_id = ?";
164+
String QUERY = "SELECT session_handle FROM " + getConfig(start).getSessionInfoTable()
165+
+ " WHERE user_id = ? AND expires_at >= ?";
163166

164-
return execute(start, QUERY, pst -> pst.setString(1, userId), result -> {
167+
return execute(start, QUERY, pst -> {
168+
pst.setString(1, userId);
169+
pst.setLong(2, currentTimeMillis());
170+
}, result -> {
165171
List<String> temp = new ArrayList<>();
166172
while (result.next()) {
167173
temp.add(result.getString("session_handle"));

0 commit comments

Comments
 (0)