-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Define TOTP recipe plugin interface #53
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
75edcbd
feat: Define TOTP recipe plugin interface
KShivendu 36056e6
fix: Improve TOTPStorage
KShivendu e56909a
fix: Changes to TOTPStorage
KShivendu 2799bb1
chores: Mention TOTP recipe in CHANGELOG
KShivendu a304185
fix(totp): Inherit from Exception instead of EmailPasswordException
KShivendu e192014
fix: Remove TotpNotEnabledException wherever its not possible to thro…
KShivendu 71b9e77
fix: Update the order of init params for TOTPDevice
KShivendu c73ed36
feat: Add optional deviceName to TOTPUsedCode
KShivendu 815c6b3
refactor: markDevicesAsVerified should return boolean
KShivendu dcf51a0
feat: Introduce equals for clean comparison in tests and rename getUs…
KShivendu dc4f7ba
feat: Add javadocs for TOTPStorage and createdTime for TOTPUsedCode
KShivendu 17abf94
feat: Add method to delete all the data for user
KShivendu ceefd3b
refactor: Improve interfaces and javadocs
KShivendu 2a172be
feat: Improve TOTP interface
KShivendu 873673c
refactor: Remove deleteAllTotpDataForUser of TOTPStorage
KShivendu 5dac287
refactor: Update getAllUsedCodes method name
KShivendu cde851a
Merge branch '2.20' into feat/totp-plugin-interface
KShivendu 8cec416
feat: Improved TOTP interface
KShivendu ba5c2ef
refactor: insertUsedCode and getAllusedCodes should be part of a tran…
KShivendu a9d39fb
refactor: Improve TOTP related transactions
KShivendu f422126
feat: Add plugin interface for active users storage layer (#62)
KShivendu f9d54b0
feat: Add more methods to active user storage interface for usage stats
KShivendu 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 hidden or 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
17 changes: 17 additions & 0 deletions
17
src/main/java/io/supertokens/pluginInterface/ActiveUsersStorage.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package io.supertokens.pluginInterface; | ||
|
|
||
| import io.supertokens.pluginInterface.exceptions.StorageQueryException; | ||
|
|
||
| public interface ActiveUsersStorage extends Storage { | ||
| /* Update the last active time of a user to now */ | ||
| void updateLastActive(String userId) throws StorageQueryException; | ||
|
|
||
| /* Count the number of users who did some activity after given timestamp */ | ||
| int countUsersActiveSince(long time) throws StorageQueryException; | ||
|
|
||
| /* Count the number of users who have enabled TOTP */ | ||
| int countUsersEnabledTotp() throws StorageQueryException; | ||
|
|
||
| /* Count the number of users who have enabled TOTP and are active */ | ||
| int countUsersEnabledTotpAndActiveSince(long time) throws StorageQueryException; | ||
| } |
This file contains hidden or 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
36 changes: 36 additions & 0 deletions
36
src/main/java/io/supertokens/pluginInterface/totp/TOTPDevice.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package io.supertokens.pluginInterface.totp; | ||
|
|
||
| public class TOTPDevice { | ||
| public final String deviceName; | ||
| public final String userId; | ||
| public final String secretKey; | ||
| public final int period; | ||
| public final int skew; | ||
| public final boolean verified; | ||
|
|
||
| public TOTPDevice(String userId, String deviceName, String secretKey, int period, int skew, boolean verified) { | ||
| this.userId = userId; | ||
| this.deviceName = deviceName; | ||
| this.secretKey = secretKey; | ||
| this.period = period; | ||
| this.skew = skew; | ||
| this.verified = verified; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj == null) { | ||
| return false; | ||
| } | ||
| if (obj == this) { | ||
| return true; | ||
| } | ||
| if (!(obj instanceof TOTPDevice)) { | ||
| return false; | ||
| } | ||
| TOTPDevice other = (TOTPDevice) obj; | ||
| return this.userId.equals(other.userId) && this.deviceName.equals(other.deviceName) | ||
| && this.secretKey.equals(other.secretKey) && this.period == other.period && this.skew == other.skew | ||
| && this.verified == other.verified; | ||
| } | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
src/main/java/io/supertokens/pluginInterface/totp/TOTPStorage.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package io.supertokens.pluginInterface.totp; | ||
|
|
||
| import io.supertokens.pluginInterface.nonAuthRecipe.NonAuthRecipeStorage; | ||
| import io.supertokens.pluginInterface.exceptions.StorageQueryException; | ||
| import io.supertokens.pluginInterface.totp.exception.DeviceAlreadyExistsException; | ||
| import io.supertokens.pluginInterface.totp.exception.UnknownDeviceException; | ||
|
|
||
| public interface TOTPStorage extends NonAuthRecipeStorage { | ||
| /** Create a new device and a new user if the user does not exist: */ | ||
| void createDevice(TOTPDevice device) | ||
| throws StorageQueryException, DeviceAlreadyExistsException; | ||
|
|
||
| /** Verify a user's device with the given name: */ | ||
| void markDeviceAsVerified(String userId, String deviceName) | ||
| throws StorageQueryException, UnknownDeviceException; | ||
|
|
||
| /** Update device name of a device: */ | ||
| void updateDeviceName(String userId, String oldDeviceName, String newDeviceName) | ||
| throws StorageQueryException, DeviceAlreadyExistsException, | ||
| UnknownDeviceException; | ||
|
|
||
| /** Get the devices for a user */ | ||
| TOTPDevice[] getDevices(String userId) | ||
| throws StorageQueryException; | ||
|
|
||
rishabhpoddar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** Remove expired codes from totp used codes for all users: */ | ||
| int removeExpiredCodes(long expiredBefore) | ||
| throws StorageQueryException; | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
src/main/java/io/supertokens/pluginInterface/totp/TOTPUsedCode.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package io.supertokens.pluginInterface.totp; | ||
|
|
||
| public class TOTPUsedCode { | ||
| public final String userId; | ||
| public final String code; | ||
| public final boolean isValid; | ||
| public final long expiryTime; | ||
| public final long createdTime; | ||
|
|
||
| public TOTPUsedCode(String userId, String code, Boolean isValidCode, long expiryTime, long createdTime) { | ||
| this.userId = userId; | ||
| this.code = code; | ||
| this.isValid = isValidCode; | ||
| this.expiryTime = expiryTime; | ||
| this.createdTime = createdTime; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj == null) { | ||
| return false; | ||
| } | ||
| if (obj == this) { | ||
| return true; | ||
| } | ||
| if (!(obj instanceof TOTPUsedCode)) { | ||
| return false; | ||
| } | ||
| TOTPUsedCode other = (TOTPUsedCode) obj; | ||
| return this.userId.equals(other.userId) && this.code.equals(other.code) | ||
| && this.isValid == other.isValid && this.expiryTime == other.expiryTime | ||
| && this.createdTime == other.createdTime; | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
...main/java/io/supertokens/pluginInterface/totp/exception/DeviceAlreadyExistsException.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package io.supertokens.pluginInterface.totp.exception; | ||
|
|
||
| public class DeviceAlreadyExistsException extends Exception { | ||
| private static final long serialVersionUID = 6848053563771647272L; | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/java/io/supertokens/pluginInterface/totp/exception/TotpNotEnabledException.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package io.supertokens.pluginInterface.totp.exception; | ||
|
|
||
| public class TotpNotEnabledException extends Exception { | ||
| private static final long serialVersionUID = 6848053563771647272L; | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/java/io/supertokens/pluginInterface/totp/exception/UnknownDeviceException.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package io.supertokens.pluginInterface.totp.exception; | ||
|
|
||
| public class UnknownDeviceException extends Exception { | ||
| private static final long serialVersionUID = 6848053563771647272L; | ||
| } |
5 changes: 5 additions & 0 deletions
5
...in/java/io/supertokens/pluginInterface/totp/exception/UsedCodeAlreadyExistsException.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package io.supertokens.pluginInterface.totp.exception; | ||
|
|
||
| public class UsedCodeAlreadyExistsException extends Exception { | ||
|
|
||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/io/supertokens/pluginInterface/totp/sqlStorage/TOTPSQLStorage.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package io.supertokens.pluginInterface.totp.sqlStorage; | ||
|
|
||
| import io.supertokens.pluginInterface.exceptions.StorageQueryException; | ||
| import io.supertokens.pluginInterface.sqlStorage.SQLStorage; | ||
| import io.supertokens.pluginInterface.sqlStorage.TransactionConnection; | ||
| import io.supertokens.pluginInterface.totp.TOTPDevice; | ||
| import io.supertokens.pluginInterface.totp.TOTPStorage; | ||
| import io.supertokens.pluginInterface.totp.TOTPUsedCode; | ||
| import io.supertokens.pluginInterface.totp.exception.TotpNotEnabledException; | ||
| import io.supertokens.pluginInterface.totp.exception.UsedCodeAlreadyExistsException; | ||
|
|
||
| public interface TOTPSQLStorage extends TOTPStorage, SQLStorage { | ||
| public int deleteDevice_Transaction(TransactionConnection con, String userId, String deviceName) | ||
| throws StorageQueryException; | ||
|
|
||
| public TOTPDevice[] getDevices_Transaction(TransactionConnection con, String userId) | ||
| throws StorageQueryException; | ||
|
|
||
| public void removeUser_Transaction(TransactionConnection con, String userId) | ||
| throws StorageQueryException; | ||
|
|
||
| /** | ||
| * Get totp used codes for user (expired/non-expired) yet (sorted by descending | ||
| * order of created time): | ||
| */ | ||
| public TOTPUsedCode[] getAllUsedCodesDescOrder_Transaction(TransactionConnection con, | ||
| String userId) | ||
| throws StorageQueryException; | ||
|
|
||
| /** Insert a used TOTP code for an existing user: */ | ||
| void insertUsedCode_Transaction(TransactionConnection con, TOTPUsedCode code) | ||
| throws StorageQueryException, TotpNotEnabledException, UsedCodeAlreadyExistsException; | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.