-
Notifications
You must be signed in to change notification settings - Fork 537
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
Changes from 3 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 693b70e
refactor: Add comments
KShivendu b10a23c
chores: Remove extra comments
KShivendu 1a14e22
Merge branch 'feat/mfa' into refactor/avoid-totp-not-enabled
KShivendu 14209d8
refactor: Completely replace totp not enabled error with unknown devi…
KShivendu 157d94a
refactor: Remove Totp not enabled error
KShivendu fc9f706
feat: Make device name optional and generate it from number of existi…
KShivendu ec6dd7e
Replace TotpNotEnabledError with UnknownUserIdTotpError
KShivendu 98b24ae
refactor: Recursively generate device name when it already exists
KShivendu 646b0b8
refactor: Remove redundant arguments
KShivendu 430704e
feat: Remove the param to allow unverified devices from the verify to…
KShivendu a7d2f27
Merge branch 'master' into refactor/avoid-totp-not-enabled
KShivendu 363492c
feat: Reject unverified devices
KShivendu 5b68216
Merge branch 'feat/mfa' into refactor/avoid-totp-not-enabled
KShivendu 59373c2
feat: Add UNKNOWN_USER_ID_ERROR to verify totp api
KShivendu 84b1b9d
feat: Throw Unknown user id error when device gets deleted during ver…
KShivendu 7ef46d1
Merge branch 'feat/mfa' into refactor/avoid-totp-not-enabled
sattvikc 29f9643
fix: core fixes
sattvikc 6be5b8c
fix: cleanup
sattvikc 0139624
fix: tests
sattvikc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
import io.supertokens.pluginInterface.totp.TOTPDevice; | ||
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.UsedCodeAlreadyExistsException; | ||
import io.supertokens.pluginInterface.totp.sqlStorage.TOTPSQLStorage; | ||
|
@@ -107,6 +106,11 @@ public static TOTPDevice registerDevice(AppIdentifierWithStorage appIdentifierWi | |
} | ||
|
||
TOTPSQLStorage totpStorage = appIdentifierWithStorage.getTOTPStorage(); | ||
|
||
if (deviceName == null) { | ||
rishabhpoddar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TOTPDevice[] devices = totpStorage.getDevices(appIdentifierWithStorage, userId); | ||
deviceName = "TOTP Device " + (devices.length + 1); | ||
rishabhpoddar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
String secret = generateSecret(); | ||
TOTPDevice device = new TOTPDevice(userId, deviceName, secret, period, skew, false); | ||
|
@@ -118,7 +122,7 @@ public static TOTPDevice registerDevice(AppIdentifierWithStorage appIdentifierWi | |
private static void checkAndStoreCode(TenantIdentifierWithStorage tenantIdentifierWithStorage, Main main, | ||
String userId, TOTPDevice[] devices, | ||
String code) | ||
throws InvalidTotpException, TotpNotEnabledException, | ||
throws InvalidTotpException, UnknownDeviceException, | ||
LimitReachedException, StorageQueryException, StorageTransactionLogicException, | ||
TenantOrAppNotFoundException { | ||
// Note that the TOTP cron runs every 1 hour, so all the expired tokens can stay | ||
|
@@ -241,7 +245,7 @@ private static void checkAndStoreCode(TenantIdentifierWithStorage tenantIdentifi | |
try { | ||
totpSQLStorage.insertUsedCode_Transaction(con, tenantIdentifierWithStorage, newCode); | ||
totpSQLStorage.commitTransaction(con); | ||
} catch (UsedCodeAlreadyExistsException | TotpNotEnabledException e) { | ||
} catch (UsedCodeAlreadyExistsException | UnknownDeviceException e) { | ||
rishabhpoddar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new StorageTransactionLogicException(e); | ||
} | ||
|
||
|
@@ -264,8 +268,8 @@ private static void checkAndStoreCode(TenantIdentifierWithStorage tenantIdentifi | |
throw (LimitReachedException) e.actualException; | ||
} else if (e.actualException instanceof InvalidTotpException) { | ||
throw (InvalidTotpException) e.actualException; | ||
} else if (e.actualException instanceof TotpNotEnabledException) { | ||
throw (TotpNotEnabledException) e.actualException; | ||
} else if (e.actualException instanceof UnknownDeviceException) { | ||
throw (UnknownDeviceException) e.actualException; | ||
} else if (e.actualException instanceof UsedCodeAlreadyExistsException) { | ||
rishabhpoddar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// retry the transaction after a small delay: | ||
int delayInMs = (int) (Math.random() * 10 + 1); | ||
|
@@ -339,7 +343,7 @@ public static boolean verifyDevice(TenantIdentifierWithStorage tenantIdentifierW | |
// This behaviour is okay so we can ignore it. | ||
try{ | ||
checkAndStoreCode(tenantIdentifierWithStorage, main, userId, new TOTPDevice[]{matchingDevice}, code); | ||
} catch (TotpNotEnabledException e) { | ||
} catch (UnknownDeviceException e) { | ||
// User must have deleted the device in parallel. | ||
throw new UnknownDeviceException(); | ||
} | ||
|
@@ -393,7 +397,7 @@ public static void verifyCode(TenantIdentifierWithStorage tenantIdentifierWithSt | |
// can ignore it. | ||
try{ | ||
checkAndStoreCode(tenantIdentifierWithStorage, main, userId, devices, code); | ||
} catch (TotpNotEnabledException e) { | ||
} catch (UnknownDeviceException e) { | ||
// User must have deleted the device in parallel | ||
// since they cannot un-verify a device (no API exists) | ||
throw new InvalidTotpException(); | ||
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. maybe it's better to throw an UnknownUserIdTotpException, since the input to this API is also the 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. Done |
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
see my comment in plugin interface PR
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.
I believe you're talking about replacing
TotpNotEnabledError
with newUnknownUserIdError
. If yes, done.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.
Please check and confirm.
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.
Done