forked from community/device-mgt-core
parent
e920df6da3
commit
aab94d8fdd
@ -1,96 +0,0 @@
|
|||||||
package io.entgra.devicemgt.apimgt.extension.publisher.api;
|
|
||||||
|
|
||||||
import io.entgra.devicemgt.apimgt.extension.publisher.api.constants.Constants;
|
|
||||||
import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.APIApplicationKey;
|
|
||||||
import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.AccessTokenInfo;
|
|
||||||
import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.APIApplicationServicesException;
|
|
||||||
import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.BadRequestException;
|
|
||||||
import okhttp3.*;
|
|
||||||
import org.apache.commons.httpclient.HttpStatus;
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.apache.commons.ssl.Base64;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.wso2.carbon.apimgt.api.model.Scope;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import static io.entgra.devicemgt.apimgt.extension.publisher.api.APIApplicationServicesImpl.getOkHttpClient;
|
|
||||||
|
|
||||||
public class PublisherRESTAPIServices {
|
|
||||||
private static final Log log = LogFactory.getLog(PublisherRESTAPIServices.class);
|
|
||||||
private static final OkHttpClient client = getOkHttpClient();
|
|
||||||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
|
||||||
|
|
||||||
public boolean isSharedScopeNameExists(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String key)
|
|
||||||
throws APIApplicationServicesException, BadRequestException {
|
|
||||||
|
|
||||||
String keyValue = new String(Base64.encodeBase64((key).getBytes())).replace("=", "");
|
|
||||||
String getScopeUrl = "https://localhost:9443/api/am/publisher/v2/scopes/" + keyValue;
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
|
||||||
.url(getScopeUrl)
|
|
||||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccessToken())
|
|
||||||
.head()
|
|
||||||
.build();
|
|
||||||
try {
|
|
||||||
Response response = client.newCall(request).execute();
|
|
||||||
if (response.code() == HttpStatus.SC_OK){
|
|
||||||
return true;
|
|
||||||
}else if(HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
||||||
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
||||||
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
||||||
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefreshToken(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret() );
|
|
||||||
//max attempt count
|
|
||||||
return isSharedScopeNameExists(apiApplicationKey,refreshedAccessToken, key);
|
|
||||||
} else if (HttpStatus.SC_BAD_REQUEST == response.code()){
|
|
||||||
log.info(response);
|
|
||||||
throw new BadRequestException(response.toString());
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
String msg = "Error occurred while processing the response";
|
|
||||||
throw new APIApplicationServicesException(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean updateSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope)
|
|
||||||
throws APIApplicationServicesException, BadRequestException {
|
|
||||||
|
|
||||||
String updateScopeUrl = "https://localhost:9443/api/am/publisher/v2/scopes/" + scope.getId();
|
|
||||||
|
|
||||||
JSONObject setScope = new JSONObject();
|
|
||||||
setScope.put("name", scope.getKey());
|
|
||||||
setScope.put("displayName", scope.getName());
|
|
||||||
setScope.put("description", scope.getDescription());
|
|
||||||
setScope.put("bindings", scope.getRoles());
|
|
||||||
|
|
||||||
|
|
||||||
RequestBody requestBody = RequestBody.Companion.create(setScope.toString(), JSON);
|
|
||||||
Request request = new Request.Builder()
|
|
||||||
.url(updateScopeUrl)
|
|
||||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccessToken())
|
|
||||||
.put(requestBody)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
try {
|
|
||||||
Response response = client.newCall(request).execute();
|
|
||||||
if (response.code() == HttpStatus.SC_OK) {
|
|
||||||
return true;
|
|
||||||
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
||||||
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
||||||
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
||||||
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefreshToken(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
||||||
return updateSharedScope(apiApplicationKey, refreshedAccessToken, scope);
|
|
||||||
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
||||||
log.info(response);
|
|
||||||
throw new BadRequestException(response.toString());
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
String msg = "Error occurred while processing the response";
|
|
||||||
throw new APIApplicationServicesException(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,53 +0,0 @@
|
|||||||
package io.entgra.devicemgt.apimgt.extension.publisher.api.dto;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This holds the token information that return from the token endpoint.
|
|
||||||
*/
|
|
||||||
public class AccessTokenInfo {
|
|
||||||
|
|
||||||
private String tokenType;
|
|
||||||
private long expiresIn;
|
|
||||||
private String refreshToken;
|
|
||||||
private String accessToken;
|
|
||||||
private String scope;
|
|
||||||
|
|
||||||
public String getTokenType() {
|
|
||||||
return tokenType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTokenType(String tokenType) {
|
|
||||||
this.tokenType = tokenType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getExpiresIn() {
|
|
||||||
return expiresIn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setExpiresIn(long expiresIn) {
|
|
||||||
this.expiresIn = expiresIn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getRefreshToken() {
|
|
||||||
return refreshToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRefreshToken(String refreshToken) {
|
|
||||||
this.refreshToken = refreshToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAccessToken() {
|
|
||||||
return accessToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAccessToken(String accessToken) {
|
|
||||||
this.accessToken = accessToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getScope() {
|
|
||||||
return scope;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setScope(String scope) {
|
|
||||||
this.scope = scope;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
package io.entgra.devicemgt.apimgt.extension.publisher.api.internal;
|
|
||||||
|
|
||||||
import io.entgra.devicemgt.apimgt.extension.publisher.api.APIApplicationServices;
|
|
||||||
import io.entgra.devicemgt.apimgt.extension.publisher.api.PublisherRESTAPIServices;
|
|
||||||
|
|
||||||
public class PublisherRESTAPIDataHolder {
|
|
||||||
|
|
||||||
private static final PublisherRESTAPIDataHolder thisInstance = new PublisherRESTAPIDataHolder();
|
|
||||||
|
|
||||||
private APIApplicationServices apiApplicationServices;
|
|
||||||
//
|
|
||||||
// private PublisherRESTAPIServices publisherRESTAPIServices;
|
|
||||||
|
|
||||||
public static PublisherRESTAPIDataHolder getInstance(){
|
|
||||||
return thisInstance;
|
|
||||||
}
|
|
||||||
public APIApplicationServices getApiApplicationServices() {
|
|
||||||
return apiApplicationServices;
|
|
||||||
}
|
|
||||||
public void setApiApplicationServices(APIApplicationServices apiApplicationServices) {
|
|
||||||
this.apiApplicationServices = apiApplicationServices;
|
|
||||||
}
|
|
||||||
// public PublisherRESTAPIServices getPublisherRESTAPIServices() {
|
|
||||||
// return publisherRESTAPIServices;
|
|
||||||
// }
|
|
||||||
// public void setPublisherRESTAPIServices(PublisherRESTAPIServices publisherRESTAPIServices) {
|
|
||||||
// this.publisherRESTAPIServices = publisherRESTAPIServices;
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
|
@ -1,67 +0,0 @@
|
|||||||
package io.entgra.devicemgt.apimgt.extension.publisher.api.util;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
||||||
import org.apache.http.conn.ssl.SSLContextBuilder;
|
|
||||||
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
|
|
||||||
import org.apache.http.impl.client.HttpClients;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
|
||||||
import org.wso2.carbon.utils.CarbonUtils;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.security.KeyManagementException;
|
|
||||||
import java.security.KeyStoreException;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
|
|
||||||
public class PublisherRESTAPIUtil {
|
|
||||||
private static final Log log = LogFactory.getLog(PublisherRESTAPIUtil.class);
|
|
||||||
private static final String HTTPS_PROTOCOL = "https";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a http client instance
|
|
||||||
*
|
|
||||||
* @param protocol- service endpoint protocol http/https
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static HttpClient getHttpClient(String protocol)
|
|
||||||
throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
|
|
||||||
HttpClient httpclient;
|
|
||||||
if (HTTPS_PROTOCOL.equals(protocol)) {
|
|
||||||
SSLContextBuilder builder = new SSLContextBuilder();
|
|
||||||
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
|
|
||||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
|
|
||||||
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).useSystemProperties().build();
|
|
||||||
} else {
|
|
||||||
httpclient = HttpClients.createDefault();
|
|
||||||
}
|
|
||||||
return httpclient;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getResponseString(HttpResponse httpResponse) throws IOException {
|
|
||||||
BufferedReader br = null;
|
|
||||||
try {
|
|
||||||
br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
|
|
||||||
String readLine;
|
|
||||||
String response = "";
|
|
||||||
while (((readLine = br.readLine()) != null)) {
|
|
||||||
response += readLine;
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
} finally {
|
|
||||||
EntityUtils.consumeQuietly(httpResponse.getEntity());
|
|
||||||
if (br != null) {
|
|
||||||
try {
|
|
||||||
br.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.warn("Error while closing the connection! " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
package io.entgra.devicemgt.apimgt.extension.publisher.api.util;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class represents the data that are required to register
|
|
||||||
* the oauth application.
|
|
||||||
*/
|
|
||||||
public class ScopeUtils {
|
|
||||||
|
|
||||||
private String key;
|
|
||||||
private String name;
|
|
||||||
private String roles;
|
|
||||||
private String description;
|
|
||||||
|
|
||||||
public String getKey() {
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setKey(String key) {
|
|
||||||
this.key = key;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getRoles() {
|
|
||||||
return roles;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRoles(String roles) {
|
|
||||||
this.roles = roles;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() {
|
|
||||||
return description;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDescription(String description) {
|
|
||||||
this.description = description;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toJSON() {
|
|
||||||
String jsonString =
|
|
||||||
"{\"name\": \"" + key + "\",\"displayName\": \"" + name +
|
|
||||||
"\", \"description\": \"" + description + "\"," + "\"bindings\": [" +
|
|
||||||
roles + "]" + " }";
|
|
||||||
|
|
||||||
// String jsonString =
|
|
||||||
// "{\"name\": \"" + name + "\",\"displayName\": \"" + name +
|
|
||||||
// "\", \"description\": \"" + description + "\"," + "\"bindings\": [" +
|
|
||||||
// "\"Internal/devicemgt-user\"" +
|
|
||||||
// "]" + " }";
|
|
||||||
return jsonString;
|
|
||||||
}
|
|
||||||
}
|
|
8
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/APIApplicationServices.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/APIApplicationServices.java
8
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/APIApplicationServices.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/APIApplicationServices.java
@ -1,8 +1,8 @@
|
|||||||
package io.entgra.devicemgt.apimgt.extension.publisher.api;
|
package io.entgra.devicemgt.apimgt.extension.rest.api;
|
||||||
|
|
||||||
import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.APIApplicationKey;
|
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||||
import io.entgra.devicemgt.apimgt.extension.publisher.api.dto.AccessTokenInfo;
|
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||||
import io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions.APIApplicationServicesException;
|
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.APIApplicationServicesException;
|
||||||
|
|
||||||
public interface APIApplicationServices {
|
public interface APIApplicationServices {
|
||||||
|
|
57
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/APIApplicationServicesImpl.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/APIApplicationServicesImpl.java
57
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/APIApplicationServicesImpl.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/APIApplicationServicesImpl.java
@ -0,0 +1,137 @@
|
|||||||
|
package io.entgra.devicemgt.apimgt.extension.rest.api;
|
||||||
|
|
||||||
|
import io.entgra.devicemgt.apimgt.extension.rest.api.constants.Constants;
|
||||||
|
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||||
|
import io.entgra.devicemgt.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||||
|
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.APIApplicationServicesException;
|
||||||
|
import io.entgra.devicemgt.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||||
|
import io.entgra.devicemgt.apimgt.extension.rest.api.util.ScopeUtils;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.apache.commons.ssl.Base64;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.Scope;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServicesImpl.getOkHttpClient;
|
||||||
|
|
||||||
|
public class PublisherRESTAPIServices {
|
||||||
|
private static final Log log = LogFactory.getLog(PublisherRESTAPIServices.class);
|
||||||
|
private static final OkHttpClient client = getOkHttpClient();
|
||||||
|
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||||
|
|
||||||
|
public JSONObject getScopes(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo)
|
||||||
|
throws APIApplicationServicesException, BadRequestException {
|
||||||
|
|
||||||
|
String getScopesUrl = "https://localhost:9443/api/am/publisher/v2/scopes?limit=1000";
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(getScopesUrl)
|
||||||
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccess_token())
|
||||||
|
.get()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.code() == HttpStatus.SC_OK) {
|
||||||
|
JSONObject jsonObject = new JSONObject(response.body().string());
|
||||||
|
return jsonObject;
|
||||||
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
||||||
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
||||||
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
||||||
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
||||||
|
//TODO: max attempt count
|
||||||
|
return getScopes(apiApplicationKey, refreshedAccessToken);
|
||||||
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
||||||
|
log.info(response);
|
||||||
|
throw new BadRequestException(response.toString());
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
String msg = "Error occurred while processing the response";
|
||||||
|
throw new APIApplicationServicesException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSharedScopeNameExists(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String key)
|
||||||
|
throws APIApplicationServicesException, BadRequestException {
|
||||||
|
|
||||||
|
String keyValue = new String(Base64.encodeBase64((key).getBytes())).replace("=", "");
|
||||||
|
String getScopeUrl = "https://localhost:9443/api/am/publisher/v2/scopes/" + keyValue;
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(getScopeUrl)
|
||||||
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccess_token())
|
||||||
|
.head()
|
||||||
|
.build();
|
||||||
|
try {
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.code() == HttpStatus.SC_OK) {
|
||||||
|
return true;
|
||||||
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
||||||
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
||||||
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
||||||
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
||||||
|
//TODO: max attempt count
|
||||||
|
return isSharedScopeNameExists(apiApplicationKey, refreshedAccessToken, key);
|
||||||
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
||||||
|
log.info(response);
|
||||||
|
throw new BadRequestException(response.toString());
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
String msg = "Error occurred while processing the response";
|
||||||
|
throw new APIApplicationServicesException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean updateSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope)
|
||||||
|
throws APIApplicationServicesException, BadRequestException {
|
||||||
|
|
||||||
|
String updateScopeUrl = "https://localhost:9443/api/am/publisher/v2/scopes/" + scope.getId();
|
||||||
|
|
||||||
|
ScopeUtils scopeUtil = new ScopeUtils();
|
||||||
|
scopeUtil.setKey(scope.getKey());
|
||||||
|
scopeUtil.setName(scope.getName());
|
||||||
|
scopeUtil.setDescription(scope.getDescription());
|
||||||
|
scopeUtil.setRoles(scope.getRoles());
|
||||||
|
String scopeString = scopeUtil.toJSON();
|
||||||
|
|
||||||
|
RequestBody requestBody = RequestBody.create(JSON, scopeString);
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(updateScopeUrl)
|
||||||
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, "Bearer " + accessTokenInfo.getAccess_token())
|
||||||
|
.put(requestBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.code() == HttpStatus.SC_OK) {
|
||||||
|
return true;
|
||||||
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
||||||
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
||||||
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
||||||
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
||||||
|
//TODO: max attempt count
|
||||||
|
return updateSharedScope(apiApplicationKey, refreshedAccessToken, scope);
|
||||||
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
||||||
|
log.info(response);
|
||||||
|
throw new BadRequestException(response.toString());
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
String msg = "Error occurred while processing the response";
|
||||||
|
throw new APIApplicationServicesException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/bean/RegistrationProfile.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/bean/RegistrationProfile.java
2
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/bean/RegistrationProfile.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/bean/RegistrationProfile.java
@ -1,4 +1,4 @@
|
|||||||
package io.entgra.devicemgt.apimgt.extension.publisher.api.bean;
|
package io.entgra.devicemgt.apimgt.extension.rest.api.bean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class represents the data that are required to register
|
* This class represents the data that are required to register
|
2
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/constants/Constants.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/constants/Constants.java
2
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/constants/Constants.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/constants/Constants.java
@ -1,4 +1,4 @@
|
|||||||
package io.entgra.devicemgt.apimgt.extension.publisher.api.constants;
|
package io.entgra.devicemgt.apimgt.extension.rest.api.constants;
|
||||||
|
|
||||||
public final class Constants {
|
public final class Constants {
|
||||||
|
|
2
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/dto/APIApplicationKey.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/dto/APIApplicationKey.java
2
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/dto/APIApplicationKey.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/dto/APIApplicationKey.java
@ -1,4 +1,4 @@
|
|||||||
package io.entgra.devicemgt.apimgt.extension.publisher.api.dto;
|
package io.entgra.devicemgt.apimgt.extension.rest.api.dto;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This holds api application consumer id and secret.
|
* This holds api application consumer id and secret.
|
@ -0,0 +1,53 @@
|
|||||||
|
package io.entgra.devicemgt.apimgt.extension.rest.api.dto;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This holds the token information that return from the token endpoint.
|
||||||
|
*/
|
||||||
|
public class AccessTokenInfo {
|
||||||
|
|
||||||
|
private String token_type;
|
||||||
|
private long expires_in;
|
||||||
|
private String refresh_token;
|
||||||
|
private String access_token;
|
||||||
|
private String scope;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
2
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/exceptions/APIApplicationServicesException.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/exceptions/APIApplicationServicesException.java
2
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/exceptions/APIApplicationServicesException.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/exceptions/APIApplicationServicesException.java
@ -1,4 +1,4 @@
|
|||||||
package io.entgra.devicemgt.apimgt.extension.publisher.api.exceptions;
|
package io.entgra.devicemgt.apimgt.extension.rest.api.exceptions;
|
||||||
|
|
||||||
public class APIApplicationServicesException extends Exception{
|
public class APIApplicationServicesException extends Exception{
|
||||||
|
|
4
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/exceptions/BadRequestException.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/exceptions/BadRequestException.java
4
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/exceptions/BadRequestException.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/exceptions/BadRequestException.java
@ -0,0 +1,21 @@
|
|||||||
|
package io.entgra.devicemgt.apimgt.extension.rest.api.internal;
|
||||||
|
|
||||||
|
import io.entgra.devicemgt.apimgt.extension.rest.api.APIApplicationServices;
|
||||||
|
|
||||||
|
public class PublisherRESTAPIDataHolder {
|
||||||
|
|
||||||
|
private static final PublisherRESTAPIDataHolder thisInstance = new PublisherRESTAPIDataHolder();
|
||||||
|
|
||||||
|
private APIApplicationServices apiApplicationServices;
|
||||||
|
|
||||||
|
public static PublisherRESTAPIDataHolder getInstance(){
|
||||||
|
return thisInstance;
|
||||||
|
}
|
||||||
|
public APIApplicationServices getApiApplicationServices() {
|
||||||
|
return apiApplicationServices;
|
||||||
|
}
|
||||||
|
public void setApiApplicationServices(APIApplicationServices apiApplicationServices) {
|
||||||
|
this.apiApplicationServices = apiApplicationServices;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
13
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/internal/PublisherRESTAPIServiceComponent.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java
13
components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.publisher.api/src/main/java/io.entgra.devicemgt.apimgt.extension.publisher.api/internal/PublisherRESTAPIServiceComponent.java → components/apimgt-extensions/io.entgra.devicemgt.apimgt.extension.rest.api/src/main/java/io/entgra/devicemgt/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java
@ -0,0 +1,57 @@
|
|||||||
|
package io.entgra.devicemgt.apimgt.extension.rest.api.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents the data that are required to register
|
||||||
|
* the oauth application.
|
||||||
|
*/
|
||||||
|
public class ScopeUtils {
|
||||||
|
|
||||||
|
private String key;
|
||||||
|
private String name;
|
||||||
|
private String roles;
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKey(String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRoles() {
|
||||||
|
return roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoles(String roles) {
|
||||||
|
this.roles = roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toJSON() {
|
||||||
|
String jsonString = "{\n" +
|
||||||
|
" \"name\":\" " + key + "\",\n" +
|
||||||
|
" \"displayName\":\" " + name + "\",\n" +
|
||||||
|
" \"description\":\" " + description + " \",\n" +
|
||||||
|
" \"bindings\":[\n" +
|
||||||
|
" \" " + roles + " \"\n" +
|
||||||
|
" ]\n" +
|
||||||
|
"}";
|
||||||
|
return jsonString;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue