Combine methods on creating application and generate tokens

consumerRestApis
Pasindu Rupasinghe 1 year ago
parent 6bf6adf2b5
commit 1889d10937

@ -18,13 +18,14 @@
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.dto.APIApplicationInfo;
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.AccessTokenInfo; 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;
public interface APIApplicationServices { public interface APIApplicationServices {
APIApplicationKey createAndRetrieveApplicationCredentials()
APIApplicationKey createAndRetrieveApplicationCredentials() throws APIServicesException; throws APIServicesException;
APIApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String tags[], APIApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String tags[],
String keyType, String username, String keyType, String username,
@ -32,8 +33,9 @@ public interface APIApplicationServices {
String validityTime, String password) String validityTime, String password)
throws APIServicesException; throws APIServicesException;
AccessTokenInfo generateAccessTokenFromRegisteredApplication(String clientId, String clientSecret) throws APIServicesException; APIApplicationInfo generateAccessTokenFromRegisteredApplication()
throws APIServicesException;
AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String clientId, String clientSecret) throws APIServicesException;
APIApplicationInfo generateAccessTokenFromRefreshToken(APIApplicationInfo applicationInfo)
throws APIServicesException;
} }

@ -19,6 +19,7 @@
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.dto.APIApplicationInfo;
import org.json.JSONObject; import org.json.JSONObject;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.util.HttpsTrustManagerUtils; import io.entgra.device.mgt.core.apimgt.extension.rest.api.util.HttpsTrustManagerUtils;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey; import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
@ -112,7 +113,7 @@ public class APIApplicationServicesImpl implements APIApplicationServices {
} }
@Override @Override
public AccessTokenInfo generateAccessTokenFromRegisteredApplication(String consumerKey, String consumerSecret) public APIApplicationInfo generateAccessTokenFromRegisteredApplication()
throws APIServicesException { throws APIServicesException {
String userName = config.getFirstProperty(Constants.SERVER_USER); String userName = config.getFirstProperty(Constants.SERVER_USER);
@ -123,21 +124,62 @@ public class APIApplicationServicesImpl implements APIApplicationServices {
params.put(Constants.PASSWORD_GRANT_TYPE_USERNAME, userName); params.put(Constants.PASSWORD_GRANT_TYPE_USERNAME, userName);
params.put(Constants.PASSWORD_GRANT_TYPE_PASSWORD, userPassword); params.put(Constants.PASSWORD_GRANT_TYPE_PASSWORD, userPassword);
params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES); params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES);
return getToken(params, consumerKey, consumerSecret);
APIApplicationKey apiApplicationKey = createAndRetrieveApplicationCredentials();
AccessTokenInfo accessTokenInfo = getToken(params, apiApplicationKey.getClientId(),
apiApplicationKey.getClientSecret());
APIApplicationInfo applicationInfo = null;
applicationInfo.setClientName(apiApplicationKey.getClientName());
applicationInfo.setClientId(apiApplicationKey.getClientId());
applicationInfo.setClientSecret(apiApplicationKey.getClientSecret());
applicationInfo.setCallBackURL(applicationInfo.getCallBackURL());
applicationInfo.setIsSaasApplication(applicationInfo.getIsSaasApplication());
applicationInfo.setAppOwner(apiApplicationKey.getIsSaasApplication());
applicationInfo.setJsonString(applicationInfo.getJsonString());
applicationInfo.setJsonAppAttribute(apiApplicationKey.getJsonAppAttribute());
applicationInfo.setToken_type(applicationInfo.getToken_type());
applicationInfo.setExpires_in(accessTokenInfo.getExpires_in());
applicationInfo.setRefresh_token(accessTokenInfo.getRefresh_token());
applicationInfo.setAccess_token(accessTokenInfo.getAccess_token());
applicationInfo.setScope(accessTokenInfo.getScope());
return applicationInfo;
} }
@Override @Override
public AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String consumerKey, String consumerSecret) public APIApplicationInfo generateAccessTokenFromRefreshToken(APIApplicationInfo applicationInfo)
throws APIServicesException { throws APIServicesException {
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.REFRESH_TOKEN_GRANT_TYPE); params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.REFRESH_TOKEN_GRANT_TYPE);
params.put(Constants.REFRESH_TOKEN_GRANT_TYPE_PARAM_NAME, refreshToken); params.put(Constants.REFRESH_TOKEN_GRANT_TYPE_PARAM_NAME, applicationInfo.getRefresh_token());
params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES); params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES);
return getToken(params, consumerKey, consumerSecret); AccessTokenInfo refreshedAccessTokenInfo = getToken(params, applicationInfo.getClientId(), applicationInfo.getClientSecret());
APIApplicationInfo refreshedApplicationInfo = null;
refreshedApplicationInfo.setClientName(applicationInfo.getClientName());
refreshedApplicationInfo.setClientId(applicationInfo.getClientId());
refreshedApplicationInfo.setClientSecret(applicationInfo.getClientSecret());
refreshedApplicationInfo.setCallBackURL(applicationInfo.getCallBackURL());
refreshedApplicationInfo.setIsSaasApplication(applicationInfo.getIsSaasApplication());
refreshedApplicationInfo.setAppOwner(applicationInfo.getIsSaasApplication());
refreshedApplicationInfo.setJsonString(applicationInfo.getJsonString());
refreshedApplicationInfo.setJsonAppAttribute(applicationInfo.getJsonAppAttribute());
refreshedApplicationInfo.setToken_type(applicationInfo.getToken_type());
refreshedApplicationInfo.setExpires_in(refreshedAccessTokenInfo.getExpires_in());
refreshedApplicationInfo.setRefresh_token(refreshedAccessTokenInfo.getRefresh_token());
refreshedApplicationInfo.setAccess_token(refreshedAccessTokenInfo.getAccess_token());
refreshedApplicationInfo.setScope(refreshedAccessTokenInfo.getScope());
return refreshedApplicationInfo;
} }
public AccessTokenInfo getToken(JSONObject nameValuePairs, String clientId, String clientSecret) private AccessTokenInfo getToken(JSONObject nameValuePairs, String clientId, String clientSecret)
throws APIServicesException { throws APIServicesException {
String tokenEndPoint = config.getFirstProperty(Constants.TOKE_END_POINT); String tokenEndPoint = config.getFirstProperty(Constants.TOKE_END_POINT);
@ -158,4 +200,58 @@ public class APIApplicationServicesImpl implements APIApplicationServices {
throw new APIServicesException(e); throw new APIServicesException(e);
} }
} }
@Override
public APIApplicationInfo apiApplicationInfoFromRegisterApplication() throws APIServicesException {
APIApplicationKey apiApplicationKey = createAndRetrieveApplicationCredentials();
AccessTokenInfo accessTokenInfo = generateAccessTokenFromRegisteredApplication(apiApplicationKey.getClientId(),
apiApplicationKey.getClientSecret());
APIApplicationInfo applicationInfo = null;
applicationInfo.setClientName(apiApplicationKey.getClientName());
applicationInfo.setClientId(apiApplicationKey.getClientId());
applicationInfo.setClientSecret(apiApplicationKey.getClientSecret());
applicationInfo.setCallBackURL(applicationInfo.getCallBackURL());
applicationInfo.setIsSaasApplication(applicationInfo.getIsSaasApplication());
applicationInfo.setAppOwner(apiApplicationKey.getIsSaasApplication());
applicationInfo.setJsonString(applicationInfo.getJsonString());
applicationInfo.setJsonAppAttribute(apiApplicationKey.getJsonAppAttribute());
applicationInfo.setToken_type(applicationInfo.getToken_type());
applicationInfo.setExpires_in(accessTokenInfo.getExpires_in());
applicationInfo.setRefresh_token(accessTokenInfo.getRefresh_token());
applicationInfo.setAccess_token(accessTokenInfo.getAccess_token());
applicationInfo.setScope(accessTokenInfo.getScope());
return applicationInfo;
}
@Override
public APIApplicationInfo apiApplicationInfoFromRefreshToken() throws APIServicesException {
APIApplicationKey apiApplicationKey = createAndRetrieveApplicationCredentials();
AccessTokenInfo accessTokenInfo = generateAccessTokenFromRegisteredApplication(apiApplicationKey.getClientId(),
apiApplicationKey.getClientSecret());
APIApplicationInfo applicationInfo = null;
applicationInfo.setClientName(apiApplicationKey.getClientName());
applicationInfo.setClientId(apiApplicationKey.getClientId());
applicationInfo.setClientSecret(apiApplicationKey.getClientSecret());
applicationInfo.setCallBackURL(applicationInfo.getCallBackURL());
applicationInfo.setIsSaasApplication(applicationInfo.getIsSaasApplication());
applicationInfo.setAppOwner(apiApplicationKey.getIsSaasApplication());
applicationInfo.setJsonString(applicationInfo.getJsonString());
applicationInfo.setJsonAppAttribute(apiApplicationKey.getJsonAppAttribute());
applicationInfo.setToken_type(applicationInfo.getToken_type());
applicationInfo.setExpires_in(accessTokenInfo.getExpires_in());
applicationInfo.setRefresh_token(accessTokenInfo.getRefresh_token());
applicationInfo.setAccess_token(accessTokenInfo.getAccess_token());
applicationInfo.setScope(accessTokenInfo.getScope());
return applicationInfo;
}
} }

