From e18b86a448b22e7acec8e43aff93dadffcc3f46a Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Tue, 3 Oct 2023 16:49:57 +0530 Subject: [PATCH] fix: refactor transaction functions (#120) --- .../ActiveUsersSQLStorage.java | 28 ++++++++++++++++++ .../pluginInterface/ActiveUsersStorage.java | 4 --- .../pluginInterface/mfa/MfaStorage.java | 4 +-- .../mfa/sqlStorage/MfaSQLStorage.java | 29 +++++++++++++++++++ .../AppIdentifierWithStorage.java | 12 ++++---- 5 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 src/main/java/io/supertokens/pluginInterface/ActiveUsersSQLStorage.java create mode 100644 src/main/java/io/supertokens/pluginInterface/mfa/sqlStorage/MfaSQLStorage.java diff --git a/src/main/java/io/supertokens/pluginInterface/ActiveUsersSQLStorage.java b/src/main/java/io/supertokens/pluginInterface/ActiveUsersSQLStorage.java new file mode 100644 index 00000000..a093dc6a --- /dev/null +++ b/src/main/java/io/supertokens/pluginInterface/ActiveUsersSQLStorage.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023, 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.pluginInterface; + +import io.supertokens.pluginInterface.exceptions.StorageQueryException; +import io.supertokens.pluginInterface.multitenancy.AppIdentifier; +import io.supertokens.pluginInterface.sqlStorage.SQLStorage; +import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; + +public interface ActiveUsersSQLStorage extends ActiveUsersStorage, SQLStorage { + /* Delete a user from active users table */ + void deleteUserActive_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId) + throws StorageQueryException; +} diff --git a/src/main/java/io/supertokens/pluginInterface/ActiveUsersStorage.java b/src/main/java/io/supertokens/pluginInterface/ActiveUsersStorage.java index e2b1e7ac..8e918ee1 100644 --- a/src/main/java/io/supertokens/pluginInterface/ActiveUsersStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/ActiveUsersStorage.java @@ -24,9 +24,5 @@ public interface ActiveUsersStorage extends NonAuthRecipeStorage { /* Count the number of users who have enabled MFA and are active */ int countUsersEnabledMfaAndActiveSince(AppIdentifier appIdentifier, long time) throws StorageQueryException; - /* Delete a user from active users table */ - void deleteUserActive_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId) - throws StorageQueryException; - int countUsersThatHaveMoreThanOneLoginMethodAndActiveSince(AppIdentifier appIdentifier, long sinceTime) throws StorageQueryException; } diff --git a/src/main/java/io/supertokens/pluginInterface/mfa/MfaStorage.java b/src/main/java/io/supertokens/pluginInterface/mfa/MfaStorage.java index 1b6eac56..77a3184c 100644 --- a/src/main/java/io/supertokens/pluginInterface/mfa/MfaStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/mfa/MfaStorage.java @@ -4,6 +4,7 @@ import io.supertokens.pluginInterface.multitenancy.AppIdentifier; import io.supertokens.pluginInterface.multitenancy.TenantIdentifier; import io.supertokens.pluginInterface.nonAuthRecipe.NonAuthRecipeStorage; +import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; public interface MfaStorage extends NonAuthRecipeStorage { // Enable (insert) a factor for a user and return true if it actually inserted something. @@ -15,9 +16,6 @@ public interface MfaStorage extends NonAuthRecipeStorage { // Disable (delete) a factor for a user and return true if it actually deleted something. boolean disableFactor(TenantIdentifier tenantIdentifier, String userId, String factorId) throws StorageQueryException; - // Delete a user across all tenants (with all the relevant factors) - boolean deleteMfaInfoForUser(AppIdentifier appIdentifier, String userId) throws StorageQueryException; - // Delete a user from a tenant (with all the relevant factors) boolean deleteMfaInfoForUser(TenantIdentifier tenantIdentifier, String userId) throws StorageQueryException; } diff --git a/src/main/java/io/supertokens/pluginInterface/mfa/sqlStorage/MfaSQLStorage.java b/src/main/java/io/supertokens/pluginInterface/mfa/sqlStorage/MfaSQLStorage.java new file mode 100644 index 00000000..087ee133 --- /dev/null +++ b/src/main/java/io/supertokens/pluginInterface/mfa/sqlStorage/MfaSQLStorage.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023, 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.pluginInterface.mfa.sqlStorage; + +import io.supertokens.pluginInterface.exceptions.StorageQueryException; +import io.supertokens.pluginInterface.mfa.MfaStorage; +import io.supertokens.pluginInterface.multitenancy.AppIdentifier; +import io.supertokens.pluginInterface.sqlStorage.SQLStorage; +import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; + +public interface MfaSQLStorage extends MfaStorage, SQLStorage { + // Delete a user across all tenants (with all the relevant factors) + boolean deleteMfaInfoForUser_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId) throws + StorageQueryException; +} diff --git a/src/main/java/io/supertokens/pluginInterface/multitenancy/AppIdentifierWithStorage.java b/src/main/java/io/supertokens/pluginInterface/multitenancy/AppIdentifierWithStorage.java index 5ba6a161..904e2870 100644 --- a/src/main/java/io/supertokens/pluginInterface/multitenancy/AppIdentifierWithStorage.java +++ b/src/main/java/io/supertokens/pluginInterface/multitenancy/AppIdentifierWithStorage.java @@ -16,18 +16,18 @@ package io.supertokens.pluginInterface.multitenancy; -import io.supertokens.pluginInterface.ActiveUsersStorage; +import io.supertokens.pluginInterface.ActiveUsersSQLStorage; import io.supertokens.pluginInterface.STORAGE_TYPE; import io.supertokens.pluginInterface.Storage; import io.supertokens.pluginInterface.authRecipe.AuthRecipeStorage; import io.supertokens.pluginInterface.dashboard.sqlStorage.DashboardSQLStorage; import io.supertokens.pluginInterface.emailpassword.sqlStorage.EmailPasswordSQLStorage; import io.supertokens.pluginInterface.emailverification.sqlStorage.EmailVerificationSQLStorage; +import io.supertokens.pluginInterface.mfa.sqlStorage.MfaSQLStorage; import io.supertokens.pluginInterface.passwordless.sqlStorage.PasswordlessSQLStorage; import io.supertokens.pluginInterface.session.SessionStorage; import io.supertokens.pluginInterface.thirdparty.sqlStorage.ThirdPartySQLStorage; import io.supertokens.pluginInterface.totp.sqlStorage.TOTPSQLStorage; -import io.supertokens.pluginInterface.mfa.MfaStorage; import io.supertokens.pluginInterface.useridmapping.UserIdMappingStorage; import io.supertokens.pluginInterface.usermetadata.sqlStorage.UserMetadataSQLStorage; import io.supertokens.pluginInterface.userroles.sqlStorage.UserRolesSQLStorage; @@ -153,19 +153,19 @@ public TOTPSQLStorage getTOTPStorage() { return (TOTPSQLStorage) this.storage; } - public MfaStorage getMfaStorage() { + public MfaSQLStorage getMfaStorage() { if (this.storage.getType() != STORAGE_TYPE.SQL) { // we only support SQL for now throw new UnsupportedOperationException(""); } - return (MfaStorage) this.storage; + return (MfaSQLStorage) this.storage; } - public ActiveUsersStorage getActiveUsersStorage() { + public ActiveUsersSQLStorage getActiveUsersStorage() { if (this.storage.getType() != STORAGE_TYPE.SQL) { // we only support SQL for now throw new UnsupportedOperationException(""); } - return (ActiveUsersStorage) this.storage; + return (ActiveUsersSQLStorage) this.storage; } }