Skip to content

Commit

Permalink
Add capability to append org id and org name to dev portal token urls
Browse files Browse the repository at this point in the history
  • Loading branch information
chamilaadhi committed Sep 24, 2024
1 parent 134b2c0 commit 62c00fc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -34,7 +36,8 @@ public Response keyManagersGet(String xWSO2Tenant, MessageContext messageContext
List<KeyManagerConfigurationDTO> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -118,12 +133,14 @@ public static JsonObject fromConfigurationMapToJson(Map configuration) {
return jsonObject;
}

public static KeyManagerListDTO toKeyManagerListDto(List<KeyManagerConfigurationDTO> keyManagerConfigurations) {
public static KeyManagerListDTO toKeyManagerListDto(List<KeyManagerConfigurationDTO> keyManagerConfigurations,
OrganizationInfo orgInfo) {

KeyManagerListDTO keyManagerListDTO = new KeyManagerListDTO();
List<KeyManagerInfoDTO> keyManagerInfoDTOList = new ArrayList<>();
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) {
keyManagerInfoDTOList.add(fromKeyManagerConfigurationDtoToKeyManagerInfoDto(keyManagerConfigurationDTO));
keyManagerInfoDTOList
.add(fromKeyManagerConfigurationDtoToKeyManagerInfoDto(keyManagerConfigurationDTO, orgInfo));
}
keyManagerListDTO.setList(keyManagerInfoDTOList);
keyManagerListDTO.setCount(keyManagerInfoDTOList.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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(",");
Expand All @@ -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
Expand Down

0 comments on commit 62c00fc

Please sign in to comment.