-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds userIdMapping support (#49)
* feat: adds userIdMapping support * completes remaining checklist items * fixes CHANGELOG.md
- Loading branch information
Showing
7 changed files
with
301 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ plugins { | |
id 'java-library' | ||
} | ||
|
||
version = "1.17.0" | ||
version = "1.18.0" | ||
|
||
repositories { | ||
mavenCentral() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"_comment": "contains a list of plugin interfaces branch names that this core supports", | ||
"versions": [ | ||
"2.15" | ||
"2.16" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
src/main/java/io/supertokens/storage/postgresql/queries/UserIdMappingQueries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* | ||
* Copyright (c) 2022, VRAI Labs and/or its affiliates. All rights reserved. | ||
* | ||
* This software is licensed under the Apache License, Version 2.0 (the | ||
* "License") as published by the Apache Software Foundation. | ||
* | ||
* You may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.supertokens.storage.postgresql.queries; | ||
|
||
import io.supertokens.pluginInterface.RowMapper; | ||
import io.supertokens.pluginInterface.exceptions.StorageQueryException; | ||
import io.supertokens.pluginInterface.useridmapping.UserIdMapping; | ||
import io.supertokens.storage.postgresql.Start; | ||
import io.supertokens.storage.postgresql.config.Config; | ||
import io.supertokens.storage.postgresql.utils.Utils; | ||
|
||
import javax.annotation.Nullable; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
|
||
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.execute; | ||
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.update; | ||
|
||
public class UserIdMappingQueries { | ||
|
||
public static String getQueryToCreateUserIdMappingTable(Start start) { | ||
String schema = Config.getConfig(start).getTableSchema(); | ||
String userIdMappingTable = Config.getConfig(start).getUserIdMappingTable(); | ||
// @formatter:off | ||
return "CREATE TABLE IF NOT EXISTS " + userIdMappingTable + " (" | ||
+ "supertokens_user_id CHAR(36) NOT NULL " | ||
+ "CONSTRAINT " + Utils.getConstraintName(schema, userIdMappingTable, "supertokens_user_id", "key") + " UNIQUE," | ||
+ "external_user_id VARCHAR(128) NOT NULL" | ||
+ " CONSTRAINT " + Utils.getConstraintName(schema, userIdMappingTable, "external_user_id", "key") + " UNIQUE," | ||
+ "external_user_id_info TEXT," | ||
+ " CONSTRAINT " + Utils.getConstraintName(schema, userIdMappingTable, null, "pkey") + | ||
" PRIMARY KEY(supertokens_user_id, external_user_id)," | ||
+ ("CONSTRAINT " + Utils.getConstraintName(schema, userIdMappingTable, "supertokens_user_id", "fkey") + | ||
" FOREIGN KEY (supertokens_user_id)" | ||
+ " REFERENCES " + Config.getConfig(start).getUsersTable() + "(user_id)" | ||
+ " ON DELETE CASCADE);"); | ||
// @formatter:on | ||
} | ||
|
||
public static void createUserIdMapping(Start start, String superTokensUserId, String externalUserId, | ||
String externalUserIdInfo) throws SQLException, StorageQueryException { | ||
String QUERY = "INSERT INTO " + Config.getConfig(start).getUserIdMappingTable() | ||
+ " (supertokens_user_id, external_user_id, external_user_id_info)" + " VALUES(?, ?, ?)"; | ||
|
||
update(start, QUERY, pst -> { | ||
pst.setString(1, superTokensUserId); | ||
pst.setString(2, externalUserId); | ||
pst.setString(3, externalUserIdInfo); | ||
}); | ||
} | ||
|
||
public static UserIdMapping getuseraIdMappingWithSuperTokensUserId(Start start, String userId) | ||
throws SQLException, StorageQueryException { | ||
String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() | ||
+ " WHERE supertokens_user_id = ?"; | ||
return execute(start, QUERY, pst -> pst.setString(1, userId), result -> { | ||
if (result.next()) { | ||
return UserIdMappingRowMapper.getInstance().mapOrThrow(result); | ||
} | ||
return null; | ||
}); | ||
} | ||
|
||
public static UserIdMapping getUserIdMappingWithExternalUserId(Start start, String userId) | ||
throws SQLException, StorageQueryException { | ||
String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() | ||
+ " WHERE external_user_id = ?"; | ||
|
||
return execute(start, QUERY, pst -> pst.setString(1, userId), result -> { | ||
if (result.next()) { | ||
return UserIdMappingRowMapper.getInstance().mapOrThrow(result); | ||
} | ||
return null; | ||
}); | ||
} | ||
|
||
public static UserIdMapping[] getUserIdMappingWithEitherSuperTokensUserIdOrExternalUserId(Start start, | ||
String userId) throws SQLException, StorageQueryException { | ||
String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable() | ||
+ " WHERE supertokens_user_id = ? OR external_user_id = ? "; | ||
|
||
return execute(start, QUERY, pst -> { | ||
pst.setString(1, userId); | ||
pst.setString(2, userId); | ||
}, result -> { | ||
ArrayList<UserIdMapping> userIdMappingArray = new ArrayList<>(); | ||
while (result.next()) { | ||
userIdMappingArray.add(UserIdMappingRowMapper.getInstance().mapOrThrow(result)); | ||
} | ||
return userIdMappingArray.toArray(UserIdMapping[]::new); | ||
}); | ||
|
||
} | ||
|
||
public static boolean deleteUserIdMappingWithSuperTokensUserId(Start start, String userId) | ||
throws SQLException, StorageQueryException { | ||
String QUERY = "DELETE FROM " + Config.getConfig(start).getUserIdMappingTable() | ||
+ " WHERE supertokens_user_id = ?"; | ||
|
||
// store the number of rows updated | ||
int rowUpdatedCount = update(start, QUERY, pst -> pst.setString(1, userId)); | ||
|
||
return rowUpdatedCount > 0; | ||
} | ||
|
||
public static boolean deleteUserIdMappingWithExternalUserId(Start start, String userId) | ||
throws SQLException, StorageQueryException { | ||
String QUERY = "DELETE FROM " + Config.getConfig(start).getUserIdMappingTable() + " WHERE external_user_id = ?"; | ||
|
||
// store the number of rows updated | ||
int rowUpdatedCount = update(start, QUERY, pst -> pst.setString(1, userId)); | ||
|
||
return rowUpdatedCount > 0; | ||
} | ||
|
||
public static boolean updateOrDeleteExternalUserIdInfoWithSuperTokensUserId(Start start, String userId, | ||
@Nullable String externalUserIdInfo) throws SQLException, StorageQueryException { | ||
String QUERY = "UPDATE " + Config.getConfig(start).getUserIdMappingTable() | ||
+ " SET external_user_id_info = ? WHERE supertokens_user_id = ?"; | ||
|
||
int rowUpdated = update(start, QUERY, pst -> { | ||
pst.setString(1, externalUserIdInfo); | ||
pst.setString(2, userId); | ||
}); | ||
|
||
return rowUpdated > 0; | ||
} | ||
|
||
public static boolean updateOrDeleteExternalUserIdInfoWithExternalUserId(Start start, String userId, | ||
@Nullable String externalUserIdInfo) throws SQLException, StorageQueryException { | ||
String QUERY = "UPDATE " + Config.getConfig(start).getUserIdMappingTable() | ||
+ " SET external_user_id_info = ? WHERE external_user_id = ?"; | ||
|
||
int rowUpdated = update(start, QUERY, pst -> { | ||
pst.setString(1, externalUserIdInfo); | ||
pst.setString(2, userId); | ||
}); | ||
|
||
return rowUpdated > 0; | ||
} | ||
|
||
private static class UserIdMappingRowMapper implements RowMapper<UserIdMapping, ResultSet> { | ||
private static final UserIdMappingRowMapper INSTANCE = new UserIdMappingRowMapper(); | ||
|
||
private UserIdMappingRowMapper() { | ||
} | ||
|
||
private static UserIdMappingRowMapper getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public UserIdMapping map(ResultSet rs) throws Exception { | ||
return new UserIdMapping(rs.getString("supertokens_user_id"), rs.getString("external_user_id"), | ||
rs.getString("external_user_id_info")); | ||
} | ||
} | ||
|
||
} |