Get API Application registration to single method and fix generateApplicationKeys method

apim420
commit 49fafb3b37

@ -21,8 +21,10 @@ package io.entgra.device.mgt.core.apimgt.application.extension;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices; import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIInfo; import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIInfo;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.ApplicationKey;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Subscription; import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Subscription;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey; import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.ApiApplicationInfo;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException; 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.BadRequestException;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
@ -111,21 +113,10 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
ConsumerRESTAPIServices consumerRESTAPIServices = ConsumerRESTAPIServices consumerRESTAPIServices =
APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices(); APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices();
APIApplicationKey apiApplicationKey; ApiApplicationInfo applicationInfo = applicationInfo(apiApplicationServices, username, password);
io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo accessTokenInfo;
try {
apiApplicationKey = apiApplicationServices.generateAndRetrieveApplicationKeys(username, password);
accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication(
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
} catch (APIServicesException e) {
String errorMsg = "Error occurred while generating the API application";
log.error(errorMsg, e);
throw new APIManagerException(errorMsg, e);
}
try { try {
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application[] applications = io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application[] applications =
consumerRESTAPIServices.getAllApplications(apiApplicationKey, accessTokenInfo, applicationName); consumerRESTAPIServices.getAllApplications(applicationInfo, applicationName);
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application = null; io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application = null;
List<APIInfo> uniqueApiList = new ArrayList<>(); List<APIInfo> uniqueApiList = new ArrayList<>();
@ -140,7 +131,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
if (!"carbon.super".equals(tenantDomain)) { if (!"carbon.super".equals(tenantDomain)) {
headerParams.put("X-WSO2-Tenant", "carbon.super"); headerParams.put("X-WSO2-Tenant", "carbon.super");
} }
apiInfos = consumerRESTAPIServices.getAllApis(apiApplicationKey, accessTokenInfo, queryParams, headerParams); apiInfos = consumerRESTAPIServices.getAllApis(applicationInfo, queryParams, headerParams);
uniqueApiList.addAll(List.of(apiInfos)); uniqueApiList.addAll(List.of(apiInfos));
Set<APIInfo> taggedAPISet = new HashSet<>(uniqueApiList); Set<APIInfo> taggedAPISet = new HashSet<>(uniqueApiList);
@ -151,7 +142,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
if (applications.length == 0) { if (applications.length == 0) {
application = new io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application(); application = new io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application();
application.setName(applicationName); application.setName(applicationName);
application = consumerRESTAPIServices.createApplication(apiApplicationKey, accessTokenInfo, application); application = consumerRESTAPIServices.createApplication(applicationInfo, application);
List<Subscription> subscriptions = new ArrayList<>(); List<Subscription> subscriptions = new ArrayList<>();
for (APIInfo apiInfo : uniqueApiList) { for (APIInfo apiInfo : uniqueApiList) {
@ -160,14 +151,13 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
subscription.setApplicationId(application.getApplicationId()); subscription.setApplicationId(application.getApplicationId());
subscriptions.add(subscription); subscriptions.add(subscription);
} }
consumerRESTAPIServices.createSubscriptions(apiApplicationKey, accessTokenInfo, subscriptions); consumerRESTAPIServices.createSubscriptions(applicationInfo, subscriptions);
} 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 =
Arrays.stream(applications).findFirst(); Arrays.stream(applications).findFirst();
application = applicationOpt.get(); application = applicationOpt.get();
Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(apiApplicationKey, accessTokenInfo, Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(applicationInfo, application.getApplicationId());
application.getApplicationId());
for (Subscription subscription : subscriptions) { for (Subscription subscription : subscriptions) {
if (uniqueApiList.contains(subscription.getApiInfo())) { if (uniqueApiList.contains(subscription.getApiInfo())) {
uniqueApiList.remove(subscription.getApiInfo()); uniqueApiList.remove(subscription.getApiInfo());
@ -184,7 +174,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
subscription.setApplicationId(application.getApplicationId()); subscription.setApplicationId(application.getApplicationId());
subscriptionList.add(subscription); subscriptionList.add(subscription);
} }
consumerRESTAPIServices.createSubscriptions(apiApplicationKey, accessTokenInfo, subscriptionList); consumerRESTAPIServices.createSubscriptions(applicationInfo, subscriptionList);
} else { } else {
String msg = "Found more than one application for application name: " + applicationName; String msg = "Found more than one application for application name: " + applicationName;
log.error(msg); log.error(msg);
@ -199,9 +189,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
} else{ } else{
//todo this method has to br modified and return different object, this is not mapped with the //todo this method has to br modified and return different object, this is not mapped with the
// response. // response.
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIKey apiKey = ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(applicationInfo, application);
consumerRESTAPIServices.generateApplicationKeys(apiApplicationKey, accessTokenInfo,
application.getApplicationId());
return null; return null;
} }
} else{ } else{
@ -560,4 +548,32 @@ Otherwise, Generate Application Keys and return them
} }
return info; return info;
} }
private ApiApplicationInfo applicationInfo(APIApplicationServices apiApplicationServices, String username, String password)
throws APIManagerException {
APIApplicationKey apiApplicationKey;
io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo accessTokenInfo;
try {
if (username == null && password == null) {
apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials();
} else {
apiApplicationKey = apiApplicationServices.generateAndRetrieveApplicationKeys(username, password);
}
accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication(
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
} catch (APIServicesException e) {
String errorMsg = "Error occurred while generating the API application";
log.error(errorMsg, e);
throw new APIManagerException(errorMsg, e);
}
ApiApplicationInfo applicationInfo = null;
applicationInfo.setClientId(apiApplicationKey.getClientId());
applicationInfo.setClientSecret(apiApplicationKey.getClientSecret());
applicationInfo.setAccess_token(accessTokenInfo.getAccess_token());
applicationInfo.setRefresh_token(accessTokenInfo.getRefresh_token());
return applicationInfo;
}
} }

