Skip to content

Commit

Permalink
Merge pull request #265 from dewniMW/emaildomainregex
Browse files Browse the repository at this point in the history
Validate email domain format
  • Loading branch information
dewniMW authored Oct 22, 2023
2 parents b8ffd66 + 9f3cfc0 commit 7e1575b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
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

0 comments on commit 7e1575b

Please sign in to comment.