Fix application key generation method #6

Merged
tcdlpds merged 1 commits from fixKeyGenerationMethod into apim420 1 year ago

@ -24,6 +24,7 @@ import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplication
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
import io.entgra.device.mgt.core.apimgt.application.extension.internal.APIApplicationManagerExtensionDataHolder;
import io.entgra.device.mgt.core.apimgt.application.extension.util.APIManagerUtil;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.KeyManager;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyAlreadyExistsException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
@ -172,7 +173,8 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService();
if (isNewApplication) {
ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(applicationInfo, application);
KeyManager keyManager = consumerRESTAPIServices.getAllKeyManagers(applicationInfo)[0];
ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(applicationInfo, application, keyManager);
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
@ -210,7 +212,11 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
}
String applicationId = metaValues[0];
String keyMappingId = metaValues[1];
//todo call the API key retrieving call, return apiApplicationKey;
ApplicationKey applicationKey = consumerRESTAPIServices.getKeyDetails(applicationInfo, applicationId, keyMappingId);
ApiApplicationKey apiApplicationKey = null;
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
return apiApplicationKey;
} catch (MetadataManagementException e) {
String msg = "Error occurred while getting meta data for meta key: " + applicationName;
log.error(msg, e);

@ -37,6 +37,9 @@ public interface ConsumerRESTAPIServices {
Application createApplication(ApiApplicationInfo applicationInfo, Application application)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
Application deleteApplication(ApiApplicationInfo apiApplicationInfo, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
@ -49,7 +52,10 @@ public interface ConsumerRESTAPIServices {
Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, List<Subscription> subscriptions)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
ApplicationKey generateApplicationKeys(ApiApplicationInfo applicationInfo, Application application)
ApplicationKey generateApplicationKeys(ApiApplicationInfo applicationInfo, Application application, KeyManager keyManager)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
ApplicationKey getKeyDetails(ApiApplicationInfo apiApplicationInfo, String applicationId, String keyMapId)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo)

@ -179,6 +179,46 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
}
@Override
public Application deleteApplication(ApiApplicationInfo apiApplicationInfo, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
String deleteScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId;
Request request = new Request.Builder()
.url(deleteScopesUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ apiApplicationInfo.getAccess_token())
.delete()
.build();
try {
Response response = client.newCall(request).execute();
if (HttpStatus.SC_OK == response.code()) {
return gson.fromJson(response.body().string(), Application.class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return deleteApplication(refreshedApiApplicationInfo, applicationId);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request body";
log.error(msg);
throw new BadRequestException(msg);
} else {
String msg = "Response : " + response.code() + response.body();
throw new UnexpectedResponseException(msg);
}
} catch (IOException e) {
String msg = "Error occurred while processing the response";
log.error(msg, e);
throw new APIServicesException(msg, e);
}
}
@Override
public Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
@ -361,20 +401,20 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
@Override
public ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, Application application)
public ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, Application application, KeyManager keyManager)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + Constants.SLASH +
String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH +
application.getApplicationId() + "/generate-keys";
String keyInfo = "{\n" +
" \"keyType\": \"PRODUCTION\",\n" +
" \"keyManager\": \"Resident Key Manager\",\n" +
" \"keyManager\": \""+ keyManager.getName() +"\",\n" +
" \"grantTypesToBeSupported\": [\n" +
" \"password\",\n" +
" \"client_credentials\"\n" +
" ],\n" +
" \"callbackUrl\": \"http://sample.com/callback/url\",\n" +
" \"callbackUrl\": \"\",\n" +
" \"scopes\": [\n" +
" \"am_application_scope\",\n" +
" \"default\"\n" +
@ -402,7 +442,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return generateApplicationKeys(refreshedApiApplicationKey, application);
return generateApplicationKeys(refreshedApiApplicationKey, application, keyManager);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request body";
log.error(msg);
@ -418,6 +458,46 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
}
}
@Override
public ApplicationKey getKeyDetails(ApiApplicationInfo apiApplicationInfo, String applicationId, String keyMapId)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getKeyDetails = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId + "/oauth-keys/" + keyMapId;
Request request = new Request.Builder()
.url(getKeyDetails)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ apiApplicationInfo.getAccess_token())
.get()
.build();
try {
Response response = client.newCall(request).execute();
if (HttpStatus.SC_OK == response.code()) {
return gson.fromJson(response.body().string(), ApplicationKey.class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
//TODO: max attempt count
return getKeyDetails(refreshedApiApplicationKey, applicationId, keyMapId);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request";
log.error(msg);
throw new BadRequestException(msg);
} else {
String msg = "Response : " + response.code() + response.body();
throw new UnexpectedResponseException(msg);
}
} catch (IOException e) {
String msg = "Error occurred while processing the response";
log.error(msg, e);
throw new APIServicesException(msg, e);
}
}
@Override
public KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo)
throws APIServicesException, BadRequestException, UnexpectedResponseException {

Loading…
Cancel
Save