backchannel logout fix

but has to manually add the backchannel logout endpoint
asgardeo_logout_issue
Deenath Geeganage 2 years ago
parent dc8c0bf90a
commit 867c5c5b94

@ -0,0 +1,42 @@
package io.entgra.ui.request.interceptor;
import com.nimbusds.jwt.SignedJWT;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
public class SessionIdStore {
private static Log log = LogFactory.getLog(SessionIdStore.class);
private static Map<String, HttpSession> sessionMap = new HashMap();
public SessionIdStore() {
}
public static void storeSession(String sid, HttpSession session) {
log.info("Storing session: " + session.getId() + " against the sid: " + sid);
sessionMap.put(sid, session);
}
public static String getSid(String idToken) throws ParseException {
String sid = (String) SignedJWT.parse(idToken).getJWTClaimsSet().getClaim("sid");
return sid;
}
public static HttpSession getSession(String sid) {
if (sid != null && sessionMap.get(sid) != null) {
log.info("Retrieving session: " + ((HttpSession) sessionMap.get(sid)).getId() + " for the sid: " + sid);
return (HttpSession) sessionMap.get(sid);
} else {
log.error("No session found for the sid: " + sid);
return null;
}
}
public static void removeSession(String sid) {
sessionMap.remove(sid);
}
}

@ -22,6 +22,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.entgra.ui.request.interceptor.beans.AuthData;
import io.entgra.ui.request.interceptor.beans.ProxyResponse;
import io.entgra.ui.request.interceptor.util.HandlerConstants;
import io.entgra.ui.request.interceptor.util.HandlerUtil;
import org.apache.commons.logging.Log;
@ -30,7 +31,6 @@ import org.apache.http.HttpHeaders;
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 javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
@ -39,6 +39,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
@MultipartConfig
@WebServlet("/ssoLoginCallback")
@ -48,12 +49,6 @@ public class SsoLoginCallbackHandler extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String code = req.getParameter("code");
try{
String token = req.getParameter("token");
System.out.println(token);
}catch(Exception e){
System.out.println("meka nemei oi");
}
HttpSession session = req.getSession(false);
String iotsCoreUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty(HandlerConstants.IOT_CORE_HOST_ENV_VAR)
@ -98,12 +93,20 @@ public class SsoLoginCallbackHandler extends HttpServlet {
authData.setAccessToken(jTokenResultAsJsonObject.get("access_token").getAsString());
authData.setRefreshToken(jTokenResultAsJsonObject.get("refresh_token").getAsString());
authData.setScope(jTokenResultAsJsonObject.get("scope").getAsString());
String idToken = (jTokenResultAsJsonObject.get("id_token").getAsString());
authData.setIdToken(idToken);
session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, authData);
HttpSession httpSession = req.getSession(false);
// String unformatedToken = jTokenResultAsJsonObject.get("id_token").getAsString();
// httpSession.setAttribute("idToken",unformatedToken.substring(1, unformatedToken.length() - 1));
httpSession.setAttribute("code",code);
try {
SessionIdStore.storeSession(SessionIdStore.getSid(idToken), session);
} catch (ParseException e) {
log.error("Error while obtaining sid from id_token", e);
}
resp.sendRedirect(session.getAttribute("redirectUrl").toString());
}
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
this.doGet(req, resp);
}
}

