Skip to content

fix: Multitenant emailpassword #607

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 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 9 additions & 9 deletions src/main/java/io/supertokens/emailpassword/EmailPassword.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
20 changes: 0 additions & 20 deletions src/main/java/io/supertokens/storageLayer/StorageLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -169,7 +171,7 @@ public void testThatAfterSignUpThePasswordIsHashedAndStoredInTheDatabase() throw

UserInfo user = EmailPassword.signUp(process.getProcess(), "[email protected]", "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",
Expand All @@ -196,7 +198,7 @@ public void testThatAfterResetPasswordGenerateTokenTheTokenIsHashedInTheDatabase
UserInfo user = EmailPassword.signUp(process.getProcess(), "[email protected]", "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));

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -374,14 +376,14 @@ public void clashingPassowordResetToken() throws Exception {
// we add a user first.
UserInfo user = EmailPassword.signUp(process.getProcess(), "[email protected]", "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()));
Expand Down Expand Up @@ -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", "[email protected]", "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", "[email protected]", "password",
System.currentTimeMillis()));
Expand Down Expand Up @@ -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", "[email protected]", "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", "[email protected]", "password",
System.currentTimeMillis()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -133,7 +134,7 @@ public void testGoodInput() throws Exception {
assertEquals(signUpUser.get("email").getAsString(), "[email protected]");
assertNotNull(signUpUser.get("id"));

UserInfo user = StorageLayer.getEmailPasswordStorage(process.getProcess())
UserInfo user = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
.getUserInfoUsingEmail(new TenantIdentifier(null, null, null), "[email protected]");
assertEquals(user.email, signUpUser.get("email").getAsString());
assertEquals(user.id, signUpUser.get("id").getAsString());
Expand Down Expand Up @@ -182,7 +183,7 @@ public void testTheNormaliseEmailFunction() throws Exception {
assertEquals(signUpUser.get("email").getAsString(), "[email protected]");
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, "[email protected]");
Expand Down