Add improvements to app registering logic

pull/305/head
tcdlpds 1 year ago
parent 088642e7ce
commit 500fa0df98

@ -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) {

@ -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<String> 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<String> supportedGrantTypes,
String callbackUrl,
boolean isMappingRequired) throws APIManagerException;
// /**

@ -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<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
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<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, 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<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
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<APIInfo> uniqueApiList,
TokenInfo tokenInfo, String keyType, String validityTime,
ApplicationGrantTypeUpdater applicationGrantTypeUpdater,
ArrayList<String> 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();
}

@ -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();
}

@ -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.";

Loading…
Cancel
Save