@ -18,13 +18,8 @@
package io.entgra.device.mgt.core.apimgt.extension.rest.api; package io.entgra.device.mgt.core.apimgt.extension.rest.api;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIInfo; import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.*;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIKey; import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.ApiApplicationInfo;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Subscription;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.KeyManager;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException; 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.BadRequestException;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
@ -33,37 +28,30 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public interface ConsumerRESTAPIServices { public interface ConsumerRESTAPIServices {
Application[] getAllApplications(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String appName) Application[] getAllApplications(ApiApplicationInfo applicationInfo, String appName)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Application getDetailsOfAnApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Application getDetailsOfAnApplication(ApiApplicationInfo applicationInfo, String applicationId)
String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Application createApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Application createApplication(ApiApplicationInfo applicationInfo, Application application)
Application application)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Subscription[] getAllSubscriptions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId)
String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
APIInfo[] getAllApis(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIInfo[] getAllApis(ApiApplicationInfo applicationInfo, Map<String, String> queryParam, Map<String, String> headerParams)
Map<String, String> queryParam, Map<String, String> headerParams)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Subscription createSubscription(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Subscription createSubscription(ApiApplicationInfo apiApplicationInfo, Subscription subscriptions)
Subscription subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Subscription[] createSubscriptions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, List<Subscription> subscriptions)
List<Subscription> subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
APIKey generateApplicationKeys(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, ApplicationKey generateApplicationKeys(ApiApplicationInfo applicationInfo, Application application)
String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
KeyManager[] getAllKeyManagers(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
} }

