Skip to content
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

refactor: Replace TOTP_NOT_ENABLED_ERROR status and make deviceName optional #729

Merged
merged 20 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
06b2cf3
refactor: Dont send TOTP_NOT_ENABLED_ERROR status
KShivendu Jun 22, 2023
693b70e
refactor: Add comments
KShivendu Jun 23, 2023
b10a23c
chores: Remove extra comments
KShivendu Jun 23, 2023
1a14e22
Merge branch 'feat/mfa' into refactor/avoid-totp-not-enabled
KShivendu Jun 26, 2023
14209d8
refactor: Completely replace totp not enabled error with unknown devi…
KShivendu Jun 27, 2023
157d94a
refactor: Remove Totp not enabled error
KShivendu Jun 27, 2023
fc9f706
feat: Make device name optional and generate it from number of existi…
KShivendu Jun 27, 2023
ec6dd7e
Replace TotpNotEnabledError with UnknownUserIdTotpError
KShivendu Jun 28, 2023
98b24ae
refactor: Recursively generate device name when it already exists
KShivendu Jun 29, 2023
646b0b8
refactor: Remove redundant arguments
KShivendu Jun 29, 2023
430704e
feat: Remove the param to allow unverified devices from the verify to…
KShivendu Jul 3, 2023
a7d2f27
Merge branch 'master' into refactor/avoid-totp-not-enabled
KShivendu Sep 27, 2023
363492c
feat: Reject unverified devices
KShivendu Sep 27, 2023
5b68216
Merge branch 'feat/mfa' into refactor/avoid-totp-not-enabled
KShivendu Sep 27, 2023
59373c2
feat: Add UNKNOWN_USER_ID_ERROR to verify totp api
KShivendu Sep 27, 2023
84b1b9d
feat: Throw Unknown user id error when device gets deleted during ver…
KShivendu Sep 27, 2023
7ef46d1
Merge branch 'feat/mfa' into refactor/avoid-totp-not-enabled
sattvikc Sep 28, 2023
29f9643
fix: core fixes
sattvikc Sep 28, 2023
6be5b8c
fix: cleanup
sattvikc Sep 28, 2023
0139624
fix: tests
sattvikc Sep 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions src/main/java/io/supertokens/inmemorydb/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
import io.supertokens.pluginInterface.totp.TOTPStorage;
import io.supertokens.pluginInterface.totp.TOTPUsedCode;
import io.supertokens.pluginInterface.totp.exception.DeviceAlreadyExistsException;
import io.supertokens.pluginInterface.totp.exception.TotpNotEnabledException;
import io.supertokens.pluginInterface.totp.exception.UnknownDeviceException;
import io.supertokens.pluginInterface.totp.exception.UnknownTotpUserIdException;
import io.supertokens.pluginInterface.totp.exception.UsedCodeAlreadyExistsException;
import io.supertokens.pluginInterface.totp.sqlStorage.TOTPSQLStorage;
import io.supertokens.pluginInterface.useridmapping.UserIdMapping;
Expand Down Expand Up @@ -2601,29 +2601,40 @@ public void revokeExpiredSessions() throws StorageQueryException {
// TOTP recipe:
@Override
public void createDevice(AppIdentifier appIdentifier, TOTPDevice device)
throws StorageQueryException, DeviceAlreadyExistsException, TenantOrAppNotFoundException {
throws DeviceAlreadyExistsException, TenantOrAppNotFoundException, StorageQueryException {
try {
TOTPQueries.createDevice(this, appIdentifier, device);
} catch (StorageTransactionLogicException e) {
if (e.actualException instanceof SQLiteException) {
String errMsg = e.actualException.getMessage();

if (isPrimaryKeyError(errMsg, Config.getConfig(this).getTotpUserDevicesTable(),
new String[]{"app_id", "user_id", "device_name"})) {
throw new DeviceAlreadyExistsException();
} else if (isForeignKeyConstraintError(
errMsg,
Config.getConfig(this).getAppsTable(),
new String[]{"app_id"},
new Object[]{appIdentifier.getAppId()})) {
throw new TenantOrAppNotFoundException(appIdentifier);
startTransaction(con -> {
try {
createDevice_Transaction(con, new AppIdentifier(null, null), device);
} catch (DeviceAlreadyExistsException | TenantOrAppNotFoundException e) {
throw new StorageTransactionLogicException(e);
}
return null;
});
} catch (StorageTransactionLogicException e) {
if (e.actualException instanceof DeviceAlreadyExistsException) {
throw (DeviceAlreadyExistsException) e.actualException;
} else if (e.actualException instanceof TenantOrAppNotFoundException) {
throw (TenantOrAppNotFoundException) e.actualException;
} else if (e.actualException instanceof StorageQueryException) {
throw (StorageQueryException) e.actualException;
}

throw new StorageQueryException(e.actualException);
}
}

@Override
public TOTPDevice createDevice_Transaction(TransactionConnection con, AppIdentifier appIdentifier, TOTPDevice device)
throws DeviceAlreadyExistsException, TenantOrAppNotFoundException, StorageQueryException {
// TODO
return null;
}

@Override
public TOTPDevice getDeviceByName_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId, String deviceName) throws StorageQueryException {
// TODO
return null;
}

@Override
public void markDeviceAsVerified(AppIdentifier appIdentifier, String userId, String deviceName)
throws StorageQueryException, UnknownDeviceException {
Expand Down Expand Up @@ -2717,7 +2728,7 @@ public TOTPDevice[] getDevices_Transaction(TransactionConnection con, AppIdentif
@Override
public void insertUsedCode_Transaction(TransactionConnection con, TenantIdentifier tenantIdentifier,
TOTPUsedCode usedCodeObj)
throws StorageQueryException, TotpNotEnabledException, UsedCodeAlreadyExistsException,
throws StorageQueryException, UnknownTotpUserIdException, UsedCodeAlreadyExistsException,
TenantOrAppNotFoundException {
Connection sqlCon = (Connection) con.getConnection();
try {
Expand All @@ -2732,7 +2743,7 @@ public void insertUsedCode_Transaction(TransactionConnection con, TenantIdentifi
Config.getConfig(this).getTotpUsersTable(),
new String[]{"app_id", "user_id"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my comment in plugin interface PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you're talking about replacing TotpNotEnabledError with new UnknownUserIdError. If yes, done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check and confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

new Object[]{tenantIdentifier.getAppId(), usedCodeObj.userId})) {
throw new TotpNotEnabledException();
throw new UnknownTotpUserIdException();

} else if (isForeignKeyConstraintError(
e.getMessage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static int countUsersEnabledTotpAndActiveSince(Start start, AppIdentifier
}

public static int countUsersEnabledMfa(Start start, AppIdentifier appIdentifier) throws SQLException, StorageQueryException {
String QUERY = "SELECT COUNT(*) as total FROM (SELECT DISTINCT user_id FROM " + Config.getConfig(start).getMfaUserFactorsTable() + "WHERE app_id = ?) AS app_mfa_users";
String QUERY = "SELECT COUNT(*) as total FROM (SELECT DISTINCT user_id FROM " + Config.getConfig(start).getMfaUserFactorsTable() + " WHERE app_id = ?) AS app_mfa_users";

return execute(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
Expand Down Expand Up @@ -127,7 +127,8 @@ public static int countUsersEnabledMfaAndActiveSince(Start start, AppIdentifier
});
}

public static int updateUserLastActive(Start start, AppIdentifier appIdentifier, String userId) throws SQLException, StorageQueryException {
public static int updateUserLastActive(Start start, AppIdentifier appIdentifier, String userId)
throws SQLException, StorageQueryException {
String QUERY = "INSERT INTO " + Config.getConfig(start).getUserLastActiveTable()
+
"(app_id, user_id, last_active_time) VALUES(?, ?, ?) ON CONFLICT(app_id, user_id) DO UPDATE SET " +
Expand Down
Loading
Loading