|
|
|
@ -20,16 +20,11 @@ package io.entgra.device.mgt.core.ui.request.interceptor.util;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonFactory;
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
|
import com.fasterxml.jackson.databind.node.TextNode;
|
|
|
|
|
import com.google.gson.*;
|
|
|
|
|
import io.entgra.device.mgt.core.ui.request.interceptor.beans.AuthData;
|
|
|
|
|
import io.entgra.device.mgt.core.ui.request.interceptor.cache.LoginCache;
|
|
|
|
|
import org.apache.commons.fileupload.FileItem;
|
|
|
|
@ -75,12 +70,7 @@ import java.security.KeyManagementException;
|
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class HandlerUtil {
|
|
|
|
|
|
|
|
|
@ -122,7 +112,7 @@ public class HandlerUtil {
|
|
|
|
|
JsonNode responseData = getResponseDataAsJsonNode(responseEntity);
|
|
|
|
|
if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
|
|
|
|
|
handlerResponse.setCode(statusCode);
|
|
|
|
|
handlerResponse.setData(responseData);
|
|
|
|
|
handlerResponse.setData(responseData.textValue());
|
|
|
|
|
handlerResponse.setStatus(ProxyResponse.Status.SUCCESS);
|
|
|
|
|
handlerResponse.setExecutorResponse("SUCCESS");
|
|
|
|
|
handlerResponse.setHeaders(response.getHeaders());
|
|
|
|
@ -137,7 +127,7 @@ public class HandlerUtil {
|
|
|
|
|
"Received " + statusCode + " response for http request : " + httpRequest.getMethod()
|
|
|
|
|
+ " " + httpRequest.getRequestUri() + ". Error message: " + responseData.textValue());
|
|
|
|
|
handlerResponse.setCode(statusCode);
|
|
|
|
|
handlerResponse.setData(responseData);
|
|
|
|
|
handlerResponse.setData(responseData.textValue());
|
|
|
|
|
handlerResponse.setStatus(ProxyResponse.Status.ERROR);
|
|
|
|
|
handlerResponse.setExecutorResponse(
|
|
|
|
|
HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(statusCode));
|
|
|
|
@ -147,7 +137,7 @@ public class HandlerUtil {
|
|
|
|
|
log.error("Received " + statusCode + " response for http request : " + httpRequest.getMethod()
|
|
|
|
|
+ " " + httpRequest.getRequestUri() + ". Error message: " + responseData.textValue());
|
|
|
|
|
handlerResponse.setCode(statusCode);
|
|
|
|
|
handlerResponse.setData(responseData);
|
|
|
|
|
handlerResponse.setData(new TextNode(responseData.toString()).textValue());
|
|
|
|
|
handlerResponse.setStatus(ProxyResponse.Status.ERROR);
|
|
|
|
|
handlerResponse
|
|
|
|
|
.setExecutorResponse(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(statusCode));
|
|
|
|
@ -276,13 +266,13 @@ public class HandlerUtil {
|
|
|
|
|
resp.setStatus(proxyResponse.getCode());
|
|
|
|
|
resp.setContentType(ContentType.APPLICATION_JSON.getMimeType());
|
|
|
|
|
resp.setCharacterEncoding(Consts.UTF_8.name());
|
|
|
|
|
JsonNode responseData = proxyResponse.getData();
|
|
|
|
|
String responseData = proxyResponse.getData();
|
|
|
|
|
|
|
|
|
|
if (!(responseData == null)) {
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
Map<String, Object> newNodeMap = new HashMap<>();
|
|
|
|
|
newNodeMap.put("data", responseData);
|
|
|
|
|
responseData = mapper.convertValue(newNodeMap, JsonNode.class);
|
|
|
|
|
responseData = mapper.convertValue(newNodeMap, JsonNode.class).textValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try (PrintWriter writer = resp.getWriter()) {
|
|
|
|
@ -481,7 +471,7 @@ public class HandlerUtil {
|
|
|
|
|
* @param supportedGrantTypes - supported grant types
|
|
|
|
|
* @return {@link StringEntity} of the payload to create the client application
|
|
|
|
|
*/
|
|
|
|
|
public static StringEntity constructAppRegPayload(ArrayNode tags, String appName, String username, String password,
|
|
|
|
|
public static StringEntity constructAppRegPayload(JsonArray tags, String appName, String username, String password,
|
|
|
|
|
String callbackUrl, ArrayList<String> supportedGrantTypes) {
|
|
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
@ -512,7 +502,7 @@ public class HandlerUtil {
|
|
|
|
|
* @param resp - HttpServletResponse
|
|
|
|
|
* @return {@link JsonNode} of UI configurations
|
|
|
|
|
*/
|
|
|
|
|
public static JsonNode getUIConfigAndPersistInSession(String uiConfigUrl, String gatewayUrl, HttpSession httpSession,
|
|
|
|
|
public static JsonObject getUIConfigAndPersistInSession(String uiConfigUrl, String gatewayUrl, HttpSession httpSession,
|
|
|
|
|
HttpServletResponse resp) throws IOException {
|
|
|
|
|
HttpGet uiConfigEndpoint = new HttpGet(uiConfigUrl);
|
|
|
|
|
ProxyResponse uiConfigResponse = HandlerUtil.execute(uiConfigEndpoint);
|
|
|
|
@ -523,15 +513,25 @@ public class HandlerUtil {
|
|
|
|
|
HandlerUtil.handleError(resp, uiConfigResponse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JsonNode responseData = uiConfigResponse.getData();
|
|
|
|
|
if (responseData == null) {
|
|
|
|
|
if (uiConfigResponse.getData() == null) {
|
|
|
|
|
log.error("UI config retrieval is failed, and didn't find UI configuration for App manager.");
|
|
|
|
|
HandlerUtil.handleError(resp, null);
|
|
|
|
|
} else {
|
|
|
|
|
httpSession.setAttribute(HandlerConstants.UI_CONFIG_KEY, responseData);
|
|
|
|
|
}
|
|
|
|
|
JsonParser jsonParser = new JsonParser();
|
|
|
|
|
|
|
|
|
|
JsonElement uiConfigJsonElement = jsonParser.parse(uiConfigResponse.getData());
|
|
|
|
|
JsonObject uiConfigJsonObject = null;
|
|
|
|
|
if (uiConfigJsonElement.isJsonObject()) {
|
|
|
|
|
uiConfigJsonObject = uiConfigJsonElement.getAsJsonObject();
|
|
|
|
|
if (uiConfigJsonObject == null) {
|
|
|
|
|
log.error(
|
|
|
|
|
"Either UI config json element is not an json object or converting rom json element to json object is failed.");
|
|
|
|
|
HandlerUtil.handleError(resp, null);
|
|
|
|
|
}
|
|
|
|
|
httpSession.setAttribute(HandlerConstants.UI_CONFIG_KEY, uiConfigJsonObject);
|
|
|
|
|
httpSession.setAttribute(HandlerConstants.PLATFORM, gatewayUrl);
|
|
|
|
|
}
|
|
|
|
|
return responseData;
|
|
|
|
|
return uiConfigJsonObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
@ -540,11 +540,12 @@ public class HandlerUtil {
|
|
|
|
|
* @param scopes - scope Array and it is retrieved by reading UI config.
|
|
|
|
|
* @return string value of the defined scopes
|
|
|
|
|
*/
|
|
|
|
|
public static String getScopeString(JsonNode scopes) {
|
|
|
|
|
if (scopes != null && scopes.isArray() && !scopes.isEmpty()) {
|
|
|
|
|
public static String getScopeString(JsonArray scopes) {
|
|
|
|
|
if (scopes != null && scopes.size() > 0) {
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
for (JsonNode objNode : scopes) {
|
|
|
|
|
builder.append(objNode.asText()).append(" ");
|
|
|
|
|
for (JsonElement scope : scopes) {
|
|
|
|
|
String tmpScope = scope.getAsString() + " ";
|
|
|
|
|
builder.append(tmpScope);
|
|
|
|
|
}
|
|
|
|
|
return builder.toString();
|
|
|
|
|
} else {
|
|
|
|
@ -648,7 +649,7 @@ public class HandlerUtil {
|
|
|
|
|
return tokenResultResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JsonNode tokenResponse = tokenResultResponse.getData();
|
|
|
|
|
String tokenResponse = tokenResultResponse.getData();
|
|
|
|
|
if (tokenResponse != null) {
|
|
|
|
|
setNewAuthData(constructAuthDataFromTokenResult(tokenResponse, authData), session);
|
|
|
|
|
return tokenResultResponse;
|
|
|
|
@ -694,11 +695,14 @@ public class HandlerUtil {
|
|
|
|
|
* @param authData {@link AuthData} existing auth data values
|
|
|
|
|
* @return new {@link AuthData} object
|
|
|
|
|
*/
|
|
|
|
|
public static AuthData constructAuthDataFromTokenResult(JsonNode tokenResult, AuthData authData) {
|
|
|
|
|
public static AuthData constructAuthDataFromTokenResult(String tokenResult, AuthData authData) {
|
|
|
|
|
AuthData newAuthData = new AuthData();
|
|
|
|
|
newAuthData.setAccessToken(tokenResult.get("access_token").textValue());
|
|
|
|
|
newAuthData.setRefreshToken(tokenResult.get("refresh_token").textValue());
|
|
|
|
|
newAuthData.setScope(tokenResult.get("scope"));
|
|
|
|
|
JsonParser jsonParser = new JsonParser();
|
|
|
|
|
JsonElement jTokenResult = jsonParser.parse(tokenResult);
|
|
|
|
|
JsonObject jTokenResultAsJsonObject = jTokenResult.getAsJsonObject();
|
|
|
|
|
newAuthData.setAccessToken(jTokenResultAsJsonObject.get("access_token").getAsString());
|
|
|
|
|
newAuthData.setRefreshToken(jTokenResultAsJsonObject.get("refresh_token").getAsString());
|
|
|
|
|
newAuthData.setScope(jTokenResultAsJsonObject.get("scope").getAsString());
|
|
|
|
|
newAuthData.setClientId(authData.getClientId());
|
|
|
|
|
newAuthData.setClientSecret(authData.getClientSecret());
|
|
|
|
|
newAuthData.setEncodedClientApp(authData.getEncodedClientApp());
|
|
|
|
|