From b8e8450108378651786eb606da2dd06650cb0d9a Mon Sep 17 00:00:00 2001 From: pasindu Date: Thu, 11 Jan 2024 04:17:32 +0530 Subject: [PATCH] Http update fixes --- .../pom.xml | 6 ++++++ .../interceptor/DefaultOauth2TokenHandler.java | 2 +- .../core/ui/request/interceptor/LoginHandler.java | 2 +- .../request/interceptor/PermissionScopeHandler.java | 10 +++++----- .../interceptor/SsoLoginCallbackHandler.java | 2 +- .../core/ui/request/interceptor/UserHandler.java | 4 ++-- .../core/ui/request/interceptor/beans/AuthData.java | 8 +++++--- .../ui/request/interceptor/util/HandlerUtil.java | 13 +++++++++++-- 8 files changed, 32 insertions(+), 15 deletions(-) diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/pom.xml b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/pom.xml index 66df853e17..ad7428b8f5 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/pom.xml +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/pom.xml @@ -154,6 +154,12 @@ org.apache.httpcomponents.client5 httpclient5 + + + org.slf4j + slf4j-api + + org.apache.httpcomponents diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/DefaultOauth2TokenHandler.java b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/DefaultOauth2TokenHandler.java index b0974a46a5..93cf1b9491 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/DefaultOauth2TokenHandler.java +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/DefaultOauth2TokenHandler.java @@ -112,7 +112,7 @@ public class DefaultOauth2TokenHandler extends HttpServlet { String defaultToken = tokenResult.get("accessToken").asText(); newDefaultAuthData.setAccessToken(defaultToken); newDefaultAuthData.setRefreshToken(tokenResult.get("refreshToken").asText()); - newDefaultAuthData.setScope(tokenResult.get("scopes").asText()); + newDefaultAuthData.setScope(tokenResult.get("scopes")); httpSession.setAttribute(HandlerConstants.SESSION_DEFAULT_AUTH_DATA_KEY, newDefaultAuthData); HandlerUtil.handleSuccess(resp, constructSuccessProxyResponse(defaultToken)); diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/LoginHandler.java b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/LoginHandler.java index 6f38a18014..d720842b71 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/LoginHandler.java +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/LoginHandler.java @@ -183,7 +183,7 @@ public class LoginHandler extends HttpServlet { authData.setEncodedClientApp(encodedClientApp); authData.setAccessToken(tokenResult.get("access_token").textValue()); authData.setRefreshToken(tokenResult.get("refresh_token").textValue()); - authData.setScope(tokenResult.get("scope").textValue()); + authData.setScope(tokenResult.get("scope")); session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, authData); return true; } catch (IOException e) { diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/PermissionScopeHandler.java b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/PermissionScopeHandler.java index 623fd48b87..42b06124f8 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/PermissionScopeHandler.java +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/PermissionScopeHandler.java @@ -36,6 +36,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; +import java.util.HashMap; import java.util.Map; @MultipartConfig @@ -57,14 +58,13 @@ public class PermissionScopeHandler extends HttpServlet { return; } - if (!StringUtils.isEmpty(authData.getScope())) { + if (!StringUtils.isEmpty(authData.getScope().toString())) { ProxyResponse proxyResponse = new ProxyResponse(); + JsonNode authDataScope = authData.getScope(); ObjectMapper mapper = new ObjectMapper(); - JsonNode node = JsonNodeFactory.instance.objectNode(); - Map nodeMap = mapper.convertValue(node, new TypeReference<>() { - }); - nodeMap.put(HandlerConstants.USER_SCOPES, authData.getScope()); + Map nodeMap = new HashMap<>(); + nodeMap.put(HandlerConstants.USER_SCOPES, authDataScope); proxyResponse.setCode(HttpStatus.SC_OK); proxyResponse.setStatus(ProxyResponse.Status.SUCCESS); proxyResponse.setData(mapper.convertValue(nodeMap, JsonNode.class)); diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/SsoLoginCallbackHandler.java b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/SsoLoginCallbackHandler.java index ee2b600eb4..2acd5fc167 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/SsoLoginCallbackHandler.java +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/SsoLoginCallbackHandler.java @@ -96,7 +96,7 @@ public class SsoLoginCallbackHandler extends HttpServlet { authData.setEncodedClientApp(session.getAttribute("encodedClientApp").toString()); authData.setAccessToken(jsonNode.get("access_token").textValue()); authData.setRefreshToken(jsonNode.get("refresh_token").textValue()); - authData.setScope(jsonNode.get("scope").textValue()); + authData.setScope(jsonNode.get("scope")); session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, authData); resp.sendRedirect(session.getAttribute("redirectUrl").toString()); } else { diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/UserHandler.java b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/UserHandler.java index dd06e5f08c..e67152cd3d 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/UserHandler.java +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/UserHandler.java @@ -129,8 +129,8 @@ public class UserHandler extends HttpServlet { HandlerUtil.handleSuccess(resp, proxyResponse); httpSession.setAttribute(HandlerConstants.USERNAME_WITH_DOMAIN, nodeMap.get("username").toString()); - log.info("Customer login", - userLogContextBuilder.setUserName(nodeMap.get("username").toString()).setUserRegistered(true).build()); +// log.info("Customer login", +// userLogContextBuilder.setUserName(nodeMap.get("username").toString()).setUserRegistered(true).build()); } catch (IOException e) { log.error("Error occurred while sending the response into the socket. ", e); } catch (JsonSyntaxException e) { diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/beans/AuthData.java b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/beans/AuthData.java index 36cb8aea59..a0c057383c 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/beans/AuthData.java +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/beans/AuthData.java @@ -19,6 +19,8 @@ package io.entgra.device.mgt.core.ui.request.interceptor.beans; +import com.fasterxml.jackson.databind.JsonNode; + public class AuthData implements java.io.Serializable { private static final long serialVersionUID = -5156750882531944849L; @@ -29,7 +31,7 @@ public class AuthData implements java.io.Serializable { private String clientId; private String clientSecret; private String encodedClientApp; - private String scope; + private JsonNode scope; public String getAccessToken() { return accessToken; @@ -79,11 +81,11 @@ public class AuthData implements java.io.Serializable { this.encodedClientApp = encodedClientApp; } - public String getScope() { + public JsonNode getScope() { return scope; } - public void setScope(String scope) { + public void setScope(JsonNode scope) { this.scope = scope; } } diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/util/HandlerUtil.java b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/util/HandlerUtil.java index b7a4b4e51c..af1141ceed 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/util/HandlerUtil.java +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/util/HandlerUtil.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.ui.request.interceptor.util; import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -264,9 +265,17 @@ public class HandlerUtil { resp.setStatus(proxyResponse.getCode()); resp.setContentType(ContentType.APPLICATION_JSON.getMimeType()); resp.setCharacterEncoding(Consts.UTF_8.name()); + JsonNode responseData = proxyResponse.getData(); + + if (!(responseData == null)) { + ObjectMapper mapper = new ObjectMapper(); + Map newNodeMap = new HashMap<>(); + newNodeMap.put("data", responseData); + responseData = mapper.convertValue(newNodeMap, JsonNode.class); + } try (PrintWriter writer = resp.getWriter()) { - writer.write(proxyResponse.getData().toString()); + writer.write(responseData != null ? responseData.toString() : "{}"); } } @@ -665,7 +674,7 @@ public class HandlerUtil { AuthData newAuthData = new AuthData(); newAuthData.setAccessToken(tokenResult.get("access_token").textValue()); newAuthData.setRefreshToken(tokenResult.get("refresh_token").textValue()); - newAuthData.setScope(tokenResult.get("scope").textValue()); + newAuthData.setScope(tokenResult.get("scope")); newAuthData.setClientId(authData.getClientId()); newAuthData.setClientSecret(authData.getClientSecret()); newAuthData.setEncodedClientApp(authData.getEncodedClientApp());