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

Introduce method to handle claim mappings with IdP group to role assignment resolving #2682

Merged
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 @@ -19,6 +19,7 @@
package org.wso2.carbon.identity.oauth2.util;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
Expand All @@ -29,7 +30,9 @@
import org.opensaml.saml.saml2.core.AttributeStatement;
import org.w3c.dom.Element;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.ClaimConfig;
Expand Down Expand Up @@ -294,30 +297,59 @@ private static List<String> getRequestedLocalClaims(ClaimMapping[] spClaimMappin
/**
* Handle claims from identity provider based on claim configurations.
*
* @param identityProvider Identity Provider
* @param identityProvider Identity Provider.
* @param attributes Relevant Claims coming from IDP
* @param tenantDomain Tenant Domain.
* @param tokenReqMsgCtx Token request message context.
* @return mapped local claims.
* @throws IdentityException
* @throws IdentityApplicationManagementException
* @return Mapped local claims.
* @throws IdentityException If an error occurred while handling claim mappings.
* @throws IdentityApplicationManagementException If an error occurred while getting service provider.
* @deprecated Use {@link #handleClaimMapping(IdentityProvider, Map, String, OAuthTokenReqMessageContext, boolean)}.
*/
@Deprecated
public static Map<String, String> handleClaimMapping(IdentityProvider identityProvider,
SujanSanjula96 marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> attributes, String tenantDomain,
OAuthTokenReqMessageContext tokenReqMsgCtx)
throws IdentityException, IdentityApplicationManagementException {

return handleClaimMapping(identityProvider, attributes, tenantDomain, tokenReqMsgCtx, false);
}

/**
* Handle claims from identity provider based on claim configurations.
*
* @param identityProvider Identity Provider
* @param attributes Relevant Claims coming from IDP
* @param tenantDomain Tenant Domain.
* @param tokenReqMsgCtx Token request message context.
* @param resolveIdPGroupAssignments Whether resolving IdP Group assignments needed.
* @return Mapped local claims.
* @throws IdentityException If an error occurred while handling claim mappings.
* @throws IdentityApplicationManagementException If an error occurred while getting service provider.
*/
public static Map<String, String> handleClaimMapping(IdentityProvider identityProvider,
Map<String, String> attributes, String tenantDomain, OAuthTokenReqMessageContext tokenReqMsgCtx)
Map<String, String> attributes, String tenantDomain,
OAuthTokenReqMessageContext tokenReqMsgCtx,
boolean resolveIdPGroupAssignments)
throws IdentityException, IdentityApplicationManagementException {

List<String> assignedRoles = null;
ServiceProvider serviceProvider = getServiceProvider(tokenReqMsgCtx);
if (resolveIdPGroupAssignments) {
assignedRoles = getAssignedRolesFromIdPGroups(identityProvider, attributes, serviceProvider,
tenantDomain);
}
boolean proxyUserAttributes = !OAuthServerConfiguration.getInstance()
.isConvertOriginalClaimsFromAssertionsToOIDCDialect();

if (proxyUserAttributes) {
setHasNonOIDCClaimsProperty(tokenReqMsgCtx);
return attributes;
return appendIdPMappedUserRolesAttributes(attributes, assignedRoles);
}

ClaimMapping[] idPClaimMappings = identityProvider.getClaimConfig().getClaimMappings();
Map<String, String> claimsAfterIdpMapping;
Map<String, String> claimsAfterSPMapping = new HashMap<>();
ServiceProvider serviceProvider = getServiceProvider(tokenReqMsgCtx);

if (ArrayUtils.isNotEmpty(idPClaimMappings)) {
if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -378,7 +410,36 @@ public static Map<String, String> handleClaimMapping(IdentityProvider identityPr
}
}
}
return claimsAfterSPMapping;
return appendIdPMappedUserRolesAttributes(claimsAfterSPMapping, assignedRoles);
}

private static List<String> getAssignedRolesFromIdPGroups(IdentityProvider identityProvider,
Map<String, String> attributes,
ServiceProvider serviceProvider,
String tenantDomain) throws FrameworkException {

if (serviceProvider == null || MapUtils.isEmpty(attributes)) {
return new ArrayList<>();
}
String applicationId = serviceProvider.getApplicationResourceId();

String idpGroupClaimURI = FrameworkUtils.getEffectiveIdpGroupClaimUri(identityProvider, tenantDomain);
if (StringUtils.isBlank(idpGroupClaimURI)) {
return new ArrayList<>();
}

return FrameworkUtils.getAppAssociatedRolesFromFederatedUserAttributes(attributes, identityProvider,
applicationId, idpGroupClaimURI, tenantDomain);
}

private static Map<String, String> appendIdPMappedUserRolesAttributes(Map<String, String> attributes,
List<String> assignedRoles) {

if (CollectionUtils.isNotEmpty(assignedRoles)) {
attributes.put(FrameworkConstants.IDP_MAPPED_USER_ROLES,
String.join(FrameworkUtils.getMultiAttributeSeparator(), assignedRoles));
}
return attributes;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.ClaimMapping;
Expand Down Expand Up @@ -172,6 +173,8 @@ private Map<String, Object> getUserClaimsInOIDCDialect(OAuthTokenReqMessageConte
// Get claim map from the cached attributes
userClaimsInOIDCDialect = getOIDCClaimsFromUserAttributes(userAttributes, requestMsgCtx);
}
// Remove the identityProviderMappedUserRoles claim since it is not an OIDC claim.
userClaimsInOIDCDialect.remove(FrameworkConstants.IDP_MAPPED_USER_ROLES);

Object hasNonOIDCClaimsProperty = requestMsgCtx.getProperty(OIDCConstants.HAS_NON_OIDC_CLAIMS);
if (isPreserverClaimUrisInAssertion(requestMsgCtx) || (hasNonOIDCClaimsProperty != null
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@
<carbon.kernel.registry.imp.pkg.version.range>[1.0.1, 2.0.0)</carbon.kernel.registry.imp.pkg.version.range>

<!-- Carbon Identity Framework version -->
<carbon.identity.framework.version>7.7.114</carbon.identity.framework.version>
<carbon.identity.framework.version>7.7.140</carbon.identity.framework.version>
<carbon.identity.framework.imp.pkg.version.range>[5.25.234, 8.0.0)
</carbon.identity.framework.imp.pkg.version.range>
<identity.oauth.xacml.version.range>[2.0.0, 3.0.0)</identity.oauth.xacml.version.range>
Expand Down
Loading