Skip to content
Draft
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 @@ -40,6 +40,7 @@

import static org.keycloak.services.validation.Validation.FIELD_PASSWORD;
import static org.keycloak.services.validation.Validation.FIELD_USERNAME;
import static org.keycloak.services.validation.Validation.MAX_USERNAME_LENGTH;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
Expand Down Expand Up @@ -173,6 +174,13 @@ private UserModel getUserFromForm(AuthenticationFlowContext context, Multivalued
// remove leading and trailing whitespace
username = username.trim();

if (username.length() > MAX_USERNAME_LENGTH) {
context.getEvent().error(Errors.USER_NOT_FOUND);
Response challengeResponse = challenge(context, getDefaultChallengeMessage(context), FIELD_USERNAME);
context.failureChallenge(AuthenticationFlowError.INVALID_USER, challengeResponse);
return null;
}

context.getEvent().detail(Details.USERNAME, username);
context.getAuthenticationSession().setAuthNote(AbstractUsernameFormAuthenticator.ATTEMPTED_USERNAME, username);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.keycloak.services.managers.AuthenticationManager;

import static org.keycloak.authentication.authenticators.util.AuthenticatorUtils.getDisabledByBruteForceEventError;
import static org.keycloak.services.validation.Validation.MAX_USERNAME_LENGTH;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
Expand All @@ -54,12 +55,23 @@ public class ValidateUsername extends AbstractDirectGrantAuthenticator {
@Override
public void authenticate(AuthenticationFlowContext context) {
String username = retrieveUsername(context);
if (username == null) {
if (username == null || username.isEmpty()) {
context.getEvent().error(Errors.USER_NOT_FOUND);
Response challengeResponse = errorResponse(Response.Status.UNAUTHORIZED.getStatusCode(), "invalid_request", "Missing parameter: username");
context.failure(AuthenticationFlowError.INVALID_USER, challengeResponse);
return;
}

// remove leading and trailing whitespace
username = username.trim();

if (username.length() > MAX_USERNAME_LENGTH) {
context.getEvent().error(Errors.INVALID_USER_CREDENTIALS);
Response challengeResponse = errorResponse(Response.Status.UNAUTHORIZED.getStatusCode(), "invalid_request", "Invalid user credentials");
context.failure(AuthenticationFlowError.INVALID_USER, challengeResponse);
return;
}

context.getEvent().detail(Details.USERNAME, username);
context.getAuthenticationSession().setAuthNote(AbstractUsernameFormAuthenticator.ATTEMPTED_USERNAME, username);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

import org.jboss.logging.Logger;

import static org.keycloak.services.validation.Validation.MAX_USERNAME_LENGTH;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
Expand Down Expand Up @@ -110,6 +112,14 @@ public void action(AuthenticationFlowContext context) {
}

username = username.trim();
if (username.length() > MAX_USERNAME_LENGTH) {
context.getEvent().error(Errors.USER_NOT_FOUND);
Response challengeResponse = context.form()
.addError(new FormMessage(Validation.FIELD_USERNAME, Messages.INVALID_USER))
.createPasswordReset();
context.failureChallenge(AuthenticationFlowError.INVALID_USER, challengeResponse);
return;
}

RealmModel realm = context.getRealm();
UserModel user = context.getSession().users().getUserByUsername(realm, username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import static org.keycloak.organization.utils.Organizations.getMatchingDomain;
import static org.keycloak.organization.utils.Organizations.isEnabledAndOrganizationsPresent;
import static org.keycloak.organization.utils.Organizations.resolveHomeBroker;
import static org.keycloak.services.validation.Validation.MAX_USERNAME_LENGTH;
import static org.keycloak.utils.StringUtil.isBlank;

public class OrganizationAuthenticator extends IdentityProviderAuthenticator {
Expand Down Expand Up @@ -139,7 +140,16 @@ public void action(AuthenticationFlowContext context) {
});
return;
}
// remove leading and trailing whitespace
username = username.trim();

if (username.length() > MAX_USERNAME_LENGTH) {
initialChallenge(context, form -> {
form.addError(new FormMessage(UserModel.USERNAME, Messages.INVALID_USERNAME));
return form.createLoginUsername();
});
return;
}
action(context, username);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Validation {
public static final String FIELD_USERNAME = "username";
public static final String FIELD_OTP_CODE = "totp";
public static final String FIELD_OTP_LABEL = "userLabel";
public static final int MAX_USERNAME_LENGTH = 255;

private static final Pattern USERNAME_PATTERN = Pattern.compile("^[\\p{IsLatin}|\\p{IsCommon}]+$");

Expand Down
Loading