Get ApiApplicationInfo and access token into single object

Co-authored-by: Pasindu Rupasinghe <pasindu@entgra.io>
Co-committed-by: Pasindu Rupasinghe <pasindu@entgra.io>
add_scope_mdm
Pasindu Rupasinghe 1 year ago committed by Lasantha Dharmakeerthi
parent 438f06ef45
commit cb2fb990e7

@ -25,6 +25,7 @@ import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManag
import io.entgra.device.mgt.core.apimgt.application.extension.internal.APIApplicationManagerExtensionDataHolder;
import io.entgra.device.mgt.core.apimgt.application.extension.util.APIManagerUtil;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.KeyManager;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.TokenInfo;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyAlreadyExistsException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
@ -119,145 +120,38 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
String keyType,
boolean isAllowedAllDomains,
String validityTime, String accessToken) throws APIManagerException {
ConsumerRESTAPIServices consumerRESTAPIServices =
APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices();
try {
List<APIInfo> uniqueApiList = new ArrayList<>();
TokenInfo tokenInfo = new TokenInfo();
tokenInfo.setApiApplicationInfo(null);
tokenInfo.setAccessToken(accessToken);
Map<String, String> headerParams = new HashMap<>();
if (!"carbon.super".equals(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true))) {
headerParams.put("X-WSO2-Tenant", "carbon.super");
return generateAndRetrieveApplicationKeys(applicationName, tags ,keyType, null, isAllowedAllDomains, validityTime, tokenInfo);
}
for (String tag : tags) {
Map<String, String> queryParams = new HashMap<>();
queryParams.put("tag", tag);
APIInfo[] apiInfos = consumerRESTAPIServices.getAllApis(null, accessToken, queryParams, headerParams);
@Override
public synchronized ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags,
String keyType, String username,
boolean isAllowedAllDomains,
String validityTime, String password)
throws APIManagerException {
uniqueApiList.addAll(List.of(apiInfos));
Set<APIInfo> taggedAPISet = new HashSet<>(uniqueApiList);
uniqueApiList.clear();
uniqueApiList.addAll(taggedAPISet);
}
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application[] applications =
consumerRESTAPIServices.getAllApplications(null, accessToken, applicationName);
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application;
boolean isNewApplication = false;
if (applications.length == 0) {
isNewApplication = true;
application = new io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application();
application.setName(applicationName);
application = consumerRESTAPIServices.createApplication(null, accessToken, application);
addSubscriptions(application, uniqueApiList, accessToken);
} else {
if (applications.length == 1) {
Optional<io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application> applicationOpt =
Arrays.stream(applications).findFirst();
application = applicationOpt.get();
Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(null, accessToken,
application.getApplicationId());
Arrays.stream(subscriptions).map(Subscription::getApiInfo).forEachOrdered(uniqueApiList::remove);
addSubscriptions(application, uniqueApiList, accessToken);
} else {
String msg = "Found more than one application for application name: " + applicationName;
log.error(msg);
throw new APIManagerException(msg);
}
}
ApiApplicationInfo applicationInfo = getApplicationInfo(username, password);
MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService();
if (isNewApplication) {
KeyManager[] keyManagers = consumerRESTAPIServices.getAllKeyManagers(null, accessToken);
KeyManager keyManager;
if (keyManagers.length == 1) {
keyManager = keyManagers[0];
} else {
String msg =
"Found invalid number of key managers. No of key managers found from the APIM: " + keyManagers.length;
throw new APIManagerException(msg);
}
ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(null, accessToken,
application.getApplicationId(), keyManager.getName(), keyType, validityTime);
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
TokenInfo tokenInfo = new TokenInfo();
tokenInfo.setApiApplicationInfo(applicationInfo);
tokenInfo.setAccessToken(null);
Metadata metaData = new Metadata();
metaData.setMetaKey(applicationName);
String metaValue = application.getApplicationId() + ":" + applicationKey.getKeyMappingId();
metaData.setMetaValue(metaValue);
try {
metadataManagementService.createMetadata(metaData);
return apiApplicationKey;
} catch (MetadataManagementException e) {
String msg = "Error occurred while creating the meta data entry for mata key: " + applicationName;
log.error(msg, e);
throw new APIManagerException(msg, e);
} catch (MetadataKeyAlreadyExistsException e) {
String msg = "Found duplicate meta value entry for meta key: " + applicationName;
log.error(msg, e);
throw new APIManagerException(msg, e);
}
} else {
try {
Metadata metaData = metadataManagementService.retrieveMetadata(applicationName);
if (metaData == null) {
String msg = "Couldn't find application key data from meta data mgt service. Meta key: "
+ applicationName;
log.error(msg);
throw new APIManagerException(msg);
}
String[] metaValues = metaData.getMetaValue().split(":");
if (metaValues.length != 2) {
String msg = "Found invalid Meta value for meta key: " + applicationName + ". Meta Value: "
+ metaData.getMetaValue();
log.error(msg);
throw new APIManagerException(msg);
}
String applicationId = metaValues[0];
String keyMappingId = metaValues[1];
ApplicationKey applicationKey = consumerRESTAPIServices.getKeyDetails(null, accessToken, applicationId,
keyMappingId);
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
return apiApplicationKey;
} catch (MetadataManagementException e) {
String msg = "Error occurred while getting meta data for meta key: " + applicationName;
log.error(msg, e);
throw new APIManagerException(msg, e);
}
}
} catch (APIServicesException e) {
String msg = "Error occurred while processing the response of APIM REST endpoints.";
log.error(msg, e);
throw new APIManagerException(msg, e);
} catch (BadRequestException e) {
String msg = "Provided incorrect payload when invoking APIM REST endpoints.";
log.error(msg, e);
throw new APIManagerException(msg, e);
} catch (UnexpectedResponseException e) {
String msg = "Error occurred while invoking APIM REST endpoints.";
log.error(msg, e);
throw new APIManagerException(msg, e);
}
return generateAndRetrieveApplicationKeys(applicationName, tags, keyType, username,isAllowedAllDomains, validityTime, tokenInfo);
}
@Override
public synchronized ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags,
private ApiApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String[] tags,
String keyType, String username,
boolean isAllowedAllDomains,
String validityTime, String password)
throws APIManagerException {
String validityTime, TokenInfo tokenInfo) throws APIManagerException {
ConsumerRESTAPIServices consumerRESTAPIServices =
APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices();
ApiApplicationInfo applicationInfo = getApplicationInfo(username, password);
try {
List<APIInfo> uniqueApiList = new ArrayList<>();
@ -270,7 +164,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
Map<String, String> queryParams = new HashMap<>();
queryParams.put("tag", tag);
APIInfo[] apiInfos = consumerRESTAPIServices.getAllApis(applicationInfo, null, queryParams, headerParams);
APIInfo[] apiInfos = consumerRESTAPIServices.getAllApis(tokenInfo, queryParams, headerParams);
uniqueApiList.addAll(List.of(apiInfos));
Set<APIInfo> taggedAPISet = new HashSet<>(uniqueApiList);
@ -279,24 +173,23 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
}
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application[] applications =
consumerRESTAPIServices.getAllApplications(applicationInfo, null, applicationName);
consumerRESTAPIServices.getAllApplications(tokenInfo, applicationName);
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application;
boolean isNewApplication = false;
if (applications.length == 0) {
isNewApplication = true;
application = new io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application();
application.setName(applicationName);
application = consumerRESTAPIServices.createApplication(applicationInfo, null, application);
addSubscriptions(application, uniqueApiList, applicationInfo);
application = consumerRESTAPIServices.createApplication(tokenInfo, application);
addSubscriptions(application, uniqueApiList, tokenInfo);
} else {
if (applications.length == 1) {
Optional<io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application> applicationOpt =
Arrays.stream(applications).findFirst();
application = applicationOpt.get();
Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(applicationInfo, null,
application.getApplicationId());
Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(tokenInfo, application.getApplicationId());
Arrays.stream(subscriptions).map(Subscription::getApiInfo).forEachOrdered(uniqueApiList::remove);
addSubscriptions(application, uniqueApiList, applicationInfo);
addSubscriptions(application, uniqueApiList, tokenInfo);
} else {
String msg = "Found more than one application for application name: " + applicationName;
log.error(msg);
@ -306,7 +199,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService();
if (isNewApplication) {
KeyManager[] keyManagers = consumerRESTAPIServices.getAllKeyManagers(applicationInfo, null);
KeyManager[] keyManagers = consumerRESTAPIServices.getAllKeyManagers(tokenInfo);
KeyManager keyManager;
if (keyManagers.length == 1) {
keyManager = keyManagers[0];
@ -315,8 +208,8 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
"Found invalid number of key managers. No of key managers found from the APIM: " + keyManagers.length;
throw new APIManagerException(msg);
}
ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(applicationInfo, null,
application.getApplicationId(), keyManager.getName(), keyType, validityTime);
ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(tokenInfo, application.getApplicationId(),
keyManager.getName(), keyType, validityTime);
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
@ -355,7 +248,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
}
String applicationId = metaValues[0];
String keyMappingId = metaValues[1];
ApplicationKey applicationKey = consumerRESTAPIServices.getKeyDetails(applicationInfo, null, applicationId, keyMappingId);
ApplicationKey applicationKey = consumerRESTAPIServices.getKeyDetails(tokenInfo, applicationId, keyMappingId);
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
@ -387,7 +280,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
*
* @param application {@link io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application}
* @param apiInfos {@link List<APIInfo>}
* @param apiApplicationInfo {@link ApiApplicationInfo}
* @param tokenInfo {@link TokenInfo}
*
* @throws BadRequestException if incorrect data provided to call subscribing REST API.
* @throws UnexpectedResponseException if error occurred while processing the subscribing REST API.
@ -395,25 +288,9 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
*/
private void addSubscriptions(
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application,
List<APIInfo> apiInfos, ApiApplicationInfo apiApplicationInfo)
List<APIInfo> apiInfos, TokenInfo tokenInfo)
throws BadRequestException, UnexpectedResponseException, APIServicesException {
ConsumerRESTAPIServices consumerRESTAPIServices =
APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices();
List<Subscription> subscriptionList = new ArrayList<>();
apiInfos.forEach(apiInfo -> {
Subscription subscription = new Subscription();
subscription.setApiId(apiInfo.getId());
subscription.setApplicationId(application.getApplicationId());
subscriptionList.add(subscription);
});
consumerRESTAPIServices.createSubscriptions(apiApplicationInfo, null, subscriptionList);
}
private void addSubscriptions(
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application,
List<APIInfo> apiInfos, String accessToken)
throws BadRequestException, UnexpectedResponseException, APIServicesException {
ConsumerRESTAPIServices consumerRESTAPIServices =
APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices();
@ -424,7 +301,8 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
subscription.setApplicationId(application.getApplicationId());
subscriptionList.add(subscription);
});
consumerRESTAPIServices.createSubscriptions(null, accessToken, subscriptionList);
consumerRESTAPIServices.createSubscriptions(tokenInfo, subscriptionList);
}
/**
@ -766,7 +644,7 @@ Otherwise, Generate Application Keys and return them
throw new APIManagerException(errorMsg, e);
}
ApiApplicationInfo applicationInfo = null;
ApiApplicationInfo applicationInfo = new ApiApplicationInfo();
applicationInfo.setClientId(apiApplicationKey.getClientId());
applicationInfo.setClientSecret(apiApplicationKey.getClientSecret());
applicationInfo.setAccess_token(accessTokenInfo.getAccess_token());

@ -19,7 +19,7 @@
package io.entgra.device.mgt.core.apimgt.extension.rest.api;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.*;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.ApiApplicationInfo;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.TokenInfo;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
@ -28,39 +28,36 @@ import java.util.List;
import java.util.Map;
public interface ConsumerRESTAPIServices {
Application[] getAllApplications(ApiApplicationInfo applicationInfo, String accessToken, String appName)
Application[] getAllApplications(TokenInfo tokenInfo, String appName)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
Application getDetailsOfAnApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
Application getDetailsOfAnApplication(TokenInfo tokenInfo, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
Application createApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, Application application)
Application createApplication(TokenInfo tokenInfo, Application application)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
Boolean deleteApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
Boolean deleteApplication(TokenInfo tokenInfo, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
Subscription[] getAllSubscriptions(TokenInfo tokenInfo, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
APIInfo[] getAllApis(ApiApplicationInfo apiApplicationInfo, String accessToken, Map<String, String> queryParams,
Map<String, String> headerParams)
APIInfo[] getAllApis(TokenInfo tokenInfo, Map<String, String> queryParams, Map<String, String> headerParams)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
Subscription createSubscription(ApiApplicationInfo apiApplicationInfo, String accessToken, Subscription subscriptions)
Subscription createSubscription(TokenInfo tokenInfo, Subscription subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, String accessToken,
List<Subscription> subscriptions)
Subscription[] createSubscriptions(TokenInfo tokenInfo, List<Subscription> subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId,
String keyManager, String validityTime, String keyType)
ApplicationKey generateApplicationKeys(TokenInfo tokenInfo, String applicationId, String keyManager, String validityTime, String keyType)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
ApplicationKey getKeyDetails(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId, String keyMapId)
ApplicationKey getKeyDetails(TokenInfo tokenInfo, String applicationId, String keyMapId)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo, String accessToken)
KeyManager[] getAllKeyManagers(TokenInfo tokenInfo)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
}

@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.*;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.ApiApplicationInfo;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.TokenInfo;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
@ -50,10 +51,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ Constants.COLON + port;
@Override
public Application[] getAllApplications(ApiApplicationInfo apiApplicationInfo, String accessToken, String appName)
public Application[] getAllApplications(TokenInfo tokenInfo, String appName)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
String getAllApplicationsUrl = endPointPrefix + Constants.APPLICATIONS_API + "?query=" + appName;
Request.Builder builder = new Request.Builder();
@ -63,7 +65,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
builder.get();
Request request = builder.build();
@ -81,7 +83,9 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return getAllApplications(refreshedApiApplicationInfo, null, appName);
tokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
tokenInfo.setAccessToken(null);
return getAllApplications(tokenInfo, appName);
} else {
String msg = "Invalid access token. Unauthorized request";
log.error(msg);
@ -103,10 +107,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public Application getDetailsOfAnApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
public Application getDetailsOfAnApplication(TokenInfo tokenInfo, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
String getDetailsOfAPPUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId;
Request.Builder builder = new Request.Builder();
@ -116,7 +121,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
builder.get();
Request request = builder.build();
@ -133,7 +138,10 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return getDetailsOfAnApplication(refreshedApiApplicationInfo, null, applicationId);
TokenInfo refreshedTokenInfo = new TokenInfo();
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
refreshedTokenInfo.setAccessToken(null);
return getDetailsOfAnApplication(refreshedTokenInfo, applicationId);
} else {
String msg = "Invalid access token. Unauthorized request";
log.error(msg);
@ -155,10 +163,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public Application createApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, Application application)
public Application createApplication(TokenInfo tokenInfo, Application application)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API;
String applicationInfo = "{\n" +
@ -179,7 +188,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
builder.post(requestBody);
Request request = builder.build();
@ -196,7 +205,10 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return createApplication(refreshedApiApplicationInfo, null, application);
TokenInfo refreshedTokenInfo = new TokenInfo();
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
refreshedTokenInfo.setAccessToken(null);
return createApplication(refreshedTokenInfo, application);
} else {
String msg = "Invalid access token. Unauthorized request";
log.error(msg);
@ -218,10 +230,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public Boolean deleteApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
public Boolean deleteApplication(TokenInfo tokenInfo, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
String deleteScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId;
Request.Builder builder = new Request.Builder();
@ -231,7 +244,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
builder.delete();
Request request = builder.build();
@ -248,7 +261,10 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return deleteApplication(refreshedApiApplicationInfo, null, applicationId);
TokenInfo refreshedTokenInfo = new TokenInfo();
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
refreshedTokenInfo.setAccessToken(null);
return deleteApplication(refreshedTokenInfo, applicationId);
} else {
String msg = "Invalid access token. Unauthorized request";
log.error(msg);
@ -270,10 +286,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
public Subscription[] getAllSubscriptions(TokenInfo tokenInfo, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "?applicationId=" + applicationId;
Request.Builder builder = new Request.Builder();
@ -283,7 +300,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
builder.get();
Request request = builder.build();
@ -299,9 +316,12 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo rehreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return getAllSubscriptions(rehreshedApiApplicationInfo, null, applicationId);
TokenInfo refreshedTokenInfo = new TokenInfo();
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
refreshedTokenInfo.setAccessToken(null);
return getAllSubscriptions(refreshedTokenInfo, applicationId);
} else {
String msg = "Invalid access token. Unauthorized request";
log.error(msg);
@ -323,11 +343,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public APIInfo[] getAllApis(ApiApplicationInfo apiApplicationInfo, String accessToken, Map<String, String> queryParams,
Map<String, String> headerParams)
public APIInfo[] getAllApis(TokenInfo tokenInfo, Map<String, String> queryParams, Map<String, String> headerParams)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
StringBuilder getAPIsURL = new StringBuilder(endPointPrefix + Constants.DEV_PORTAL_API);
for (Map.Entry<String, String> query : queryParams.entrySet()) {
@ -341,7 +361,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
for (Map.Entry<String, String> header : headerParams.entrySet()) {
builder.addHeader(header.getKey(), header.getValue());
@ -360,9 +380,12 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo rehreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return getAllApis(rehreshedApiApplicationInfo, null, queryParams, headerParams);
TokenInfo refreshedTokenInfo = new TokenInfo();
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
refreshedTokenInfo.setAccessToken(null);
return getAllApis(refreshedTokenInfo, queryParams, headerParams);
} else {
String msg = "Invalid access token. Unauthorized request";
log.error(msg);
@ -384,10 +407,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public Subscription createSubscription(ApiApplicationInfo apiApplicationInfo, String accessToken, Subscription subscriptions)
public Subscription createSubscription(TokenInfo tokenInfo, Subscription subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
String createSubscriptionUrl = endPointPrefix + Constants.SUBSCRIPTION_API;
String subscriptionObject = "{\n" +
@ -406,7 +430,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
builder.post(requestBody);
Request request = builder.build();
@ -423,7 +447,10 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return createSubscription(refreshedApiApplicationInfo, null, subscriptions);
TokenInfo refreshedTokenInfo = new TokenInfo();
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
refreshedTokenInfo.setAccessToken(null);
return createSubscription(refreshedTokenInfo, subscriptions);
} else {
String msg = "Invalid access token. Unauthorized request";
log.error(msg);
@ -445,11 +472,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, String accessToken,
List<Subscription> subscriptions)
public Subscription[] createSubscriptions(TokenInfo tokenInfo, List<Subscription> subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
String createSubscriptionsUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "/multiple";
String subscriptionsList = gson.toJson(subscriptions);
@ -462,7 +489,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
builder.post(requestBody);
Request request = builder.build();
@ -480,7 +507,10 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return createSubscriptions(refreshedApiApplicationInfo, null, subscriptions);
TokenInfo refreshedTokenInfo = new TokenInfo();
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
refreshedTokenInfo.setAccessToken(null);
return createSubscriptions(refreshedTokenInfo, subscriptions);
} else {
String msg = "Invalid access token. Unauthorized request";
log.error(msg);
@ -502,11 +532,12 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId,
String keyManager, String validityTime, String keyType)
public ApplicationKey generateApplicationKeys(TokenInfo tokenInfo, String applicationId, String keyManager,
String validityTime, String keyType)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
String generateApplicationKeysUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH +
applicationId + "/generate-keys";
@ -535,7 +566,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
builder.post(requestBody);
Request request = builder.build();
@ -550,9 +581,12 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return generateApplicationKeys(refreshedApiApplicationKey, null, applicationId, keyManager, validityTime, keyType);
TokenInfo refreshedTokenInfo = new TokenInfo();
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
refreshedTokenInfo.setAccessToken(null);
return generateApplicationKeys(refreshedTokenInfo, applicationId, keyManager, validityTime, keyType);
} else {
String msg = "Invalid access token. Unauthorized request";
log.error(msg);
@ -574,10 +608,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public ApplicationKey getKeyDetails(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId, String keyMapId)
public ApplicationKey getKeyDetails(TokenInfo tokenInfo, String applicationId, String keyMapId)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
String getKeyDetails = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId + "/oauth-keys/" + keyMapId;
Request.Builder builder = new Request.Builder();
@ -587,7 +622,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
builder.get();
Request request = builder.build();
@ -602,9 +637,12 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return getKeyDetails(refreshedApiApplicationKey, null, applicationId, keyMapId);
TokenInfo refreshedTokenInfo = new TokenInfo();
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
refreshedTokenInfo.setAccessToken(null);
return getKeyDetails(refreshedTokenInfo, applicationId, keyMapId);
} else {
String msg = "Invalid access token. Unauthorized request";
log.error(msg);
@ -626,10 +664,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo, String accessToken)
public KeyManager[] getAllKeyManagers(TokenInfo tokenInfo)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
boolean token = isTokenNull(apiApplicationInfo, accessToken);
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
String getAllKeyManagersUrl = endPointPrefix + Constants.KEY_MANAGERS_API;
Request.Builder builder = new Request.Builder();
@ -639,7 +678,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
+ tokenInfo.getAccessToken());
}
builder.get();
Request request = builder.build();
@ -657,7 +696,10 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return getAllKeyManagers(refreshedApiApplicationInfo, null);
TokenInfo refreshedTokenInfo = new TokenInfo();
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
refreshedTokenInfo.setAccessToken(null);
return getAllKeyManagers(refreshedTokenInfo);
} else {
String msg = "Invalid or null access token";
log.error(msg);

@ -0,0 +1,44 @@
/*
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.device.mgt.core.apimgt.extension.rest.api.dto;
/**
* This holds the API application information and access token for REST APIS cals.
*/
public class TokenInfo {
private ApiApplicationInfo apiApplicationInfo;
private String accessToken;
public ApiApplicationInfo getApiApplicationInfo() {
return apiApplicationInfo;
}
public void setApiApplicationInfo(ApiApplicationInfo apiApplicationInfo) {
this.apiApplicationInfo = apiApplicationInfo;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
}

@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.apimgt.keymgt.extension.service;
import com.google.gson.Gson;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.TokenInfo;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
import io.entgra.device.mgt.core.apimgt.keymgt.extension.*;
@ -431,11 +432,14 @@ public class KeyMgtServiceImpl implements KeyMgtService {
*/
private Application getApplication(String applicationName, String accessToken) throws KeyMgtException {
TokenInfo tokenInfo = new TokenInfo();
tokenInfo.setApiApplicationInfo(null);
tokenInfo.setAccessToken(accessToken);
try {
ConsumerRESTAPIServices consumerRESTAPIServices =
KeyMgtDataHolder.getInstance().getConsumerRESTAPIServices();
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application[] applications =
consumerRESTAPIServices.getAllApplications(null, accessToken, applicationName);
consumerRESTAPIServices.getAllApplications(tokenInfo, applicationName);
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application applicationFromRestCall;
if (applications.length == 1) {

Loading…
Cancel
Save