Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate email domain format #265

Merged
merged 1 commit into from
Oct 22, 2023
Merged
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 @@ -60,4 +60,12 @@ boolean isDiscoveryConfigurationEnabled(String organizationId)
* @return the list of events.
*/
List<String> requiredEventValidations();

/**
* Check if the given discovery attribute values are in valid format.
*
* @param attributeValues The discovery attribute values.
* @return If the given discovery attribute values are in valid format.
*/
boolean areAttributeValuesInValidFormat(List<String> attributeValues);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

import static org.wso2.carbon.identity.organization.discovery.service.constant.DiscoveryConstants.PRE_ADD_USER_EMAIL_DOMAIN_VALIDATE;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DISCOVERY_CONFIG_DISABLED;
Expand All @@ -46,6 +47,7 @@ public class EmailDomainBasedDiscoveryHandler implements AttributeBasedOrganizat
private static final String EMAIL_DOMAIN_DISCOVERY_ENABLE_CONFIG = "emailDomain.enable";
private static final OrganizationConfigManager organizationConfigManager = OrganizationDiscoveryServiceHolder
.getInstance().getOrganizationConfigManager();
private static final Pattern EMAIL_DOMAIN_PATTERN = Pattern.compile("^[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");

@Override
public String getType() {
Expand Down Expand Up @@ -88,4 +90,10 @@ public List<String> requiredEventValidations() {

return Collections.singletonList(PRE_ADD_USER_EMAIL_DOMAIN_VALIDATE);
}

@Override
public boolean areAttributeValuesInValidFormat(List<String> attributeValues) {

return attributeValues.stream().allMatch(emailDomain -> EMAIL_DOMAIN_PATTERN.matcher(emailDomain).matches());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DISCOVERY_CONFIG_DISABLED;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DUPLICATE_DISCOVERY_ATTRIBUTE_TYPES;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_EMPTY_DISCOVERY_ATTRIBUTES;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_DISCOVERY_ATTRIBUTE_VALUE;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_ORGANIZATION;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNAUTHORIZED_ORG_FOR_DISCOVERY_ATTRIBUTE_MANAGEMENT;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNSUPPORTED_DISCOVERY_ATTRIBUTE;
Expand Down Expand Up @@ -159,14 +160,6 @@ public String getOrganizationIdByDiscoveryAttribute(String attributeType, String
return null;
}

private boolean isDiscoveryConfigurationEnabled(String rootOrganizationId, String type) throws
OrganizationManagementException {

AttributeBasedOrganizationDiscoveryHandler attributeBasedOrganizationDiscoveryHandler =
OrganizationDiscoveryServiceHolder.getInstance().getAttributeBasedOrganizationDiscoveryHandler(type);
return attributeBasedOrganizationDiscoveryHandler.isDiscoveryConfigurationEnabled(rootOrganizationId);
}

private void validateRootOrganization(String rootOrganizationId, String organizationId)
throws OrganizationManagementClientException {

Expand Down Expand Up @@ -199,11 +192,17 @@ private void validateOrganizationDiscoveryAttributes(boolean excludeCurrentOrgan
throw handleClientException(ERROR_CODE_UNSUPPORTED_DISCOVERY_ATTRIBUTE, attributeType);
}

if (!isDiscoveryConfigurationEnabled(rootOrganizationId, attributeType)) {
AttributeBasedOrganizationDiscoveryHandler discoveryHandler = OrganizationDiscoveryServiceHolder
.getInstance().getAttributeBasedOrganizationDiscoveryHandler(attributeType);

if (!discoveryHandler.isDiscoveryConfigurationEnabled(rootOrganizationId)) {
throw handleClientException(ERROR_CODE_DISCOVERY_CONFIG_DISABLED, getOrganizationId());
}

attribute.setValues(attribute.getValues().stream().distinct().collect(Collectors.toList()));
if (!discoveryHandler.areAttributeValuesInValidFormat(attribute.getValues())) {
throw handleClientException(ERROR_CODE_INVALID_DISCOVERY_ATTRIBUTE_VALUE, attributeType);
}
boolean discoveryAttributeTaken = organizationDiscoveryDAO.isDiscoveryAttributeExistInHierarchy
(excludeCurrentOrganization, rootOrganizationId, organizationId, attributeType,
attribute.getValues());
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@
<org.wso2.identity.organization.mgt.imp.pkg.version.range>[1.0.0,2.0.0)
</org.wso2.identity.organization.mgt.imp.pkg.version.range>

<identity.organization.management.core.version>1.0.72</identity.organization.management.core.version>
<identity.organization.management.core.version>1.0.77</identity.organization.management.core.version>
<org.wso2.identity.organization.mgt.core.imp.pkg.version.range>[1.0.0,2.0.0)
</org.wso2.identity.organization.mgt.core.imp.pkg.version.range>

Expand Down
Loading