Skip to content

Commit

Permalink
Add maxPasswordAllowedLength validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Malith-19 committed Dec 6, 2024
1 parent 3779031 commit e21fd5f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -22,6 +22,7 @@
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.input.validation.mgt.exceptions.InputValidationMgtClientException;
import org.wso2.carbon.identity.input.validation.mgt.model.Property;
import org.wso2.carbon.identity.input.validation.mgt.model.ValidationContext;
Expand All @@ -33,7 +34,9 @@
import java.util.stream.Collectors;

import static org.wso2.carbon.identity.input.validation.mgt.utils.Constants.Configs.MAX_LENGTH;
import static org.wso2.carbon.identity.input.validation.mgt.utils.Constants.Configs.MAX_PASSWORD_ALLOWED_LENGTH;
import static org.wso2.carbon.identity.input.validation.mgt.utils.Constants.Configs.MIN_LENGTH;
import static org.wso2.carbon.identity.input.validation.mgt.utils.Constants.Configs.PASSWORD;
import static org.wso2.carbon.identity.input.validation.mgt.utils.Constants.ErrorMessages.ERROR_DEFAULT_MIN_MAX_MISMATCH;
import static org.wso2.carbon.identity.input.validation.mgt.utils.Constants.ErrorMessages.ERROR_INVALID_VALIDATOR_PROPERTY_VALUE;
import static org.wso2.carbon.identity.input.validation.mgt.utils.Constants.ErrorMessages.ERROR_PROPERTY_NOT_SUPPORTED;
Expand Down Expand Up @@ -93,6 +96,21 @@ public boolean validateProps(ValidationContext context) throws InputValidationMg
String.format(ERROR_DEFAULT_MIN_MAX_MISMATCH.getDescription(), this.getClass().getSimpleName(),
properties.get(MIN_LENGTH), properties.get(MAX_LENGTH)));
}

// Validate the max length for the password field.
if (PASSWORD.equals(context.getField())) {
int maxPasswordValue = Integer.parseInt(IdentityUtil.getProperty(MAX_PASSWORD_ALLOWED_LENGTH));
if (properties.get(MAX_LENGTH) != null &&
Integer.parseInt(properties.get(MAX_LENGTH)) > maxPasswordValue) {
if (log.isDebugEnabled()) {
log.error(String.format("The property %s should be less than or equal to %s for the tenant %s.",
MAX_LENGTH, maxPasswordValue, context.getTenantDomain()));
}
throw new InputValidationMgtClientException(ERROR_PROPERTY_TYPE_MISMATCH.getCode(),
String.format(ERROR_PROPERTY_TYPE_MISMATCH.getDescription(), MAX_LENGTH, maxPasswordValue,
context.getTenantDomain()));
}
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -69,6 +69,7 @@ public static class Configs {
public static final String MAX_CONSECUTIVE_CHR = "max.consecutive.character";
public static final String ENABLE_VALIDATOR = "enable.validator";
public static final String ENABLE_SPECIAL_CHARACTERS = "enable.special.characters";
public static final String MAX_PASSWORD_ALLOWED_LENGTH = "PasswordPolicy.MaxPasswordAllowedLength";

// Keys for password regEx validation.
public static final String JS_REGEX = "regex";
Expand Down

0 comments on commit e21fd5f

Please sign in to comment.