@ -18,6 +18,7 @@
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.dto.APIApplicationInfo;
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.AccessTokenInfo; 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;
@ -35,84 +36,38 @@ import org.wso2.carbon.apimgt.api.model.Documentation;
import java.util.List; import java.util.List;
public interface PublisherRESTAPIServices { public interface PublisherRESTAPIServices {
boolean isSharedScopeNameExists(APIApplicationInfo applicationInfo, String key)
JSONObject getScopes(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean isSharedScopeNameExists(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String key)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean addNewSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean updateSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
JSONObject getApi(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIIdentifier apiIdentifier)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
JSONObject getApis(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) boolean addNewSharedScope(APIApplicationInfo applicationInfo, Scope scope)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
JSONObject addAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIInfo api) JSONObject getApi(APIApplicationInfo applicationInfo, APIIdentifier apiIdentifier)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean updateApi(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIInfo api) JSONObject getApis(APIApplicationInfo applicationInfo)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean saveAsyncApiDefinition(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid, JSONObject addAPI(APIApplicationInfo applicationInfo, APIInfo api)
String asyncApiDefinition)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
JSONObject getAllApiSpecificMediationPolicies(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, boolean updateApi(APIApplicationInfo applicationInfo, APIInfo api)
APIIdentifier apiIdentifier)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean addApiSpecificMediationPolicy(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, boolean saveAsyncApiDefinition(APIApplicationInfo applicationInfo, String uuid, String asyncApiDefinition)
String uuid, Mediation mediation)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean updateApiSpecificMediationPolicyContent(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, boolean addApiSpecificMediationPolicy(APIApplicationInfo applicationInfo, String uuid, Mediation mediation)
String uuid, Mediation mediation)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean changeLifeCycleStatus(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, boolean changeLifeCycleStatus(APIApplicationInfo applicationInfo,
String uuid, String action) String uuid, String action)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
JSONObject getAPIRevisions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid, JSONObject addAPIRevision(APIApplicationInfo applicationInfo, APIRevision apiRevision)
Boolean deploymentStatus)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
JSONObject addAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, boolean deployAPIRevision(APIApplicationInfo applicationInfo, String uuid,
APIRevision apiRevision)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean deployAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid,
String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeploymentList) String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeploymentList)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
abstract boolean undeployAPIRevisionDeployment(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
JSONObject apiRevisionDeployment, String uuid)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean deleteAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
JSONObject apiRevision, String uuid)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
JSONObject getDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
String uuid)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean deleteDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
String uuid, String documentID)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
Documentation addDocumentation(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
String uuid, Documentation documentation)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
boolean addDocumentationContent(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
APIInfo api, String docId, String docContent)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
} }

