From a09d72c33ccecee5757dd29eafd959311ba14e3e Mon Sep 17 00:00:00 2001 From: Dharmakeerthi Lasantha Date: Sat, 10 Jun 2023 11:43:27 +0530 Subject: [PATCH] Add todo Comments --- .../pom.xml | 13 ------- .../APIManagementProviderServiceImpl.java | 36 +++++++++---------- .../extension/api/KeyManagerServiceImpl.java | 1 + .../extension/service/KeyMgtServiceImpl.java | 4 +++ .../impl/DeviceManagementServiceImpl.java | 1 + 5 files changed, 22 insertions(+), 33 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml index ed7542738a7..6ad08c770a4 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml @@ -129,19 +129,6 @@ org.json.simple;version="[1.1,2)", org.osgi.framework.*;version="${imp.package.version.osgi.framework}", org.osgi.service.*;version="${imp.package.version.osgi.service}", - org.wso2.carbon.apimgt.api;version="${carbon.api.mgt.version.range}", - org.wso2.carbon.apimgt.api.dto;version="${carbon.api.mgt.version.range}", - org.wso2.carbon.apimgt.api.model;version="${carbon.api.mgt.version.range}", - io.entgra.device.mgt.core.apimgt.application.extension.bean, - io.entgra.device.mgt.core.apimgt.application.extension.dto, - io.entgra.device.mgt.core.apimgt.application.extension.exception, - org.wso2.carbon.apimgt.impl;version="${carbon.api.mgt.version.range}", - org.wso2.carbon.apimgt.impl.utils;version="${carbon.api.mgt.version.range}", - org.wso2.carbon.context;version="${carbon.kernel.version.range}", - io.entgra.device.mgt.core.identity.jwt.client.extension, - io.entgra.device.mgt.core.identity.jwt.client.extension.dto, - io.entgra.device.mgt.core.identity.jwt.client.extension.exception, - io.entgra.device.mgt.core.identity.jwt.client.extension.service, io.entgra.device.mgt.core.apimgt.extension.rest.api, org.wso2.carbon.apimgt.api;version="[9.28,10)", org.wso2.carbon.apimgt.api.dto;version="[9.28,10)", diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java index 67bf9ef56b7..ad58ab4f334 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java @@ -109,12 +109,11 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe String validityTime, String password) throws APIManagerException { - APIApplicationServices apiApplicationServices = APIApplicationManagerExtensionDataHolder.getInstance() - .getApiApplicationServices(); + ConsumerRESTAPIServices consumerRESTAPIServices = APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices(); - ApiApplicationInfo applicationInfo = applicationInfo(apiApplicationServices, username, password); + ApiApplicationInfo applicationInfo = getApplicationInfo(username, password); try { List uniqueApiList = new ArrayList<>(); @@ -138,7 +137,9 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application[] applications = consumerRESTAPIServices.getAllApplications(applicationInfo, applicationName); io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application; + boolean isNewApplication = false; if (applications.length == 0) { + isNewApplication = true; application = new io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application(); application.setName(applicationName); application = consumerRESTAPIServices.createApplication(applicationInfo, application); @@ -158,22 +159,14 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe } } - if (application != null) { - if (!application.getKeys().isEmpty()) { - //todo return Application Keys - return null; - } else{ - - ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(applicationInfo, application); - ApiApplicationKey apiApplicationKey = new ApiApplicationKey(); - apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey()); - apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret()); - return apiApplicationKey; - } - } else{ - String msg = "Application retrieval process failed."; - log.error(msg); - throw new APIManagerException(msg); + if (isNewApplication) { + ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(applicationInfo, application); + ApiApplicationKey apiApplicationKey = new ApiApplicationKey(); + apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey()); + apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret()); + return apiApplicationKey; + } else { + return null; } } catch (APIServicesException e) { String msg = "Error occurred while processing the response of APIM REST endpoints."; @@ -555,9 +548,12 @@ Otherwise, Generate Application Keys and return them return info; } - private ApiApplicationInfo applicationInfo(APIApplicationServices apiApplicationServices, String username, String password) + private ApiApplicationInfo getApplicationInfo(String username, String password) throws APIManagerException { + APIApplicationServices apiApplicationServices = APIApplicationManagerExtensionDataHolder.getInstance() + .getApiApplicationServices(); + APIApplicationKey apiApplicationKey; io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo accessTokenInfo; try { diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/api/KeyManagerServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/api/KeyManagerServiceImpl.java index 96f506d99ec..dbfee329064 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/api/KeyManagerServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/api/KeyManagerServiceImpl.java @@ -45,6 +45,7 @@ public class KeyManagerServiceImpl implements KeyManagerService { public Response dynamicClientRegistration(DCRRequest dcrRequest) { try { KeyMgtService keyMgtService = new KeyMgtServiceImpl(); + //todo lasantha - can pass password from here - modify DCRRequest object DCRResponse resp = keyMgtService.dynamicClientRegistration(dcrRequest.getApplicationName(), dcrRequest.getUsername(), dcrRequest.getGrantTypes(), dcrRequest.getCallBackUrl(), dcrRequest.getTags(), dcrRequest.getIsSaasApp(), dcrRequest.getValidityPeriod()); return Response.status(Response.Status.CREATED).entity(gson.toJson(resp)).build(); diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java index aee387abc4e..936011e90f5 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java @@ -118,10 +118,13 @@ public class KeyMgtServiceImpl implements KeyMgtService { createUserIfNotExists(subTenantUserUsername, subTenantUserPassword); // DCR for the requesting user + //todo lasantha -> need to pass password of user OAuthApplication dcrApplication = createOauthApplication(clientName, owner, tags, validityPeriod); String requestingUserAccessToken = createAccessToken(dcrApplication); // get application id + //todo --> can use requestingUserAccessToken token here to get application data - modify getApplication + // method signature Application application = getApplication(clientName, owner); String applicationUUID = application.getUUID(); @@ -420,6 +423,7 @@ public class KeyMgtServiceImpl implements KeyMgtService { try { APIManagerFactory apiManagerFactory = APIManagerFactory.getInstance(); APIConsumer apiConsumer = apiManagerFactory.getAPIConsumer(owner); + //todo modify the method signature and use access token and call REST API to get application data return null; // todo:apim - apiConsumer.getApplicationsByName(owner, applicationName, ""); // // curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://localhost:9443/api/am/devportal/v3/applications?query=CalculatorApp" diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java index d1ddfaa44f7..579f09c6063 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -797,6 +797,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { KeyMgtService keyMgtService = new KeyMgtServiceImpl(); try { + //todo - lasantha - can't get password from here DCRResponse dcrResponse = keyMgtService.dynamicClientRegistration(applicationName, username, "client_credentials", null, new String[] {"device_management"}, false, validityTime); deviceConfig.setClientId(dcrResponse.getClientId());