Http update fixes

pull/313/head
Pasindu Rupasinghe 10 months ago
parent cac6982d6b
commit b8e8450108

@ -154,6 +154,12 @@
<dependency> <dependency>
<groupId>org.apache.httpcomponents.client5</groupId> <groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId> <artifactId>httpclient5</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>

@ -112,7 +112,7 @@ public class DefaultOauth2TokenHandler extends HttpServlet {
String defaultToken = tokenResult.get("accessToken").asText(); String defaultToken = tokenResult.get("accessToken").asText();
newDefaultAuthData.setAccessToken(defaultToken); newDefaultAuthData.setAccessToken(defaultToken);
newDefaultAuthData.setRefreshToken(tokenResult.get("refreshToken").asText()); 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); httpSession.setAttribute(HandlerConstants.SESSION_DEFAULT_AUTH_DATA_KEY, newDefaultAuthData);
HandlerUtil.handleSuccess(resp, constructSuccessProxyResponse(defaultToken)); HandlerUtil.handleSuccess(resp, constructSuccessProxyResponse(defaultToken));

@ -183,7 +183,7 @@ public class LoginHandler extends HttpServlet {
authData.setEncodedClientApp(encodedClientApp); authData.setEncodedClientApp(encodedClientApp);
authData.setAccessToken(tokenResult.get("access_token").textValue()); authData.setAccessToken(tokenResult.get("access_token").textValue());
authData.setRefreshToken(tokenResult.get("refresh_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); session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, authData);
return true; return true;
} catch (IOException e) { } catch (IOException e) {

@ -36,6 +36,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
@MultipartConfig @MultipartConfig
@ -57,14 +58,13 @@ public class PermissionScopeHandler extends HttpServlet {
return; return;
} }
if (!StringUtils.isEmpty(authData.getScope())) { if (!StringUtils.isEmpty(authData.getScope().toString())) {
ProxyResponse proxyResponse = new ProxyResponse(); ProxyResponse proxyResponse = new ProxyResponse();
JsonNode authDataScope = authData.getScope();
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode node = JsonNodeFactory.instance.objectNode(); Map<String, Object> nodeMap = new HashMap<>();
Map<String, Object> nodeMap = mapper.convertValue(node, new TypeReference<>() { nodeMap.put(HandlerConstants.USER_SCOPES, authDataScope);
});
nodeMap.put(HandlerConstants.USER_SCOPES, authData.getScope());
proxyResponse.setCode(HttpStatus.SC_OK); proxyResponse.setCode(HttpStatus.SC_OK);
proxyResponse.setStatus(ProxyResponse.Status.SUCCESS); proxyResponse.setStatus(ProxyResponse.Status.SUCCESS);
proxyResponse.setData(mapper.convertValue(nodeMap, JsonNode.class)); proxyResponse.setData(mapper.convertValue(nodeMap, JsonNode.class));

@ -96,7 +96,7 @@ public class SsoLoginCallbackHandler extends HttpServlet {
authData.setEncodedClientApp(session.getAttribute("encodedClientApp").toString()); authData.setEncodedClientApp(session.getAttribute("encodedClientApp").toString());
authData.setAccessToken(jsonNode.get("access_token").textValue()); authData.setAccessToken(jsonNode.get("access_token").textValue());
authData.setRefreshToken(jsonNode.get("refresh_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); session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, authData);
resp.sendRedirect(session.getAttribute("redirectUrl").toString()); resp.sendRedirect(session.getAttribute("redirectUrl").toString());
} else { } else {

@ -129,8 +129,8 @@ public class UserHandler extends HttpServlet {
HandlerUtil.handleSuccess(resp, proxyResponse); HandlerUtil.handleSuccess(resp, proxyResponse);
httpSession.setAttribute(HandlerConstants.USERNAME_WITH_DOMAIN, nodeMap.get("username").toString()); httpSession.setAttribute(HandlerConstants.USERNAME_WITH_DOMAIN, nodeMap.get("username").toString());
log.info("Customer login", // log.info("Customer login",
userLogContextBuilder.setUserName(nodeMap.get("username").toString()).setUserRegistered(true).build()); // userLogContextBuilder.setUserName(nodeMap.get("username").toString()).setUserRegistered(true).build());
} catch (IOException e) { } catch (IOException e) {
log.error("Error occurred while sending the response into the socket. ", e); log.error("Error occurred while sending the response into the socket. ", e);
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {

@ -19,6 +19,8 @@
package io.entgra.device.mgt.core.ui.request.interceptor.beans; package io.entgra.device.mgt.core.ui.request.interceptor.beans;
import com.fasterxml.jackson.databind.JsonNode;
public class AuthData implements java.io.Serializable { public class AuthData implements java.io.Serializable {
private static final long serialVersionUID = -5156750882531944849L; private static final long serialVersionUID = -5156750882531944849L;
@ -29,7 +31,7 @@ public class AuthData implements java.io.Serializable {
private String clientId; private String clientId;
private String clientSecret; private String clientSecret;
private String encodedClientApp; private String encodedClientApp;
private String scope; private JsonNode scope;
public String getAccessToken() { public String getAccessToken() {
return accessToken; return accessToken;
@ -79,11 +81,11 @@ public class AuthData implements java.io.Serializable {
this.encodedClientApp = encodedClientApp; this.encodedClientApp = encodedClientApp;
} }
public String getScope() { public JsonNode getScope() {
return scope; return scope;
} }
public void setScope(String scope) { public void setScope(JsonNode scope) {
this.scope = scope; this.scope = scope;
} }
} }

@ -19,6 +19,7 @@
package io.entgra.device.mgt.core.ui.request.interceptor.util; package io.entgra.device.mgt.core.ui.request.interceptor.util;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
@ -264,9 +265,17 @@ public class HandlerUtil {
resp.setStatus(proxyResponse.getCode()); resp.setStatus(proxyResponse.getCode());
resp.setContentType(ContentType.APPLICATION_JSON.getMimeType()); resp.setContentType(ContentType.APPLICATION_JSON.getMimeType());
resp.setCharacterEncoding(Consts.UTF_8.name()); resp.setCharacterEncoding(Consts.UTF_8.name());
JsonNode 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);
}
try (PrintWriter writer = resp.getWriter()) { 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(); AuthData newAuthData = new AuthData();
newAuthData.setAccessToken(tokenResult.get("access_token").textValue()); newAuthData.setAccessToken(tokenResult.get("access_token").textValue());
newAuthData.setRefreshToken(tokenResult.get("refresh_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.setClientId(authData.getClientId());
newAuthData.setClientSecret(authData.getClientSecret()); newAuthData.setClientSecret(authData.getClientSecret());
newAuthData.setEncodedClientApp(authData.getEncodedClientApp()); newAuthData.setEncodedClientApp(authData.getEncodedClientApp());

Loading…
Cancel
Save