@ -18,11 +18,8 @@
package io.entgra.ui.request.interceptor;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.*;
import io.entgra.ui.request.interceptor.beans.ProxyResponse;
import io.entgra.ui.request.interceptor.cache.LoginCache;
import io.entgra.ui.request.interceptor.cache.OAuthApp;
import io.entgra.ui.request.interceptor.cache.OAuthAppCacheKey;
@ -43,7 +40,6 @@ import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import io.entgra.ui.request.interceptor.beans.ProxyResponse;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@ -312,7 +308,13 @@ public class SsoLoginHandler extends HttpServlet {
HandlerConstants.JWT_BEARER_GRANT_TYPE
})
);
jsonObject.put(HandlerConstants.CALLBACK_URL_KEY, iotsCoreUrl + baseContextPath + HandlerConstants.SSO_LOGIN_CALLBACK);
String logoutRedirect = "";
if (applicationName.equals("entgra")) {
logoutRedirect = iotsCoreUrl + "/endpoint-mgt";
} else {
logoutRedirect = (iotsCoreUrl + "/" + applicationName);
}
jsonObject.put(HandlerConstants.CALLBACK_URL_KEY, "regexp=(" + iotsCoreUrl + baseContextPath + HandlerConstants.SSO_LOGIN_CALLBACK + "|" + logoutRedirect + ")");
String payload = jsonObject.toString();
return new StringEntity(payload, ContentType.APPLICATION_JSON);
}

@ -0,0 +1,44 @@
package io.entgra.ui.request.interceptor;
import com.nimbusds.jwt.SignedJWT;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
@MultipartConfig
@WebServlet("/ssoLogoutCallback")
public class SsoLogoutCallbackHandler extends HttpServlet {
private static final Log log = LogFactory.getLog(SsoLogoutCallbackHandler.class);
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
this.doPost(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
log.info("BackChannel Logout Invoked");
try {
String sid = (String) SignedJWT.parse(req.getParameter("logout_token")).getJWTClaimsSet().getClaim("sid");
HttpSession session = SessionIdStore.getSession(sid);
if (session != null) {
session.invalidate();
SessionIdStore.removeSession(sid);
log.info("Session invalidated successfully for sid: " + sid);
} else {
log.info("Cannot find corresponding session for sid: " + sid);
}
} catch (ParseException e) {
log.error("Error in generating Logout Token.", e);
}
}
}

@ -18,17 +18,13 @@
package io.entgra.ui.request.interceptor;
import io.entgra.ui.request.interceptor.beans.AuthData;
import io.entgra.ui.request.interceptor.beans.ProxyResponse;
import io.entgra.ui.request.interceptor.util.HandlerConstants;
import io.entgra.ui.request.interceptor.util.HandlerUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import io.entgra.ui.request.interceptor.beans.ProxyResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
@ -36,71 +32,42 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;
import java.io.IOException;
@MultipartConfig
@WebServlet("/ssoLogout")
public class SsoLogoutHandler extends HttpServlet {
private static final Log log = LogFactory.getLog(SsoLogoutHandler.class);
private static Log log = LogFactory.getLog(SsoLogoutHandler.class);
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
// for (String path : HandlerConstants.SSO_LOGOUT_COOKIE_PATHS) {
// removeCookie(HandlerConstants.JSESSIONID_KEY, path, resp);
// }
// removeCookie(HandlerConstants.COMMON_AUTH_ID_KEY, "/", resp);
ProxyResponse proxyResponse = new ProxyResponse();
proxyResponse.setStatus(ProxyResponse.Status.SUCCESS);
proxyResponse.setCode(HttpStatus.SC_OK);
String iotsCoreUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty(HandlerConstants.IOT_CORE_HOST_ENV_VAR)
+ HandlerConstants.COLON + HandlerUtil.getCorePort(req.getScheme());
String baseContextPath = req.getContextPath();
String applicationName = baseContextPath.substring(1, baseContextPath.indexOf("-ui-request-handler"));
String logoutRedirect = "";
if (applicationName.equals("entgra")) {
logoutRedirect = iotsCoreUrl + "/endpoint-mgt";
} else {
logoutRedirect = (iotsCoreUrl + "/" + applicationName);
}
HttpSession session = req.getSession(false);
String clientId = null;
String code = null;
AuthData authData = null;
if (session != null) {
clientId = (String) session.getAttribute("clientId");
code = (String) session.getAttribute("code");
System.out.println(code);
// session.invalidate();
authData = (AuthData) session.getAttribute("authInfo");
}
try {
// String redirect = "https://localhost:9443/oidc/logout?response_type=code&client_id=" + clientId +"&state&scope=openid&redirect_uri=" + "https://localhost:9443/commonauth";
String redirect = "https://localhost:9443/oidc/logout"+
"?code=" + code +
// "?id_token_hint=" + code +
"&post_logout_redirect_uri=" + "https://localhost:9443/commonauth";
System.out.println(redirect);
System.out.println("\n\n\n");
System.out.println(code);
if(session!=null){
session.invalidate();
}
// resp.sendRedirect("https://localhost:9443/oidc/logout"+
// "?id_token_hint=" + code+"&post_logout_redirect_uri=https://localhost:9443/commonauth");
// HttpGet getApplicationEndpoint = new HttpGet(redirect);
// ProxyResponse getApplicationResponse = HandlerUtil.execute(getApplicationEndpoint);
// System.out.println(getApplicationResponse.getCode());
// HttpGet getApplicationEndpoint2 = new HttpGet("https://api.asgardeo.io/t/noobie/oidc/logout");
// ProxyResponse getApplicationResponse2 = HandlerUtil.execute(getApplicationEndpoint2);
// System.out.println(getApplicationResponse2);
String redirect = iotsCoreUrl + "/oidc/logout" +
"?id_token_hint=" + authData.getIdToken() +
"&post_logout_redirect_uri=" + logoutRedirect +
"&state=xyz";
resp.sendRedirect(redirect);
return;
} catch (IOException e) {
// } catch (Exception e) {
log.error("123 bumble bee");
log.error("Error occured while redirecting");
}
// try {
// HandlerUtil.handleSuccess(resp, proxyResponse);
// } catch (IOException e) {
// log.error("Error occurred when processing logout request.", e);
// }
}
private static void removeCookie(String cookieName,String path, HttpServletResponse response) {
Cookie cookie = new Cookie(cookieName, "");
cookie.setPath(path);
cookie.setValue(null);
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}