@ -19,14 +19,10 @@
package io.entgra.device.mgt.core.apimgt.extension.rest.api; package io.entgra.device.mgt.core.apimgt.extension.rest.api;
import com.google.gson.Gson; import com.google.gson.Gson;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIInfo; import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.*;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIKey;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Subscription;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.KeyManager;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants; import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo; 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.exceptions.APIServicesException; 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.BadRequestException;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
@ -54,14 +50,14 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ Constants.COLON + port; + Constants.COLON + port;
@Override @Override
public Application[] getAllApplications(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String appName) public Application[] getAllApplications(ApiApplicationInfo applicationInfo, String appName)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllApplicationsUrl = endPointPrefix + Constants.APPLICATIONS_API + "?query=" + appName; String getAllApplicationsUrl = endPointPrefix + Constants.APPLICATIONS_API + "?query=" + appName;
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getAllApplicationsUrl) .url(getAllApplicationsUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token()) + applicationInfo.getAccess_token())
.get() .get()
.build(); .build();
@ -73,10 +69,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices. AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); applicationInfo.getClientId(), applicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(applicationInfo, refreshedAccessToken);
//TODO: max attempt count //TODO: max attempt count
return getAllApplications(apiApplicationKey, refreshedAccessToken, appName); return getAllApplications(refreshedApiApplicationInfo, appName);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request"; String msg = "Bad Request, Invalid request";
log.error(msg); log.error(msg);
@ -93,15 +90,14 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Application getDetailsOfAnApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public Application getDetailsOfAnApplication(ApiApplicationInfo apiApplicationInfo, String applicationId)
String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllApplicationsUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId; String getAllApplicationsUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId;
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getAllApplicationsUrl) .url(getAllApplicationsUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token()) + apiApplicationInfo.getAccess_token())
.get() .get()
.build(); .build();
@ -112,10 +108,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices. AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count //TODO: max attempt count
return getDetailsOfAnApplication(apiApplicationKey, refreshedAccessToken, applicationId); return getDetailsOfAnApplication(refreshedApiApplicationInfo, applicationId);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request"; String msg = "Bad Request, Invalid request";
log.error(msg); log.error(msg);
@ -132,8 +129,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Application createApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public Application createApplication(ApiApplicationInfo apiApplicationInfo, Application application)
Application application)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API; String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API;
@ -152,7 +148,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getAllScopesUrl) .url(getAllScopesUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token()) + apiApplicationInfo.getAccess_token())
.post(requestBody) .post(requestBody)
.build(); .build();
@ -163,10 +159,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices. AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count //TODO: max attempt count
return createApplication(apiApplicationKey, refreshedAccessToken, application); return createApplication(refreshedApiApplicationInfo, application);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request body"; String msg = "Bad Request, Invalid request body";
log.error(msg); log.error(msg);
@ -183,15 +180,14 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Subscription[] getAllSubscriptions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId)
String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "?applicationId=" + applicationId; String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "?applicationId=" + applicationId;
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getAllScopesUrl) .url(getAllScopesUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token()) + apiApplicationInfo.getAccess_token())
.get() .get()
.build(); .build();
@ -203,10 +199,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices. AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo rehreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count //TODO: max attempt count
return getAllSubscriptions(apiApplicationKey, refreshedAccessToken, applicationId); return getAllSubscriptions(rehreshedApiApplicationInfo, applicationId);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request"; String msg = "Bad Request, Invalid request";
log.error(msg); log.error(msg);
@ -223,8 +220,8 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public APIInfo[] getAllApis(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public APIInfo[] getAllApis(ApiApplicationInfo applicationInfo, Map<String, String> queryParams,
Map<String, String> queryParams, Map<String, String> headerParams) Map<String, String> headerParams)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAPIsURL = endPointPrefix + Constants.DEV_PORTAL_API; String getAPIsURL = endPointPrefix + Constants.DEV_PORTAL_API;
@ -236,7 +233,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
Request.Builder builder = new Request.Builder(); Request.Builder builder = new Request.Builder();
builder.url(getAPIsURL); builder.url(getAPIsURL);
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token()); + applicationInfo.getAccess_token());
for (Map.Entry<String, String> header : headerParams.entrySet()) { for (Map.Entry<String, String> header : headerParams.entrySet()) {
builder.addHeader(header.getKey(), header.getValue()); builder.addHeader(header.getKey(), header.getValue());
} }
@ -251,10 +248,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices. AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); applicationInfo.getClientId(), applicationInfo.getClientSecret());
ApiApplicationInfo rehreshedApiApplicationInfo = returnApplicationInfo(applicationInfo, refreshedAccessToken);
//TODO: max attempt count //TODO: max attempt count
return getAllApis(apiApplicationKey, refreshedAccessToken, queryParams, headerParams); return getAllApis(rehreshedApiApplicationInfo, queryParams, headerParams);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request"; String msg = "Bad Request, Invalid request";
log.error(msg); log.error(msg);
@ -271,8 +269,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Subscription createSubscription(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public Subscription createSubscription(ApiApplicationInfo applicationInfo, Subscription subscriptions)
Subscription subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API; String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API;
@ -288,7 +285,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getAllScopesUrl) .url(getAllScopesUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token()) + applicationInfo.getAccess_token())
.post(requestBody) .post(requestBody)
.build(); .build();
@ -299,10 +296,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices. AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); applicationInfo.getClientId(), applicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(applicationInfo, refreshedAccessToken);
//TODO: max attempt count //TODO: max attempt count
return createSubscription(apiApplicationKey, refreshedAccessToken, subscriptions); return createSubscription(refreshedApiApplicationInfo, subscriptions);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request body"; String msg = "Bad Request, Invalid request body";
log.error(msg); log.error(msg);
@ -319,8 +317,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Subscription[] createSubscriptions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, List<Subscription> subscriptions)
List<Subscription> subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "/multiple"; String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "/multiple";
@ -331,7 +328,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getAllScopesUrl) .url(getAllScopesUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token()) + apiApplicationInfo.getAccess_token())
.post(requestBody) .post(requestBody)
.build(); .build();
@ -343,10 +340,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices. AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count //TODO: max attempt count
return createSubscriptions(apiApplicationKey, refreshedAccessToken, subscriptions); return createSubscriptions(refreshedApiApplicationInfo, subscriptions);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request body"; String msg = "Bad Request, Invalid request body";
log.error(msg); log.error(msg);
@ -363,15 +361,25 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public APIKey generateApplicationKeys(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, Application application)
String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + Constants.SLASH + applicationId + String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + Constants.SLASH +
"/generate-keys"; application.getApplicationId() + "/generate-keys";
String keyInfo = "{\n" + String keyInfo = "{\n" +
" \"validityPeriod\": 3600,\n" + " \"keyType\": \"PRODUCTION\",\n" +
" \"keyManager\": \"Resident Key Manager\",\n" +
" \"grantTypesToBeSupported\": [\n" +
" \"password\",\n" +
" \"client_credentials\"\n" +
" ],\n" +
" \"callbackUrl\": \"http://sample.com/callback/url\",\n" +
" \"scopes\": [\n" +
" \"am_application_scope\",\n" +
" \"default\"\n" +
" ],\n" +
" \"validityTime\": 3600,\n" +
" \"additionalProperties\": {}\n" + " \"additionalProperties\": {}\n" +
"}"; "}";
@ -379,21 +387,22 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getAllScopesUrl) .url(getAllScopesUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token()) + apiApplicationInfo.getAccess_token())
.post(requestBody) .post(requestBody)
.build(); .build();
try { try {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
if (HttpStatus.SC_OK == response.code()) { if (HttpStatus.SC_OK == response.code()) {
return gson.fromJson(response.body().string(), APIKey.class); return gson.fromJson(response.body().string(), ApplicationKey.class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices. AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count //TODO: max attempt count
return generateApplicationKeys(apiApplicationKey, refreshedAccessToken, applicationId); return generateApplicationKeys(refreshedApiApplicationKey, application);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request body"; String msg = "Bad Request, Invalid request body";
log.error(msg); log.error(msg);
@ -410,14 +419,14 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public KeyManager[] getAllKeyManagers(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) public KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllKeyManagersUrl = endPointPrefix + Constants.KEY_MANAGERS_API; String getAllKeyManagersUrl = endPointPrefix + Constants.KEY_MANAGERS_API;
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getAllKeyManagersUrl) .url(getAllKeyManagersUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token()) + apiApplicationInfo.getAccess_token())
.get() .get()
.build(); .build();
@ -429,10 +438,11 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices. AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count //TODO: max attempt count
return getAllKeyManagers(apiApplicationKey, refreshedAccessToken); return getAllKeyManagers(refreshedApiApplicationInfo);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request"; String msg = "Bad Request, Invalid request";
log.error(msg); log.error(msg);
@ -447,4 +457,15 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
throw new APIServicesException(msg, e); throw new APIServicesException(msg, e);
} }
} }
private ApiApplicationInfo returnApplicationInfo(ApiApplicationInfo refreshedApplicationInfo, AccessTokenInfo refreshedToken) {
ApiApplicationInfo applicationInfo = null;
applicationInfo.setClientId(refreshedApplicationInfo.getClientId());
applicationInfo.setClientSecret(refreshedApplicationInfo.getClientSecret());
applicationInfo.setAccess_token(refreshedToken.getAccess_token());
applicationInfo.setRefresh_token(refreshedToken.getRefresh_token());
return applicationInfo;
}
} }

