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,55 @@
// 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();

Check warning on line 263 in modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java

View check run for this annotation

Codecov / codecov/patch

modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java#L263

Added line #L263 was not covered by tests
pavinduLakshan marked this conversation as resolved.
Show resolved Hide resolved

if (!isValidLocale(localeAttributeValue)) {
throw new BadRequestException

Check warning on line 266 in modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java

View check run for this annotation

Codecov / codecov/patch

modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java#L266

Added line #L266 was not covered by tests
("Provided locale value " + localeAttributeValue + " is invalid");
}
}
}

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

private static boolean isValidLocale(String localeStr) {

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

Check warning on line 280 in modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java

View check run for this annotation

Codecov / codecov/patch

modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java#L280

Added line #L280 was not covered by tests
}

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

Check warning on line 284 in modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java

View check run for this annotation

Codecov / codecov/patch

modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java#L284

Added line #L284 was not covered by tests

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

Check warning on line 287 in modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java

View check run for this annotation

Codecov / codecov/patch

modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java#L287

Added line #L287 was not covered by tests
}

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

Check warning on line 291 in modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java

View check run for this annotation

Codecov / codecov/patch

modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java#L290-L291

Added lines #L290 - L291 were not covered by tests

// 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;

Check warning on line 297 in modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java

View check run for this annotation

Codecov / codecov/patch

modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java#L297

Added line #L297 was not covered by tests
}
}

return false; // If no matching locale is found

Check warning on line 301 in modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java

View check run for this annotation

Codecov / codecov/patch

modules/charon-core/src/main/java/org/wso2/charon3/core/schema/ServerSideValidator.java#L301

Added line #L301 was not covered by tests
}

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