Skip to content

Commit d921477

Browse files
authored
fix: backports for core 8.0 (#968)
* fix: userIdMapping queries * fix: session * fix: active users with account linking * fix: refresh session * fix: session refresh * fix: tests
1 parent 3282cb5 commit d921477

33 files changed

+703
-73
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
66
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [8.0.2] - 2024-03-21
9+
10+
- Fixes userIdMapping queries
11+
- Fixes issue with session creation for users with userIdMapping and accounts linked
12+
- Fixes active users tracking while linking accounts
13+
- Adds a new required `useDynamicSigningKey` into the request body of `RefreshSessionAPI`
14+
- This enables smooth switching between `useDynamicAccessTokenSigningKey` settings by allowing refresh calls to
15+
change the signing key type of a session
16+
817
## [8.0.1] - 2024-03-11
918

1019
- Making this version backward compatible. Breaking changes in `8.0.0` can now be ignored.

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 = "8.0.1"
22+
version = "8.0.2"
2323

2424

2525
repositories {

src/main/java/io/supertokens/ActiveUsers.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package io.supertokens;
22

3+
import io.supertokens.pluginInterface.ActiveUsersStorage;
34
import io.supertokens.pluginInterface.Storage;
45
import io.supertokens.pluginInterface.StorageUtils;
56
import io.supertokens.pluginInterface.authRecipe.sqlStorage.AuthRecipeSQLStorage;
67
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
78
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
89
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
910
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
11+
import io.supertokens.pluginInterface.sqlStorage.SQLStorage;
1012
import io.supertokens.storageLayer.StorageLayer;
1113
import org.jetbrains.annotations.TestOnly;
1214

@@ -37,6 +39,20 @@ public static int countUsersActiveSince(Main main, AppIdentifier appIdentifier,
3739
return StorageUtils.getActiveUsersStorage(storage).countUsersActiveSince(appIdentifier, time);
3840
}
3941

42+
public static void updateLastActiveAfterLinking(Main main, AppIdentifier appIdentifier, String primaryUserId,
43+
String recipeUserId)
44+
throws StorageQueryException, TenantOrAppNotFoundException, StorageTransactionLogicException {
45+
ActiveUsersStorage activeUsersStorage =
46+
StorageUtils.getActiveUsersStorage(StorageLayer.getStorage(appIdentifier.getAsPublicTenantIdentifier(), main));
47+
48+
((SQLStorage) activeUsersStorage).startTransaction(con -> {
49+
activeUsersStorage.deleteUserActive_Transaction(con, appIdentifier, recipeUserId);
50+
return null;
51+
});
52+
53+
updateLastActive(appIdentifier, main, primaryUserId);
54+
}
55+
4056
@TestOnly
4157
public static int countUsersActiveSince(Main main, long time)
4258
throws StorageQueryException, TenantOrAppNotFoundException {

src/main/java/io/supertokens/authRecipe/AuthRecipe.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,8 @@ private static void deleteNonAuthRecipeUser(TransactionConnection con, AppIdenti
925925
.deleteUserActive_Transaction(con, appIdentifier, userId);
926926
StorageUtils.getTOTPStorage(storage)
927927
.removeUser_Transaction(con, appIdentifier, userId);
928+
StorageUtils.getActiveUsersStorage(storage)
929+
.deleteUserActive_Transaction(con, appIdentifier, userId);
928930
}
929931

930932
private static void deleteAuthRecipeUser(TransactionConnection con,

src/main/java/io/supertokens/inmemorydb/Start.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,11 @@ public SessionInfo getSessionInfo_Transaction(TenantIdentifier tenantIdentifier,
520520
@Override
521521
public void updateSessionInfo_Transaction(TenantIdentifier tenantIdentifier, TransactionConnection con,
522522
String sessionHandle, String refreshTokenHash2,
523-
long expiry) throws StorageQueryException {
523+
long expiry, boolean useStaticKey) throws StorageQueryException {
524524
Connection sqlCon = (Connection) con.getConnection();
525525
try {
526526
SessionQueries.updateSessionInfo_Transaction(this, sqlCon, tenantIdentifier, sessionHandle,
527-
refreshTokenHash2, expiry);
527+
refreshTokenHash2, expiry, useStaticKey);
528528
} catch (SQLException e) {
529529
throw new StorageQueryException(e);
530530
}
@@ -2196,10 +2196,11 @@ public boolean updateOrDeleteExternalUserIdInfo(AppIdentifier appIdentifier, Str
21962196
}
21972197

21982198
@Override
2199-
public HashMap<String, String> getUserIdMappingForSuperTokensIds(ArrayList<String> userIds)
2199+
public HashMap<String, String> getUserIdMappingForSuperTokensIds(AppIdentifier appIdentifier,
2200+
ArrayList<String> userIds)
22002201
throws StorageQueryException {
22012202
try {
2202-
return UserIdMappingQueries.getUserIdMappingWithUserIds(this, userIds);
2203+
return UserIdMappingQueries.getUserIdMappingWithUserIds(this, appIdentifier, userIds);
22032204
} catch (SQLException e) {
22042205
throw new StorageQueryException(e);
22052206
}

src/main/java/io/supertokens/inmemorydb/queries/EmailVerificationQueries.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
289289
// calculating the verified emails
290290

291291
HashMap<String, String> supertokensUserIdToExternalUserIdMap = UserIdMappingQueries.getUserIdMappingWithUserIds_Transaction(start,
292-
sqlCon, supertokensUserIds);
292+
sqlCon, appIdentifier, supertokensUserIds);
293293
HashMap<String, String> externalUserIdToSupertokensUserIdMap = new HashMap<>();
294294

295295
List<String> supertokensOrExternalUserIdsToQuery = new ArrayList<>();
@@ -357,7 +357,7 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
357357
// We have external user id stored in the email verification table, so we need to fetch the mapped userids for
358358
// calculating the verified emails
359359
HashMap<String, String> supertokensUserIdToExternalUserIdMap = UserIdMappingQueries.getUserIdMappingWithUserIds(start,
360-
supertokensUserIds);
360+
appIdentifier, supertokensUserIds);
361361
HashMap<String, String> externalUserIdToSupertokensUserIdMap = new HashMap<>();
362362
List<String> supertokensOrExternalUserIdsToQuery = new ArrayList<>();
363363
for (String userId : supertokensUserIds) {

src/main/java/io/supertokens/inmemorydb/queries/SessionQueries.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,19 @@ public static SessionInfo getSessionInfo_Transaction(Start start, Connection con
147147

148148
public static void updateSessionInfo_Transaction(Start start, Connection con, TenantIdentifier tenantIdentifier,
149149
String sessionHandle,
150-
String refreshTokenHash2, long expiry)
150+
String refreshTokenHash2, long expiry, boolean useStaticKey)
151151
throws SQLException, StorageQueryException {
152152
String QUERY = "UPDATE " + getConfig(start).getSessionInfoTable()
153-
+ " SET refresh_token_hash_2 = ?, expires_at = ?"
153+
+ " SET refresh_token_hash_2 = ?, expires_at = ?, use_static_key = ?"
154154
+ " WHERE app_id = ? AND tenant_id = ? AND session_handle = ?";
155155

156156
update(con, QUERY, pst -> {
157157
pst.setString(1, refreshTokenHash2);
158158
pst.setLong(2, expiry);
159-
pst.setString(3, tenantIdentifier.getAppId());
160-
pst.setString(4, tenantIdentifier.getTenantId());
161-
pst.setString(5, sessionHandle);
159+
pst.setBoolean(3, useStaticKey);
160+
pst.setString(4, tenantIdentifier.getAppId());
161+
pst.setString(5, tenantIdentifier.getTenantId());
162+
pst.setString(6, sessionHandle);
162163
});
163164
}
164165

src/main/java/io/supertokens/inmemorydb/queries/UserIdMappingQueries.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ public static UserIdMapping[] getUserIdMappingWithEitherSuperTokensUserIdOrExter
136136

137137
}
138138

139-
public static HashMap<String, String> getUserIdMappingWithUserIds(Start start, List<String> userIds)
139+
public static HashMap<String, String> getUserIdMappingWithUserIds(Start start,
140+
AppIdentifier appIdentifier,
141+
List<String> userIds)
140142
throws SQLException, StorageQueryException {
141143

142144
if (userIds.size() == 0) {
@@ -145,7 +147,8 @@ public static HashMap<String, String> getUserIdMappingWithUserIds(Start start, L
145147

146148
// No need to filter based on tenantId because the id list is already filtered for a tenant
147149
StringBuilder QUERY = new StringBuilder(
148-
"SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() + " WHERE supertokens_user_id IN (");
150+
"SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() + " WHERE app_id = ? AND " +
151+
"supertokens_user_id IN (");
149152
for (int i = 0; i < userIds.size(); i++) {
150153
QUERY.append("?");
151154
if (i != userIds.size() - 1) {
@@ -155,9 +158,10 @@ public static HashMap<String, String> getUserIdMappingWithUserIds(Start start, L
155158
}
156159
QUERY.append(")");
157160
return execute(start, QUERY.toString(), pst -> {
161+
pst.setString(1, appIdentifier.getAppId());
158162
for (int i = 0; i < userIds.size(); i++) {
159-
// i+1 cause this starts with 1 and not 0
160-
pst.setString(i + 1, userIds.get(i));
163+
// i+2 cause this starts with 1 and not 0, 1 is appId
164+
pst.setString(i + 2, userIds.get(i));
161165
}
162166
}, result -> {
163167
HashMap<String, String> userIdMappings = new HashMap<>();
@@ -169,7 +173,9 @@ public static HashMap<String, String> getUserIdMappingWithUserIds(Start start, L
169173
});
170174
}
171175

172-
public static HashMap<String, String> getUserIdMappingWithUserIds_Transaction(Start start, Connection sqlCon, List<String> userIds)
176+
public static HashMap<String, String> getUserIdMappingWithUserIds_Transaction(Start start, Connection sqlCon,
177+
AppIdentifier appIdentifier,
178+
List<String> userIds)
173179
throws SQLException, StorageQueryException {
174180

175181
if (userIds.size() == 0) {
@@ -178,7 +184,8 @@ public static HashMap<String, String> getUserIdMappingWithUserIds_Transaction(St
178184

179185
// No need to filter based on tenantId because the id list is already filtered for a tenant
180186
StringBuilder QUERY = new StringBuilder(
181-
"SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() + " WHERE supertokens_user_id IN (");
187+
"SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() + " WHERE app_id = ? AND " +
188+
"supertokens_user_id IN (");
182189
for (int i = 0; i < userIds.size(); i++) {
183190
QUERY.append("?");
184191
if (i != userIds.size() - 1) {
@@ -188,9 +195,10 @@ public static HashMap<String, String> getUserIdMappingWithUserIds_Transaction(St
188195
}
189196
QUERY.append(")");
190197
return execute(sqlCon, QUERY.toString(), pst -> {
198+
pst.setString(1, appIdentifier.getAppId());
191199
for (int i = 0; i < userIds.size(); i++) {
192-
// i+1 cause this starts with 1 and not 0
193-
pst.setString(i + 1, userIds.get(i));
200+
// i+2 cause this starts with 1 and not 0, 1 is appId
201+
pst.setString(i + 2, userIds.get(i));
194202
}
195203
}, result -> {
196204
HashMap<String, String> userIdMappings = new HashMap<>();

0 commit comments

Comments
 (0)