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

Improve DialectConfigParser to build the final server supported claims set #6535

Merged
merged 4 commits into from
Feb 19, 2025
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 @@ -52,6 +52,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants.ErrorMessage.ERROR_CODE_CANNOT_ADD_TO_EXTERNAL_DIALECT;
import static org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants.ErrorMessage.ERROR_CODE_CLAIM_LENGTH_LIMIT;
import static org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants.ErrorMessage.ERROR_CODE_CLAIM_PROPERTY_CHAR_LIMIT_EXCEED;
import static org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants.ErrorMessage.ERROR_CODE_EMPTY_CLAIM_DIALECT;
Expand Down Expand Up @@ -522,7 +523,16 @@ public void addExternalClaim(ExternalClaim externalClaim, String tenantDomain) t
externalClaim.getMappedLocalClaim(), externalClaim.getClaimDialectURI()));
}

// Add listener
ClaimMetadataEventPublisherProxy.getInstance().publishPreAddExternalClaim(tenantId, externalClaim);
if (MapUtils.isNotEmpty(IdentityUtil.threadLocalProperties.get()) &&
Boolean.TRUE.equals(IdentityUtil.threadLocalProperties.get()
.get(ClaimConstants.EXTERNAL_CLAIM_ADDITION_NOT_ALLOWED_FOR_DIALECT))) {
IdentityUtil.threadLocalProperties.get()
.remove(ClaimConstants.EXTERNAL_CLAIM_ADDITION_NOT_ALLOWED_FOR_DIALECT);
throw new ClaimMetadataClientException(ERROR_CODE_CANNOT_ADD_TO_EXTERNAL_DIALECT.getCode(),
String.format(ERROR_CODE_CANNOT_ADD_TO_EXTERNAL_DIALECT.getMessage(),
externalClaim.getClaimDialectURI()));
}

this.unifiedClaimMetadataManager.addExternalClaim(externalClaim, tenantId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class ClaimConstants {
public static final String MAX_LENGTH = "maxLength";
public static final String IS_SYSTEM_CLAIM = "isSystemClaim";
public static final String SHARED_PROFILE_VALUE_RESOLVING_METHOD = "SharedProfileValueResolvingMethod";
public static final String EXTERNAL_CLAIM_ADDITION_NOT_ALLOWED_FOR_DIALECT =
"ExternalClaimAdditionNotAllowedForDialect";

/**
* Enum for error messages.
Expand Down Expand Up @@ -112,6 +114,8 @@ public enum ErrorMessage {
ERROR_CODE_INVALID_SHARED_PROFILE_VALUE_RESOLVING_METHOD("CMT-60014",
"Invalid shared profile value resolving method: %s"),
ERROR_CODE_INVALID_ATTRIBUTE_PROFILE("CMT-600015", "Invalid attribute profile name."),
ERROR_CODE_CANNOT_ADD_TO_EXTERNAL_DIALECT("CMT-60016",
"Adding claims to dialect %s is not allowed"),

// Server Errors
ERROR_CODE_DELETE_IDN_CLAIM_MAPPED_ATTRIBUTE("65001", "Error occurred while deleting claim " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class DialectConfigParser {

private static final String SCHEMA_FILE_NAME = "schemas.xml";
private static final String SCHEMAS_NAMESPACE = "http://wso2.org/projects/carbon/carbon.xml";
private static final String DEFAULT_SCHEMA_CONFIG = "DefaultSchema";
private static final String ADD_SCHEMA_CONFIG = "AddSchema";
private static final String REMOVE_SCHEMA_CONFIG = "RemoveSchema";
private static final String SCHEMAS_CONFIG = "Schemas";
Expand All @@ -54,6 +55,7 @@ public class DialectConfigParser {
private static final Log log = LogFactory.getLog(DialectConfigParser.class);

private final String schemasFilePath;
private Map<String, String> claimsMap = Collections.emptyMap();
private Map<String, String> additionsToDefaultDialects = Collections.emptyMap();
private Map<String, String> removalsFromDefaultDialects = Collections.emptyMap();

Expand Down Expand Up @@ -86,9 +88,20 @@ private void buildConfiguration() {
try (InputStream inputStream = Files.newInputStream(schemaPath)) {
StAXOMBuilder builder = new StAXOMBuilder(inputStream);
OMElement rootElement = builder.getDocumentElement();
claimsMap = buildSchemasConfiguration(rootElement, DEFAULT_SCHEMA_CONFIG);
additionsToDefaultDialects = buildSchemasConfiguration(rootElement, ADD_SCHEMA_CONFIG);
removalsFromDefaultDialects = buildSchemasConfiguration(rootElement, REMOVE_SCHEMA_CONFIG);

if (additionsToDefaultDialects != null) {
additionsToDefaultDialects.forEach((key, value) -> {
if (!claimsMap.containsKey(key)) {
claimsMap.put(key, value);
}
});
}
if (removalsFromDefaultDialects != null) {
removalsFromDefaultDialects.forEach((key, value) -> claimsMap.remove(key));
}
} catch (IOException | XMLStreamException e) {
throw IdentityRuntimeException.error("Error occurred while reading schema configuration in path: " +
schemasFilePath, e);
Expand Down Expand Up @@ -129,6 +142,16 @@ private Map<String, String> buildSchemasConfiguration(OMElement rootElement, Str
return dataMap;
}

/**
* Return claims supported by the server.
*
* @return Claim Map.
*/
public Map<String, String> getClaimsMap() {

return claimsMap;
}

/**
* Get the additions to the default schema.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<SchemaManager xmlns="http://wso2.org/projects/carbon/carbon.xml">
<DefaultSchema>
<Schemas>
<Schema id="urn:ietf:params:scim:schemas:core:2.0">
<Attribute>urn:ietf:params:scim:schemas:core:2.0:User:id</Attribute>
<Attribute>urn:ietf:params:scim:schemas:core:2.0:User:meta.created</Attribute>
</Schema>
</Schemas>
</DefaultSchema>
<AddSchema>
<Schemas>
<Schema id="urn:ietf:params:scim:schemas:core:2.0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
"POST_DELETE_LOCAL_CLAIM",
"POST_UPDATE_EXTERNAL_CLAIM",
"POST_DELETE_EXTERNAL_CLAIM",
"PRE_ADD_EXTERNAL_CLAIM",
"POST_ADD_EXTERNAL_CLAIM",
"POST_UPDATE_CLAIM_DIALECT",
"POST_DELETE_CLAIM_DIALECT"
Expand Down