@ -31,6 +31,8 @@ public class AuthData implements java.io.Serializable {
private String encodedClientApp;
private String scope;
private String idToken;
public String getAccessToken() {
return accessToken;
}
@ -86,4 +88,12 @@ public class AuthData implements java.io.Serializable {
public void setScope(String scope) {
this.scope = scope;
}
public String getIdToken() {
return idToken;
}
public void setIdToken(String idToken) {
this.idToken = idToken;
}
}

@ -18,12 +18,9 @@
package io.entgra.ui.request.interceptor.util;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import io.entgra.ui.request.interceptor.beans.AuthData;
import io.entgra.ui.request.interceptor.beans.ProxyResponse;
import io.entgra.ui.request.interceptor.cache.LoginCache;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
@ -54,7 +51,6 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import io.entgra.ui.request.interceptor.beans.ProxyResponse;
import org.xml.sax.SAXException;
import javax.servlet.http.HttpServletRequest;
@ -63,12 +59,7 @@ import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.*;
import java.util.Enumeration;
import java.util.List;
@ -193,7 +184,6 @@ public class HandlerUtil {
}
/***
* Handle error requests.
*
@ -652,6 +642,7 @@ public class HandlerUtil {
// handleError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR);
return tokenResultResponse;
}
public static ProxyResponse getTokenResult(AuthData authData, String keymanagerUrl) throws IOException {
HttpPost tokenEndpoint = new HttpPost(keymanagerUrl + HandlerConstants.OAUTH2_TOKEN_ENDPOINT);
StringEntity tokenEndpointPayload = new StringEntity(
@ -711,7 +702,7 @@ public class HandlerUtil {
public static String getHeaderValue(String headerName, Header[] headers) {
String headerValue = null;
for(Header header : headers) {
for (Header header : headers) {
if (header.getName().equalsIgnoreCase(headerName)) {
headerValue = header.getValue();
}

Loading…
Cancel
Save