From 2d296a3453f9317e5f432450f651b0062f753875 Mon Sep 17 00:00:00 2001 From: pasindu Date: Wed, 26 Jul 2023 12:44:40 +0530 Subject: [PATCH] Implement map application keys using dcr response --- .../APIManagementProviderServiceImpl.java | 9 ++- .../rest/api/ConsumerRESTAPIServices.java | 3 + .../rest/api/ConsumerRESTAPIServicesImpl.java | 67 ++++++++++++++++++- 3 files changed, 76 insertions(+), 3 deletions(-) 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 8c12ac45ecd..3c622751862 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 @@ -250,8 +250,13 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe log.error(msg); throw new APIManagerException(msg); } - ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(tokenInfo, application.getApplicationId(), - keyManager.getName(), validityTime, keyType); + + ApiApplicationInfo applicationInfo = getApplicationInfo(null, null); + tokenInfo.setApiApplicationInfo(applicationInfo); + + ApplicationKey applicationKey = consumerRESTAPIServices.mapApplicationKeys(tokenInfo, application, + keyManager.getName(), keyType); + ApiApplicationKey apiApplicationKey = new ApiApplicationKey(); apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey()); apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret()); diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServices.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServices.java index 5ed98034e2b..8ab2c1f0ad0 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServices.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServices.java @@ -55,6 +55,9 @@ public interface ConsumerRESTAPIServices { ApplicationKey generateApplicationKeys(TokenInfo tokenInfo, String applicationId, String keyManager, String validityTime, String keyType) throws APIServicesException, BadRequestException, UnexpectedResponseException; + ApplicationKey mapApplicationKeys(TokenInfo tokenInfo, Application application, String keyManager, String keyType) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + ApplicationKey getKeyDetails(TokenInfo tokenInfo, String applicationId, String keyMapId) throws APIServicesException, BadRequestException, UnexpectedResponseException; diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServicesImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServicesImpl.java index 293894ee711..572756dff72 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServicesImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/ConsumerRESTAPIServicesImpl.java @@ -606,6 +606,71 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices { } } + @Override + public ApplicationKey mapApplicationKeys(TokenInfo tokenInfo, Application application, String keyManager, String keyType) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo(); + boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken()); + String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + + application.getApplicationId() + "/map-keys"; + + String payload = "{\n" + + " \"consumerKey\": \"" + apiApplicationInfo.getClientId() + "\",\n" + + " \"consumerSecret\": \"" + apiApplicationInfo.getClientSecret() + "\",\n" + + " \"keyManager\": \"" + keyManager + "\",\n" + + " \"keyType\": \"" + keyType + "\"\n" + + "}"; + RequestBody requestBody = RequestBody.create(JSON, payload); + + Request.Builder builder = new Request.Builder(); + builder.url(getAllScopesUrl); + if (!token) { + builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + apiApplicationInfo.getAccess_token()); + } else { + builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + tokenInfo.getAccessToken()); + } + builder.post(requestBody); + Request request = builder.build(); + + try { + Response response = client.newCall(request).execute(); + if (HttpStatus.SC_OK == response.code()) { + return gson.fromJson(response.body().string(), ApplicationKey.class); + } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { + if (!token) { + APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); + AccessTokenInfo refreshedAccessToken = apiApplicationServices. + generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(), + apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret()); + ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken); + //TODO: max attempt count + TokenInfo refreshedTokenInfo = new TokenInfo(); + refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo); + refreshedTokenInfo.setAccessToken(null); + return mapApplicationKeys(refreshedTokenInfo, application, keyManager, keyType); + } else { + String msg = "Invalid access token. Unauthorized request"; + log.error(msg); + throw new APIServicesException(msg); + } + } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { + String msg = "Bad Request, Invalid request body"; + log.error(msg); + throw new BadRequestException(msg); + } else { + String msg = "Response : " + response.code() + response.body(); + throw new UnexpectedResponseException(msg); + } + } catch (IOException e) { + String msg = "Error occurred while processing the response"; + log.error(msg, e); + throw new APIServicesException(msg, e); + } + } + @Override public ApplicationKey getKeyDetails(TokenInfo tokenInfo, String applicationId, String keyMapId) throws APIServicesException, BadRequestException, UnexpectedResponseException { @@ -733,7 +798,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices { private boolean isTokenNull(ApiApplicationInfo apiApplicationInfo, String accessToken) throws BadRequestException { boolean token; - if ((!(accessToken == null) && apiApplicationInfo == null)) { + if ((!(accessToken == null))) { token = true; } else if (!(apiApplicationInfo == null) && accessToken == null) { token = false;