Skip to content

Commit 28d139b

Browse files
authored
fix: emailpassword storage (#607)
1 parent 4f1018d commit 28d139b

File tree

6 files changed

+35
-51
lines changed

6 files changed

+35
-51
lines changed

src/main/java/io/supertokens/emailpassword/EmailPassword.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static UserInfo signUp(TenantIdentifierWithStorage tenantIdentifierWithSt
110110

111111
try {
112112
UserInfo user = new UserInfo(userId, email, hashedPassword, timeJoined);
113-
((EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage()).signUp(tenantIdentifierWithStorage, user);
113+
tenantIdentifierWithStorage.getEmailPasswordStorage().signUp(tenantIdentifierWithStorage, user);
114114

115115
return user;
116116

@@ -159,7 +159,7 @@ public static ImportUserResponse importUserWithPasswordHash(TenantIdentifierWith
159159
long timeJoined = System.currentTimeMillis();
160160

161161
UserInfo userInfo = new UserInfo(userId, email, passwordHash, timeJoined);
162-
EmailPasswordSQLStorage storage = (EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage();
162+
EmailPasswordSQLStorage storage = tenantIdentifierWithStorage.getEmailPasswordStorage();
163163

164164
try {
165165
storage.signUp(tenantIdentifierWithStorage, userInfo);
@@ -221,7 +221,7 @@ public static UserInfo signIn(TenantIdentifierWithStorage tenantIdentifierWithSt
221221
throw new BadPermissionException("Email password login not enabled for tenant");
222222
}
223223

224-
UserInfo user = ((EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage())
224+
UserInfo user = tenantIdentifierWithStorage.getEmailPasswordStorage()
225225
.getUserInfoUsingEmail(tenantIdentifierWithStorage, email);
226226

227227
if (user == null) {
@@ -296,7 +296,7 @@ public static String generatePasswordResetToken(TenantIdentifierWithStorage tena
296296
String hashedToken = Utils.hashSHA256(token);
297297

298298
try {
299-
((EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage()).addPasswordResetToken(
299+
tenantIdentifierWithStorage.getEmailPasswordStorage().addPasswordResetToken(
300300
tenantIdentifierWithStorage.toAppIdentifier(), new PasswordResetTokenInfo(userId,
301301
hashedToken, System.currentTimeMillis() +
302302
getPasswordResetTokenLifetime(tenantIdentifierWithStorage, main)));
@@ -327,7 +327,7 @@ public static String resetPassword(TenantIdentifierWithStorage tenantIdentifierW
327327
String hashedToken = Utils.hashSHA256(token);
328328
String hashedPassword = PasswordHashing.getInstance(main)
329329
.createHashWithSalt(tenantIdentifierWithStorage.toAppIdentifier(), password);
330-
EmailPasswordSQLStorage storage = (EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage();
330+
EmailPasswordSQLStorage storage = tenantIdentifierWithStorage.getEmailPasswordStorage();
331331

332332
PasswordResetTokenInfo resetInfo = storage.getPasswordResetTokenInfo(
333333
tenantIdentifierWithStorage.toAppIdentifier(), hashedToken);
@@ -399,7 +399,7 @@ public static void updateUsersEmailOrPassword(AppIdentifierWithStorage appIdenti
399399
@Nullable String password)
400400
throws StorageQueryException, StorageTransactionLogicException,
401401
UnknownUserIdException, DuplicateEmailException, TenantOrAppNotFoundException {
402-
EmailPasswordSQLStorage storage = (EmailPasswordSQLStorage) appIdentifierWithStorage.getStorage();
402+
EmailPasswordSQLStorage storage = appIdentifierWithStorage.getEmailPasswordStorage();
403403
try {
404404
storage.startTransaction(transaction -> {
405405
try {
@@ -455,12 +455,12 @@ public static UserInfo getUserUsingId(Main main, String userId)
455455

456456
public static UserInfo getUserUsingId(AppIdentifierWithStorage appIdentifierWithStorage, String userId)
457457
throws StorageQueryException, TenantOrAppNotFoundException {
458-
return ((EmailPasswordSQLStorage) appIdentifierWithStorage.getStorage()).getUserInfoUsingId(
459-
appIdentifierWithStorage, userId);
458+
return appIdentifierWithStorage.getEmailPasswordStorage().getUserInfoUsingId(appIdentifierWithStorage, userId);
460459
}
461460

462461
public static UserInfo getUserUsingEmail(TenantIdentifierWithStorage tenantIdentifierWithStorage, String email)
463462
throws StorageQueryException, TenantOrAppNotFoundException {
464-
return ((EmailPasswordSQLStorage) tenantIdentifierWithStorage.getStorage()).getUserInfoUsingEmail(tenantIdentifierWithStorage, email);
463+
return tenantIdentifierWithStorage.getEmailPasswordStorage().getUserInfoUsingEmail(
464+
tenantIdentifierWithStorage, email);
465465
}
466466
}

src/main/java/io/supertokens/storageLayer/StorageLayer.java

-20
Original file line numberDiff line numberDiff line change
@@ -349,26 +349,6 @@ public static SessionStorage getSessionStorage(Main main) {
349349
}
350350
}
351351

352-
public static EmailPasswordSQLStorage getEmailPasswordStorage(TenantIdentifier tenantIdentifier,
353-
Main main) throws TenantOrAppNotFoundException {
354-
// TODO remove this function
355-
if (getInstance(tenantIdentifier, main).storage.getType() != STORAGE_TYPE.SQL) {
356-
// we only support SQL for now
357-
throw new UnsupportedOperationException("");
358-
}
359-
return (EmailPasswordSQLStorage) getInstance(tenantIdentifier, main).storage;
360-
}
361-
362-
@TestOnly
363-
public static EmailPasswordSQLStorage getEmailPasswordStorage(Main main) {
364-
// TODO remove this function
365-
try {
366-
return getEmailPasswordStorage(new TenantIdentifier(null, null, null), main);
367-
} catch (TenantOrAppNotFoundException e) {
368-
throw new IllegalStateException(e);
369-
}
370-
}
371-
372352
public static EmailVerificationSQLStorage getEmailVerificationStorage(TenantIdentifier tenantIdentifier,
373353
Main main) throws
374354
TenantOrAppNotFoundException {

src/test/java/io/supertokens/test/emailpassword/DeleteExpiredPasswordResetTokensCronjobTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.supertokens.pluginInterface.STORAGE_TYPE;
2424
import io.supertokens.pluginInterface.emailpassword.PasswordResetTokenInfo;
2525
import io.supertokens.pluginInterface.emailpassword.UserInfo;
26+
import io.supertokens.pluginInterface.emailpassword.sqlStorage.EmailPasswordSQLStorage;
2627
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
2728
import io.supertokens.storageLayer.StorageLayer;
2829
import io.supertokens.test.TestingProcessManager;
@@ -73,12 +74,12 @@ public void checkingCronJob() throws Exception {
7374
EmailPassword.generatePasswordResetToken(process.getProcess(), user.id);
7475
EmailPassword.generatePasswordResetToken(process.getProcess(), user.id);
7576

76-
assert (StorageLayer.getEmailPasswordStorage(process.getProcess())
77+
assert (((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
7778
.getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id).length == 4);
7879

7980
Thread.sleep(3000);
8081

81-
PasswordResetTokenInfo[] tokens = StorageLayer.getEmailPasswordStorage(process.getProcess())
82+
PasswordResetTokenInfo[] tokens = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
8283
.getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id);
8384

8485
assert (tokens.length == 2);

src/test/java/io/supertokens/test/emailpassword/EmailPasswordTest.java

+18-16
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicatePasswordResetTokenException;
3030
import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateUserIdException;
3131
import io.supertokens.pluginInterface.emailpassword.exceptions.UnknownUserIdException;
32+
import io.supertokens.pluginInterface.emailpassword.sqlStorage.EmailPasswordSQLStorage;
3233
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
3334
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
35+
import io.supertokens.pluginInterface.multitenancy.TenantIdentifierWithStorage;
3436
import io.supertokens.storageLayer.StorageLayer;
3537
import io.supertokens.test.TestingProcessManager;
3638
import io.supertokens.test.Utils;
@@ -77,12 +79,12 @@ public void testStorageLayerGetMailPasswordStorageLayerThrowsExceptionIfTypeIsNo
7779

7880
if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
7981
try {
80-
StorageLayer.getEmailPasswordStorage(process.getProcess());
82+
new TenantIdentifierWithStorage(null, null, null, StorageLayer.getStorage(process.getProcess())).getEmailPasswordStorage();
8183
throw new Exception("Should not come here");
8284
} catch (UnsupportedOperationException e) {
8385
}
8486
} else {
85-
StorageLayer.getEmailPasswordStorage(process.getProcess());
87+
new TenantIdentifierWithStorage(null, null, null, StorageLayer.getStorage(process.getProcess())).getEmailPasswordStorage();
8688
}
8789

8890
process.kill();
@@ -169,7 +171,7 @@ public void testThatAfterSignUpThePasswordIsHashedAndStoredInTheDatabase() throw
169171

170172
UserInfo user = EmailPassword.signUp(process.getProcess(), "[email protected]", "validPass123");
171173

172-
UserInfo userInfo = StorageLayer.getEmailPasswordStorage(process.getProcess())
174+
UserInfo userInfo = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
173175
.getUserInfoUsingEmail(new TenantIdentifier(null, null, null), user.email);
174176
assertNotEquals(userInfo.passwordHash, "validPass123");
175177
assertTrue(PasswordHashing.getInstance(process.getProcess()).verifyPasswordWithHash("validPass123",
@@ -196,7 +198,7 @@ public void testThatAfterResetPasswordGenerateTokenTheTokenIsHashedInTheDatabase
196198
UserInfo user = EmailPassword.signUp(process.getProcess(), "[email protected]", "validPass123");
197199

198200
String resetToken = EmailPassword.generatePasswordResetToken(process.getProcess(), user.id);
199-
PasswordResetTokenInfo resetTokenInfo = StorageLayer.getEmailPasswordStorage(process.getProcess())
201+
PasswordResetTokenInfo resetTokenInfo = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
200202
.getPasswordResetTokenInfo(new AppIdentifier(null, null),
201203
io.supertokens.utils.Utils.hashSHA256(resetToken));
202204

@@ -227,7 +229,7 @@ public void testThatAfterResetPasswordIsCompletedThePasswordIsHashedInTheDatabas
227229

228230
EmailPassword.resetPassword(process.getProcess(), resetToken, "newValidPass123");
229231

230-
UserInfo userInfo = StorageLayer.getEmailPasswordStorage(process.getProcess())
232+
UserInfo userInfo = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
231233
.getUserInfoUsingEmail(new TenantIdentifier(null, null, null), user.email);
232234
assertNotEquals(userInfo.passwordHash, "newValidPass123");
233235

@@ -256,7 +258,7 @@ public void passwordResetTokenExpiredCheck() throws Exception {
256258

257259
String tok = EmailPassword.generatePasswordResetToken(process.getProcess(), user.id);
258260

259-
assert (StorageLayer.getEmailPasswordStorage(process.getProcess())
261+
assert (((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
260262
.getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id).length == 1);
261263

262264
Thread.sleep(20);
@@ -268,7 +270,7 @@ public void passwordResetTokenExpiredCheck() throws Exception {
268270

269271
}
270272

271-
assert (StorageLayer.getEmailPasswordStorage(process.getProcess())
273+
assert (((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
272274
.getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id).length == 0);
273275

274276
process.kill();
@@ -292,14 +294,14 @@ public void multiplePasswordResetTokensPerUserAndThenVerifyWithSignin() throws E
292294
String tok = EmailPassword.generatePasswordResetToken(process.getProcess(), user.id);
293295
EmailPassword.generatePasswordResetToken(process.getProcess(), user.id);
294296

295-
PasswordResetTokenInfo[] tokens = StorageLayer.getEmailPasswordStorage(process.getProcess())
297+
PasswordResetTokenInfo[] tokens = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
296298
.getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id);
297299

298300
assert (tokens.length == 3);
299301

300302
EmailPassword.resetPassword(process.getProcess(), tok, "newPassword");
301303

302-
tokens = StorageLayer.getEmailPasswordStorage(process.getProcess())
304+
tokens = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
303305
.getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null), user.id);
304306
assert (tokens.length == 0);
305307

@@ -328,7 +330,7 @@ public void zeroPasswordTokens() throws Exception {
328330
if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
329331
return;
330332
}
331-
PasswordResetTokenInfo[] tokens = StorageLayer.getEmailPasswordStorage(process.getProcess())
333+
PasswordResetTokenInfo[] tokens = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
332334
.getAllPasswordResetTokenInfoForUser(new AppIdentifier(null, null),
333335
"8ed86166-bfd8-4234-9dfe-abca9606dbd5");
334336

@@ -374,14 +376,14 @@ public void clashingPassowordResetToken() throws Exception {
374376
// we add a user first.
375377
UserInfo user = EmailPassword.signUp(process.getProcess(), "[email protected]", "password");
376378

377-
StorageLayer.getEmailPasswordStorage(process.getProcess())
379+
((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
378380
.addPasswordResetToken(new AppIdentifier(null, null), new PasswordResetTokenInfo(
379381
user.id, "token",
380382
System.currentTimeMillis() +
381383
Config.getConfig(process.getProcess()).getPasswordResetTokenLifetime()));
382384

383385
try {
384-
StorageLayer.getEmailPasswordStorage(process.getProcess())
386+
((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
385387
.addPasswordResetToken(new AppIdentifier(null, null),
386388
new PasswordResetTokenInfo(user.id, "token", System.currentTimeMillis()
387389
+ Config.getConfig(process.getProcess()).getPasswordResetTokenLifetime()));
@@ -427,13 +429,13 @@ public void clashingUserIdDuringSignUp() throws Exception {
427429
return;
428430
}
429431

430-
StorageLayer.getEmailPasswordStorage(process.getProcess())
432+
((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
431433
.signUp(new TenantIdentifier(null, null, null), new UserInfo(
432434
"8ed86166-bfd8-4234-9dfe-abca9606dbd5", "[email protected]", "password",
433435
System.currentTimeMillis()));
434436

435437
try {
436-
StorageLayer.getEmailPasswordStorage(process.getProcess())
438+
((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
437439
.signUp(new TenantIdentifier(null, null, null),
438440
new UserInfo("8ed86166-bfd8-4234-9dfe-abca9606dbd5", "[email protected]", "password",
439441
System.currentTimeMillis()));
@@ -480,13 +482,13 @@ public void clashingEmailAndUserIdDuringSignUp() throws Exception {
480482
return;
481483
}
482484

483-
StorageLayer.getEmailPasswordStorage(process.getProcess())
485+
((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
484486
.signUp(new TenantIdentifier(null, null, null), new UserInfo(
485487
"8ed86166-bfd8-4234-9dfe-abca9606dbd5", "[email protected]", "password",
486488
System.currentTimeMillis()));
487489

488490
try {
489-
StorageLayer.getEmailPasswordStorage(process.getProcess())
491+
((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
490492
.signUp(new TenantIdentifier(null, null, null),
491493
new UserInfo("8ed86166-bfd8-4234-9dfe-abca9606dbd5", "[email protected]", "password",
492494
System.currentTimeMillis()));

src/test/java/io/supertokens/test/emailpassword/api/ImportUserWithPasswordHashAPITest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public void testSigningInAUserWhenStoredPasswordHashIsIncorrect() throws Excepti
331331
long timeJoined = System.currentTimeMillis();
332332

333333
UserInfo userInfo = new UserInfo("userId", email, combinedPasswordHash, timeJoined);
334-
EmailPasswordSQLStorage storage = StorageLayer.getEmailPasswordStorage(process.getProcess());
334+
EmailPasswordSQLStorage storage = (EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess());
335335

336336
storage.signUp(new TenantIdentifier(null, null, null), userInfo);
337337

@@ -374,7 +374,7 @@ public void testSigningInAUserWithFirebasePasswordHashWithoutSettingTheSignerKey
374374
long timeJoined = System.currentTimeMillis();
375375

376376
UserInfo userInfo = new UserInfo("userId", email, combinedPasswordHash, timeJoined);
377-
EmailPasswordSQLStorage storage = StorageLayer.getEmailPasswordStorage(process.getProcess());
377+
EmailPasswordSQLStorage storage = (EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess());
378378

379379
storage.signUp(new TenantIdentifier(null, null, null), userInfo);
380380

src/test/java/io/supertokens/test/emailpassword/api/SignUpAPITest2_7.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.supertokens.ProcessState;
2121
import io.supertokens.pluginInterface.STORAGE_TYPE;
2222
import io.supertokens.pluginInterface.emailpassword.UserInfo;
23+
import io.supertokens.pluginInterface.emailpassword.sqlStorage.EmailPasswordSQLStorage;
2324
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
2425
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
2526
import io.supertokens.storageLayer.StorageLayer;
@@ -133,7 +134,7 @@ public void testGoodInput() throws Exception {
133134
assertEquals(signUpUser.get("email").getAsString(), "[email protected]");
134135
assertNotNull(signUpUser.get("id"));
135136

136-
UserInfo user = StorageLayer.getEmailPasswordStorage(process.getProcess())
137+
UserInfo user = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
137138
.getUserInfoUsingEmail(new TenantIdentifier(null, null, null), "[email protected]");
138139
assertEquals(user.email, signUpUser.get("email").getAsString());
139140
assertEquals(user.id, signUpUser.get("id").getAsString());
@@ -182,7 +183,7 @@ public void testTheNormaliseEmailFunction() throws Exception {
182183
assertEquals(signUpUser.get("email").getAsString(), "[email protected]");
183184
assertNotNull(signUpUser.get("id"));
184185

185-
UserInfo userInfo = StorageLayer.getEmailPasswordStorage(process.getProcess())
186+
UserInfo userInfo = ((EmailPasswordSQLStorage) StorageLayer.getStorage(process.getProcess()))
186187
.getUserInfoUsingId(new AppIdentifier(null, null), signUpUser.get("id").getAsString());
187188

188189
assertEquals(userInfo.email, "[email protected]");

0 commit comments

Comments
 (0)