From 500fa0df982fcafe04290ef0f3bfd7e596b379f3 Mon Sep 17 00:00:00 2001 From: tcdlpds Date: Wed, 20 Sep 2023 09:30:16 +0530 Subject: [PATCH] Add improvements to app registering logic --- ...ApiApplicationRegistrationServiceImpl.java | 30 +++++----- .../APIManagementProviderService.java | 23 ++++--- .../APIManagementProviderServiceImpl.java | 60 +++++++++++-------- .../application/mgt/core/util/OAuthUtils.java | 2 +- .../impl/DeviceManagementServiceImpl.java | 8 +-- 5 files changed, 69 insertions(+), 54 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java index e3ce294a21e..8b2562d6567 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java @@ -66,7 +66,7 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi applicationName, APIUtil.getDefaultTags(), ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, false, ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getRealmConfiguration().getAdminPassword(), null, false); + .getRealmConfiguration().getAdminPassword(), null, null, null, false); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build(); } catch (APIManagerException e) { String msg = "Error occurred while registering an application '" + applicationName + "'"; @@ -109,23 +109,24 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi if (username.equals(registrationProfile.getUsername())) { synchronized (ApiApplicationRegistrationServiceImpl.class) { - ApplicationGrantTypeUpdater applicationGrantTypeUpdater = null; - if (registrationProfile.getSupportedGrantTypes() != null && !registrationProfile.getSupportedGrantTypes().isEmpty()) { - applicationGrantTypeUpdater = new ApplicationGrantTypeUpdater(); - applicationGrantTypeUpdater.setSupportedGrantTypes(registrationProfile.getSupportedGrantTypes()); - - } else if (StringUtils.isNotEmpty(registrationProfile.getCallbackUrl())) { - return Response.status(Response.Status.BAD_REQUEST).entity("Callback URL should be Empty when" + - " request does not contain supported grant types to update grant types of the " + - "application." - ).build(); - } +// ApplicationGrantTypeUpdater applicationGrantTypeUpdater = null; +// if (registrationProfile.getSupportedGrantTypes() != null && !registrationProfile.getSupportedGrantTypes().isEmpty()) { +// applicationGrantTypeUpdater = new ApplicationGrantTypeUpdater(); +// applicationGrantTypeUpdater.setSupportedGrantTypes(registrationProfile.getSupportedGrantTypes()); +// +// } else if (StringUtils.isNotEmpty(registrationProfile.getCallbackUrl())) { +// return Response.status(Response.Status.BAD_REQUEST).entity("Callback URL should be Empty when" + +// " request does not contain supported grant types to update grant types of the " + +// "application." +// ).build(); +// } ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( applicationName, registrationProfile.getTags(), ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, registrationProfile.isAllowedToAllDomains(), validityPeriod, - registrationProfile.getPassword(), applicationGrantTypeUpdater, false); + registrationProfile.getPassword(), null, registrationProfile.getSupportedGrantTypes(), + registrationProfile.getCallbackUrl(), false); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build(); } } @@ -138,7 +139,8 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi applicationName, registrationProfile.getTags(), ApiApplicationConstants.DEFAULT_TOKEN_TYPE, registrationProfile.getUsername(), registrationProfile.isAllowedToAllDomains(), validityPeriod, - registrationProfile.getPassword(), null, false); + registrationProfile.getPassword(), null, registrationProfile.getSupportedGrantTypes(), + registrationProfile.getCallbackUrl(), false); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build(); } } catch (APIManagerException e) { 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/APIManagementProviderService.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderService.java index 1b81b29ce30..3aa1835f78c 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderService.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderService.java @@ -24,6 +24,8 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.App import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.TokenInfo; import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo; +import java.util.ArrayList; + /** * This comprise on operation that is been done with api manager from CDMF. This service needs to be implemented in APIM. */ @@ -52,18 +54,21 @@ public interface APIManagementProviderService { // String keyType, String username, boolean isAllowedAllDomains, // String validityTime) throws APIManagerException; - ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags, - String keyType, String username, - boolean isAllowedAllDomains, - String validityTime, String password, - ApplicationGrantTypeUpdater applicationGrantTypeUpdater, - boolean isMappingRequired) throws APIManagerException; +// ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags, +// String keyType, String username, +// boolean isAllowedAllDomains, +// String validityTime, String password, +// ArrayList supportedGrantTypes, +// String callbackUrl, +// boolean isMappingRequired) throws APIManagerException; ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags, - String keyType, + String keyType, String username, boolean isAllowedAllDomains, - String validityTime, TokenInfo tokenInfo, - ApplicationGrantTypeUpdater applicationGrantTypeUpdater, + String validityTime, + String password, String accessToken, + ArrayList supportedGrantTypes, + String callbackUrl, boolean isMappingRequired) throws APIManagerException; // /** 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 6b5ecc63caf..6d0d4b4f1d6 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 @@ -120,32 +120,44 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe // tokenInfo.setAccessToken(accessToken); // return generateAndRetrieveApplicationKeys(applicationName, tags, keyType, isAllowedAllDomains, validityTime, // tokenInfo, null, false); +// } + +// @Override +// public synchronized ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags, +// String keyType, String username, +// boolean isAllowedAllDomains, +// String validityTime, String password, +// ArrayList supportedGrantTypes, +// String callbackUrl, +// boolean isMappingRequired) +// throws APIManagerException { +// +// ApiApplicationInfo applicationInfo = getApplicationInfo(username, password); +// TokenInfo tokenInfo = new TokenInfo(); +// tokenInfo.setApiApplicationInfo(applicationInfo); +// tokenInfo.setAccessToken(null); +// return generateAndRetrieveApplicationKeys(applicationName, tags, keyType, isAllowedAllDomains, validityTime, +// tokenInfo, supportedGrantTypes, callbackUrl, isMappingRequired); // } @Override public synchronized ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags, String keyType, String username, boolean isAllowedAllDomains, - String validityTime, String password, - ApplicationGrantTypeUpdater applicationGrantTypeUpdater, + String validityTime, + String password, String accessToken, + ArrayList supportedGrantTypes, + String callbackUrl, boolean isMappingRequired) throws APIManagerException { - ApiApplicationInfo applicationInfo = getApplicationInfo(username, password); TokenInfo tokenInfo = new TokenInfo(); - tokenInfo.setApiApplicationInfo(applicationInfo); - tokenInfo.setAccessToken(null); - return generateAndRetrieveApplicationKeys(applicationName, tags, keyType, isAllowedAllDomains, validityTime, - tokenInfo, applicationGrantTypeUpdater, isMappingRequired); - } - - @Override - public synchronized ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags, - String keyType, - boolean isAllowedAllDomains, - String validityTime, TokenInfo tokenInfo, - ApplicationGrantTypeUpdater applicationGrantTypeUpdater, - boolean isMappingRequired) throws APIManagerException { + if (StringUtils.isEmpty(accessToken)) { + ApiApplicationInfo applicationInfo = getApplicationInfo(username, password); + tokenInfo.setApiApplicationInfo(applicationInfo); + } else { + tokenInfo.setAccessToken(accessToken); + } ConsumerRESTAPIServices consumerRESTAPIServices = APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices(); @@ -171,7 +183,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe consumerRESTAPIServices.getAllApplications(tokenInfo, applicationName); if (applications.length == 0) { return handleNewAPIApplication(applicationName, uniqueApiList, tokenInfo, keyType, - validityTime, applicationGrantTypeUpdater, isMappingRequired); + validityTime, supportedGrantTypes, callbackUrl, isMappingRequired); } else { if (applications.length == 1) { Optional applicationOpt = @@ -185,7 +197,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe // Todo add a comment consumerRESTAPIServices.deleteApplication(tokenInfo, application.getApplicationId()); return handleNewAPIApplication(applicationName, uniqueApiList, tokenInfo, keyType, - validityTime, applicationGrantTypeUpdater, isMappingRequired); + validityTime, supportedGrantTypes, callbackUrl, isMappingRequired); } else { Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(tokenInfo, application.getApplicationId()); for (Subscription subscription : subscriptions) { @@ -239,7 +251,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe private ApiApplicationKey handleNewAPIApplication(String applicationName, List uniqueApiList, TokenInfo tokenInfo, String keyType, String validityTime, - ApplicationGrantTypeUpdater applicationGrantTypeUpdater, + ArrayList supportedGrantTypes, String callbackUrl, boolean isMappingRequired) throws APIManagerException { ConsumerRESTAPIServices consumerRESTAPIServices = APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices(); @@ -264,9 +276,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe tokenInfo.setApiApplicationInfo(getApplicationInfo(null, null)); ApplicationKey applicationKey; - if (isMappingRequired) { - } if (isMappingRequired) { // If we need to get opaque token instead of the JWT token, we have to do the mapping. Therefore,, if @@ -279,11 +289,9 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe } // ApplicationKey updateGrantType(TokenInfo tokenInfo, String applicationId, String keyMapId, String keyManager, // String supportedGrantTypes, String callbackUrl) - if (applicationGrantTypeUpdater != null) { + if (supportedGrantTypes != null || StringUtils.isNotEmpty(callbackUrl)) { applicationKey = consumerRESTAPIServices.updateGrantType(tokenInfo, application.getApplicationId(), - applicationKey.getKeyMappingId(), keyManager.getName(), - applicationGrantTypeUpdater.getSupportedGrantTypes(), - applicationGrantTypeUpdater.getCallbackUrl()); + applicationKey.getKeyMappingId(), keyManager.getName(), supportedGrantTypes, callbackUrl); } @@ -671,7 +679,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe registrationProfile.getTags(), tokenType, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() .getRealmConfiguration().getAdminUserName(), registrationProfile.isAllowedToAllDomains(), validityPeriod, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getRealmConfiguration().getAdminPassword(), null, false); + .getRealmConfiguration().getAdminPassword(), null, null, null, false); } finally { PrivilegedCarbonContext.endTenantFlow(); } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/OAuthUtils.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/OAuthUtils.java index 35dced984ab..96e4c40fd1f 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/OAuthUtils.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/OAuthUtils.java @@ -66,7 +66,7 @@ public class OAuthUtils { registrationProfile.getTags(), Constants.ApplicationInstall.DEFAULT_TOKEN_TYPE, username, registrationProfile.isAllowedToAllDomains(), Constants.ApplicationInstall.DEFAULT_VALIDITY_PERIOD, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getRealmConfiguration().getAdminPassword(), null, false); + .getRealmConfiguration().getAdminPassword(), null, null, null, false); } finally { PrivilegedCarbonContext.endTenantFlow(); } 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 5905831436b..f7f46b7df1d 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 @@ -828,11 +828,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { "perm:users:send-invitation"); APIManagementProviderService apiManagementProviderService = DeviceMgtAPIUtils.getAPIManagementService(); - TokenInfo tokenInfo = new TokenInfo(); - tokenInfo.setAccessToken(accessTokenInfo.getAccessToken()); +// TokenInfo tokenInfo = new TokenInfo(); +// tokenInfo.setAccessToken(accessTokenInfo.getAccessToken()); apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(applicationName, - new String[] {"device_management"}, "PRODUCTION", false, String.valueOf(validityTime), - tokenInfo, null, true); + new String[] {"device_management"}, "PRODUCTION", null, false, String.valueOf(validityTime), + null, accessTokenInfo.getAccessToken(), null, null,true); } catch (JWTClientException e) { String msg = "Error while generating an application tokens for Tenant Admin.";