Add improvements to app registering logic

remotes/1728937285237262521/tmp_refs/heads/master
tcdlpds 1 year ago
parent 088642e7ce
commit 500fa0df98

@ -66,7 +66,7 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
applicationName, APIUtil.getDefaultTags(), applicationName, APIUtil.getDefaultTags(),
ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, false, ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, false,
ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() 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(); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build();
} catch (APIManagerException e) { } catch (APIManagerException e) {
String msg = "Error occurred while registering an application '" + applicationName + "'"; String msg = "Error occurred while registering an application '" + applicationName + "'";
@ -109,23 +109,24 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
if (username.equals(registrationProfile.getUsername())) { if (username.equals(registrationProfile.getUsername())) {
synchronized (ApiApplicationRegistrationServiceImpl.class) { synchronized (ApiApplicationRegistrationServiceImpl.class) {
ApplicationGrantTypeUpdater applicationGrantTypeUpdater = null; // ApplicationGrantTypeUpdater applicationGrantTypeUpdater = null;
if (registrationProfile.getSupportedGrantTypes() != null && !registrationProfile.getSupportedGrantTypes().isEmpty()) { // if (registrationProfile.getSupportedGrantTypes() != null && !registrationProfile.getSupportedGrantTypes().isEmpty()) {
applicationGrantTypeUpdater = new ApplicationGrantTypeUpdater(); // applicationGrantTypeUpdater = new ApplicationGrantTypeUpdater();
applicationGrantTypeUpdater.setSupportedGrantTypes(registrationProfile.getSupportedGrantTypes()); // applicationGrantTypeUpdater.setSupportedGrantTypes(registrationProfile.getSupportedGrantTypes());
//
} else if (StringUtils.isNotEmpty(registrationProfile.getCallbackUrl())) { // } else if (StringUtils.isNotEmpty(registrationProfile.getCallbackUrl())) {
return Response.status(Response.Status.BAD_REQUEST).entity("Callback URL should be Empty when" + // 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 " + // " request does not contain supported grant types to update grant types of the " +
"application." // "application."
).build(); // ).build();
} // }
ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
applicationName, registrationProfile.getTags(), applicationName, registrationProfile.getTags(),
ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username,
registrationProfile.isAllowedToAllDomains(), validityPeriod, 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(); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build();
} }
} }
@ -138,7 +139,8 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
applicationName, registrationProfile.getTags(), applicationName, registrationProfile.getTags(),
ApiApplicationConstants.DEFAULT_TOKEN_TYPE, registrationProfile.getUsername(), ApiApplicationConstants.DEFAULT_TOKEN_TYPE, registrationProfile.getUsername(),
registrationProfile.isAllowedToAllDomains(), validityPeriod, 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(); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build();
} }
} catch (APIManagerException e) { } catch (APIManagerException e) {

@ -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.apimgt.extension.rest.api.dto.TokenInfo;
import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo; 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. * 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 keyType, String username, boolean isAllowedAllDomains,
// String validityTime) throws APIManagerException; // String validityTime) throws APIManagerException;
ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags, // ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags,
String keyType, String username, // String keyType, String username,
boolean isAllowedAllDomains, // boolean isAllowedAllDomains,
String validityTime, String password, // String validityTime, String password,
ApplicationGrantTypeUpdater applicationGrantTypeUpdater, // ArrayList<String> supportedGrantTypes,
boolean isMappingRequired) throws APIManagerException; // String callbackUrl,
// boolean isMappingRequired) throws APIManagerException;
ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags, ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags,
String keyType, String keyType, String username,
boolean isAllowedAllDomains, boolean isAllowedAllDomains,
String validityTime, TokenInfo tokenInfo, String validityTime,
ApplicationGrantTypeUpdater applicationGrantTypeUpdater, String password, String accessToken,
ArrayList<String> supportedGrantTypes,
String callbackUrl,
boolean isMappingRequired) throws APIManagerException; boolean isMappingRequired) throws APIManagerException;
// /** // /**

