Improve default-token servlet response

revert-70ac1926
tcdlpds@gmail.com 4 years ago
parent 7333329e78
commit 89613a3b3d

@ -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;
}
}

@ -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";
}

@ -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());

Loading…
Cancel
Save