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

Resolve associated roles of shared app #258

Merged
merged 6 commits into from
Oct 23, 2023
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
@@ -1,18 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com).
~ Copyright (c) 2022-2023, WSO2 LLC. (http://www.wso2.com).
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ WSO2 LLC. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Expand Down Expand Up @@ -86,7 +88,10 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.claim.metadata.mgt</artifactId>
</dependency>

<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.role.v2.mgt.core</artifactId>
</dependency>
<!--Test Dependencies-->
<dependency>
<groupId>org.testng</groupId>
Expand Down Expand Up @@ -181,6 +186,8 @@
org.wso2.carbon.identity.event.event; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.event; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.event.services; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.role.v2.mgt.core.*;
version="${carbon.identity.package.import.version.range}",
</Import-Package>
</instructions>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.com).
* Copyright (c) 2022-2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -26,6 +26,7 @@
import org.wso2.carbon.identity.organization.management.application.listener.ApplicationSharingManagerListener;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationUserResidentResolverService;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
import org.wso2.carbon.idp.mgt.IdpManager;
import org.wso2.carbon.user.core.service.RealmService;

Expand All @@ -46,8 +47,9 @@ public class OrgApplicationMgtDataHolder {
private IdpManager idpManager;
private ApplicationSharingManagerListener applicationSharingManagerListener;
private IdentityEventService identityEventService;

private ClaimMetadataManagementService claimMetadataManagementService;
private RoleManagementService roleManagementServiceV2;

private OrgApplicationMgtDataHolder() {

}
Expand Down Expand Up @@ -248,4 +250,24 @@ public void setIdentityEventService(IdentityEventService identityEventService) {

this.identityEventService = identityEventService;
}

/**
* Get {@link RoleManagementService}.
*
* @return RoleManagementService.
*/
public RoleManagementService getRoleManagementServiceV2() {

return roleManagementServiceV2;
}

/**
* Set {@link RoleManagementService}.
*
* @param roleManagementServiceV2 Instance of {@link RoleManagementService}.
*/
public void setRoleManagementServiceV2(RoleManagementService roleManagementServiceV2) {

this.roleManagementServiceV2 = roleManagementServiceV2;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2022-2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -42,6 +42,7 @@
import org.wso2.carbon.identity.organization.management.application.listener.OrganizationCreationHandler;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationUserResidentResolverService;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
import org.wso2.carbon.idp.mgt.IdpManager;
import org.wso2.carbon.user.core.service.RealmService;

Expand Down Expand Up @@ -238,4 +239,22 @@ protected void unsetClaimMetaDataManagementService(ClaimMetadataManagementServic
log.debug("Unset the claim metadata management service.");
OrgApplicationMgtDataHolder.getInstance().setClaimMetadataManagementService(null);
}

@Reference(
name = "org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService",
service = org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetRoleManagementServiceV2")
protected void setRoleManagementServiceV2(RoleManagementService roleManagementService) {

OrgApplicationMgtDataHolder.getInstance().setRoleManagementServiceV2(roleManagementService);
log.debug("RoleManagementServiceV2 set in OrgApplicationMgtServiceComponent bundle.");
}

protected void unsetRoleManagementServiceV2(RoleManagementService roleManagementService) {

OrgApplicationMgtDataHolder.getInstance().setRoleManagementServiceV2(null);
log.debug("RoleManagementServiceV2 unset in OrgApplicationMgtServiceComponent bundle.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.AssociatedRolesConfig;
import org.wso2.carbon.identity.application.common.model.Claim;
import org.wso2.carbon.identity.application.common.model.ClaimConfig;
import org.wso2.carbon.identity.application.common.model.ClaimMapping;
import org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.RoleV2;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.ServiceProviderProperty;
import org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig;
Expand All @@ -45,10 +47,14 @@
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementClientException;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
import org.wso2.carbon.identity.role.v2.mgt.core.exception.IdentityRoleManagementException;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.lang.String.format;
import static org.wso2.carbon.identity.organization.management.application.constant.OrgApplicationMgtConstants.DELETE_FRAGMENT_APPLICATION;
Expand Down Expand Up @@ -210,15 +216,61 @@ public boolean doPostGetServiceProvider(ServiceProvider serviceProvider, String
.setUseUserstoreDomainInRoles(mainApplication
.getLocalAndOutBoundAuthenticationConfig().isUseUserstoreDomainInRoles());
}

// Set application's associated roles.
AssociatedRolesConfig associatedRolesConfigOfMainApp = mainApplication.getAssociatedRolesConfig();
if (associatedRolesConfigOfMainApp != null) {
AssociatedRolesConfig associatedRolesConfigForSharedApp =
getAssociatedRolesConfigForSharedApp(associatedRolesConfigOfMainApp, tenantDomain);
serviceProvider.setAssociatedRolesConfig(associatedRolesConfigForSharedApp);
}
}
} catch (OrganizationManagementException e) {
} catch (OrganizationManagementException | IdentityRoleManagementException e) {
throw new IdentityApplicationManagementException
("Error while retrieving the fragment application details.", e);
}
}
return super.doPostGetServiceProvider(serviceProvider, applicationName, tenantDomain);
}

private AssociatedRolesConfig getAssociatedRolesConfigForSharedApp(
AssociatedRolesConfig associatedRolesConfigOfMainApp, String tenantDomainOfSharedApp)
throws IdentityRoleManagementException {

String allowedAudience = associatedRolesConfigOfMainApp.getAllowedAudience();
RoleV2[] mainAppRoles = associatedRolesConfigOfMainApp.getRoles();
List<RoleV2> mainappRoleList = Arrays.asList(mainAppRoles);
AssociatedRolesConfig associatedRolesConfigForSharedApp = new AssociatedRolesConfig();
associatedRolesConfigForSharedApp.setAllowedAudience(allowedAudience);
List<String> mainAppRoleIds =
mainappRoleList.stream().map(RoleV2::getId).collect(Collectors.toList());
Map<String, String> mainRoleToSharedRoleMappingsBySubOrg =
getRoleManagementServiceV2().getMainRoleToSharedRoleMappingsBySubOrg(mainAppRoleIds,
tenantDomainOfSharedApp);

RoleV2[] associatedRolesOfSharedApp = mainRoleToSharedRoleMappingsBySubOrg.entrySet().stream()
.map(entry -> {
String sharedRoleId = entry.getValue();
String mainRoleId = entry.getKey();

// Find the main role by ID and retrieve its name.
String mainRoleName = mainappRoleList.stream()
.filter(role -> role.getId().equals(mainRoleId))
.findFirst()
.map(RoleV2::getName)
.orElse(null);

RoleV2 sharedRole = new RoleV2();
sharedRole.setId(sharedRoleId);
sharedRole.setName(mainRoleName);
return sharedRole;
})
.toArray(RoleV2[]::new);

associatedRolesConfigForSharedApp.setRoles(associatedRolesOfSharedApp);
return associatedRolesConfigForSharedApp;
}

@Override
public boolean doPreDeleteApplication(String applicationName, String tenantDomain, String userName)
throws IdentityApplicationManagementException {
Expand Down Expand Up @@ -305,6 +357,11 @@ private OrganizationManager getOrganizationManager() {
return OrgApplicationMgtDataHolder.getInstance().getOrganizationManager();
}

private RoleManagementService getRoleManagementServiceV2() {

return OrgApplicationMgtDataHolder.getInstance().getRoleManagementServiceV2();
}

/**
* Add application roles claim mapping to the filtered claim mappings.
*
Expand Down
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@
<artifactId>org.wso2.carbon.identity.governance</artifactId>
<version>${org.wso2.carbon.identity.governance.version}</version>
</dependency>

<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.role.v2.mgt.core</artifactId>
<version>${carbon.identity.framework.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
Expand Down Expand Up @@ -499,7 +503,7 @@
<carbon.multitenancy.package.import.version.range>[4.7.0,5.0.0)
</carbon.multitenancy.package.import.version.range>

<carbon.identity.framework.version>5.25.400</carbon.identity.framework.version>
<carbon.identity.framework.version>5.25.426</carbon.identity.framework.version>
<carbon.identity.package.import.version.range>[5.20.0, 7.0.0)
</carbon.identity.package.import.version.range>

Expand Down