Skip to content

Commit

Permalink
fix: mfa cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Oct 12, 2023
1 parent 8b17c95 commit 70e3fbc
Show file tree
Hide file tree
Showing 33 changed files with 44 additions and 1,376 deletions.
5 changes: 1 addition & 4 deletions ee/src/main/java/io/supertokens/ee/EEFeatureFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ private JsonObject getMFAStats() throws StorageQueryException, TenantOrAppNotFou
}

mfaStats.add("maus", mfaMauArr);
mfaStats.add("totp", getTOTPStats());

int mfaTotalUsers = 0;
for (Storage storage : storages) {
Expand Down Expand Up @@ -387,10 +388,6 @@ public JsonObject getPaidFeatureStats() throws StorageQueryException, TenantOrAp
usageStats.add(EE_FEATURES.DASHBOARD_LOGIN.toString(), getDashboardLoginStats());
}

if (feature == EE_FEATURES.TOTP) {
usageStats.add(EE_FEATURES.TOTP.toString(), getTOTPStats());
}

if (feature == EE_FEATURES.MFA) {
usageStats.add(EE_FEATURES.MFA.toString(), getMFAStats());
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/io/supertokens/authRecipe/AuthRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -976,8 +974,6 @@ public static boolean deleteNonAuthRecipeUser(TenantIdentifierWithStorage
.removeUser(tenantIdentifierWithStorage, userId);
finalDidExist = finalDidExist || didExist;

didExist = tenantIdentifierWithStorage.getMfaStorage()
.deleteMfaInfoForUser(tenantIdentifierWithStorage, userId);
finalDidExist = finalDidExist || didExist;

return finalDidExist;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/supertokens/featureflag/EE_FEATURES.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public enum EE_FEATURES {
ACCOUNT_LINKING("account_linking"), MULTI_TENANCY("multi_tenancy"), TEST("test"),
DASHBOARD_LOGIN("dashboard_login"), TOTP("totp"), MFA("mfa");
DASHBOARD_LOGIN("dashboard_login"), MFA("mfa");

private final String name;

Expand Down
67 changes: 0 additions & 67 deletions src/main/java/io/supertokens/inmemorydb/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -2813,73 +2813,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();
Expand Down
52 changes: 6 additions & 46 deletions src/main/java/io/supertokens/mfa/Mfa.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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.");
}
}
27 changes: 3 additions & 24 deletions src/main/java/io/supertokens/totp/Totp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import com.eatthepath.otp.TimeBasedOneTimePasswordGenerator;
import io.supertokens.Main;
import io.supertokens.config.Config;
import io.supertokens.featureflag.EE_FEATURES;
import io.supertokens.featureflag.FeatureFlag;
import io.supertokens.featureflag.exceptions.FeatureNotEnabledException;
import io.supertokens.mfa.Mfa;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.AppIdentifierWithStorage;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifierWithStorage;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
Expand Down Expand Up @@ -70,17 +68,6 @@ private static boolean checkCode(TOTPDevice device, String code) {
return false;
}

private static boolean isTotpEnabled(AppIdentifier appIdentifier, Main main)
throws StorageQueryException, TenantOrAppNotFoundException {
EE_FEATURES[] features = FeatureFlag.getInstance(main, appIdentifier).getEnabledFeatures();
for (EE_FEATURES f : features) {
if (f == EE_FEATURES.TOTP) {
return true;
}
}
return false;
}

@TestOnly
public static TOTPDevice registerDevice(Main main, String userId,
String deviceName, int skew, int period)
Expand Down Expand Up @@ -126,11 +113,7 @@ public static TOTPDevice registerDevice(AppIdentifierWithStorage appIdentifierWi
throws StorageQueryException, DeviceAlreadyExistsException, NoSuchAlgorithmException,
FeatureNotEnabledException, TenantOrAppNotFoundException, StorageTransactionLogicException {

if (!isTotpEnabled(appIdentifierWithStorage, main)) {
throw new FeatureNotEnabledException(
"TOTP feature is not enabled. Please subscribe to a SuperTokens core license key to enable this " +
"feature.");
}
Mfa.checkForMFAFeature(appIdentifierWithStorage, main);

String secret = generateSecret();
TOTPDevice device = new TOTPDevice(userId, deviceName, secret, period, skew, false);
Expand Down Expand Up @@ -403,11 +386,7 @@ public static void verifyCode(TenantIdentifierWithStorage tenantIdentifierWithSt
StorageQueryException, StorageTransactionLogicException, FeatureNotEnabledException,
TenantOrAppNotFoundException {

if (!isTotpEnabled(tenantIdentifierWithStorage.toAppIdentifierWithStorage(), main)) {
throw new FeatureNotEnabledException(
"TOTP feature is not enabled. Please subscribe to a SuperTokens core license key to enable this " +
"feature.");
}
Mfa.checkForMFAFeature(tenantIdentifierWithStorage.toAppIdentifierWithStorage(), main);

TOTPSQLStorage totpStorage = tenantIdentifierWithStorage.getTOTPStorage();

Expand Down
7 changes: 0 additions & 7 deletions src/main/java/io/supertokens/webserver/Webserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
import io.supertokens.webserver.api.emailverification.VerifyEmailAPI;
import io.supertokens.webserver.api.jwt.JWKSAPI;
import io.supertokens.webserver.api.jwt.JWTSigningAPI;
import io.supertokens.webserver.api.mfa.DisableFactorAPI;
import io.supertokens.webserver.api.mfa.EnableFactorAPI;
import io.supertokens.webserver.api.mfa.ListFactorsAPI;
import io.supertokens.webserver.api.multitenancy.*;
import io.supertokens.webserver.api.multitenancy.thirdparty.CreateOrUpdateThirdPartyConfigAPI;
import io.supertokens.webserver.api.multitenancy.thirdparty.RemoveThirdPartyConfigAPI;
Expand Down Expand Up @@ -232,10 +229,6 @@ private void setupRoutes() {
addAPI(new GetDashboardSessionsForUserAPI(main));
addAPI(new SearchTagsAPI(main));

addAPI(new ListFactorsAPI(main));
addAPI(new EnableFactorAPI(main));
addAPI(new DisableFactorAPI(main));

addAPI(new CreateOrUpdateConnectionUriDomainAPI(main));
addAPI(new RemoveConnectionUriDomainAPI(main));
addAPI(new ListConnectionUriDomainsAPI(main));
Expand Down

This file was deleted.

Loading

0 comments on commit 70e3fbc

Please sign in to comment.