Skip to content

Commit

Permalink
fix: mfa changes (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc authored Dec 25, 2023
1 parent 9f8de85 commit f705920
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ public String getTenantFirstFactorsTable() {
return addPrefixToTableName("tenant_first_factors");
}

public String getTenantDefaultRequiredFactorIdsTable() {
return addPrefixToTableName("tenant_default_required_factor_ids");
public String getTenantRequiredSecondaryFactorsTable() {
return addPrefixToTableName("tenant_required_secondary_factors");
}

public String getTenantThirdPartyProvidersTable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,9 @@ public static void createTablesIfNotExists(Start start) throws SQLException, Sto
update(start, MultitenancyQueries.getQueryToCreateFirstFactorsTable(start), NO_OP_SETTER);
}

if (!doesTableExists(start, Config.getConfig(start).getTenantDefaultRequiredFactorIdsTable())) {
if (!doesTableExists(start, Config.getConfig(start).getTenantRequiredSecondaryFactorsTable())) {
getInstance(start).addState(CREATING_NEW_TABLE, null);
update(start, MultitenancyQueries.getQueryToCreateDefaultRequiredFactorIdsTable(start), NO_OP_SETTER);

// index
update(start,
MultitenancyQueries.getQueryToCreateOrderIndexForDefaultRequiredFactorIdsTable(start),
NO_OP_SETTER);
update(start, MultitenancyQueries.getQueryToCreateRequiredSecondaryFactorsTable(start), NO_OP_SETTER);
}

if (!doesTableExists(start, Config.getConfig(start).getTenantThirdPartyProvidersTable())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ static String getQueryToCreateTenantConfigsTable(Start start) {
+ "email_password_enabled BOOLEAN,"
+ "passwordless_enabled BOOLEAN,"
+ "third_party_enabled BOOLEAN,"
+ "totp_enabled BOOLEAN,"
+ "has_first_factors BOOLEAN DEFAULT FALSE,"
+ "has_default_required_factor_ids BOOLEAN DEFAULT FALSE,"
+ "PRIMARY KEY (connection_uri_domain, app_id, tenant_id)"
+ ");";
// @formatter:on
Expand Down Expand Up @@ -126,28 +123,21 @@ public static String getQueryToCreateFirstFactorsTable(Start start) {
// @formatter:on
}

public static String getQueryToCreateDefaultRequiredFactorIdsTable(Start start) {
String tableName = Config.getConfig(start).getTenantDefaultRequiredFactorIdsTable();
public static String getQueryToCreateRequiredSecondaryFactorsTable(Start start) {
String tableName = Config.getConfig(start).getTenantRequiredSecondaryFactorsTable();
// @formatter:off
return "CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ "connection_uri_domain VARCHAR(256) DEFAULT '',"
+ "app_id VARCHAR(64) DEFAULT 'public',"
+ "tenant_id VARCHAR(64) DEFAULT 'public',"
+ "factor_id VARCHAR(128),"
+ "order_idx INTEGER NOT NULL,"
+ "PRIMARY KEY (connection_uri_domain, app_id, tenant_id, factor_id),"
+ "FOREIGN KEY (connection_uri_domain, app_id, tenant_id)"
+ " REFERENCES " + Config.getConfig(start).getTenantConfigsTable() + " (connection_uri_domain, app_id, tenant_id) ON DELETE CASCADE,"
+ " UNIQUE (connection_uri_domain, app_id, tenant_id, order_idx)"
+ ");";
+ " REFERENCES " + Config.getConfig(start).getTenantConfigsTable()
+ " (connection_uri_domain, app_id, tenant_id) ON DELETE CASCADE);";
// @formatter:on
}

public static String getQueryToCreateOrderIndexForDefaultRequiredFactorIdsTable(Start start) {
return "CREATE INDEX tenant_default_required_factor_ids_order_idx_index ON "
+ getConfig(start).getTenantDefaultRequiredFactorIdsTable() + " (order_idx ASC);";
}

private static void executeCreateTenantQueries(Start start, Connection sqlCon, TenantConfig tenantConfig)
throws SQLException, StorageTransactionLogicException, StorageQueryException {

Expand Down Expand Up @@ -186,7 +176,7 @@ private static void executeCreateTenantQueries(Start start, Connection sqlCon, T
}

MfaSqlHelper.createFirstFactors(start, sqlCon, tenantConfig.tenantIdentifier, tenantConfig.firstFactors);
MfaSqlHelper.createDefaultRequiredFactorIds(start, sqlCon, tenantConfig.tenantIdentifier, tenantConfig.defaultRequiredFactorIds);
MfaSqlHelper.createRequiredSecondaryFactors(start, sqlCon, tenantConfig.tenantIdentifier, tenantConfig.requiredSecondaryFactors);
}

public static void createTenantConfig(Start start, TenantConfig tenantConfig) throws StorageQueryException, StorageTransactionLogicException {
Expand Down Expand Up @@ -268,10 +258,10 @@ public static TenantConfig[] getAllTenants(Start start) throws StorageQueryExcep
// Map (tenantIdentifier) -> firstFactors
HashMap<TenantIdentifier, String[]> firstFactorsMap = MfaSqlHelper.selectAllFirstFactors(start);

// Map (tenantIdentifier) -> defaultRequiredFactorIds
HashMap<TenantIdentifier, String[]> defaultRequiredFactorIdsMap = MfaSqlHelper.selectAllDefaultRequiredFactorIds(start);
// Map (tenantIdentifier) -> requiredSecondaryFactors
HashMap<TenantIdentifier, String[]> requiredSecondaryFactorsMap = MfaSqlHelper.selectAllRequiredSecondaryFactors(start);

return TenantConfigSQLHelper.selectAll(start, providerMap, firstFactorsMap, defaultRequiredFactorIdsMap);
return TenantConfigSQLHelper.selectAll(start, providerMap, firstFactorsMap, requiredSecondaryFactorsMap);
} catch (SQLException throwables) {
throw new StorageQueryException(throwables);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public static HashMap<TenantIdentifier, String[]> selectAllFirstFactors(Start st
});
}

public static HashMap<TenantIdentifier, String[]> selectAllDefaultRequiredFactorIds(Start start)
public static HashMap<TenantIdentifier, String[]> selectAllRequiredSecondaryFactors(Start start)
throws SQLException, StorageQueryException {
String QUERY = "SELECT connection_uri_domain, app_id, tenant_id, factor_id, order_idx FROM "
+ getConfig(start).getTenantDefaultRequiredFactorIdsTable() + " ORDER BY order_idx ASC;";
String QUERY = "SELECT connection_uri_domain, app_id, tenant_id, factor_id FROM "
+ getConfig(start).getTenantRequiredSecondaryFactorsTable() + ";";
return execute(start, QUERY, pst -> {}, result -> {
HashMap<TenantIdentifier, List<String>> defaultRequiredFactors = new HashMap<>();

Expand Down Expand Up @@ -97,24 +97,20 @@ public static void createFirstFactors(Start start, Connection sqlCon, TenantIden
}
}

public static void createDefaultRequiredFactorIds(Start start, Connection sqlCon, TenantIdentifier tenantIdentifier, String[] defaultRequiredFactorIds)
public static void createRequiredSecondaryFactors(Start start, Connection sqlCon, TenantIdentifier tenantIdentifier, String[] requiredSecondaryFactors)
throws SQLException, StorageQueryException {
if (defaultRequiredFactorIds == null || defaultRequiredFactorIds.length == 0) {
if (requiredSecondaryFactors == null || requiredSecondaryFactors.length == 0) {
return;
}

String QUERY = "INSERT INTO " + getConfig(start).getTenantDefaultRequiredFactorIdsTable() + "(connection_uri_domain, app_id, tenant_id, factor_id, order_idx) VALUES (?, ?, ?, ?, ?);";
int orderIdx = 0;
for (String factorId : defaultRequiredFactorIds) {
int finalOrderIdx = orderIdx;
String QUERY = "INSERT INTO " + getConfig(start).getTenantRequiredSecondaryFactorsTable() + "(connection_uri_domain, app_id, tenant_id, factor_id) VALUES (?, ?, ?, ?);";
for (String factorId : requiredSecondaryFactors) {
update(sqlCon, QUERY, pst -> {
pst.setString(1, tenantIdentifier.getConnectionUriDomain());
pst.setString(2, tenantIdentifier.getAppId());
pst.setString(3, tenantIdentifier.getTenantId());
pst.setString(4, factorId);
pst.setInt(5, finalOrderIdx);
});
orderIdx++;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ public class TenantConfigSQLHelper {
public static class TenantConfigRowMapper implements RowMapper<TenantConfig, ResultSet> {
ThirdPartyConfig.Provider[] providers;
String[] firstFactors;
String[] defaultRequiredFactorIds;
String[] requiredSecondaryFactors;

private TenantConfigRowMapper(ThirdPartyConfig.Provider[] providers, String[] firstFactors, String[] defaultRequiredFactorIds) {
private TenantConfigRowMapper(ThirdPartyConfig.Provider[] providers, String[] firstFactors, String[] requiredSecondaryFactors) {
this.providers = providers;
this.firstFactors = firstFactors;
this.defaultRequiredFactorIds = defaultRequiredFactorIds;
this.requiredSecondaryFactors = requiredSecondaryFactors;
}

public static TenantConfigRowMapper getInstance(ThirdPartyConfig.Provider[] providers, String[] firstFactors, String[] defaultRequiredFactorIds) {
return new TenantConfigRowMapper(providers, firstFactors, defaultRequiredFactorIds);
public static TenantConfigRowMapper getInstance(ThirdPartyConfig.Provider[] providers, String[] firstFactors, String[] requiredSecondaryFactors) {
return new TenantConfigRowMapper(providers, firstFactors, requiredSecondaryFactors);
}

@Override
Expand All @@ -58,9 +58,8 @@ public TenantConfig map(ResultSet result) throws StorageQueryException {
new EmailPasswordConfig(result.getBoolean("email_password_enabled")),
new ThirdPartyConfig(result.getBoolean("third_party_enabled"), this.providers),
new PasswordlessConfig(result.getBoolean("passwordless_enabled")),
new TotpConfig(result.getBoolean("totp_enabled")),
result.getBoolean("has_first_factors") ? firstFactors : null,
result.getBoolean("has_default_required_factor_ids") ? defaultRequiredFactorIds : null,
firstFactors.length == 0 ? null : firstFactors,
requiredSecondaryFactors.length == 0 ? null : requiredSecondaryFactors,
JsonUtils.stringToJsonObject(result.getString("core_config"))
);
} catch (Exception e) {
Expand All @@ -69,11 +68,10 @@ public TenantConfig map(ResultSet result) throws StorageQueryException {
}
}

public static TenantConfig[] selectAll(Start start, HashMap<TenantIdentifier, HashMap<String, ThirdPartyConfig.Provider>> providerMap, HashMap<TenantIdentifier, String[]> firstFactorsMap, HashMap<TenantIdentifier, String[]> defaultRequiredFactorIdsMap)
public static TenantConfig[] selectAll(Start start, HashMap<TenantIdentifier, HashMap<String, ThirdPartyConfig.Provider>> providerMap, HashMap<TenantIdentifier, String[]> firstFactorsMap, HashMap<TenantIdentifier, String[]> requiredSecondaryFactorsMap)
throws SQLException, StorageQueryException {
String QUERY = "SELECT connection_uri_domain, app_id, tenant_id, core_config,"
+ " email_password_enabled, passwordless_enabled, third_party_enabled,"
+ " totp_enabled, has_first_factors, has_default_required_factor_ids FROM "
+ " email_password_enabled, passwordless_enabled, third_party_enabled FROM "
+ getConfig(start).getTenantConfigsTable() + ";";

TenantConfig[] tenantConfigs = execute(start, QUERY, pst -> {}, result -> {
Expand All @@ -86,9 +84,9 @@ public static TenantConfig[] selectAll(Start start, HashMap<TenantIdentifier, Ha
}
String[] firstFactors = firstFactorsMap.containsKey(tenantIdentifier) ? firstFactorsMap.get(tenantIdentifier) : new String[0];

String[] defaultRequiredFactorIds = defaultRequiredFactorIdsMap.containsKey(tenantIdentifier) ? defaultRequiredFactorIdsMap.get(tenantIdentifier) : new String[0];
String[] requiredSecondaryFactors = requiredSecondaryFactorsMap.containsKey(tenantIdentifier) ? requiredSecondaryFactorsMap.get(tenantIdentifier) : new String[0];

temp.add(TenantConfigSQLHelper.TenantConfigRowMapper.getInstance(providers, firstFactors, defaultRequiredFactorIds).mapOrThrow(result));
temp.add(TenantConfigSQLHelper.TenantConfigRowMapper.getInstance(providers, firstFactors, requiredSecondaryFactors).mapOrThrow(result));
}
TenantConfig[] finalResult = new TenantConfig[temp.size()];
for (int i = 0; i < temp.size(); i++) {
Expand All @@ -103,9 +101,8 @@ public static void create(Start start, Connection sqlCon, TenantConfig tenantCon
throws SQLException, StorageTransactionLogicException {
String QUERY = "INSERT INTO " + getConfig(start).getTenantConfigsTable()
+ "(connection_uri_domain, app_id, tenant_id, core_config,"
+ " email_password_enabled, passwordless_enabled, third_party_enabled,"
+ " totp_enabled, has_first_factors, has_default_required_factor_ids)"
+ " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
+ " email_password_enabled, passwordless_enabled, third_party_enabled)"
+ " VALUES(?, ?, ?, ?, ?, ?, ?)";

try {
update(sqlCon, QUERY, pst -> {
Expand All @@ -116,9 +113,6 @@ public static void create(Start start, Connection sqlCon, TenantConfig tenantCon
pst.setBoolean(5, tenantConfig.emailPasswordConfig.enabled);
pst.setBoolean(6, tenantConfig.passwordlessConfig.enabled);
pst.setBoolean(7, tenantConfig.thirdPartyConfig.enabled);
pst.setBoolean(8, tenantConfig.totpConfig.enabled);
pst.setBoolean(9, tenantConfig.firstFactors != null);
pst.setBoolean(10, tenantConfig.defaultRequiredFactorIds != null);
});
} catch (StorageQueryException e) {
throw new StorageTransactionLogicException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public void confirmHikariLoggerClosedOnlyWhenProcessEnds() throws Exception {
new EmailPasswordConfig(true),
new ThirdPartyConfig(true, null),
new PasswordlessConfig(true),
new TotpConfig(false), null, null,
null, null,
config
), false);

Expand Down
Loading

0 comments on commit f705920

Please sign in to comment.