Get token separately for REST calls

Co-authored-by: Pasindu Rupasinghe <pasindu@entgra.io>
Co-committed-by: Pasindu Rupasinghe <pasindu@entgra.io>
remotes/1725333865317989910/master
Pasindu Rupasinghe 1 year ago committed by Lasantha Dharmakeerthi
parent d40dccc240
commit 7dfcc0e33c

@ -138,7 +138,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
Map<String, String> queryParams = new HashMap<>(); Map<String, String> queryParams = new HashMap<>();
queryParams.put("tag", tag); queryParams.put("tag", tag);
APIInfo[] apiInfos = consumerRESTAPIServices.getAllApis(applicationInfo, queryParams, headerParams); APIInfo[] apiInfos = consumerRESTAPIServices.getAllApis(applicationInfo, null, queryParams, headerParams);
uniqueApiList.addAll(List.of(apiInfos)); uniqueApiList.addAll(List.of(apiInfos));
Set<APIInfo> taggedAPISet = new HashSet<>(uniqueApiList); Set<APIInfo> taggedAPISet = new HashSet<>(uniqueApiList);
@ -147,21 +147,22 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
} }
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(applicationInfo, applicationName); consumerRESTAPIServices.getAllApplications(applicationInfo, null, applicationName);
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application; io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application;
boolean isNewApplication = false; boolean isNewApplication = false;
if (applications.length == 0) { if (applications.length == 0) {
isNewApplication = true; isNewApplication = true;
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(applicationInfo, application); application = consumerRESTAPIServices.createApplication(applicationInfo, null, application);
addSubscriptions(application, uniqueApiList, applicationInfo); addSubscriptions(application, uniqueApiList, applicationInfo);
} 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(applicationInfo, application.getApplicationId()); Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(applicationInfo, null,
application.getApplicationId());
Arrays.stream(subscriptions).map(Subscription::getApiInfo).forEachOrdered(uniqueApiList::remove); Arrays.stream(subscriptions).map(Subscription::getApiInfo).forEachOrdered(uniqueApiList::remove);
addSubscriptions(application, uniqueApiList, applicationInfo); addSubscriptions(application, uniqueApiList, applicationInfo);
} else { } else {
@ -173,7 +174,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService(); MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService();
if (isNewApplication) { if (isNewApplication) {
KeyManager[] keyManagers = consumerRESTAPIServices.getAllKeyManagers(applicationInfo); KeyManager[] keyManagers = consumerRESTAPIServices.getAllKeyManagers(applicationInfo, null);
KeyManager keyManager; KeyManager keyManager;
if (keyManagers.length == 1) { if (keyManagers.length == 1) {
keyManager = keyManagers[0]; keyManager = keyManagers[0];
@ -182,7 +183,8 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
"Found invalid number of key managers. No of key managers found from the APIM: " + keyManagers.length; "Found invalid number of key managers. No of key managers found from the APIM: " + keyManagers.length;
throw new APIManagerException(msg); throw new APIManagerException(msg);
} }
ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(applicationInfo, application, keyManager); ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(applicationInfo, null,
application.getApplicationId(), keyManager.getName(), keyType, validityTime);
ApiApplicationKey apiApplicationKey = new ApiApplicationKey(); ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey()); apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret()); apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
@ -221,7 +223,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
} }
String applicationId = metaValues[0]; String applicationId = metaValues[0];
String keyMappingId = metaValues[1]; String keyMappingId = metaValues[1];
ApplicationKey applicationKey = consumerRESTAPIServices.getKeyDetails(applicationInfo, applicationId, keyMappingId); ApplicationKey applicationKey = consumerRESTAPIServices.getKeyDetails(applicationInfo, null, applicationId, keyMappingId);
ApiApplicationKey apiApplicationKey = new ApiApplicationKey(); ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey()); apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret()); apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
@ -273,7 +275,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
subscription.setApplicationId(application.getApplicationId()); subscription.setApplicationId(application.getApplicationId());
subscriptionList.add(subscription); subscriptionList.add(subscription);
}); });
consumerRESTAPIServices.createSubscriptions(apiApplicationInfo, subscriptionList); consumerRESTAPIServices.createSubscriptions(apiApplicationInfo, null, subscriptionList);
} }
/** /**

@ -28,36 +28,39 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public interface ConsumerRESTAPIServices { public interface ConsumerRESTAPIServices {
Application[] getAllApplications(ApiApplicationInfo applicationInfo, String appName) Application[] getAllApplications(ApiApplicationInfo applicationInfo, String accessToken, String appName)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Application getDetailsOfAnApplication(ApiApplicationInfo applicationInfo, String applicationId) Application getDetailsOfAnApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Application createApplication(ApiApplicationInfo applicationInfo, Application application) Application createApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, Application application)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Application deleteApplication(ApiApplicationInfo apiApplicationInfo, String applicationId) Boolean deleteApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId) Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
APIInfo[] getAllApis(ApiApplicationInfo applicationInfo, Map<String, String> queryParam, Map<String, String> headerParams) APIInfo[] getAllApis(ApiApplicationInfo apiApplicationInfo, String accessToken, Map<String, String> queryParams,
Map<String, String> headerParams)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Subscription createSubscription(ApiApplicationInfo apiApplicationInfo, Subscription subscriptions) Subscription createSubscription(ApiApplicationInfo apiApplicationInfo, String accessToken, Subscription subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, List<Subscription> subscriptions) Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, String accessToken,
List<Subscription> subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
ApplicationKey generateApplicationKeys(ApiApplicationInfo applicationInfo, Application application, KeyManager keyManager) ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId,
String keyManager, String validityTime, String keyType)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
ApplicationKey getKeyDetails(ApiApplicationInfo apiApplicationInfo, String applicationId, String keyMapId) ApplicationKey getKeyDetails(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId, String keyMapId)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo) KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo, String accessToken)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
} }

@ -50,16 +50,22 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
+ Constants.COLON + port; + Constants.COLON + port;
@Override @Override
public Application[] getAllApplications(ApiApplicationInfo applicationInfo, String appName) public Application[] getAllApplications(ApiApplicationInfo applicationInfo, String accessToken, 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()
.url(getAllApplicationsUrl) Request.Builder builder = new Request.Builder();
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER builder.url(getAllApplicationsUrl);
+ applicationInfo.getAccess_token()) if (!(applicationInfo == null) && accessToken == null) {
.get() builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
.build(); + applicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
builder.get();
Request request = builder.build();
try { try {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
@ -67,13 +73,19 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
JSONArray applicationList = (JSONArray) new JSONObject(response.body().string()).get("list"); JSONArray applicationList = (JSONArray) new JSONObject(response.body().string()).get("list");
return gson.fromJson(applicationList.toString(), Application[].class); return gson.fromJson(applicationList.toString(), Application[].class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); if (!(applicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(applicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
applicationInfo.getClientId(), applicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(applicationInfo.getRefresh_token(),
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(applicationInfo, refreshedAccessToken); applicationInfo.getClientId(), applicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(applicationInfo, refreshedAccessToken);
return getAllApplications(refreshedApiApplicationInfo, appName); //TODO: max attempt count
return getAllApplications(refreshedApiApplicationInfo, null, appName);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);
@ -90,29 +102,41 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Application getDetailsOfAnApplication(ApiApplicationInfo apiApplicationInfo, String applicationId) public Application getDetailsOfAnApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllApplicationsUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId; String getDetailsOfAPPUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId;
Request request = new Request.Builder()
.url(getAllApplicationsUrl) Request.Builder builder = new Request.Builder();
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER builder.url(getDetailsOfAPPUrl);
+ apiApplicationInfo.getAccess_token()) if (!(apiApplicationInfo == null) && accessToken == null) {
.get() builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
.build(); + apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
builder.get();
Request request = builder.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(), Application.class); return gson.fromJson(response.body().string(), Application.class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); if (!(apiApplicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
return getDetailsOfAnApplication(refreshedApiApplicationInfo, applicationId); //TODO: max attempt count
return getDetailsOfAnApplication(refreshedApiApplicationInfo, null, applicationId);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);
@ -129,7 +153,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Application createApplication(ApiApplicationInfo apiApplicationInfo, Application application) public Application createApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, Application application)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API; String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API;
@ -143,27 +167,38 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
" \"attributes\": " + application.getAttributes().toString() + ",\n" + " \"attributes\": " + application.getAttributes().toString() + ",\n" +
" \"subscriptionScopes\": " + gson.toJson(application.getSubscriptionScopes()) + "\n" + " \"subscriptionScopes\": " + gson.toJson(application.getSubscriptionScopes()) + "\n" +
"}"; "}";
RequestBody requestBody = RequestBody.create(JSON, applicationInfo); RequestBody requestBody = RequestBody.create(JSON, applicationInfo);
Request request = new Request.Builder()
.url(getAllScopesUrl) Request.Builder builder = new Request.Builder();
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER builder.url(getAllScopesUrl);
+ apiApplicationInfo.getAccess_token()) if (!(apiApplicationInfo == null) && accessToken == null) {
.post(requestBody) builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
.build(); + apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
builder.post(requestBody);
Request request = builder.build();
try { try {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
if (HttpStatus.SC_CREATED == response.code()) { if (HttpStatus.SC_CREATED == response.code()) {
return gson.fromJson(response.body().string(), Application.class); return gson.fromJson(response.body().string(), Application.class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); if (!(apiApplicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
return createApplication(refreshedApiApplicationInfo, application); //TODO: max attempt count
return createApplication(refreshedApiApplicationInfo, null, application);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);
@ -180,30 +215,41 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Application deleteApplication(ApiApplicationInfo apiApplicationInfo, String applicationId) public Boolean deleteApplication(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String deleteScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId; String deleteScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId;
Request request = new Request.Builder() Request.Builder builder = new Request.Builder();
.url(deleteScopesUrl) builder.url(deleteScopesUrl);
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER if (!(apiApplicationInfo == null) && accessToken == null) {
+ apiApplicationInfo.getAccess_token()) builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
.delete() + apiApplicationInfo.getAccess_token());
.build(); } else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
builder.delete();
Request request = builder.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(), Application.class); return true;
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); if (!(apiApplicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
return deleteApplication(refreshedApiApplicationInfo, applicationId); //TODO: max attempt count
return deleteApplication(refreshedApiApplicationInfo, null, applicationId);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);
@ -220,16 +266,22 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId) public Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String accessToken, 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()
.url(getAllScopesUrl) Request.Builder builder = new Request.Builder();
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER builder.url(getAllScopesUrl);
+ apiApplicationInfo.getAccess_token()) if (!(apiApplicationInfo == null) && accessToken == null) {
.get() builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
.build(); + apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
builder.get();
Request request = builder.build();
try { try {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
@ -237,13 +289,19 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
JSONArray subscriptionList = (JSONArray) new JSONObject(response.body().string()).get("list"); JSONArray subscriptionList = (JSONArray) new JSONObject(response.body().string()).get("list");
return gson.fromJson(subscriptionList.toString(), Subscription[].class); return gson.fromJson(subscriptionList.toString(), Subscription[].class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); if (!(apiApplicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
ApiApplicationInfo rehreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo rehreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
return getAllSubscriptions(rehreshedApiApplicationInfo, applicationId); //TODO: max attempt count
return getAllSubscriptions(rehreshedApiApplicationInfo, null, applicationId);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);
@ -260,20 +318,25 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public APIInfo[] getAllApis(ApiApplicationInfo applicationInfo, Map<String, String> queryParams, public APIInfo[] getAllApis(ApiApplicationInfo apiApplicationInfo, String accessToken, 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; StringBuilder getAPIsURL = new StringBuilder(endPointPrefix + Constants.DEV_PORTAL_API);
for (Map.Entry<String, String> query : queryParams.entrySet()) { for (Map.Entry<String, String> query : queryParams.entrySet()) {
getAPIsURL = getAPIsURL + Constants.AMPERSAND + query.getKey() + Constants.EQUAL + query.getValue(); getAPIsURL.append(Constants.AMPERSAND).append(query.getKey()).append(Constants.EQUAL).append(query.getValue());
} }
Request.Builder builder = new Request.Builder(); Request.Builder builder = new Request.Builder();
builder.url(getAPIsURL); builder.url(getAPIsURL.toString());
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER if (!(apiApplicationInfo == null) && accessToken == null) {
+ applicationInfo.getAccess_token()); builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
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());
} }
@ -286,13 +349,19 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
JSONArray apiList = (JSONArray) new JSONObject(response.body().string()).get("list"); JSONArray apiList = (JSONArray) new JSONObject(response.body().string()).get("list");
return gson.fromJson(apiList.toString(), APIInfo[].class); return gson.fromJson(apiList.toString(), APIInfo[].class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); if (!(apiApplicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(applicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
applicationInfo.getClientId(), applicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
ApiApplicationInfo rehreshedApiApplicationInfo = returnApplicationInfo(applicationInfo, refreshedAccessToken); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo rehreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
return getAllApis(rehreshedApiApplicationInfo, queryParams, headerParams); //TODO: max attempt count
return getAllApis(rehreshedApiApplicationInfo, null, queryParams, headerParams);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);
@ -309,10 +378,10 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Subscription createSubscription(ApiApplicationInfo applicationInfo, Subscription subscriptions) public Subscription createSubscription(ApiApplicationInfo apiApplicationInfo, String accessToken, Subscription subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API; String createSubscriptionUrl = endPointPrefix + Constants.SUBSCRIPTION_API;
String subscriptionObject = "{\n" + String subscriptionObject = "{\n" +
" \"applicationId\": \"" + subscriptions.getApplicationId() + "\",\n" + " \"applicationId\": \"" + subscriptions.getApplicationId() + "\",\n" +
@ -322,25 +391,37 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
"}"; "}";
RequestBody requestBody = RequestBody.create(JSON, subscriptionObject); RequestBody requestBody = RequestBody.create(JSON, subscriptionObject);
Request request = new Request.Builder()
.url(getAllScopesUrl) Request.Builder builder = new Request.Builder();
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER builder.url(createSubscriptionUrl);
+ applicationInfo.getAccess_token()) if (!(apiApplicationInfo == null) && accessToken == null) {
.post(requestBody) builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
.build(); + apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
builder.post(requestBody);
Request request = builder.build();
try { try {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
if (HttpStatus.SC_CREATED == response.code()) { if (HttpStatus.SC_CREATED == response.code()) {
return gson.fromJson(response.body().string(), Subscription.class); return gson.fromJson(response.body().string(), Subscription.class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); if (!(apiApplicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(applicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
applicationInfo.getClientId(), applicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(applicationInfo, refreshedAccessToken); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
return createSubscription(refreshedApiApplicationInfo, subscriptions); //TODO: max attempt count
return createSubscription(refreshedApiApplicationInfo, null, subscriptions);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);
@ -357,20 +438,26 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, List<Subscription> subscriptions) public Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, String accessToken,
List<Subscription> subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "/multiple"; String createSubscriptionsUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "/multiple";
String subscriptionsList = gson.toJson(subscriptions); String subscriptionsList = gson.toJson(subscriptions);
RequestBody requestBody = RequestBody.create(JSON, subscriptionsList); RequestBody requestBody = RequestBody.create(JSON, subscriptionsList);
Request request = new Request.Builder()
.url(getAllScopesUrl) Request.Builder builder = new Request.Builder();
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER builder.url(createSubscriptionsUrl);
+ apiApplicationInfo.getAccess_token()) if (!(apiApplicationInfo == null) && accessToken == null) {
.post(requestBody) builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
.build(); + apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
builder.post(requestBody);
Request request = builder.build();
try { try {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
@ -378,13 +465,19 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
JSONArray subscriptionsArray = (JSONArray) new JSONObject(response.body().string()).get("list"); JSONArray subscriptionsArray = (JSONArray) new JSONObject(response.body().string()).get("list");
return gson.fromJson(subscriptionsArray.toString(), Subscription[].class); return gson.fromJson(subscriptionsArray.toString(), Subscription[].class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); if (!(apiApplicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
return createSubscriptions(refreshedApiApplicationInfo, subscriptions); //TODO: max attempt count
return createSubscriptions(refreshedApiApplicationInfo, null, subscriptions);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);
@ -401,15 +494,16 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, Application application, KeyManager keyManager) public ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId,
String keyManager, String validityTime, String keyType)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + String generateApplicationKeysUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH +
application.getApplicationId() + "/generate-keys"; applicationId + "/generate-keys";
String keyInfo = "{\n" + String keyInfo = "{\n" +
" \"keyType\": \"PRODUCTION\",\n" + " \"keyType\": \"" + keyType + "\",\n" +
" \"keyManager\": \""+ keyManager.getName() +"\",\n" + " \"keyManager\": \"" + keyManager + "\",\n" +
" \"grantTypesToBeSupported\": [\n" + " \"grantTypesToBeSupported\": [\n" +
" \"password\",\n" + " \"password\",\n" +
" \"client_credentials\"\n" + " \"client_credentials\"\n" +
@ -419,30 +513,42 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
" \"am_application_scope\",\n" + " \"am_application_scope\",\n" +
" \"default\"\n" + " \"default\"\n" +
" ],\n" + " ],\n" +
" \"validityTime\": 3600,\n" + " \"validityTime\": " + validityTime + ",\n" +
" \"additionalProperties\": {}\n" + " \"additionalProperties\": {}\n" +
"}"; "}";
RequestBody requestBody = RequestBody.create(JSON, keyInfo); RequestBody requestBody = RequestBody.create(JSON, keyInfo);
Request request = new Request.Builder()
.url(getAllScopesUrl) Request.Builder builder = new Request.Builder();
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER builder.url(generateApplicationKeysUrl);
+ apiApplicationInfo.getAccess_token()) if (!(apiApplicationInfo == null) && accessToken == null) {
.post(requestBody) builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
.build(); + apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
builder.post(requestBody);
Request request = builder.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(), ApplicationKey.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(); if (!(apiApplicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
return generateApplicationKeys(refreshedApiApplicationKey, application, keyManager); //TODO: max attempt count
return generateApplicationKeys(refreshedApiApplicationKey, null, applicationId, keyManager, validityTime, keyType);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);
@ -459,30 +565,41 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public ApplicationKey getKeyDetails(ApiApplicationInfo apiApplicationInfo, String applicationId, String keyMapId) public ApplicationKey getKeyDetails(ApiApplicationInfo apiApplicationInfo, String accessToken, String applicationId, String keyMapId)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getKeyDetails = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId + "/oauth-keys/" + keyMapId; String getKeyDetails = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId + "/oauth-keys/" + keyMapId;
Request request = new Request.Builder() Request.Builder builder = new Request.Builder();
.url(getKeyDetails) builder.url(getKeyDetails);
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER if (!(apiApplicationInfo == null) && accessToken == null) {
+ apiApplicationInfo.getAccess_token()) builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
.get() + apiApplicationInfo.getAccess_token());
.build(); } else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
builder.get();
Request request = builder.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(), ApplicationKey.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(); if (!(apiApplicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
return getKeyDetails(refreshedApiApplicationKey, applicationId, keyMapId); //TODO: max attempt count
return getKeyDetails(refreshedApiApplicationKey, null, applicationId, keyMapId);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);
@ -499,16 +616,22 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
@Override @Override
public KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo) public KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo, String accessToken)
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()
.url(getAllKeyManagersUrl) Request.Builder builder = new Request.Builder();
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER builder.url(getAllKeyManagersUrl);
+ apiApplicationInfo.getAccess_token()) if (!(apiApplicationInfo == null) && accessToken == null) {
.get() builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
.build(); + apiApplicationInfo.getAccess_token());
} else {
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessToken);
}
builder.get();
Request request = builder.build();
try { try {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
@ -516,13 +639,19 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
JSONArray keyManagerList = (JSONArray) new JSONObject(response.body().string()).get("list"); JSONArray keyManagerList = (JSONArray) new JSONObject(response.body().string()).get("list");
return gson.fromJson(keyManagerList.toString(), KeyManager[].class); return gson.fromJson(keyManagerList.toString(), KeyManager[].class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); if (!(apiApplicationInfo == null) && accessToken == null) {
AccessTokenInfo refreshedAccessToken = apiApplicationServices. APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(), AccessTokenInfo refreshedAccessToken = apiApplicationServices.
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret()); generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken); apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
//TODO: max attempt count ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
return getAllKeyManagers(refreshedApiApplicationInfo); //TODO: max attempt count
return getAllKeyManagers(refreshedApiApplicationInfo, null);
} else {
String msg = "Invalid or null access token";
log.error(msg);
throw new BadRequestException(msg);
}
} 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);

@ -19,6 +19,10 @@
package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl;
import com.google.gson.Gson; import com.google.gson.Gson;
import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderService;
import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderServiceImpl;
import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey;
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
import io.entgra.device.mgt.core.apimgt.keymgt.extension.DCRResponse; import io.entgra.device.mgt.core.apimgt.keymgt.extension.DCRResponse;
import io.entgra.device.mgt.core.apimgt.keymgt.extension.TokenRequest; import io.entgra.device.mgt.core.apimgt.keymgt.extension.TokenRequest;
import io.entgra.device.mgt.core.apimgt.keymgt.extension.TokenResponse; import io.entgra.device.mgt.core.apimgt.keymgt.extension.TokenResponse;
@ -825,6 +829,13 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
deviceConfig.setClientId(dcrResponse.getClientId()); deviceConfig.setClientId(dcrResponse.getClientId());
deviceConfig.setClientSecret(dcrResponse.getClientSecret()); deviceConfig.setClientSecret(dcrResponse.getClientSecret());
APIManagementProviderService apiManagementProviderService = new APIManagementProviderServiceImpl();
ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(applicationName,
new String[] {"device_management"}, null, username, false, String.valueOf(validityTime), null);
deviceConfig.setClientId(apiApplicationKey.getConsumerKey());
deviceConfig.setClientSecret(apiApplicationKey.getConsumerSecret());
StringBuilder scopes = new StringBuilder("device:" + type.replace(" ", "") + ":" + id); StringBuilder scopes = new StringBuilder("device:" + type.replace(" ", "") + ":" + id);
for (String topic : mqttEventTopicStructure) { for (String topic : mqttEventTopicStructure) {
if (topic.contains("${deviceId}")) { if (topic.contains("${deviceId}")) {
@ -871,6 +882,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (APIManagerException e) {
String msg = "Error while calling rest Call for application key generation";
log.error(msg, e);
} }
return Response.status(Response.Status.OK).entity(deviceConfig).build(); return Response.status(Response.Status.OK).entity(deviceConfig).build();

Loading…
Cancel
Save