diff --git a/src/main/java/io/supertokens/emailpassword/EmailPassword.java b/src/main/java/io/supertokens/emailpassword/EmailPassword.java index 1172c91c8..1cc77b015 100644 --- a/src/main/java/io/supertokens/emailpassword/EmailPassword.java +++ b/src/main/java/io/supertokens/emailpassword/EmailPassword.java @@ -110,7 +110,7 @@ public static UserInfo signUp(TenantIdentifierWithStorage tenantIdentifierWithSt try { UserInfo user = new UserInfo(userId, email, hashedPassword, timeJoined); - ((EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage()).signUp(tenantIdentifierWithStorage, user); + tenantIdentifierWithStorage.getEmailPasswordStorage().signUp(tenantIdentifierWithStorage, user); return user; @@ -159,7 +159,7 @@ public static ImportUserResponse importUserWithPasswordHash(TenantIdentifierWith long timeJoined = System.currentTimeMillis(); UserInfo userInfo = new UserInfo(userId, email, passwordHash, timeJoined); - EmailPasswordSQLStorage storage = (EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage(); + EmailPasswordSQLStorage storage = tenantIdentifierWithStorage.getEmailPasswordStorage(); try { storage.signUp(tenantIdentifierWithStorage, userInfo); @@ -221,7 +221,7 @@ public static UserInfo signIn(TenantIdentifierWithStorage tenantIdentifierWithSt throw new BadPermissionException("Email password login not enabled for tenant"); } - UserInfo user = ((EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage()) + UserInfo user = tenantIdentifierWithStorage.getEmailPasswordStorage() .getUserInfoUsingEmail(tenantIdentifierWithStorage, email); if (user == null) { @@ -296,7 +296,7 @@ public static String generatePasswordResetToken(TenantIdentifierWithStorage tena String hashedToken = Utils.hashSHA256(token); try { - ((EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage()).addPasswordResetToken( + tenantIdentifierWithStorage.getEmailPasswordStorage().addPasswordResetToken( tenantIdentifierWithStorage.toAppIdentifier(), new PasswordResetTokenInfo(userId, hashedToken, System.currentTimeMillis() + getPasswordResetTokenLifetime(tenantIdentifierWithStorage, main))); @@ -327,7 +327,7 @@ public static String resetPassword(TenantIdentifierWithStorage tenantIdentifierW String hashedToken = Utils.hashSHA256(token); String hashedPassword = PasswordHashing.getInstance(main) .createHashWithSalt(tenantIdentifierWithStorage.toAppIdentifier(), password); - EmailPasswordSQLStorage storage = (EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage(); + EmailPasswordSQLStorage storage = tenantIdentifierWithStorage.getEmailPasswordStorage(); PasswordResetTokenInfo resetInfo = storage.getPasswordResetTokenInfo( tenantIdentifierWithStorage.toAppIdentifier(), hashedToken); @@ -399,7 +399,7 @@ public static void updateUsersEmailOrPassword(AppIdentifierWithStorage appIdenti @Nullable String password) throws StorageQueryException, StorageTransactionLogicException, UnknownUserIdException, DuplicateEmailException, TenantOrAppNotFoundException { - EmailPasswordSQLStorage storage = (EmailPasswordSQLStorage) appIdentifierWithStorage.getStorage(); + EmailPasswordSQLStorage storage = appIdentifierWithStorage.getEmailPasswordStorage(); try { storage.startTransaction(transaction -> { try { @@ -455,12 +455,12 @@ public static UserInfo getUserUsingId(Main main, String userId) public static UserInfo getUserUsingId(AppIdentifierWithStorage appIdentifierWithStorage, String userId) throws StorageQueryException, TenantOrAppNotFoundException { - return ((EmailPasswordSQLStorage) appIdentifierWithStorage.getStorage()).getUserInfoUsingId( - appIdentifierWithStorage, userId); + return appIdentifierWithStorage.getEmailPasswordStorage().getUserInfoUsingId(appIdentifierWithStorage, userId); } public static UserInfo getUserUsingEmail(TenantIdentifierWithStorage tenantIdentifierWithStorage, String email) throws StorageQueryException, TenantOrAppNotFoundException { - return ((EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage()).getUserInfoUsingEmail(tenantIdentifierWithStorage, email); + return tenantIdentifierWithStorage.getEmailPasswordStorage().getUserInfoUsingEmail( + tenantIdentifierWithStorage, email); } } diff --git a/src/main/java/io/supertokens/storageLayer/StorageLayer.java b/src/main/java/io/supertokens/storageLayer/StorageLayer.java index e9f752bc1..a87da502d 100644 --- a/src/main/java/io/supertokens/storageLayer/StorageLayer.java +++ b/src/main/java/io/supertokens/storageLayer/StorageLayer.java @@ -349,26 +349,6 @@ public static SessionStorage getSessionStorage(Main main) { } } - public static EmailPasswordSQLStorage getEmailPasswordStorage(TenantIdentifier tenantIdentifier, - Main main) throws TenantOrAppNotFoundException { - // TODO remove this function - if (getInstance(tenantIdentifier, main).storage.getType() != STORAGE_TYPE.SQL) { - // we only support SQL for now - throw new UnsupportedOperationException(""); - } - return (EmailPasswordSQLStorage) getInstance(tenantIdentifier, main).storage; - } - - @TestOnly - public static EmailPasswordSQLStorage getEmailPasswordStorage(Main main) { - // TODO remove this function - try { - return getEmailPasswordStorage(new TenantIdentifier(null, null, null), main); - } catch (TenantOrAppNotFoundException e) { - throw new IllegalStateException(e); - } - } - public static EmailVerificationSQLStorage getEmailVerificationStorage(TenantIdentifier tenantIdentifier, Main main) throws TenantOrAppNotFoundException { diff --git a/src/test/java/io/supertokens/test/emailpassword/DeleteExpiredPasswordResetTokensCronjobTest.java b/src/test/java/io/supertokens/test/emailpassword/DeleteExpiredPasswordResetTokensCronjobTest.java index 3cebcaadb..813f2ecc6 100644 --- a/src/test/java/io/supertokens/test/emailpassword/DeleteExpiredPasswordResetTokensCronjobTest.java +++ b/src/test/java/io/supertokens/test/emailpassword/DeleteExpiredPasswordResetTokensCronjobTest.java @@ -23,6 +23,7 @@ import io.supertokens.pluginInterface.STORAGE_TYPE; import io.supertokens.pluginInterface.emailpassword.PasswordResetTokenInfo; import io.supertokens.pluginInterface.emailpassword.UserInfo; +import io.supertokens.pluginInterface.emailpassword.sqlStorage.EmailPasswordSQLStorage; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; import io.supertokens.storageLayer.StorageLayer; import io.supertokens.test.TestingProcessManager; @@ -73,12 +74,12 @@ public void checkingCronJob() throws Exception { EmailPassword.generatePasswordResetToken(process.getProcess(), user.id); EmailPassword.generatePasswordResetToken(process.getProcess(), user.id); - assert (StorageLayer.getEmailPasswordStorage(process.getProcess()) + assert (((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id).length == 4); Thread.sleep(3000); - PasswordResetTokenInfo[] tokens = StorageLayer.getEmailPasswordStorage(process.getProcess()) + PasswordResetTokenInfo[] tokens = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id); assert (tokens.length == 2); diff --git a/src/test/java/io/supertokens/test/emailpassword/EmailPasswordTest.java b/src/test/java/io/supertokens/test/emailpassword/EmailPasswordTest.java index 8b78fc850..1be859347 100644 --- a/src/test/java/io/supertokens/test/emailpassword/EmailPasswordTest.java +++ b/src/test/java/io/supertokens/test/emailpassword/EmailPasswordTest.java @@ -29,8 +29,10 @@ import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicatePasswordResetTokenException; import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateUserIdException; import io.supertokens.pluginInterface.emailpassword.exceptions.UnknownUserIdException; +import io.supertokens.pluginInterface.emailpassword.sqlStorage.EmailPasswordSQLStorage; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; import io.supertokens.pluginInterface.multitenancy.TenantIdentifier; +import io.supertokens.pluginInterface.multitenancy.TenantIdentifierWithStorage; import io.supertokens.storageLayer.StorageLayer; import io.supertokens.test.TestingProcessManager; import io.supertokens.test.Utils; @@ -77,12 +79,12 @@ public void testStorageLayerGetMailPasswordStorageLayerThrowsExceptionIfTypeIsNo if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { try { - StorageLayer.getEmailPasswordStorage(process.getProcess()); + new TenantIdentifierWithStorage(null, null, null, StorageLayer.getStorage(process.getProcess())).getEmailPasswordStorage(); throw new Exception("Should not come here"); } catch (UnsupportedOperationException e) { } } else { - StorageLayer.getEmailPasswordStorage(process.getProcess()); + new TenantIdentifierWithStorage(null, null, null, StorageLayer.getStorage(process.getProcess())).getEmailPasswordStorage(); } process.kill(); @@ -169,7 +171,7 @@ public void testThatAfterSignUpThePasswordIsHashedAndStoredInTheDatabase() throw UserInfo user = EmailPassword.signUp(process.getProcess(), "random@gmail.com", "validPass123"); - UserInfo userInfo = StorageLayer.getEmailPasswordStorage(process.getProcess()) + UserInfo userInfo = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getUserInfoUsingEmail(new TenantIdentifier(null, null, null), user.email); assertNotEquals(userInfo.passwordHash, "validPass123"); assertTrue(PasswordHashing.getInstance(process.getProcess()).verifyPasswordWithHash("validPass123", @@ -196,7 +198,7 @@ public void testThatAfterResetPasswordGenerateTokenTheTokenIsHashedInTheDatabase UserInfo user = EmailPassword.signUp(process.getProcess(), "random@gmail.com", "validPass123"); String resetToken = EmailPassword.generatePasswordResetToken(process.getProcess(), user.id); - PasswordResetTokenInfo resetTokenInfo = StorageLayer.getEmailPasswordStorage(process.getProcess()) + PasswordResetTokenInfo resetTokenInfo = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getPasswordResetTokenInfo(new AppIdentifier(null, null), io.supertokens.utils.Utils.hashSHA256(resetToken)); @@ -227,7 +229,7 @@ public void testThatAfterResetPasswordIsCompletedThePasswordIsHashedInTheDatabas EmailPassword.resetPassword(process.getProcess(), resetToken, "newValidPass123"); - UserInfo userInfo = StorageLayer.getEmailPasswordStorage(process.getProcess()) + UserInfo userInfo = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getUserInfoUsingEmail(new TenantIdentifier(null, null, null), user.email); assertNotEquals(userInfo.passwordHash, "newValidPass123"); @@ -256,7 +258,7 @@ public void passwordResetTokenExpiredCheck() throws Exception { String tok = EmailPassword.generatePasswordResetToken(process.getProcess(), user.id); - assert (StorageLayer.getEmailPasswordStorage(process.getProcess()) + assert (((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id).length == 1); Thread.sleep(20); @@ -268,7 +270,7 @@ public void passwordResetTokenExpiredCheck() throws Exception { } - assert (StorageLayer.getEmailPasswordStorage(process.getProcess()) + assert (((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id).length == 0); process.kill(); @@ -292,14 +294,14 @@ public void multiplePasswordResetTokensPerUserAndThenVerifyWithSignin() throws E String tok = EmailPassword.generatePasswordResetToken(process.getProcess(), user.id); EmailPassword.generatePasswordResetToken(process.getProcess(), user.id); - PasswordResetTokenInfo[] tokens = StorageLayer.getEmailPasswordStorage(process.getProcess()) + PasswordResetTokenInfo[] tokens = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id); assert (tokens.length == 3); EmailPassword.resetPassword(process.getProcess(), tok, "newPassword"); - tokens = StorageLayer.getEmailPasswordStorage(process.getProcess()) + tokens = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id); assert (tokens.length == 0); @@ -328,7 +330,7 @@ public void zeroPasswordTokens() throws Exception { if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { return; } - PasswordResetTokenInfo[] tokens = StorageLayer.getEmailPasswordStorage(process.getProcess()) + PasswordResetTokenInfo[] tokens = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), "8ed86166-bfd8-4234-9dfe-abca9606dbd5"); @@ -374,14 +376,14 @@ public void clashingPassowordResetToken() throws Exception { // we add a user first. UserInfo user = EmailPassword.signUp(process.getProcess(), "test1@example.com", "password"); - StorageLayer.getEmailPasswordStorage(process.getProcess()) + ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .addPasswordResetToken(new AppIdentifier(null, null), new PasswordResetTokenInfo( user.id, "token", System.currentTimeMillis() + Config.getConfig(process.getProcess()).getPasswordResetTokenLifetime())); try { - StorageLayer.getEmailPasswordStorage(process.getProcess()) + ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .addPasswordResetToken(new AppIdentifier(null, null), new PasswordResetTokenInfo(user.id, "token", System.currentTimeMillis() + Config.getConfig(process.getProcess()).getPasswordResetTokenLifetime())); @@ -427,13 +429,13 @@ public void clashingUserIdDuringSignUp() throws Exception { return; } - StorageLayer.getEmailPasswordStorage(process.getProcess()) + ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .signUp(new TenantIdentifier(null, null, null), new UserInfo( "8ed86166-bfd8-4234-9dfe-abca9606dbd5", "test@example.com", "password", System.currentTimeMillis())); try { - StorageLayer.getEmailPasswordStorage(process.getProcess()) + ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .signUp(new TenantIdentifier(null, null, null), new UserInfo("8ed86166-bfd8-4234-9dfe-abca9606dbd5", "test1@example.com", "password", System.currentTimeMillis())); @@ -480,13 +482,13 @@ public void clashingEmailAndUserIdDuringSignUp() throws Exception { return; } - StorageLayer.getEmailPasswordStorage(process.getProcess()) + ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .signUp(new TenantIdentifier(null, null, null), new UserInfo( "8ed86166-bfd8-4234-9dfe-abca9606dbd5", "test@example.com", "password", System.currentTimeMillis())); try { - StorageLayer.getEmailPasswordStorage(process.getProcess()) + ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .signUp(new TenantIdentifier(null, null, null), new UserInfo("8ed86166-bfd8-4234-9dfe-abca9606dbd5", "test@example.com", "password", System.currentTimeMillis())); diff --git a/src/test/java/io/supertokens/test/emailpassword/api/ImportUserWithPasswordHashAPITest.java b/src/test/java/io/supertokens/test/emailpassword/api/ImportUserWithPasswordHashAPITest.java index eb3df5838..58903f7b8 100644 --- a/src/test/java/io/supertokens/test/emailpassword/api/ImportUserWithPasswordHashAPITest.java +++ b/src/test/java/io/supertokens/test/emailpassword/api/ImportUserWithPasswordHashAPITest.java @@ -331,7 +331,7 @@ public void testSigningInAUserWhenStoredPasswordHashIsIncorrect() throws Excepti long timeJoined = System.currentTimeMillis(); UserInfo userInfo = new UserInfo("userId", email, combinedPasswordHash, timeJoined); - EmailPasswordSQLStorage storage = StorageLayer.getEmailPasswordStorage(process.getProcess()); + EmailPasswordSQLStorage storage = (EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()); storage.signUp(new TenantIdentifier(null, null, null), userInfo); @@ -374,7 +374,7 @@ public void testSigningInAUserWithFirebasePasswordHashWithoutSettingTheSignerKey long timeJoined = System.currentTimeMillis(); UserInfo userInfo = new UserInfo("userId", email, combinedPasswordHash, timeJoined); - EmailPasswordSQLStorage storage = StorageLayer.getEmailPasswordStorage(process.getProcess()); + EmailPasswordSQLStorage storage = (EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()); storage.signUp(new TenantIdentifier(null, null, null), userInfo); diff --git a/src/test/java/io/supertokens/test/emailpassword/api/SignUpAPITest2_7.java b/src/test/java/io/supertokens/test/emailpassword/api/SignUpAPITest2_7.java index d4a6f43dd..202b2952c 100644 --- a/src/test/java/io/supertokens/test/emailpassword/api/SignUpAPITest2_7.java +++ b/src/test/java/io/supertokens/test/emailpassword/api/SignUpAPITest2_7.java @@ -20,6 +20,7 @@ import io.supertokens.ProcessState; import io.supertokens.pluginInterface.STORAGE_TYPE; import io.supertokens.pluginInterface.emailpassword.UserInfo; +import io.supertokens.pluginInterface.emailpassword.sqlStorage.EmailPasswordSQLStorage; import io.supertokens.pluginInterface.multitenancy.AppIdentifier; import io.supertokens.pluginInterface.multitenancy.TenantIdentifier; import io.supertokens.storageLayer.StorageLayer; @@ -133,7 +134,7 @@ public void testGoodInput() throws Exception { assertEquals(signUpUser.get("email").getAsString(), "random@gmail.com"); assertNotNull(signUpUser.get("id")); - UserInfo user = StorageLayer.getEmailPasswordStorage(process.getProcess()) + UserInfo user = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getUserInfoUsingEmail(new TenantIdentifier(null, null, null), "random@gmail.com"); assertEquals(user.email, signUpUser.get("email").getAsString()); assertEquals(user.id, signUpUser.get("id").getAsString()); @@ -182,7 +183,7 @@ public void testTheNormaliseEmailFunction() throws Exception { assertEquals(signUpUser.get("email").getAsString(), "random@gmail.com"); assertNotNull(signUpUser.get("id")); - UserInfo userInfo = StorageLayer.getEmailPasswordStorage(process.getProcess()) + UserInfo userInfo = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess())) .getUserInfoUsingId(new AppIdentifier(null, null), signUpUser.get("id").getAsString()); assertEquals(userInfo.email, "random@gmail.com");