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 @@ -137,6 +137,8 @@ on the registration screen. After registering their profile, the user is require
(or optionally other credentials like OTP or Passkey) as a subsequent step. It is possible to disable this and keep the old behavior by
enabling the switch *Always set password on register form* in the realm registration flow, on the *Password validation* authenticator. Note
that this option is deprecated and exists mostly for backward compatibility. It may be removed in the future.
Even with this switch enabled, when the user verifies the email in a different browser than the one used for registration, the registration does not continue in the original browser, and the user needs to log in.
To skip asking for the username again in that login, enable *Continue as registered user* in the configuration of the *Verify Email* required action.

For the details, see the link:{adminguide_link}[{adminguide_name}].

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.keycloak.authentication.actiontoken.verifyemail.VerifyEmailActionToken;
import org.keycloak.authentication.requiredactions.util.EmailCooldownManager;
import org.keycloak.common.util.Time;
import org.keycloak.cookie.CookieProvider;
import org.keycloak.cookie.CookieType;
import org.keycloak.email.EmailException;
import org.keycloak.email.EmailTemplateProvider;
import org.keycloak.events.Details;
Expand All @@ -47,9 +49,12 @@
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RequiredActionConfigModel;
import org.keycloak.models.UserModel;
import org.keycloak.policy.MaxAuthAgePasswordPolicyProviderFactory;
import org.keycloak.protocol.AuthorizationEndpointBase;
import org.keycloak.protocol.RestartLoginCookie;
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.provider.ProviderConfigProperty;
import org.keycloak.services.Urls;
import org.keycloak.services.messages.Messages;
Expand All @@ -71,6 +76,35 @@ public class VerifyEmail implements RequiredActionProvider, RequiredActionFactor

// Auth note to set that verifyEmail is triggered during registration
private static final String VERIFY_EMAIL_DURING_REGISTRATION = "VERIFY_EMAIL_DURING_REGISTRATION";
// Client note in the restart cookie set while a newly registered user waits for email verification
private static final String REGISTRATION_RESTART_NOTE = "verify_email_registration_restart";
public static final String CONTINUE_AS_REGISTERED_USER = "continue-as-registered-user";

/**
* For an authentication session just restarted from the restart cookie, returns the newly registered user when the
* restart is for a registration that was waiting for email verification, and the user has verified their email since
* (typically in another browser, which removes the authentication session). Otherwise returns null.
* Clears the marker from the authentication session, so it applies to this restart only.
*/
public static UserModel getUserVerifiedAfterRegistrationRestart(KeycloakSession session, RealmModel realm, AuthenticationSessionModel authSession) {
String restartNote = authSession.getClientNote(REGISTRATION_RESTART_NOTE);
authSession.removeClientNote(REGISTRATION_RESTART_NOTE);
String username = authSession.getClientNote(OIDCLoginProtocol.LOGIN_HINT_PARAM);
if (!"true".equals(restartNote) || username == null) {
return null;
}
UserModel user = session.users().getUserByUsername(realm, username);
return user != null && user.isEnabled() && user.isEmailVerified() ? user : null;
}

/**
* Whether the login restarted after the registered user verified their email in another browser should continue with
* that user already chosen, see {@link #CONTINUE_AS_REGISTERED_USER}.
*/
public static boolean isContinueAsRegisteredUser(RealmModel realm) {
RequiredActionConfigModel config = realm.getRequiredActionConfigByAlias(UserModel.RequiredAction.VERIFY_EMAIL.name());
return config != null && Boolean.parseBoolean(config.getConfigValue(CONTINUE_AS_REGISTERED_USER));
}