@ -20,6 +20,7 @@ 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.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.APIApplicationInfo;
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.AccessTokenInfo; 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;
@ -99,7 +100,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public boolean isSharedScopeNameExists(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String key) public boolean isSharedScopeNameExists(APIApplicationInfo applicationInfo, String key)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String keyValue = new String(Base64.encodeBase64((key).getBytes())).replace(Constants.QUERY_KEY_VALUE_SEPARATOR, String keyValue = new String(Base64.encodeBase64((key).getBytes())).replace(Constants.QUERY_KEY_VALUE_SEPARATOR,
@ -109,7 +110,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getScopeUrl) .url(getScopeUrl)
.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())
.head() .head()
.build(); .build();
try { try {
@ -118,11 +119,10 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
return true; return true;
} 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. APIApplicationInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo);
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return isSharedScopeNameExists(apiApplicationKey, refreshedAccessToken, key); return isSharedScopeNameExists(applicationInfo,key);
} 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);
@ -143,7 +143,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public boolean addNewSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope) public boolean addNewSharedScope(APIApplicationInfo applicationInfo, Scope scope)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String addNewSharedScopeEndPoint = endPointPrefix + Constants.SCOPE_API_ENDPOINT; String addNewSharedScopeEndPoint = endPointPrefix + Constants.SCOPE_API_ENDPOINT;
@ -159,7 +159,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(addNewSharedScopeEndPoint) .url(addNewSharedScopeEndPoint)
.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();
@ -169,11 +169,10 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
return true; return true;
} 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. APIApplicationInfo refreshedApiApplicationInfo = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo);
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return addNewSharedScope(apiApplicationKey, refreshedAccessToken, scope); return addNewSharedScope(refreshedApiApplicationInfo, scope);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid scope object"; String msg = "Bad Request, Invalid scope object";
log.error(msg); log.error(msg);
@ -237,14 +236,14 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public JSONObject getApi(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIIdentifier apiIdentifier) public JSONObject getApi(APIApplicationInfo applicationInfo, APIIdentifier apiIdentifier)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllApi = endPointPrefix + Constants.API_ENDPOINT + apiIdentifier.getUUID(); String getAllApi = endPointPrefix + Constants.API_ENDPOINT + apiIdentifier.getUUID();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getAllApi) .url(getAllApi)
.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();
@ -255,11 +254,10 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
return jsonObject; return jsonObject;
} 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. APIApplicationInfo refreshedApplicationInfo = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo);
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return getApi(apiApplicationKey, refreshedAccessToken, apiIdentifier); return getApi(refreshedApplicationInfo, apiIdentifier);
} 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);
@ -276,14 +274,14 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public JSONObject getApis(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) public JSONObject getApis(APIApplicationInfo applicationInfo)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllApis = endPointPrefix + Constants.GET_ALL_APIS; String getAllApis = endPointPrefix + Constants.GET_ALL_APIS;
Request request = new Request.Builder() Request request = new Request.Builder()
.url(getAllApis) .url(getAllApis)
.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();
@ -294,11 +292,10 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
return jsonObject; return jsonObject;
} 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. APIApplicationInfo refreshedApiApplicationInfo = apiApplicationServices
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), .generateAccessTokenFromRefreshToken(applicationInfo);
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return getApis(apiApplicationKey, refreshedAccessToken); return getApis(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);
@ -315,7 +312,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public JSONObject addAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIInfo api) public JSONObject addAPI(APIApplicationInfo applicationInfo, APIInfo api)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String addAPIEndPoint = endPointPrefix + Constants.API_ENDPOINT; String addAPIEndPoint = endPointPrefix + Constants.API_ENDPOINT;
@ -370,7 +367,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(addAPIEndPoint) .url(addAPIEndPoint)
.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();
@ -381,11 +378,9 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
return jsonObject; return jsonObject;
} 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. APIApplicationInfo refreshedApplicationToken = apiApplicationServices.generateAccessTokenFromRefreshToken(applicationInfo);
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return addAPI(apiApplicationKey, refreshedAccessToken, api); return addAPI(refreshedApplicationToken, api);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid API request body"; String msg = "Bad Request, Invalid API request body";
log.error(msg); log.error(msg);
@ -402,7 +397,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public boolean updateApi(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIInfo api) public boolean updateApi(APIApplicationInfo applicationInfo, APIInfo api)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String updateAPIEndPoint = endPointPrefix + Constants.API_ENDPOINT + api.getId(); String updateAPIEndPoint = endPointPrefix + Constants.API_ENDPOINT + api.getId();
@ -457,7 +452,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(updateAPIEndPoint) .url(updateAPIEndPoint)
.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())
.put(requestBody) .put(requestBody)
.build(); .build();
@ -468,11 +463,10 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} 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. APIApplicationInfo refreshedApplicationInfo = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo);
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return updateApi(apiApplicationKey, refreshedAccessToken, api); return updateApi(refreshedApplicationInfo, api);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid API request body"; String msg = "Bad Request, Invalid API request body";
log.error(msg); log.error(msg);
@ -489,8 +483,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public boolean saveAsyncApiDefinition(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public boolean saveAsyncApiDefinition(APIApplicationInfo applicationInfo, String uuid, String asyncApiDefinition)
String uuid, String asyncApiDefinition)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String addNewScope = endPointPrefix + Constants.API_ENDPOINT + uuid; String addNewScope = endPointPrefix + Constants.API_ENDPOINT + uuid;
@ -499,7 +492,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(addNewScope) .url(addNewScope)
.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())
.put(requestBody) .put(requestBody)
.build(); .build();
@ -509,11 +502,10 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
return true; return true;
} 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. APIApplicationInfo refreshedApplicationToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo);
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return saveAsyncApiDefinition(apiApplicationKey, refreshedAccessToken, uuid, asyncApiDefinition); return saveAsyncApiDefinition(refreshedApplicationToken, uuid, asyncApiDefinition);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid API definition request body"; String msg = "Bad Request, Invalid API definition request body";
log.error(msg); log.error(msg);
@ -571,8 +563,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public boolean addApiSpecificMediationPolicy(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public boolean addApiSpecificMediationPolicy(APIApplicationInfo applicationInfo, String uuid, Mediation mediation)
String uuid, Mediation mediation)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String addAPIMediation = endPointPrefix + Constants.API_ENDPOINT + uuid + "/mediation-policies/" + mediation.getUuid() String addAPIMediation = endPointPrefix + Constants.API_ENDPOINT + uuid + "/mediation-policies/" + mediation.getUuid()
@ -582,7 +573,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(addAPIMediation) .url(addAPIMediation)
.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();
@ -592,11 +583,10 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
return true; return true;
} 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. APIApplicationInfo refreshedApplicationToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo);
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return addApiSpecificMediationPolicy(apiApplicationKey, refreshedAccessToken, uuid, mediation); return addApiSpecificMediationPolicy(refreshedApplicationToken, uuid, mediation);
} 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);
@ -658,7 +648,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public boolean changeLifeCycleStatus(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public boolean changeLifeCycleStatus(APIApplicationInfo applicationInfo,
String uuid, String action) String uuid, String action)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
@ -669,7 +659,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(changeAPIStatusEndPoint) .url(changeAPIStatusEndPoint)
.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();
@ -679,11 +669,10 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
return true; return true;
} 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. APIApplicationInfo refreshedApplicationInfo = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo);
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return changeLifeCycleStatus(apiApplicationKey, refreshedAccessToken, uuid, action); return changeLifeCycleStatus(refreshedApplicationInfo, uuid, action);
} 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);
@ -742,7 +731,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public JSONObject addAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIRevision apiRevision) public JSONObject addAPIRevision(APIApplicationInfo applicationInfo, APIRevision apiRevision)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
String addNewScope = endPointPrefix + Constants.API_ENDPOINT + apiRevision.getApiUUID() + "/revisions"; String addNewScope = endPointPrefix + Constants.API_ENDPOINT + apiRevision.getApiUUID() + "/revisions";
@ -755,7 +744,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(addNewScope) .url(addNewScope)
.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();
@ -766,11 +755,10 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
return jsonObject; return jsonObject;
} 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. APIApplicationInfo refreshedApplicationInfo = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo);
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return addAPIRevision(apiApplicationKey, refreshedAccessToken, apiRevision); return addAPIRevision(refreshedApplicationInfo, apiRevision);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid API revision request body"; String msg = "Bad Request, Invalid API revision request body";
log.error(msg); log.error(msg);
@ -787,7 +775,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
} }
@Override @Override
public boolean deployAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid, public boolean deployAPIRevision(APIApplicationInfo applicationInfo, String uuid,
String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeploymentList) String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeploymentList)
throws APIServicesException, BadRequestException, UnexpectedResponseException { throws APIServicesException, BadRequestException, UnexpectedResponseException {
@ -806,7 +794,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(deployAPIRevisionEndPoint) .url(deployAPIRevisionEndPoint)
.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();
@ -816,11 +804,10 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
return true; return true;
} 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. APIApplicationInfo refreshedApplicationInfo = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), generateAccessTokenFromRefreshToken(applicationInfo);
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count //TODO: max attempt count
return deployAPIRevision(apiApplicationKey, refreshedAccessToken, uuid, apiRevisionId, return deployAPIRevision(refreshedApplicationInfo, uuid, apiRevisionId,
apiRevisionDeploymentList); apiRevisionDeploymentList);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) { } else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid API revision request body"; String msg = "Bad Request, Invalid API revision request body";

