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

Add locale attribute validation for SCIM Patch #415

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ public SCIMResponse updateWithPATCH(String existingId, String scimObjectString,
}
}


sadilchamishka marked this conversation as resolved.
Show resolved Hide resolved
public String getUserName(String scimObjectString) throws CharonException {

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.wso2.charon3.core.schema;

import org.apache.commons.lang.StringUtils;
import org.wso2.charon3.core.attributes.Attribute;
import org.wso2.charon3.core.attributes.SimpleAttribute;
import org.wso2.charon3.core.exceptions.BadRequestException;
import org.wso2.charon3.core.exceptions.CharonException;
Expand All @@ -30,6 +32,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

/**
Expand Down Expand Up @@ -248,11 +252,57 @@ public static AbstractSCIMObject validateUpdatedSCIMObject(AbstractSCIMObject ol
// Check for required attributes.
validateSCIMObjectForRequiredAttributes(newObject, resourceSchema);
}

Map<String, Attribute> attributes = validatedObject.getAttributeList();

for (Map.Entry<String, Attribute> entry : attributes.entrySet()) {
String key = entry.getKey();
Attribute value = entry.getValue();

if (value instanceof SimpleAttribute && StringUtils.equals(key, SCIMConstants.UserSchemaConstants.LOCALE)) {
String localeAttributeValue = ((SimpleAttribute) value).getValue().toString();
pavinduLakshan marked this conversation as resolved.
Show resolved Hide resolved

if (!isValidLocale(localeAttributeValue)) {
throw new BadRequestException
("Provided locale value " + localeAttributeValue + " is invalid");
}

break;
}
}

// Check for schema list.
validateSchemaList(validatedObject, resourceSchema);
return validatedObject;
}

private static boolean isValidLocale(String localeStr) {

if (localeStr == null || StringUtils.isEmpty(localeStr)) {
return false;
}

// Split the locale string into parts (language and country)
String[] parts = localeStr.split("-");

if (parts.length != 2) {
return false; // Must have exactly two parts: language and country
}

String language = parts[0];
String country = parts[1];

// Check if the locale is available in the system
for (Locale availableLocale : Locale.getAvailableLocales()) {
pavinduLakshan marked this conversation as resolved.
Show resolved Hide resolved
if (availableLocale.getLanguage().equals(language) &&
availableLocale.getCountry().equals(country)) {
return true;
}
}

return false; // If no matching locale is found
}

/*
* This method is to add meta data to the resource type resource
*
Expand Down
Loading