8000 Apply VERIFY_EMAIL resend cooldown on new authentication sessions by julioo19 · Pull Request #54947 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,18 @@ private void process(RequiredActionContext context, boolean isChallenge) {

// Do not allow resending e-mail by simple page refresh, i.e. when e-mail sent, it should be resent properly via email-verification endpoint
if (!Objects.equals(authSession.getAuthNote(Constants.VERIFY_EMAIL_KEY), email) && !(isCurrentActionTriggeredFromAIA(context) && isChallenge)) {
// Adding the cooldown entry first to prevent concurrent operations
EmailCooldownManager.addCooldownEntry(context, EMAIL_RESEND_COOLDOWN_KEY_PREFIX);
authSession.setAuthNote(Constants.VERIFY_EMAIL_KEY, email);
EventBuilder event = context.getEvent().clone().event(EventType.SEND_VERIFY_EMAIL).detail(Details.EMAIL, email);
challenge = sendVerifyEmail(context, event);
Long remaining = EmailCooldownManager.retrieveCooldownEntry(context, EMAIL_RESEND_COOLDOWN_KEY_PREFIX);
if (remaining != null) {
// A verification e-mail was sent recently for this user (possibly from another auth session):
// render the page without sending and without setting the note
challenge = loginFormsProvider.createResponse(UserModel.RequiredAction.VERIFY_EMAIL);
} else {
// Adding the cooldown entry first to prevent concurrent operations
EmailCooldownManager.addCooldownEntry(context, EMAIL_RESEND_COOLDOWN_KEY_PREFIX);
authSession.setAuthNote(Constants.VERIFY_EMAIL_KEY, email);
EventBuilder event = context.getEvent().clone().event(EventType.SEND_VERIFY_EMAIL).detail(Details.EMAIL, email);
challenge = sendVerifyEmail(context, event);
}
} else {
challenge = loginFormsProvider.createResponse(UserModel.RequiredAction.VERIFY_EMAIL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,41 @@ public void verifyEmailResendTooFast() {

}

@Test
public void verifyEmailNewSessionRespectsResendCooldown() {
oauth.openLoginForm();
loginPage.login("test-user@localhost", "password");

verifyEmailPage.assertCurrent();
Assertions.assertEquals(1, mail.getReceivedMessages().length);

// Clearing the cookies makes the next login start a new authentication session
driver.manage().deleteAllCookies();

oauth.openLoginForm();
loginPage.login("test-user@localhost", "password");

verifyEmailPage.assertCurrent();

// The resend cooldown is per user, so the new session must not send another e-mail
Assertions.assertEquals(1, mail.getReceivedMessages().length);

verifyEmailPage.clickResendEmail();
assertThat(verifyEmailPage.getFeedbackText(), Matchers.containsString("You must wait"));

try {
// Move the clock past the cooldown, a new session sends the e-mail again
timeOffSet.set(40);
driver.manage().deleteAllCookies();
oauth.openLoginForm();
loginPage.login("test-user@localhost", "password");
verifyEmailPage.assertCurrent();
Assertions.assertEquals(2, mail.getReceivedMessages().length);
} finally {
timeOffSet.set(0);
}
}

@Test
public void verifyEmailResendWithRefreshes() throws IOException {
oauth.openLoginForm();
Expand Down
0