@ -0,0 +1,130 @@
package io.entgra.device.mgt.core.apimgt.extension.rest.api.dto;
public class APIApplicationInfo {
private String clientName;
private String clientId;
private String clientSecret;
private String callBackURL;
private String isSaasApplication;
private String appOwner;
private String jsonString;
private String jsonAppAttribute;
private String tokenType;
private String token_type;
private long expires_in;
private String refresh_token;
private String access_token;
private String scope;
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
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 getCallBackURL() {
return callBackURL;
}
public void setCallBackURL(String callBackURL) {
this.callBackURL = callBackURL;
}
public String getIsSaasApplication() {
return isSaasApplication;
}
public void setIsSaasApplication(String isSaasApplication) {
this.isSaasApplication = isSaasApplication;
}
public String getAppOwner() {
return appOwner;
}
public void setAppOwner(String appOwner) {
this.appOwner = appOwner;
}
public String getJsonString() {
return jsonString;
}
public void setJsonString(String jsonString) {
this.jsonString = jsonString;
}
public String getJsonAppAttribute() {
return jsonAppAttribute;
}
public void setJsonAppAttribute(String jsonAppAttribute) {
this.jsonAppAttribute = jsonAppAttribute;
}
public String getTokenType() {
return tokenType;
}
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public String getToken_type() {
return token_type;
}
public void setToken_type(String token_type) {
this.token_type = token_type;
}
public long getExpires_in() {
return expires_in;
}
public void setExpires_in(long expires_in) {
this.expires_in = expires_in;
}
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;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
}

