package org.keycloak.auth;
import org.jboss.logging.Logger;
import org.keycloak.authentication.AuthenticationFlowContext;
import org.keycloak.authentication.AuthenticationFlowError;
import org.keycloak.authentication.Authenticator;
import org.keycloak.authentication.authenticators.browser.UsernamePasswordForm;
import jakarta.ws.rs.core.MultivaluedMap;
public class UsernameAutoCompleteAuthenticator extends UsernamePasswordForm implements Authenticator {
private static final Logger logger = Logger.getLogger(UsernameAutoCompleteAuthenticator.class);
private static final String EMAIL_DOMAIN = "@iftm.edu.br";
@Override
public void authenticate(AuthenticationFlowContext context) {
super.authenticate(context);
}
@Override
public void action(AuthenticationFlowContext context) {
logger.info("*** AUTOCOMPLETE *** itworks");
// Retrieve the username from the form data
MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters();
String username = formData.getFirst("username");
if (username != null) {
if (username.matches("\\d{11}")) {
// Username is a sequence of 11 numbers, no modification needed
logger.warnf(" *** AUTOCOMPLETE *** Username is a CPF, no modification needed: %s", username);
} else if (!username.contains("@")) {
// Append the email domain if not already present
username += EMAIL_DOMAIN;
logger.infof(" *** AUTOCOMPLETE *** Updated username with domain: %s", username);
// None of this seems to work
formData.putSingle("username", username);
context.getAuthenticationSession().setAuthNote("username", username);
context.getSession().setAttribute("username", username);
} else {
logger.warnf(" *** AUTOCOMPLETE *** Username already contains '@', no modification needed: %s", username);
}
}
// Call the default UsernamePasswordForm behavior
super.action(context);
// Ensure the flow status is set
if (context.getStatus() == null) {
logger.warn("Flow status is null. Setting failure status.");
context.failure(AuthenticationFlowError.INTERNAL_ERROR);
}
logger.debug("*** AUTOCOMPLETE *** Exiting action method.");
}
@Override public boolean requiresUser() { return false; }
@Override public boolean configuredFor(org.keycloak.models.KeycloakSession session, org.keycloak.models.RealmModel realm, org.keycloak.models.UserModel user) { return true; }
@Override public void setRequiredActions(org.keycloak.models.KeycloakSession session, org.keycloak.models.RealmModel realm, org.keycloak.models.UserModel user) {}
@Override public void close() {}
}