From 73df5e2fedc7bfb428448e65c416872d784148c0 Mon Sep 17 00:00:00 2001 From: Amalka Subasinghe Date: Mon, 10 May 2021 13:06:30 +0530 Subject: [PATCH] cleaned up components/ui-request-interceptor --- .../io.entgra.ui.request.interceptor/pom.xml | 20 +++++-------------- .../ui/request/interceptor/LoginHandler.java | 13 +++--------- .../interceptor/SsoLoginCallbackHandler.java | 2 +- .../request/interceptor/SsoLoginHandler.java | 6 +++--- .../ui/request/interceptor/UserHandler.java | 8 +++++--- .../request/interceptor/util/HandlerUtil.java | 19 ++---------------- 6 files changed, 19 insertions(+), 49 deletions(-) diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/pom.xml b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/pom.xml index 783643a999..d41942e3f6 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/pom.xml +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/pom.xml @@ -103,6 +103,11 @@ org.wso2.carbon.apimgt.application.extension provided + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + provided + org.springframework spring-web @@ -113,21 +118,6 @@ cxf-bundle-jaxrs provided - - - - - - - - - - - - - - - javax.servlet javax.servlet-api diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java index 5a664552b8..cd0ff14a20 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java @@ -24,6 +24,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import io.entgra.ui.request.interceptor.beans.AuthData; +import io.entgra.ui.request.interceptor.beans.ProxyResponse; import io.entgra.ui.request.interceptor.exceptions.LoginException; import io.entgra.ui.request.interceptor.util.HandlerConstants; import io.entgra.ui.request.interceptor.util.HandlerUtil; @@ -34,17 +35,12 @@ import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; -import org.apache.http.protocol.HTTP; import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService; import org.wso2.carbon.apimgt.application.extension.APIManagementProviderServiceImpl; import org.wso2.carbon.apimgt.application.extension.constants.ApiApplicationConstants; import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey; import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException; -import org.wso2.carbon.authenticator.stub.AuthenticationAdmin; -import org.wso2.carbon.authenticator.stub.Login; import org.wso2.carbon.context.PrivilegedCarbonContext; -import io.entgra.ui.request.interceptor.beans.ProxyResponse; -import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; @@ -81,7 +77,8 @@ public class LoginHandler extends HttpServlet { //setting session to expiry in 5 minutes httpSession.setMaxInactiveInterval(Math.toIntExact(HandlerConstants.TIMEOUT)); //todo: amalka do we need this remote call? - JsonObject uiConfigJsonObject = HandlerUtil.getUIConfigAndPersistInSession(uiConfigUrl, gatewayUrl, httpSession, resp); + JsonObject uiConfigJsonObject = HandlerUtil.getUIConfigAndPersistInSession( + uiConfigUrl, gatewayUrl, httpSession, resp); JsonArray tags = uiConfigJsonObject.get("appRegistration").getAsJsonObject().get("tags").getAsJsonArray(); JsonArray scopes = uiConfigJsonObject.get("scopes").getAsJsonArray(); @@ -140,11 +137,7 @@ public class LoginHandler extends HttpServlet { HttpServletResponse resp, JsonArray scopes) throws LoginException { JsonParser jsonParser = new JsonParser(); try { -// JsonElement jClientAppResult = jsonParser.parse(clientAppResult); if (clientId != null && clientSecret != null) { -// JsonObject jClientAppResultAsJsonObject = jClientAppResult.getAsJsonObject(); -// String clientId = jClientAppResultAsJsonObject.get("client_id").getAsString(); -// String clientSecret = jClientAppResultAsJsonObject.get("client_secret").getAsString(); String encodedClientApp = Base64.getEncoder() .encodeToString((clientId + HandlerConstants.COLON + clientSecret).getBytes()); diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginCallbackHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginCallbackHandler.java index 641aa6d990..8ee6ec776b 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginCallbackHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginCallbackHandler.java @@ -61,7 +61,7 @@ public class SsoLoginCallbackHandler extends HttpServlet { String iotsCoreUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.core.host") + HandlerConstants.COLON + iotsCorePort; - HttpPost tokenEndpoint = new HttpPost(iotsCoreUrl + HandlerConstants.TOKEN_ENDPOINT); + HttpPost tokenEndpoint = new HttpPost(gatewayUrl + HandlerConstants.TOKEN_ENDPOINT); tokenEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + session.getAttribute("encodedClientApp")); tokenEndpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString()); diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginHandler.java index 23e76b81ae..1ade046d43 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginHandler.java @@ -101,7 +101,7 @@ public class SsoLoginHandler extends HttpServlet { */ private void dynamicClientRegistration(HttpServletRequest req, HttpServletResponse resp) { try { - File userMgtConf = new File("repository/conf/user-mgt.xml"); + File userMgtConf = new File("conf/user-mgt.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(userMgtConf); @@ -136,7 +136,7 @@ public class SsoLoginHandler extends HttpServlet { JsonArray scopes = uiConfigJsonObject.get("scopes").getAsJsonArray(); // Register the client application - HttpPost apiRegEndpoint = new HttpPost(iotsCoreUrl + HandlerConstants.APP_REG_ENDPOINT); + HttpPost apiRegEndpoint = new HttpPost(gatewayUrl + HandlerConstants.APP_REG_ENDPOINT); encodedAdminCredentials = Base64.getEncoder() .encodeToString((adminUsername + HandlerConstants.COLON + adminPassword).getBytes()); apiRegEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + @@ -262,7 +262,7 @@ public class SsoLoginHandler extends HttpServlet { * @throws IOException IO exception throws if an error occurred when invoking token endpoint */ private ProxyResponse getTokenResult(String encodedClientApp) throws IOException { - HttpPost tokenEndpoint = new HttpPost(iotsCoreUrl + HandlerConstants.TOKEN_ENDPOINT); + HttpPost tokenEndpoint = new HttpPost(gatewayUrl + HandlerConstants.TOKEN_ENDPOINT); tokenEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + encodedClientApp); tokenEndpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString()); diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java index 67b2834516..d6ef958a5c 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java @@ -33,6 +33,8 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import io.entgra.ui.request.interceptor.beans.ProxyResponse; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; @@ -72,9 +74,9 @@ public class UserHandler extends HttpServlet { HttpPost introspectionEndpoint = new HttpPost(serverUrl + HandlerConstants.INTROSPECT_ENDPOINT); introspectionEndpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString()); - //todo:amalka - String username = "admin"; - String password = "admin"; + DeviceManagementConfig dmc = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + String username = dmc.getKeyManagerConfigurations().getAdminUsername(); + String password = dmc.getKeyManagerConfigurations().getAdminPassword(); introspectionEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + Base64.getEncoder() .encodeToString((username + HandlerConstants.COLON + password).getBytes())); StringEntity introspectionPayload = new StringEntity("token=" + accessToken, diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java index bc864c03ee..4582468a0c 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java @@ -23,8 +23,6 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import com.sun.org.apache.xml.internal.serialize.OutputFormat; -import com.sun.org.apache.xml.internal.serialize.XMLSerializer; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -38,12 +36,12 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.apache.xml.serialize.OutputFormat; +import org.apache.xml.serialize.XMLSerializer; import org.json.JSONException; import org.json.JSONObject; import org.w3c.dom.Document; import io.entgra.ui.request.interceptor.beans.ProxyResponse; -import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService; -import org.wso2.carbon.context.PrivilegedCarbonContext; import org.xml.sax.SAXException; import javax.servlet.http.HttpServletRequest; @@ -82,7 +80,6 @@ public class HandlerUtil { HandlerConstants.INTERNAL_ERROR_CODE)); return proxyResponse; } else { - int statusCode = response.getStatusLine().getStatusCode(); try (BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) { StringBuilder result = new StringBuilder(); @@ -403,16 +400,4 @@ public class HandlerUtil { return stringOutput; } - - public static APIManagementProviderService getAPIManagementProviderService() { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - APIManagementProviderService apiManagementProviderService = - (APIManagementProviderService) ctx.getOSGiService(APIManagementProviderService.class, null); - if (apiManagementProviderService == null) { - String msg = "API management provider service has not initialized."; - log.error(msg); - throw new IllegalStateException(msg); - } - return apiManagementProviderService; - } }