mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Implement map application keys using dcr response
This commit is contained in:
commit
b0f1964760
@ -250,8 +250,13 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
|
||||
log.error(msg);
|
||||
throw new APIManagerException(msg);
|
||||
}
|
||||
ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(tokenInfo, application.getApplicationId(),
|
||||
keyManager.getName(), validityTime, keyType);
|
||||
|
||||
ApiApplicationInfo applicationInfo = getApplicationInfo(null, null);
|
||||
tokenInfo.setApiApplicationInfo(applicationInfo);
|
||||
|
||||
ApplicationKey applicationKey = consumerRESTAPIServices.mapApplicationKeys(tokenInfo, application,
|
||||
keyManager.getName(), keyType);
|
||||
|
||||
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
|
||||
apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey());
|
||||
apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret());
|
||||
|
||||
@ -55,6 +55,9 @@ public interface ConsumerRESTAPIServices {
|
||||
ApplicationKey generateApplicationKeys(TokenInfo tokenInfo, String applicationId, String keyManager, String validityTime, String keyType)
|
||||
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
||||
|
||||
ApplicationKey mapApplicationKeys(TokenInfo tokenInfo, Application application, String keyManager, String keyType)
|
||||
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
||||
|
||||
ApplicationKey getKeyDetails(TokenInfo tokenInfo, String applicationId, String keyMapId)
|
||||
throws APIServicesException, BadRequestException, UnexpectedResponseException;
|
||||
|
||||
|
||||
@ -606,6 +606,71 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApplicationKey mapApplicationKeys(TokenInfo tokenInfo, Application application, String keyManager, String keyType)
|
||||
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
||||
|
||||
ApiApplicationInfo apiApplicationInfo = tokenInfo.getApiApplicationInfo();
|
||||
boolean token = isTokenNull(apiApplicationInfo, tokenInfo.getAccessToken());
|
||||
String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH +
|
||||
application.getApplicationId() + "/map-keys";
|
||||
|
||||
String payload = "{\n" +
|
||||
" \"consumerKey\": \"" + apiApplicationInfo.getClientId() + "\",\n" +
|
||||
" \"consumerSecret\": \"" + apiApplicationInfo.getClientSecret() + "\",\n" +
|
||||
" \"keyManager\": \"" + keyManager + "\",\n" +
|
||||
" \"keyType\": \"" + keyType + "\"\n" +
|
||||
"}";
|
||||
RequestBody requestBody = RequestBody.create(JSON, payload);
|
||||
|
||||
Request.Builder builder = new Request.Builder();
|
||||
builder.url(getAllScopesUrl);
|
||||
if (!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
|
||||
+ tokenInfo.getAccessToken());
|
||||
}
|
||||
builder.post(requestBody);
|
||||
Request request = builder.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()) {
|
||||
if (!token) {
|
||||
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
||||
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
||||
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
||||
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
||||
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
||||
//TODO: max attempt count
|
||||
TokenInfo refreshedTokenInfo = new TokenInfo();
|
||||
refreshedTokenInfo.setApiApplicationInfo(refreshedApiApplicationInfo);
|
||||
refreshedTokenInfo.setAccessToken(null);
|
||||
return mapApplicationKeys(refreshedTokenInfo, application, keyManager, keyType);
|
||||
} else {
|
||||
String msg = "Invalid access token. Unauthorized request";
|
||||
log.error(msg);
|
||||
throw new APIServicesException(msg);
|
||||
}
|
||||
} 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 ApplicationKey getKeyDetails(TokenInfo tokenInfo, String applicationId, String keyMapId)
|
||||
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
||||
@ -733,7 +798,7 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
||||
private boolean isTokenNull(ApiApplicationInfo apiApplicationInfo, String accessToken) throws BadRequestException {
|
||||
|
||||
boolean token;
|
||||
if ((!(accessToken == null) && apiApplicationInfo == null)) {
|
||||
if ((!(accessToken == null))) {
|
||||
token = true;
|
||||
} else if (!(apiApplicationInfo == null) && accessToken == null) {
|
||||
token = false;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user