-
Notifications
You must be signed in to change notification settings - Fork 556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: mfa cleanup #837
fix: mfa cleanup #837
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -934,8 +934,6 @@ private static void deleteNonAuthRecipeUser(TransactionConnection con, AppIdenti | |
.deleteAllRolesForUser_Transaction(con, appIdentifierWithStorage, userId); | ||
appIdentifierWithStorage.getActiveUsersStorage() | ||
.deleteUserActive_Transaction(con, appIdentifierWithStorage, userId); | ||
appIdentifierWithStorage.getMfaStorage() | ||
.deleteMfaInfoForUser_Transaction(con, appIdentifierWithStorage, userId); | ||
} | ||
|
||
private static void deleteAuthRecipeUser(TransactionConnection con, | ||
|
@@ -976,8 +974,6 @@ public static boolean deleteNonAuthRecipeUser(TenantIdentifierWithStorage | |
.removeUser(tenantIdentifierWithStorage, userId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this on a tenant level and not app level? |
||
finalDidExist = finalDidExist || didExist; | ||
|
||
didExist = tenantIdentifierWithStorage.getMfaStorage() | ||
.deleteMfaInfoForUser(tenantIdentifierWithStorage, userId); | ||
finalDidExist = finalDidExist || didExist; | ||
|
||
return finalDidExist; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -619,12 +619,7 @@ public boolean isUserIdBeingUsedInNonAuthRecipe(AppIdentifier appIdentifier, Str | |
} else if (className.equals(JWTRecipeStorage.class.getName())) { | ||
return false; | ||
} else if (className.equals(MfaStorage.class.getName())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this if |
||
try { | ||
MultitenancyQueries.getAllTenants(this); | ||
return MfaQueries.listFactors(this, appIdentifier, userId).length > 0; | ||
} catch (SQLException e) { | ||
throw new StorageQueryException(e); | ||
} | ||
return false; /* nothing here */ | ||
} else { | ||
throw new IllegalStateException("ClassName: " + className + " is not part of NonAuthRecipeStorage"); | ||
} | ||
|
@@ -724,11 +719,7 @@ public void addInfoToNonAuthRecipesBasedOnUserId(TenantIdentifier tenantIdentifi | |
throw new StorageQueryException(e); | ||
} | ||
} else if (className.equals(MfaStorage.class.getName())) { | ||
try { | ||
MfaQueries.enableFactor(this, tenantIdentifier, userId, "emailpassword"); | ||
} catch (SQLException e) { | ||
throw new StorageQueryException(e); | ||
} | ||
/* nothing here */ | ||
} else { | ||
throw new IllegalStateException("ClassName: " + className + " is not part of NonAuthRecipeStorage"); | ||
} | ||
|
@@ -2813,73 +2804,6 @@ public int removeExpiredCodes(TenantIdentifier tenantIdentifier, long expiredBef | |
} | ||
} | ||
|
||
|
||
// MFA recipe: | ||
@Override | ||
public boolean enableFactor(TenantIdentifier tenantIdentifier, String userId, String factor) | ||
throws StorageQueryException { | ||
try { | ||
int insertedCount = MfaQueries.enableFactor(this, tenantIdentifier, userId, factor); | ||
if (insertedCount == 0) { | ||
return false; | ||
} | ||
return true; | ||
} catch (SQLException e) { | ||
throw new StorageQueryException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public String[] listFactors(TenantIdentifier tenantIdentifier, String userId) | ||
throws StorageQueryException { | ||
try { | ||
return MfaQueries.listFactors(this, tenantIdentifier, userId); | ||
} catch (SQLException e) { | ||
throw new StorageQueryException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean disableFactor(TenantIdentifier tenantIdentifier, String userId, String factor) | ||
throws StorageQueryException { | ||
try { | ||
int deletedCount = MfaQueries.disableFactor(this, tenantIdentifier, userId, factor); | ||
if (deletedCount == 0) { | ||
return false; | ||
} | ||
return true; | ||
} catch (SQLException e) { | ||
throw new StorageQueryException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean deleteMfaInfoForUser_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId) | ||
throws StorageQueryException { | ||
try { | ||
int deletedCount = MfaQueries.deleteUser_Transaction(this, (Connection) con.getConnection(), appIdentifier, userId); | ||
if (deletedCount == 0) { | ||
return false; | ||
} | ||
return true; | ||
} catch (SQLException e) { | ||
throw new StorageQueryException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean deleteMfaInfoForUser(TenantIdentifier tenantIdentifier, String userId) throws StorageQueryException { | ||
try { | ||
int deletedCount = MfaQueries.deleteUserFromTenant(this, tenantIdentifier, userId); | ||
if (deletedCount == 0) { | ||
return false; | ||
} | ||
return true; | ||
} catch (SQLException e) { | ||
throw new StorageQueryException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Set<String> getValidFieldsInConfig() { | ||
return SQLiteConfig.getValidFields(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,94 +31,4 @@ | |
import static io.supertokens.inmemorydb.QueryExecutorTemplate.update; | ||
|
||
public class MfaQueries { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete this whole file |
||
public static String getQueryToCreateUserFactorsTable(Start start) { | ||
return "CREATE TABLE IF NOT EXISTS " + Config.getConfig(start).getMfaUserFactorsTable() + " (" | ||
+ "app_id VARCHAR(64) DEFAULT 'public'," | ||
+ "tenant_id VARCHAR(64) DEFAULT 'public'," | ||
+ "user_id VARCHAR(128) NOT NULL," | ||
+ "factor_id VARCHAR(64) NOT NULL," | ||
+ "PRIMARY KEY (app_id, tenant_id, user_id, factor_id)," | ||
+ "FOREIGN KEY (app_id, tenant_id)" | ||
+ "REFERENCES " + Config.getConfig(start).getTenantsTable() + " (app_id, tenant_id) ON DELETE CASCADE);"; | ||
} | ||
|
||
public static int enableFactor(Start start, TenantIdentifier tenantIdentifier, String userId, String factorId) | ||
throws StorageQueryException, SQLException { | ||
String QUERY = "INSERT INTO " + Config.getConfig(start).getMfaUserFactorsTable() + " (app_id, tenant_id, user_id, factor_id) VALUES (?, ?, ?, ?) ON CONFLICT DO NOTHING"; | ||
|
||
return update(start, QUERY, pst -> { | ||
pst.setString(1, tenantIdentifier.getAppId()); | ||
pst.setString(2, tenantIdentifier.getTenantId()); | ||
pst.setString(3, userId); | ||
pst.setString(4, factorId); | ||
}); | ||
} | ||
|
||
|
||
public static String[] listFactors(Start start, TenantIdentifier tenantIdentifier, String userId) | ||
throws StorageQueryException, SQLException { | ||
String QUERY = "SELECT factor_id FROM " + Config.getConfig(start).getMfaUserFactorsTable() + " WHERE app_id = ? AND tenant_id = ? AND user_id = ?"; | ||
|
||
return execute(start, QUERY, pst -> { | ||
pst.setString(1, tenantIdentifier.getAppId()); | ||
pst.setString(2, tenantIdentifier.getTenantId()); | ||
pst.setString(3, userId); | ||
}, result -> { | ||
List<String> factors = new ArrayList<>(); | ||
while (result.next()) { | ||
factors.add(result.getString("factor_id")); | ||
} | ||
|
||
return factors.toArray(String[]::new); | ||
}); | ||
} | ||
|
||
public static String[] listFactors(Start start, AppIdentifier appIdentifier, String userId) | ||
throws StorageQueryException, SQLException { | ||
String QUERY = "SELECT factor_id FROM " + Config.getConfig(start).getMfaUserFactorsTable() + " WHERE app_id = ? AND user_id = ?"; | ||
|
||
return execute(start, QUERY, pst -> { | ||
pst.setString(1, appIdentifier.getAppId()); | ||
pst.setString(2, userId); | ||
}, result -> { | ||
List<String> factors = new ArrayList<>(); | ||
while (result.next()) { | ||
factors.add(result.getString("factor_id")); | ||
} | ||
|
||
return factors.toArray(String[]::new); | ||
}); | ||
} | ||
|
||
public static int disableFactor(Start start, TenantIdentifier tenantIdentifier, String userId, String factorId) | ||
throws StorageQueryException, SQLException { | ||
String QUERY = "DELETE FROM " + Config.getConfig(start).getMfaUserFactorsTable() + " WHERE app_id = ? AND tenant_id = ? AND user_id = ? AND factor_id = ?"; | ||
|
||
return update(start, QUERY, pst -> { | ||
pst.setString(1, tenantIdentifier.getAppId()); | ||
pst.setString(2, tenantIdentifier.getTenantId()); | ||
pst.setString(3, userId); | ||
pst.setString(4, factorId); | ||
}); | ||
} | ||
|
||
public static int deleteUser_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier, String userId) throws StorageQueryException, SQLException { | ||
String QUERY = "DELETE FROM " + Config.getConfig(start).getMfaUserFactorsTable() + " WHERE app_id = ? AND user_id = ?"; | ||
|
||
return update(sqlCon, QUERY, pst -> { | ||
pst.setString(1, appIdentifier.getAppId()); | ||
pst.setString(2, userId); | ||
}); | ||
} | ||
|
||
public static int deleteUserFromTenant(Start start, TenantIdentifier tenantIdentifier, String userId) | ||
throws StorageQueryException, SQLException { | ||
String QUERY = "DELETE FROM " + Config.getConfig(start).getMfaUserFactorsTable() + " WHERE app_id = ? AND tenant_id = ? AND user_id = ?"; | ||
|
||
return update(start, QUERY, pst -> { | ||
pst.setString(1, tenantIdentifier.getAppId()); | ||
pst.setString(2, tenantIdentifier.getTenantId()); | ||
pst.setString(3, userId); | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,60 +5,20 @@ | |||||
import io.supertokens.featureflag.FeatureFlag; | ||||||
import io.supertokens.featureflag.exceptions.FeatureNotEnabledException; | ||||||
import io.supertokens.pluginInterface.exceptions.StorageQueryException; | ||||||
import io.supertokens.pluginInterface.mfa.MfaStorage; | ||||||
import io.supertokens.pluginInterface.multitenancy.AppIdentifier; | ||||||
import io.supertokens.pluginInterface.multitenancy.TenantIdentifierWithStorage; | ||||||
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException; | ||||||
|
||||||
public class Mfa { | ||||||
private static boolean isMfaEnabled(AppIdentifier appIdentifier, Main main) | ||||||
throws StorageQueryException, TenantOrAppNotFoundException { | ||||||
public static void checkForMFAFeature(AppIdentifier appIdentifier, Main main) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
throws StorageQueryException, TenantOrAppNotFoundException, FeatureNotEnabledException { | ||||||
EE_FEATURES[] features = FeatureFlag.getInstance(main, appIdentifier).getEnabledFeatures(); | ||||||
for (EE_FEATURES f : features) { | ||||||
if (f == EE_FEATURES.MFA) { | ||||||
return true; | ||||||
return; | ||||||
} | ||||||
} | ||||||
return false; | ||||||
} | ||||||
|
||||||
public static boolean enableFactor(TenantIdentifierWithStorage tenantIdentifierWithStorage, Main main, String userId, String factorId) | ||||||
throws | ||||||
StorageQueryException, FeatureNotEnabledException, TenantOrAppNotFoundException { | ||||||
if (!isMfaEnabled(tenantIdentifierWithStorage.toAppIdentifier(), main)) { | ||||||
throw new FeatureNotEnabledException( | ||||||
"MFA feature is not enabled. Please subscribe to a SuperTokens core license key to enable this " + | ||||||
"feature."); | ||||||
} | ||||||
|
||||||
MfaStorage mfaStorage = tenantIdentifierWithStorage.getMfaStorage(); | ||||||
return mfaStorage.enableFactor(tenantIdentifierWithStorage, userId, factorId); | ||||||
} | ||||||
|
||||||
public static String[] listFactors(TenantIdentifierWithStorage tenantIdentifierWithStorage, Main main, String userId) | ||||||
throws | ||||||
StorageQueryException, TenantOrAppNotFoundException, FeatureNotEnabledException { | ||||||
if (!isMfaEnabled(tenantIdentifierWithStorage.toAppIdentifier(), main)) { | ||||||
throw new FeatureNotEnabledException( | ||||||
"MFA feature is not enabled. Please subscribe to a SuperTokens core license key to enable this " + | ||||||
"feature."); | ||||||
} | ||||||
|
||||||
MfaStorage mfaStorage = tenantIdentifierWithStorage.getMfaStorage(); | ||||||
return mfaStorage.listFactors(tenantIdentifierWithStorage, userId); | ||||||
} | ||||||
|
||||||
public static boolean disableFactor(TenantIdentifierWithStorage tenantIdentifierWithStorage, Main main, String userId, String factorId) | ||||||
throws | ||||||
StorageQueryException, TenantOrAppNotFoundException, FeatureNotEnabledException { | ||||||
|
||||||
if (!isMfaEnabled(tenantIdentifierWithStorage.toAppIdentifier(), main)) { | ||||||
throw new FeatureNotEnabledException( | ||||||
"MFA feature is not enabled. Please subscribe to a SuperTokens core license key to enable this " + | ||||||
"feature."); | ||||||
} | ||||||
|
||||||
MfaStorage mfaStorage = tenantIdentifierWithStorage.getMfaStorage(); | ||||||
return mfaStorage.disableFactor(tenantIdentifierWithStorage, userId, factorId); | ||||||
throw new FeatureNotEnabledException( | ||||||
"MFA feature is not enabled. Please subscribe to a SuperTokens core license key to enable this " + | ||||||
"feature."); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where are we deleting totp devices etc?