@Override
public void evaluateTriggers(RequiredActionContext context) {
Expand Down Expand Up @@ -133,6 +167,15 @@ private void process(RequiredActionContext context, boolean isChallenge) {
loginFormsProvider.setAuthenticationSession(context.getAuthenticationSession());
Response challenge;
authSession.setClientNote(AuthorizationEndpointBase.APP_INITIATED_FLOW, null);
if ("true".equals(authSession.getAuthNote(NEW_USER_REGISTERED))) {
// When the email is verified in another browser, this authentication session is removed (see VerifyEmailActionTokenHandler).
// Make the restart go to the login page, with the registered username as login_hint (client note set during registration).
RestartLoginCookie restart = new RestartLoginCookie(authSession);
restart.setAction(AuthenticationSessionModel.Action.AUTHENTICATE.name());
restart.getNotes().put(REGISTRATION_RESTART_NOTE, "true");
context.getSession().getProvider(CookieProvider.class)
.set(CookieType.AUTH_RESTART, RestartLoginCookie.encodeAndEncrypt(context.getSession(), restart));
}

// 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)) {
Expand Down Expand Up @@ -217,7 +260,16 @@ public List<ProviderConfigProperty> getConfigMetadata() {
maxAge.setType(ProviderConfigProperty.STRING_TYPE);
maxAge.setDefaultValue(MaxAuthAgePasswordPolicyProviderFactory.DEFAULT_MAX_AUTH_AGE);

return List.of(maxAge, EmailCooldownManager.createCooldownConfigProperty());
ProviderConfigProperty continueAsRegisteredUser = new ProviderConfigProperty();
continueAsRegisteredUser.setName(CONTINUE_AS_REGISTERED_USER);
continueAsRegisteredUser.setLabel("Continue as registered user");
continueAsRegisteredUser.setHelpText("When a user registers, then verifies their email in another browser, the login in the browser used " +
"for the registration restarts with the registered user already chosen, so the user isn't asked for their username again. " +
"The user still has to authenticate as the browser flow requires, for example with their password.");
continueAsRegisteredUser.setType(ProviderConfigProperty.BOOLEAN_TYPE);
continueAsRegisteredUser.setDefaultValue(false);

return List.of(maxAge, EmailCooldownManager.createCooldownConfigProperty(), continueAsRegisteredUser);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.keycloak.models.OrganizationModel;
import org.keycloak.organization.utils.Organizations;
import org.keycloak.protocol.AuthorizationEndpointBase;
import org.keycloak.protocol.RestartLoginCookie;
import org.keycloak.protocol.oidc.OIDCAdvancedConfigWrapper;
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.protocol.oidc.endpoints.request.AuthorizationEndpointRequest;
Expand Down Expand Up @@ -462,6 +463,8 @@ private Response buildRegister() {

AuthenticationProcessor processor = createProcessor(authenticationSession, flowId, LoginActionsService.REGISTRATION_PATH);
authenticationSession.setClientNote(APP_INITIATED_FLOW, LoginActionsService.REGISTRATION_PATH);
// Same as for /auth, so the registration can restart if the authentication session is removed
RestartLoginCookie.setRestartCookie(session, authenticationSession);
LocaleUtil.processLocaleParam(session, realm, authenticationSession);

return processor.authenticate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jakarta.ws.rs.core.UriInfo;

import org.keycloak.authentication.AuthenticationProcessor;
import org.keycloak.authentication.requiredactions.VerifyEmail;
import org.keycloak.common.ClientConnection;
import org.keycloak.common.util.ObjectUtil;
import org.keycloak.events.Details;
Expand All @@ -36,6 +37,8 @@
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.utils.FormMessage;
import org.keycloak.protocol.AuthorizationEndpointBase;
import org.keycloak.protocol.ClientData;
import org.keycloak.protocol.LoginProtocol;
Expand Down Expand Up @@ -438,12 +441,23 @@ protected Response restartAuthenticationSessionFromCookie(RootAuthenticationSess

if (authSession != null) {

event.clone();
event.detail(Details.RESTART_AFTER_TIMEOUT, "true");
event.error(Errors.EXPIRED_CODE);
UserModel verifiedUser = VerifyEmail.getUserVerifiedAfterRegistrationRestart(session, realm, authSession);
if (verifiedUser != null) {
// Email verified in another browser, which removed the authentication session of the registration. Not a timeout.
if (VerifyEmail.isContinueAsRegisteredUser(realm)) {
// Don't ask for the username again. The user still authenticates as the browser flow requires.
authSession.setAuthenticatedUser(verifiedUser);
}
new AuthenticationProcessor().setAuthenticationSession(authSession)
.setForwardedSuccessMessage(new FormMessage(null, Messages.EMAIL_VERIFIED));
} else {
event.clone();
event.detail(Details.RESTART_AFTER_TIMEOUT, "true");
event.error(Errors.EXPIRED_CODE);

String warningMessage = Messages.LOGIN_TIMEOUT;
authSession.setAuthNote(LoginActionsService.FORWARDED_ERROR_MESSAGE_NOTE, warningMessage);
String warningMessage = Messages.LOGIN_TIMEOUT;
authSession.setAuthNote(LoginActionsService.FORWARDED_ERROR_MESSAGE_NOTE, warningMessage);
}

String flowPath = authSession.getClientNote(AuthorizationEndpointBase.APP_INITIATED_FLOW);
if (flowPath == null) {
Expand Down
Loading