@ -22,6 +22,7 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationService
import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices; import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServicesImpl; import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServicesImpl;
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.APIApplicationInfo;
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.AccessTokenInfo; 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;
@ -103,12 +104,14 @@ public class APIPublisherServiceImpl implements APIPublisherService {
.getOSGiService(RealmService.class, null); .getOSGiService(RealmService.class, null);
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
APIApplicationKey apiApplicationKey; APIApplicationInfo apiApplicationInfo;
AccessTokenInfo accessTokenInfo; // APIApplicationKey apiApplicationKey;
// AccessTokenInfo accessTokenInfo;
try { try {
apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials(); apiApplicationInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication();
accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication( // apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials();
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); // accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication(
// apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
} catch (APIServicesException e) { } catch (APIServicesException e) {
String errorMsg = "Error occurred while generating the API application"; String errorMsg = "Error occurred while generating the API application";
log.error(errorMsg, e); log.error(errorMsg, e);
@ -156,7 +159,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
apiConfig.getName(), apiConfig.getVersion()); apiConfig.getName(), apiConfig.getVersion());
PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl();
JSONArray apiList = (JSONArray) publisherRESTAPIServices.getApis(apiApplicationKey, accessTokenInfo).get("list"); JSONArray apiList = (JSONArray) publisherRESTAPIServices.getApis(apiApplicationInfo).get("list");
boolean apiFound = false; boolean apiFound = false;
for (int i = 0; i < apiList.length(); i++) { for (int i = 0; i < apiList.length(); i++) {
JSONObject apiObj = apiList.getJSONObject(i); JSONObject apiObj = apiList.getJSONObject(i);
@ -170,21 +173,20 @@ public class APIPublisherServiceImpl implements APIPublisherService {
if (!apiFound) { if (!apiFound) {
// add new scopes as shared scopes // add new scopes as shared scopes
for (ApiScope apiScope : apiConfig.getScopes()) { for (ApiScope apiScope : apiConfig.getScopes()) {
if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationInfo, apiScope.getKey())) {
apiScope.getKey())) {
Scope scope = new Scope(); Scope scope = new Scope();
scope.setName(apiScope.getName()); scope.setName(apiScope.getName());
scope.setDescription(apiScope.getDescription()); scope.setDescription(apiScope.getDescription());
scope.setKey(apiScope.getKey()); scope.setKey(apiScope.getKey());
scope.setRoles(apiScope.getRoles()); scope.setRoles(apiScope.getRoles());
publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope); publisherRESTAPIServices.addNewSharedScope(apiApplicationInfo, scope);
} }
} }
APIInfo api = getAPI(apiConfig, true); APIInfo api = getAPI(apiConfig, true);
JSONObject createdAPI = publisherRESTAPIServices.addAPI(apiApplicationKey, accessTokenInfo, api); JSONObject createdAPI = publisherRESTAPIServices.addAPI(apiApplicationInfo, api);
if (apiConfig.getEndpointType() != null && "WS".equals(apiConfig.getEndpointType())) { if (apiConfig.getEndpointType() != null && "WS".equals(apiConfig.getEndpointType())) {
publisherRESTAPIServices.saveAsyncApiDefinition(apiApplicationKey, accessTokenInfo, publisherRESTAPIServices.saveAsyncApiDefinition(apiApplicationInfo, createdAPI.getString("id"),
createdAPI.getString("id"), apiConfig.getAsyncApiDefinition()); apiConfig.getAsyncApiDefinition());
} }
if (CREATED_STATUS.equals(createdAPI.getString("lifeCycleStatus"))) { if (CREATED_STATUS.equals(createdAPI.getString("lifeCycleStatus"))) {
// if endpoint type "dynamic" and then add in sequence // if endpoint type "dynamic" and then add in sequence
@ -194,17 +196,15 @@ public class APIPublisherServiceImpl implements APIPublisherService {
mediation.setConfig(apiConfig.getInSequenceConfig()); mediation.setConfig(apiConfig.getInSequenceConfig());
mediation.setType("in"); mediation.setType("in");
mediation.setGlobal(false); mediation.setGlobal(false);
publisherRESTAPIServices.addApiSpecificMediationPolicy(apiApplicationKey, publisherRESTAPIServices.addApiSpecificMediationPolicy(apiApplicationInfo,
accessTokenInfo, createdAPI.getString("id"), mediation); createdAPI.getString("id"), mediation);
} }
publisherRESTAPIServices.changeLifeCycleStatus(apiApplicationKey, accessTokenInfo, publisherRESTAPIServices.changeLifeCycleStatus(apiApplicationInfo, createdAPI.getString("id"), PUBLISH_ACTION);
createdAPI.getString("id"), PUBLISH_ACTION);
APIRevision apiRevision = new APIRevision(); APIRevision apiRevision = new APIRevision();
apiRevision.setApiUUID(createdAPI.getString("id")); apiRevision.setApiUUID(createdAPI.getString("id"));
apiRevision.setDescription("Initial Revision"); apiRevision.setDescription("Initial Revision");
String apiRevisionId = publisherRESTAPIServices.addAPIRevision(apiApplicationKey, String apiRevisionId = publisherRESTAPIServices.addAPIRevision(apiApplicationInfo, apiRevision).getString("id");
accessTokenInfo, apiRevision).getString("id");
APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment(); APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
apiRevisionDeployment.setDeployment(API_PUBLISH_ENVIRONMENT); apiRevisionDeployment.setDeployment(API_PUBLISH_ENVIRONMENT);
@ -213,8 +213,8 @@ public class APIPublisherServiceImpl implements APIPublisherService {
List<APIRevisionDeployment> apiRevisionDeploymentList = new ArrayList<>(); List<APIRevisionDeployment> apiRevisionDeploymentList = new ArrayList<>();
apiRevisionDeploymentList.add(apiRevisionDeployment); apiRevisionDeploymentList.add(apiRevisionDeployment);
publisherRESTAPIServices.deployAPIRevision(apiApplicationKey, accessTokenInfo, publisherRESTAPIServices.deployAPIRevision(apiApplicationInfo, createdAPI.getString("id"),
createdAPI.getString("id"), apiRevisionId, apiRevisionDeploymentList); apiRevisionId, apiRevisionDeploymentList);
} }
} else { } else {
if (WebappPublisherConfig.getInstance().isEnabledUpdateApi()) { if (WebappPublisherConfig.getInstance().isEnabledUpdateApi()) {
@ -237,8 +237,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
for (ApiScope apiScope : apiConfig.getScopes()) { for (ApiScope apiScope : apiConfig.getScopes()) {
// if the scope is not available as shared scope, and it is assigned to an API as a local scope // if the scope is not available as shared scope, and it is assigned to an API as a local scope
// need remove the local scope and add as a shared scope // need remove the local scope and add as a shared scope
if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationInfo, apiScope.getKey())) {
apiScope.getKey())) {
//resolve- todo:apim- resolve //resolve- todo:apim- resolve
// if (apiProvider.isScopeKeyAssignedLocally(apiIdentifier, apiScope.getKey(), tenantId)) { // if (apiProvider.isScopeKeyAssignedLocally(apiIdentifier, apiScope.getKey(), tenantId)) {
if (true) { if (true) {
@ -251,20 +250,19 @@ public class APIPublisherServiceImpl implements APIPublisherService {
scope.setDescription(apiScope.getDescription()); scope.setDescription(apiScope.getDescription());
scope.setKey(apiScope.getKey()); scope.setKey(apiScope.getKey());
scope.setRoles(apiScope.getRoles()); scope.setRoles(apiScope.getRoles());
publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope); publisherRESTAPIServices.addNewSharedScope(apiApplicationInfo, scope);
} }
} }
} }
// Get existing API // Get existing API
JSONObject existingAPI = publisherRESTAPIServices.getApi(apiApplicationKey, accessTokenInfo, JSONObject existingAPI = publisherRESTAPIServices.getApi(apiApplicationInfo, apiIdentifier);
apiIdentifier);
if (scopesToMoveAsSharedScopes.size() > 0) { if (scopesToMoveAsSharedScopes.size() > 0) {
// update API to remove local scopes // update API to remove local scopes
APIInfo api = getAPI(apiConfig, false); APIInfo api = getAPI(apiConfig, false);
api.setLifeCycleStatus(existingAPI.getString("lifeCycleStatus")); api.setLifeCycleStatus(existingAPI.getString("lifeCycleStatus"));
publisherRESTAPIServices.updateApi(apiApplicationKey, accessTokenInfo, api); publisherRESTAPIServices.updateApi(apiApplicationInfo, api);
for (ApiScope apiScope : scopesToMoveAsSharedScopes) { for (ApiScope apiScope : scopesToMoveAsSharedScopes) {
Scope scope = new Scope(); Scope scope = new Scope();
@ -272,19 +270,19 @@ public class APIPublisherServiceImpl implements APIPublisherService {
scope.setDescription(apiScope.getDescription()); scope.setDescription(apiScope.getDescription());
scope.setKey(apiScope.getKey()); scope.setKey(apiScope.getKey());
scope.setRoles(apiScope.getRoles()); scope.setRoles(apiScope.getRoles());
publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope); publisherRESTAPIServices.addNewSharedScope(apiApplicationInfo, scope);
} }
} }
existingAPI = publisherRESTAPIServices.getApi(apiApplicationKey, accessTokenInfo, apiIdentifier); existingAPI = publisherRESTAPIServices.getApi(apiApplicationInfo, apiIdentifier);
APIInfo api = getAPI(apiConfig, true); APIInfo api = getAPI(apiConfig, true);
api.setLastUpdatedTime(existingAPI.getString("lifeCycleStatus")); api.setLastUpdatedTime(existingAPI.getString("lifeCycleStatus"));
api.setId(existingAPI.getString("id")); api.setId(existingAPI.getString("id"));
publisherRESTAPIServices.updateApi(apiApplicationKey, accessTokenInfo, api); publisherRESTAPIServices.updateApi(apiApplicationInfo, api);
if (apiConfig.getEndpointType() != null && "WS".equals(apiConfig.getEndpointType())) { if (apiConfig.getEndpointType() != null && "WS".equals(apiConfig.getEndpointType())) {
publisherRESTAPIServices.saveAsyncApiDefinition(apiApplicationKey, accessTokenInfo, publisherRESTAPIServices.saveAsyncApiDefinition(apiApplicationInfo, existingAPI.getString("id"),
existingAPI.getString("id"), apiConfig.getAsyncApiDefinition()); apiConfig.getAsyncApiDefinition());
} }
// if endpoint type "dynamic" and then add /update in sequence // if endpoint type "dynamic" and then add /update in sequence
@ -311,8 +309,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
} }
} }
if (!isMediationPolicyFound) { if (!isMediationPolicyFound) {
publisherRESTAPIServices.addApiSpecificMediationPolicy(apiApplicationKey, publisherRESTAPIServices.addApiSpecificMediationPolicy(apiApplicationInfo, existingAPI.getString("id"), mediation);
accessTokenInfo, existingAPI.getString("id"), mediation);
} }
} }
@ -338,8 +335,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
APIRevision apiRevision = new APIRevision(); APIRevision apiRevision = new APIRevision();
apiRevision.setApiUUID(existingAPI.getString("id")); apiRevision.setApiUUID(existingAPI.getString("id"));
apiRevision.setDescription("Updated Revision"); apiRevision.setDescription("Updated Revision");
String apiRevisionId = publisherRESTAPIServices.addAPIRevision(apiApplicationKey, String apiRevisionId = publisherRESTAPIServices.addAPIRevision(apiApplicationInfo, apiRevision).getString("id");
accessTokenInfo, apiRevision).getString("id");
APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment(); APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
apiRevisionDeployment.setDeployment(API_PUBLISH_ENVIRONMENT); apiRevisionDeployment.setDeployment(API_PUBLISH_ENVIRONMENT);
@ -349,12 +345,11 @@ public class APIPublisherServiceImpl implements APIPublisherService {
List<APIRevisionDeployment> apiRevisionDeploymentList = new ArrayList<>(); List<APIRevisionDeployment> apiRevisionDeploymentList = new ArrayList<>();
apiRevisionDeploymentList.add(apiRevisionDeployment); apiRevisionDeploymentList.add(apiRevisionDeployment);
publisherRESTAPIServices.deployAPIRevision(apiApplicationKey, accessTokenInfo, publisherRESTAPIServices.deployAPIRevision(apiApplicationInfo, existingAPI.getString("id"),
existingAPI.getString("id"), apiRevisionId, apiRevisionDeploymentList); apiRevisionId, apiRevisionDeploymentList);
if (CREATED_STATUS.equals(existingAPI.getString("lifeCycleStatus"))) { if (CREATED_STATUS.equals(existingAPI.getString("lifeCycleStatus"))) {
publisherRESTAPIServices.changeLifeCycleStatus(apiApplicationKey,accessTokenInfo, publisherRESTAPIServices.changeLifeCycleStatus(apiApplicationInfo, existingAPI.getString("id"), PUBLISH_ACTION);
existingAPI.getString("id"), PUBLISH_ACTION);
} }
} }
} }
@ -433,12 +428,14 @@ public class APIPublisherServiceImpl implements APIPublisherService {
tenants.addAll(config.getTenants().getTenant()); tenants.addAll(config.getTenants().getTenant());
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
APIApplicationKey apiApplicationKey; APIApplicationInfo applicationInfo;
AccessTokenInfo accessTokenInfo; // APIApplicationKey apiApplicationKey;
// AccessTokenInfo accessTokenInfo;
try { try {
apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials(); applicationInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication();
accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication( // apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials();
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); // accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication(
// apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
} catch (APIServicesException e) { } catch (APIServicesException e) {
String errorMsg = "Error occurred while generating the API application"; String errorMsg = "Error occurred while generating the API application";
log.error(errorMsg, e); log.error(errorMsg, e);
@ -506,7 +503,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
} }
} }
if (publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, scope.getKey())) { if (publisherRESTAPIServices.isSharedScopeNameExists(applicationInfo, scope.getKey())) {
publisherRESTAPIServices.updateSharedScope(apiApplicationKey, accessTokenInfo, scope); publisherRESTAPIServices.updateSharedScope(apiApplicationKey, accessTokenInfo, scope);
} else { } else {
// todo: come to this level means, that scope is removed from API, but haven't removed from the scope-role-permission-mappings list // todo: come to this level means, that scope is removed from API, but haven't removed from the scope-role-permission-mappings list

Loading…
Cancel
Save