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 2 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 @@ -30,6 +30,7 @@
import org.w3c.dom.Element;
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.ClaimConfig;
Expand Down Expand Up @@ -291,33 +292,64 @@ private static List<String> getRequestedLocalClaims(ClaimMapping[] spClaimMappin
return requestedLocalClaims;
}

/**
* 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.
* @return Mapped local claims.
* @throws IdentityException
* @throws IdentityApplicationManagementException
*/
public static Map<String, String> handleClaimMapping(IdentityProvider identityProvider,
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.
* @return mapped local claims.
* @return Mapped local claims.
* @throws IdentityException
* @throws IdentityApplicationManagementException
*/
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 = null;
if (resolveIdPGroupAssignments) {
serviceProvider = getServiceProvider(tokenReqMsgCtx);
String applicationId = serviceProvider.getApplicationResourceId();
assignedRoles = getAssignedRolesFromIdPGroups(identityProvider, attributes, applicationId,
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 (serviceProvider == null) {
serviceProvider = getServiceProvider(tokenReqMsgCtx);
}

if (ArrayUtils.isNotEmpty(idPClaimMappings)) {
if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -378,7 +410,30 @@ 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, String applicationId,
String tenantDomain) {

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
Loading