From b284bf127675855e96b458bddee047dc4a305b1f Mon Sep 17 00:00:00 2001 From: tcdlpds Date: Thu, 8 Aug 2024 20:44:53 +0530 Subject: [PATCH] Refactor source --- .../extension/api/KeyManagerServiceImpl.java | 1 - .../extension/service/KeyMgtServiceImpl.java | 57 ++++++++++--------- .../impl/DeviceManagementServiceImpl.java | 1 - 3 files changed, 30 insertions(+), 29 deletions(-) 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 54cb0e98e1..573316be81 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 @@ -46,7 +46,6 @@ 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(), dcrRequest.getPassword(), 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 b6e1e70ca4..2752bfb4d1 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 @@ -30,7 +30,13 @@ import io.entgra.device.mgt.core.apimgt.keymgt.extension.internal.KeyMgtDataHold import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager; import io.entgra.device.mgt.core.device.mgt.core.config.DeviceManagementConfig; import io.entgra.device.mgt.core.device.mgt.core.config.keymanager.KeyManagerConfigurations; -import okhttp3.*; +import okhttp3.Credentials; +import okhttp3.FormBody; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.JSONObject; @@ -51,7 +57,11 @@ import javax.net.ssl.X509TrustManager; import java.io.IOException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; public class KeyMgtServiceImpl implements KeyMgtService { @@ -99,7 +109,6 @@ public class KeyMgtServiceImpl implements KeyMgtService { return new DCRResponse(dcrApplication.getClientId(), dcrApplication.getClientSecret()); } else { // super-tenant admin dcr and token generation - //todo lasantha null passed in last two params OAuthApplication superTenantOauthApp = createOauthApplication( KeyMgtConstants.RESERVED_OAUTH_APP_NAME_PREFIX + KeyMgtConstants.SUPER_TENANT, kmConfig.getAdminUsername(), null, validityPeriod, kmConfig.getAdminPassword(), null, null); @@ -124,17 +133,11 @@ public class KeyMgtServiceImpl implements KeyMgtService { createUserIfNotExists(subTenantUserUsername, subTenantUserPassword); // DCR for the requesting user - //todo lasantha -> need to pass password of user - //todo lasantha null passed in last two params - OAuthApplication dcrApplication = createOauthApplication(clientName, owner, tags, validityPeriod, password, null, null); String requestingUserAccessToken = createAccessToken(dcrApplication); // get application id - //todo --> can use requestingUserAccessToken token here to get application data - modify getApplication - // method signature - io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application = getApplication(clientName, requestingUserAccessToken); String applicationUUID = application.getApplicationId(); @@ -237,16 +240,16 @@ public class KeyMgtServiceImpl implements KeyMgtService { } } catch (APIManagementException e) { msg = "Error occurred while retrieving application"; - log.error(msg); - throw new KeyMgtException(msg); + log.error(msg, e); + throw new KeyMgtException(msg, e); } catch (IOException e) { msg = "Error occurred while mapping application keys"; - log.error(msg); - throw new KeyMgtException(msg); + log.error(msg, e); + throw new KeyMgtException(msg, e); } catch (UserStoreException e) { msg = "Error occurred while fetching tenant id"; - log.error(msg); - throw new KeyMgtException(msg); + log.error(msg, e); + throw new KeyMgtException(msg, e); } } @@ -283,7 +286,8 @@ public class KeyMgtServiceImpl implements KeyMgtService { client.newCall(request).execute(); } catch (IOException e) { msg = "Error occurred while mapping application keys"; - throw new KeyMgtException(msg); + log.error(msg, e); + throw new KeyMgtException(msg, e); } } @@ -309,8 +313,8 @@ public class KeyMgtServiceImpl implements KeyMgtService { } } catch (UserStoreException e) { msg = "Error when trying to fetch tenant details"; - log.error(msg); - throw new KeyMgtException(msg); + log.error(msg, e); + throw new KeyMgtException(msg, e); } } @@ -325,7 +329,6 @@ public class KeyMgtServiceImpl implements KeyMgtService { private OAuthApplication createOauthApplication (String clientName, String owner, String[] tags, int validityPeriod, String ownerPassword, List supportedGrantTypes, String callbackUrl) throws KeyMgtException { - //todo modify this to pass the password as well String oauthAppCreationPayloadStr = createOauthAppCreationPayload(clientName, owner, tags, validityPeriod, ownerPassword, supportedGrantTypes, callbackUrl); RequestBody oauthAppCreationPayload = RequestBody.Companion.create(oauthAppCreationPayloadStr, JSON); @@ -333,7 +336,6 @@ public class KeyMgtServiceImpl implements KeyMgtService { String dcrEndpoint = kmConfig.getServerUrl() + KeyMgtConstants.DCR_ENDPOINT; String username, password; - //todo why can't we use owner details here? if (KeyMgtConstants.SUPER_TENANT.equals(MultitenantUtils.getTenantDomain(owner))) { username = kmConfig.getAdminUsername(); password = kmConfig.getAdminPassword(); @@ -342,7 +344,6 @@ public class KeyMgtServiceImpl implements KeyMgtService { password = subTenantUserPassword; } - //todo why can't we use owner details for authentication Request request = new Request.Builder() .url(dcrEndpoint) .addHeader(KeyMgtConstants.AUTHORIZATION_HEADER, Credentials.basic(username, password)) @@ -352,8 +353,9 @@ public class KeyMgtServiceImpl implements KeyMgtService { Response response = client.newCall(request).execute(); return gson.fromJson(response.body().string(), OAuthApplication.class); } catch (IOException e) { - msg = "Error occurred while processing the response" + e; - throw new KeyMgtException(msg); + msg = "Error occurred while processing the response." ; + log.error(msg, e); + throw new KeyMgtException(msg, e); } } @@ -384,7 +386,8 @@ public class KeyMgtServiceImpl implements KeyMgtService { return jsonObject.getString("access_token"); } catch (IOException e) { msg = "Error occurred while reading access token from response"; - throw new KeyMgtException(msg); + log.error(msg, e); + throw new KeyMgtException(msg, e); } } @@ -456,15 +459,15 @@ public class KeyMgtServiceImpl implements KeyMgtService { } catch (io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException e) { msg = "Error while trying to retrieve the application"; log.error(msg, e); - throw new KeyMgtException(msg); + throw new KeyMgtException(msg, e); } catch (UnexpectedResponseException e) { msg = "Received invalid response for the API applications retrieving REST API call."; log.error(msg, e); - throw new KeyMgtException(msg); + throw new KeyMgtException(msg, e); } catch (APIServicesException e) { msg = "Error occurred while processing the API Response."; log.error(msg, e); - throw new KeyMgtException(msg); + throw new KeyMgtException(msg, e); } } 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 bd614f5b1e..1d0b0994c9 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 @@ -957,7 +957,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { KeyMgtService keyMgtService = new KeyMgtServiceImpl(); try { - //todo - lasantha - can't get password from here ApiApplicationKey apiApplicationKey; try { APIApplicationServices apiApplicationServices = DeviceMgtAPIUtils.getApiApplicationServices();