@ -0,0 +1,138 @@
/*
* 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.bean.APIMConsumer;
import org.json.JSONObject;
import java.util.List;
/**
* This class represents the Consumer Application key Information.
*/
public class ApplicationKey {
private String keyMappingId;
private String keyManager;
private String consumerKey;
private String consumerSecret;
private List<String> supportedGrantTypes;
private String callbackUrl;
private String keyState;
private String keyType;
private String mode;
private String groupId;
private JSONObject token;
private JSONObject additionalProperties;
public String getKeyMappingId() {
return keyMappingId;
}
public void setKeyMappingId(String keyMappingId) {
this.keyMappingId = keyMappingId;
}
public String getKeyManager() {
return keyManager;
}
public void setKeyManager(String keyManager) {
this.keyManager = keyManager;
}
public String getConsumerKey() {
return consumerKey;
}
public void setConsumerKey(String consumerKey) {
this.consumerKey = consumerKey;
}
public String getConsumerSecret() {
return consumerSecret;
}
public void setConsumerSecret(String consumerSecret) {
this.consumerSecret = consumerSecret;
}
public List<String> getSupportedGrantTypes() {
return supportedGrantTypes;
}
public void setSupportedGrantTypes(List<String> supportedGrantTypes) {
this.supportedGrantTypes = supportedGrantTypes;
}
public String getCallbackUrl() {
return callbackUrl;
}
public void setCallbackUrl(String callbackUrl) {
this.callbackUrl = callbackUrl;
}
public String getKeyState() {
return keyState;
}
public void setKeyState(String keyState) {
this.keyState = keyState;
}
public String getKeyType() {
return keyType;
}
public void setKeyType(String keyType) {
this.keyType = keyType;
}
public String getMode() {
return mode;
}
public void setMode(String mode) {
this.mode = mode;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public JSONObject getToken() {
return token;
}
public void setToken(JSONObject token) {
this.token = token;
}
public JSONObject getAdditionalProperties() {
return additionalProperties;
}
public void setAdditionalProperties(JSONObject additionalProperties) {
this.additionalProperties = additionalProperties;
}
}

@ -0,0 +1,61 @@
/*
* 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 client information and token information.
*/
public class ApiApplicationInfo {
private String clientId;
private String clientSecret;
private String refresh_token;
private String access_token;
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public String getRefresh_token() {
return refresh_token;
}
public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
}
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
}
Loading…
Cancel
Save