mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Fix application key generation method
This commit is contained in:
commit
ad537f3461
@ -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.exception.APIManagerException;
|
||||||
import io.entgra.device.mgt.core.apimgt.application.extension.internal.APIApplicationManagerExtensionDataHolder;
|
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.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.MetadataKeyAlreadyExistsException;
|
||||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
|
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
|
||||||
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
|
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();
|
MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService();
|
||||||
if (isNewApplication) {
|
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 apiApplicationKey = new ApiApplicationKey();
|
||||||
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
|
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
|
||||||
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
|
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
|
||||||
@ -210,7 +212,11 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
|
|||||||
}
|
}
|
||||||
String applicationId = metaValues[0];
|
String applicationId = metaValues[0];
|
||||||
String keyMappingId = metaValues[1];
|
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) {
|
} catch (MetadataManagementException e) {
|
||||||
String msg = "Error occurred while getting meta data for meta key: " + applicationName;
|
String msg = "Error occurred while getting meta data for meta key: " + applicationName;
|
||||||
log.error(msg, e);
|
log.error(msg, e);
|
||||||
|
|||||||
@ -37,6 +37,9 @@ public interface ConsumerRESTAPIServices {
|
|||||||
Application createApplication(ApiApplicationInfo applicationInfo, Application application)
|
Application createApplication(ApiApplicationInfo applicationInfo, Application application)
|
||||||
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
||||||
|
|
||||||
|
Application deleteApplication(ApiApplicationInfo apiApplicationInfo, String applicationId)
|
||||||
|
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
||||||
|
|
||||||
Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId)
|
Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId)
|
||||||
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
||||||
|
|
||||||
@ -49,7 +52,10 @@ public interface ConsumerRESTAPIServices {
|
|||||||
Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, List<Subscription> subscriptions)
|
Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, List<Subscription> subscriptions)
|
||||||
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
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;
|
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
||||||
|
|
||||||
KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo)
|
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
|
@Override
|
||||||
public Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId)
|
public Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId)
|
||||||
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
||||||
@ -361,20 +401,20 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, Application application)
|
public ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, Application application, KeyManager keyManager)
|
||||||
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
||||||
|
|
||||||
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + Constants.SLASH +
|
String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH +
|
||||||
application.getApplicationId() + "/generate-keys";
|
application.getApplicationId() + "/generate-keys";
|
||||||
|
|
||||||
String keyInfo = "{\n" +
|
String keyInfo = "{\n" +
|
||||||
" \"keyType\": \"PRODUCTION\",\n" +
|
" \"keyType\": \"PRODUCTION\",\n" +
|
||||||
" \"keyManager\": \"Resident Key Manager\",\n" +
|
" \"keyManager\": \""+ keyManager.getName() +"\",\n" +
|
||||||
" \"grantTypesToBeSupported\": [\n" +
|
" \"grantTypesToBeSupported\": [\n" +
|
||||||
" \"password\",\n" +
|
" \"password\",\n" +
|
||||||
" \"client_credentials\"\n" +
|
" \"client_credentials\"\n" +
|
||||||
" ],\n" +
|
" ],\n" +
|
||||||
" \"callbackUrl\": \"http://sample.com/callback/url\",\n" +
|
" \"callbackUrl\": \"\",\n" +
|
||||||
" \"scopes\": [\n" +
|
" \"scopes\": [\n" +
|
||||||
" \"am_application_scope\",\n" +
|
" \"am_application_scope\",\n" +
|
||||||
" \"default\"\n" +
|
" \"default\"\n" +
|
||||||
@ -402,7 +442,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
|||||||
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
||||||
ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
ApiApplicationInfo refreshedApiApplicationKey = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
||||||
//TODO: max attempt count
|
//TODO: max attempt count
|
||||||
return generateApplicationKeys(refreshedApiApplicationKey, application);
|
return generateApplicationKeys(refreshedApiApplicationKey, application, keyManager);
|
||||||
} 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);
|
||||||
@ -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
|
@Override
|
||||||
public KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo)
|
public KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo)
|
||||||
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user