From 62c00fc81ad6111eaa5932a4b9d7ff7685b14157 Mon Sep 17 00:00:00 2001 From: Chamila Adhikarinayake Date: Tue, 24 Sep 2024 11:36:36 +0530 Subject: [PATCH] Add capability to append org id and org name to dev portal token urls --- .../v1/impl/KeyManagersApiServiceImpl.java | 5 ++- .../v1/mappings/KeyManagerMappingUtil.java | 31 ++++++++++++++----- .../impl/OAuthOpaqueAuthenticatorImpl.java | 17 +++++++--- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/KeyManagersApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/KeyManagersApiServiceImpl.java index cab23de7858e..a68b1803daed 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/KeyManagersApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/KeyManagersApiServiceImpl.java @@ -7,6 +7,7 @@ import org.wso2.carbon.apimgt.api.APIConsumer; import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO; +import org.wso2.carbon.apimgt.api.model.OrganizationInfo; import org.wso2.carbon.apimgt.impl.APIAdminImpl; import org.wso2.carbon.apimgt.impl.APIConsumerImpl; import org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil; @@ -26,6 +27,7 @@ public Response keyManagersGet(String xWSO2Tenant, MessageContext messageContext String organization = RestApiUtil.getOrganization(messageContext); try { + OrganizationInfo orgInfo = RestApiUtil.getOrganizationInfo(messageContext); APIAdmin apiAdmin = new APIAdminImpl(); APIConsumer apiConsumer = new APIConsumerImpl(); String username = RestApiCommonUtil.getLoggedInUsername(); @@ -34,7 +36,8 @@ public Response keyManagersGet(String xWSO2Tenant, MessageContext messageContext List globalKeyManagerConfigurations = apiAdmin.getGlobalKeyManagerConfigurations(); permittedKeyManagerConfigurations.addAll(globalKeyManagerConfigurations); - return Response.ok(KeyManagerMappingUtil.toKeyManagerListDto(permittedKeyManagerConfigurations)).build(); + return Response.ok(KeyManagerMappingUtil.toKeyManagerListDto(permittedKeyManagerConfigurations, orgInfo)) + .build(); } catch (APIManagementException e) { RestApiUtil.handleInternalServerError( diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/KeyManagerMappingUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/KeyManagerMappingUtil.java index 29750278f3e2..8267cd946090 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/KeyManagerMappingUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/KeyManagerMappingUtil.java @@ -8,6 +8,7 @@ import org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO; import org.wso2.carbon.apimgt.api.model.ConfigurationDto; import org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration; +import org.wso2.carbon.apimgt.api.model.OrganizationInfo; import org.wso2.carbon.apimgt.impl.APIConstants; import org.wso2.carbon.apimgt.impl.utils.APIUtil; import org.wso2.carbon.apimgt.rest.api.store.v1.dto.KeyManagerApplicationConfigurationDTO; @@ -22,7 +23,7 @@ public class KeyManagerMappingUtil { public static KeyManagerInfoDTO fromKeyManagerConfigurationDtoToKeyManagerInfoDto( - KeyManagerConfigurationDTO keyManagerConfigurationDTO) { + KeyManagerConfigurationDTO keyManagerConfigurationDTO, OrganizationInfo orgInfo) { KeyManagerInfoDTO keyManagerInfoDTO = new KeyManagerInfoDTO(); keyManagerInfoDTO.setId(keyManagerConfigurationDTO.getUuid()); @@ -62,8 +63,13 @@ public static KeyManagerInfoDTO fromKeyManagerConfigurationDtoToKeyManagerInfoDt if (jsonObject.has(APIConstants.KeyManager.DISPLAY_TOKEN_ENDPOINT) && !jsonObject.get(APIConstants.KeyManager.DISPLAY_TOKEN_ENDPOINT).isJsonNull() && !jsonObject.get(APIConstants.KeyManager.DISPLAY_TOKEN_ENDPOINT).getAsString().trim().isEmpty()) { - keyManagerInfoDTO.setTokenEndpoint( - jsonObject.get(APIConstants.KeyManager.DISPLAY_TOKEN_ENDPOINT).getAsString()); + if (orgInfo != null && orgInfo.getName() != null && orgInfo.getId() != null) { + keyManagerInfoDTO.setTokenEndpoint(jsonObject.get(APIConstants.KeyManager.DISPLAY_TOKEN_ENDPOINT) + .getAsString().replace("{org_name}", orgInfo.getName()).replace("{org_id}", orgInfo.getId())); + } else { + keyManagerInfoDTO + .setTokenEndpoint(jsonObject.get(APIConstants.KeyManager.DISPLAY_TOKEN_ENDPOINT).getAsString()); + } } else { if (jsonObject.has(APIConstants.KeyManager.TOKEN_ENDPOINT)){ keyManagerInfoDTO.setTokenEndpoint( @@ -73,8 +79,17 @@ public static KeyManagerInfoDTO fromKeyManagerConfigurationDtoToKeyManagerInfoDt if (jsonObject.has(APIConstants.KeyManager.DISPLAY_REVOKE_ENDPOINT) && !jsonObject.get(APIConstants.KeyManager.DISPLAY_REVOKE_ENDPOINT).isJsonNull() && !jsonObject.get(APIConstants.KeyManager.DISPLAY_REVOKE_ENDPOINT).getAsString().trim().isEmpty()) { - keyManagerInfoDTO.setRevokeEndpoint( - jsonObject.get(APIConstants.KeyManager.DISPLAY_REVOKE_ENDPOINT).getAsString()); + if (orgInfo != null && orgInfo.getName() != null && orgInfo.getId() != null) { + keyManagerInfoDTO.setRevokeEndpoint( + jsonObject.get(APIConstants.KeyManager.DISPLAY_REVOKE_ENDPOINT) + .getAsString() + .replace("{org_name}", orgInfo.getName()) + .replace("{org_id}", orgInfo.getId()) + ); + } else { + keyManagerInfoDTO.setRevokeEndpoint( + jsonObject.get(APIConstants.KeyManager.DISPLAY_REVOKE_ENDPOINT).getAsString()); + } } else { if (jsonObject.has(APIConstants.KeyManager.REVOKE_ENDPOINT)) { keyManagerInfoDTO.setRevokeEndpoint( @@ -118,12 +133,14 @@ public static JsonObject fromConfigurationMapToJson(Map configuration) { return jsonObject; } - public static KeyManagerListDTO toKeyManagerListDto(List keyManagerConfigurations) { + public static KeyManagerListDTO toKeyManagerListDto(List keyManagerConfigurations, + OrganizationInfo orgInfo) { KeyManagerListDTO keyManagerListDTO = new KeyManagerListDTO(); List keyManagerInfoDTOList = new ArrayList<>(); for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) { - keyManagerInfoDTOList.add(fromKeyManagerConfigurationDtoToKeyManagerInfoDto(keyManagerConfigurationDTO)); + keyManagerInfoDTOList + .add(fromKeyManagerConfigurationDtoToKeyManagerInfoDto(keyManagerConfigurationDTO, orgInfo)); } keyManagerListDTO.setList(keyManagerInfoDTOList); keyManagerListDTO.setCount(keyManagerInfoDTOList.size()); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthOpaqueAuthenticatorImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthOpaqueAuthenticatorImpl.java index d1b9f687c101..b9dd63e21861 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthOpaqueAuthenticatorImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthOpaqueAuthenticatorImpl.java @@ -253,11 +253,17 @@ public OrganizationInfo getOrganizationInfo(String tenantDomain, String username int tenantId = MultitenantConstants.SUPER_TENANT_ID; APIManagerConfiguration config = ServiceReferenceHolder.getInstance(). getAPIManagerConfigurationService().getAPIManagerConfiguration(); - String claim = config.getFirstProperty(APIConstants.API_STORE_GROUP_EXTRACTOR_CLAIM_URI); - if (StringUtils.isBlank(claim)) { - claim = "http://wso2.org/claims/organization"; + String orgNameClaim = config.getOrgAccessControl().getOrgNameLocalClaim(); + String orgIdClaim = config.getOrgAccessControl().getOrgIdLocalClaim(); + if (StringUtils.isBlank(orgNameClaim)) { + orgNameClaim = "http://wso2.org/claims/organization"; } + if (StringUtils.isBlank(orgIdClaim)) { + orgIdClaim = "http://wso2.org/claims/organizationid"; + } + String organization = null; + String organizationId = null; String[] groupIdArray = null; try { if (tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) { @@ -276,7 +282,9 @@ public OrganizationInfo getOrganizationInfo(String tenantDomain, String username UserRealm realm = (UserRealm) realmService.getTenantUserRealm(tenantId); UserStoreManager manager = realm.getUserStoreManager(); organization = - manager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername(username), claim, null); + manager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername(username), orgNameClaim, null); + organizationId = + manager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername(username), orgIdClaim, null); if (organization != null) { if (organization.contains(",")) { groupIdArray = organization.split(","); @@ -287,6 +295,7 @@ public OrganizationInfo getOrganizationInfo(String tenantDomain, String username organization = organization.trim(); groupIdArray = new String[] {organization}; orgInfo.setName(organization); // check for multiple orgs + orgInfo.setId(organizationId); } } else { // If claim is null then returning a empty string