From 89613a3b3dfdb52ba997975e5596e09fcbe2e470 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Mon, 25 Jan 2021 13:05:20 +0530 Subject: [PATCH] Improve default-token servlet response --- .../interceptor/DefaultTokenHandler.java | 22 ++++++++++++++++--- .../interceptor/util/HandlerConstants.java | 7 ++++++ .../authenticator/BasicAuthAuthenticator.java | 10 +++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java index 1cff1d9d114..5ffafa6bd6b 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/DefaultTokenHandler.java @@ -17,6 +17,7 @@ package io.entgra.ui.request.interceptor; +import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -28,6 +29,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.http.HttpHeaders; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.wso2.carbon.device.application.mgt.common.ProxyResponse; @@ -122,13 +124,27 @@ public class DefaultTokenHandler extends HttpServlet { /** * Get Success Proxy Response - * @param responseString Response String + * @param defaultAccessToken Access token which has default scope * @return {@link ProxyResponse} */ - private ProxyResponse constructSuccessProxyResponse (String responseString) { + private ProxyResponse constructSuccessProxyResponse (String defaultAccessToken) { + + URIBuilder ub = new URIBuilder(); + ub.setScheme(HandlerConstants.WSS_PROTOCOL); + ub.setHost(System.getProperty(System.getProperty(HandlerConstants.IOT_CORE_HOST_ENV_VAR))); + ub.setPort(Integer.parseInt(System.getProperty(HandlerConstants.IOT_CORE_PORT_ENV_VAR))); + ub.setPath(HandlerConstants.REMOTE_SESSION_CONTEXT); + + JsonObject responseJsonObj = new JsonObject(); + responseJsonObj.addProperty("default-access-token", defaultAccessToken); + responseJsonObj.addProperty("remote-session-base-url", ub.toString()); + + Gson gson = new Gson(); + String payload = gson.toJson(responseJsonObj); + ProxyResponse proxyResponse = new ProxyResponse(); proxyResponse.setCode(HttpStatus.SC_OK); - proxyResponse.setData(responseString); + proxyResponse.setData(payload); return proxyResponse; } } diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java index 22af32df094..3d4255c0aad 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java @@ -69,4 +69,11 @@ public class HandlerConstants { public static final String AX_PREFIX = "ax2317:"; public static final String PAYLOADS_DIR = "repository/resources/payloads"; public static final String SOAP_ACTION_HEADER = "SOAPAction"; + + public static final String WSS_PROTOCOL = "wss"; + public static final String REMOTE_SESSION_CONTEXT = "/remote/session/clients"; + + public static final String IOT_CORE_HOST_ENV_VAR = "iot.core.host"; + public static final String IOT_CORE_PORT_ENV_VAR = "iot.core.https.port"; + } diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BasicAuthAuthenticator.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BasicAuthAuthenticator.java index 0f4a7b77001..4bd7779dda1 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BasicAuthAuthenticator.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BasicAuthAuthenticator.java @@ -81,7 +81,17 @@ public class BasicAuthAuthenticator implements WebappAuthenticator { AuthenticationInfo authenticationInfo = new AuthenticationInfo(); Credentials credentials = getCredentials(request); try { + if (credentials == null) { + authenticationInfo.setMessage("Found invalid payload to authenticate."); + authenticationInfo.setStatus(Status.FAILURE); + return authenticationInfo; + } int tenantId = Utils.getTenantIdOFUser(credentials.getUsername()); + if (tenantId == -1) { + authenticationInfo.setMessage("Tenant Domain doesn't exists or tenant domain hasn't loaded properly."); + authenticationInfo.setStatus(Status.FAILURE); + return authenticationInfo; + } UserStoreManager userStore = AuthenticatorFrameworkDataHolder.getInstance().getRealmService(). getTenantUserRealm(tenantId).getUserStoreManager(); String username = MultitenantUtils.getTenantAwareUsername(credentials.getUsername());