@ -120,32 +120,44 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
// tokenInfo.setAccessToken(accessToken); // tokenInfo.setAccessToken(accessToken);
// return generateAndRetrieveApplicationKeys(applicationName, tags, keyType, isAllowedAllDomains, validityTime, // return generateAndRetrieveApplicationKeys(applicationName, tags, keyType, isAllowedAllDomains, validityTime,
// tokenInfo, null, false); // tokenInfo, null, false);
// }
// @Override
// public synchronized ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags,
// String keyType, String username,
// boolean isAllowedAllDomains,
// String validityTime, String password,
// ArrayList<String> 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 @Override
public synchronized ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags, public synchronized ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags,
String keyType, String username, String keyType, String username,
boolean isAllowedAllDomains, boolean isAllowedAllDomains,
String validityTime, String password, String validityTime,
ApplicationGrantTypeUpdater applicationGrantTypeUpdater, String password, String accessToken,
ArrayList<String> supportedGrantTypes,
String callbackUrl,
boolean isMappingRequired) boolean isMappingRequired)
throws APIManagerException { throws APIManagerException {
ApiApplicationInfo applicationInfo = getApplicationInfo(username, password);
TokenInfo tokenInfo = new TokenInfo(); TokenInfo tokenInfo = new TokenInfo();
tokenInfo.setApiApplicationInfo(applicationInfo); if (StringUtils.isEmpty(accessToken)) {
tokenInfo.setAccessToken(null); ApiApplicationInfo applicationInfo = getApplicationInfo(username, password);
return generateAndRetrieveApplicationKeys(applicationName, tags, keyType, isAllowedAllDomains, validityTime, tokenInfo.setApiApplicationInfo(applicationInfo);
tokenInfo, applicationGrantTypeUpdater, isMappingRequired); } else {
} tokenInfo.setAccessToken(accessToken);
}
@Override
public synchronized ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags,
String keyType,
boolean isAllowedAllDomains,
String validityTime, TokenInfo tokenInfo,
ApplicationGrantTypeUpdater applicationGrantTypeUpdater,
boolean isMappingRequired) throws APIManagerException {
ConsumerRESTAPIServices consumerRESTAPIServices = ConsumerRESTAPIServices consumerRESTAPIServices =
APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices(); APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices();
@ -171,7 +183,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
consumerRESTAPIServices.getAllApplications(tokenInfo, applicationName); consumerRESTAPIServices.getAllApplications(tokenInfo, applicationName);
if (applications.length == 0) { if (applications.length == 0) {
return handleNewAPIApplication(applicationName, uniqueApiList, tokenInfo, keyType, return handleNewAPIApplication(applicationName, uniqueApiList, tokenInfo, keyType,
validityTime, applicationGrantTypeUpdater, isMappingRequired); validityTime, supportedGrantTypes, callbackUrl, isMappingRequired);
} else { } else {
if (applications.length == 1) { if (applications.length == 1) {
Optional<io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application> applicationOpt = Optional<io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application> applicationOpt =
@ -185,7 +197,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
// Todo add a comment // Todo add a comment
consumerRESTAPIServices.deleteApplication(tokenInfo, application.getApplicationId()); consumerRESTAPIServices.deleteApplication(tokenInfo, application.getApplicationId());
return handleNewAPIApplication(applicationName, uniqueApiList, tokenInfo, keyType, return handleNewAPIApplication(applicationName, uniqueApiList, tokenInfo, keyType,
validityTime, applicationGrantTypeUpdater, isMappingRequired); validityTime, supportedGrantTypes, callbackUrl, isMappingRequired);
} else { } else {
Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(tokenInfo, application.getApplicationId()); Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(tokenInfo, application.getApplicationId());
for (Subscription subscription : subscriptions) { for (Subscription subscription : subscriptions) {
@ -239,7 +251,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
private ApiApplicationKey handleNewAPIApplication(String applicationName, List<APIInfo> uniqueApiList, private ApiApplicationKey handleNewAPIApplication(String applicationName, List<APIInfo> uniqueApiList,
TokenInfo tokenInfo, String keyType, String validityTime, TokenInfo tokenInfo, String keyType, String validityTime,
ApplicationGrantTypeUpdater applicationGrantTypeUpdater, ArrayList<String> supportedGrantTypes, String callbackUrl,
boolean isMappingRequired) throws APIManagerException { boolean isMappingRequired) throws APIManagerException {
ConsumerRESTAPIServices consumerRESTAPIServices = ConsumerRESTAPIServices consumerRESTAPIServices =
APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices(); APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices();
@ -264,9 +276,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
tokenInfo.setApiApplicationInfo(getApplicationInfo(null, null)); tokenInfo.setApiApplicationInfo(getApplicationInfo(null, null));
ApplicationKey applicationKey; ApplicationKey applicationKey;
if (isMappingRequired) {
}
if (isMappingRequired) { if (isMappingRequired) {
// If we need to get opaque token instead of the JWT token, we have to do the mapping. Therefore,, if // 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, // ApplicationKey updateGrantType(TokenInfo tokenInfo, String applicationId, String keyMapId, String keyManager,
// String supportedGrantTypes, String callbackUrl) // String supportedGrantTypes, String callbackUrl)
if (applicationGrantTypeUpdater != null) { if (supportedGrantTypes != null || StringUtils.isNotEmpty(callbackUrl)) {
applicationKey = consumerRESTAPIServices.updateGrantType(tokenInfo, application.getApplicationId(), applicationKey = consumerRESTAPIServices.updateGrantType(tokenInfo, application.getApplicationId(),
applicationKey.getKeyMappingId(), keyManager.getName(), applicationKey.getKeyMappingId(), keyManager.getName(), supportedGrantTypes, callbackUrl);
applicationGrantTypeUpdater.getSupportedGrantTypes(),
applicationGrantTypeUpdater.getCallbackUrl());
} }
@ -671,7 +679,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
registrationProfile.getTags(), tokenType, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() registrationProfile.getTags(), tokenType, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getRealmConfiguration().getAdminUserName(), .getRealmConfiguration().getAdminUserName(),
registrationProfile.isAllowedToAllDomains(), validityPeriod, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() registrationProfile.isAllowedToAllDomains(), validityPeriod, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getRealmConfiguration().getAdminPassword(), null, false); .getRealmConfiguration().getAdminPassword(), null, null, null, false);
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }

@ -66,7 +66,7 @@ public class OAuthUtils {
registrationProfile.getTags(), Constants.ApplicationInstall.DEFAULT_TOKEN_TYPE, registrationProfile.getTags(), Constants.ApplicationInstall.DEFAULT_TOKEN_TYPE,
username, registrationProfile.isAllowedToAllDomains(), username, registrationProfile.isAllowedToAllDomains(),
Constants.ApplicationInstall.DEFAULT_VALIDITY_PERIOD, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() Constants.ApplicationInstall.DEFAULT_VALIDITY_PERIOD, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getRealmConfiguration().getAdminPassword(), null, false); .getRealmConfiguration().getAdminPassword(), null, null, null, false);
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }

@ -828,11 +828,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
"perm:users:send-invitation"); "perm:users:send-invitation");
APIManagementProviderService apiManagementProviderService = DeviceMgtAPIUtils.getAPIManagementService(); APIManagementProviderService apiManagementProviderService = DeviceMgtAPIUtils.getAPIManagementService();
TokenInfo tokenInfo = new TokenInfo(); // TokenInfo tokenInfo = new TokenInfo();
tokenInfo.setAccessToken(accessTokenInfo.getAccessToken()); // tokenInfo.setAccessToken(accessTokenInfo.getAccessToken());
apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(applicationName, apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(applicationName,
new String[] {"device_management"}, "PRODUCTION", false, String.valueOf(validityTime), new String[] {"device_management"}, "PRODUCTION", null, false, String.valueOf(validityTime),
tokenInfo, null, true); null, accessTokenInfo.getAccessToken(), null, null,true);
} catch (JWTClientException e) { } catch (JWTClientException e) {
String msg = "Error while generating an application tokens for Tenant Admin."; String msg = "Error while generating an application tokens for Tenant Admin.";

Loading…
Cancel
Save