Skip to content

Commit

Permalink
fix: mark fake email as verified in emailpassword sign up
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Jan 23, 2024
1 parent 350c973 commit 7660739
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
40 changes: 38 additions & 2 deletions src/main/java/io/supertokens/emailpassword/EmailPassword.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,16 @@ public static AuthRecipeUserInfo signUp(Main main, @Nonnull String email, @Nonnu
}
}

@TestOnly
public static AuthRecipeUserInfo signUp(TenantIdentifierWithStorage tenantIdentifierWithStorage, Main main,
@Nonnull String email, @Nonnull String password)
throws DuplicateEmailException, StorageQueryException, TenantOrAppNotFoundException,
BadPermissionException {
return signUp(tenantIdentifierWithStorage, main, email, password, false);
}

public static AuthRecipeUserInfo signUp(TenantIdentifierWithStorage tenantIdentifierWithStorage, Main main,
@Nonnull String email, @Nonnull String password)
@Nonnull String email, @Nonnull String password, boolean setVerifiedForFakeEmails)
throws DuplicateEmailException, StorageQueryException, TenantOrAppNotFoundException,
BadPermissionException {

Expand All @@ -115,9 +123,37 @@ public static AuthRecipeUserInfo signUp(TenantIdentifierWithStorage tenantIdenti
long timeJoined = System.currentTimeMillis();

try {
return tenantIdentifierWithStorage.getEmailPasswordStorage()
AuthRecipeUserInfo newUser = tenantIdentifierWithStorage.getEmailPasswordStorage()
.signUp(tenantIdentifierWithStorage, userId, email, hashedPassword, timeJoined);

if (setVerifiedForFakeEmails && Utils.isFakeEmail(email)) {
try {
AuthRecipeUserInfo finalUser = newUser;
tenantIdentifierWithStorage.getEmailVerificationStorage().startTransaction(con -> {
try {

tenantIdentifierWithStorage.getEmailVerificationStorage()
.updateIsEmailVerified_Transaction(tenantIdentifierWithStorage.toAppIdentifier(), con,
finalUser.getSupertokensUserId(), email, true);
tenantIdentifierWithStorage.getEmailVerificationStorage()
.commitTransaction(con);

return null;
} catch (TenantOrAppNotFoundException e) {
throw new StorageTransactionLogicException(e);
}
});
newUser.loginMethods[0].setVerified(); // newly created user has only one loginMethod
} catch (StorageTransactionLogicException e) {
if (e.actualException instanceof TenantOrAppNotFoundException) {
throw (TenantOrAppNotFoundException) e.actualException;
}
throw new StorageQueryException(e);
}

}

return newUser;
} catch (DuplicateUserIdException ignored) {
// we retry with a new userId (while loop)
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/supertokens/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ public static boolean verifyWithPublicKey(String content, String signature, Stri
return sign.verify(decoder.decode(signature));
}

public static boolean isFakeEmail(String email) {
return email.endsWith("@stfakeemail.supertokens.com") || email.endsWith(".fakeemail.com");
}

public static class PubPriKey {
public String publicKey;
public String privateKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I

try {
TenantIdentifierWithStorage tenant = this.getTenantIdentifierWithStorageFromRequest(req);
AuthRecipeUserInfo user = EmailPassword.signUp(tenant, super.main, normalisedEmail, password);
AuthRecipeUserInfo user = EmailPassword.signUp(
tenant, super.main, normalisedEmail, password,
getVersionFromRequest(req).greaterThanOrEqualTo(SemVer.v5_0));

ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main, user.getSupertokensUserId());

Expand Down

0 comments on commit 7660739

Please sign in to comment.