Search in sources :

Example 1 with TremoloHttpSession

use of com.tremolosecurity.proxy.TremoloHttpSession in project OpenUnison by TremoloSecurity.

the class PostProcess method getHttp.

public CloseableHttpClient getHttp(String finalURL, HttpServletRequest request, UrlHolder holder) {
    ConfigManager cfgMgr = holder.getConfig();
    HttpSession session = request.getSession();
    PoolingHttpClientConnectionManager phcm = (PoolingHttpClientConnectionManager) session.getAttribute("TREMOLO_HTTP_POOL");
    CloseableHttpClient http = (CloseableHttpClient) session.getAttribute("TREMOLO_HTTP_CLIENT");
    if (http == null) {
        if (holder.getApp().getCookieConfig() == null || holder.getApp().getCookieConfig().isCookiesEnabled() == null || holder.getApp().getCookieConfig().isCookiesEnabled()) {
            // create a new connection manager and client
            phcm = new PoolingHttpClientConnectionManager(cfgMgr.getHttpClientSocketRegistry());
            BigInteger num = cfgMgr.getCfg().getThreadsPerRoute();
            if (num == null) {
                phcm.setDefaultMaxPerRoute(6);
            } else {
                phcm.setDefaultMaxPerRoute(num.intValue());
            }
            phcm.setDefaultSocketConfig(SocketConfig.custom().setSoKeepAlive(true).build());
            http = HttpClients.custom().setConnectionManager(phcm).setDefaultRequestConfig(cfgMgr.getGlobalHttpClientConfig()).build();
            session.setAttribute("TREMOLO_HTTP_POOL", phcm);
            session.setAttribute("TREMOLO_HTTP_CLIENT", http);
            LogoutUtil.insertFirstLogoutHandler(request, new CloseHttpConnectionsOnLogout(http, phcm));
        } else {
            // no session, need to create single connection
            BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager(cfgMgr.getHttpClientSocketRegistry());
            RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setRedirectsEnabled(false).setConnectionRequestTimeout(10000).setSocketTimeout(10000).setConnectTimeout(10000).build();
            http = HttpClients.custom().setConnectionManager(bhcm).setDefaultRequestConfig(rc).build();
            session.setAttribute("TREMOLO_HTTP_CM", bhcm);
            session.setAttribute("TREMOLO_HTTP_CLIENT", http);
            // remove from the session pool
            SessionManager sessionMgr = (SessionManager) GlobalEntries.getGlobalEntries().getConfigManager().getContext().getAttribute(ProxyConstants.TREMOLO_SESSION_MANAGER);
            sessionMgr.removeSessionFromCache((TremoloHttpSession) session);
        }
    }
    return http;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpSession(javax.servlet.http.HttpSession) TremoloHttpSession(com.tremolosecurity.proxy.TremoloHttpSession) SessionManager(com.tremolosecurity.proxy.SessionManager) BigInteger(java.math.BigInteger) ConfigManager(com.tremolosecurity.config.util.ConfigManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 2 with TremoloHttpSession

use of com.tremolosecurity.proxy.TremoloHttpSession in project OpenUnison by TremoloSecurity.

the class AppConfig method doFilter.

@Override
public void doFilter(HttpFilterRequest request, HttpFilterResponse response, HttpFilterChain chain) throws Exception {
    synchronized (this.appConfig) {
        if (this.appConfig.cookieName == null) {
            this.loadConfigData(this.filterConfig);
        }
        if (this.appConfig.cookieName == null) {
            response.sendError(401);
            return;
        }
    }
    request.setAttribute("com.tremolosecurity.unison.proxy.noRedirectOnError", "com.tremolosecurity.unison.proxy.noRedirectOnError");
    ArrayList<Cookie> sessionCookies = request.getCookies(this.appConfig.cookieName);
    if (sessionCookies == null || sessionCookies.isEmpty()) {
        response.sendError(401);
    } else {
        for (Cookie cookie : sessionCookies) {
            TremoloHttpSession session = SessionManagerImpl.findSessionFromCookie(cookie, this.appConfig.secretKey, (SessionManagerImpl) GlobalEntries.getGlobalEntries().get(ProxyConstants.TREMOLO_SESSION_MANAGER));
            if (session == null) {
                response.sendError(401);
            } else {
                AuthInfo userData = ((AuthController) session.getAttribute(ProxyConstants.AUTH_CTL)).getAuthInfo();
                if (userData == null || !userData.isAuthComplete() || userData.getAuthLevel() == 0) {
                    response.sendError(401);
                } else {
                    SessionInfo si = new SessionInfo();
                    if (this.appConfig.timeoutSeconds > 0) {
                        ExternalSessionExpires extSession = (ExternalSessionExpires) session.getAttribute(SessionManagerImpl.TREMOLO_EXTERNAL_SESSION);
                        int extMinLeft = -1;
                        int stdMinLeft = -1;
                        if (extSession != null) {
                            long expires = extSession.getExpires();
                            if (expires <= 0) {
                                extMinLeft = -1;
                            } else {
                                extMinLeft = (int) ((expires - System.currentTimeMillis()) / 1000 / 60);
                            }
                        }
                        DateTime lastAccessed = (DateTime) session.getAttribute(SessionManagerImpl.TREMOLO_SESSION_LAST_ACCESSED);
                        DateTime now = new DateTime();
                        DateTime expires = lastAccessed.plusSeconds(this.appConfig.timeoutSeconds);
                        stdMinLeft = (int) ((expires.getMillis() - System.currentTimeMillis()) / 1000 / 60);
                        if (extMinLeft > stdMinLeft) {
                            si.setMinsLeft(extMinLeft);
                        } else {
                            si.setMinsLeft(stdMinLeft);
                        }
                    } else {
                        si.setMinsLeft(-1);
                    }
                    String json = gson.toJson(si);
                    response.setContentType("application/json");
                    response.getWriter().println(json.trim());
                    response.sendError(200);
                }
            }
        }
    }
}
Also used : Cookie(javax.servlet.http.Cookie) AuthInfo(com.tremolosecurity.proxy.auth.AuthInfo) ExternalSessionExpires(com.tremolosecurity.proxy.ExternalSessionExpires) AuthController(com.tremolosecurity.proxy.auth.AuthController) TremoloHttpSession(com.tremolosecurity.proxy.TremoloHttpSession) DateTime(org.joda.time.DateTime)

Aggregations

TremoloHttpSession (com.tremolosecurity.proxy.TremoloHttpSession)2 ConfigManager (com.tremolosecurity.config.util.ConfigManager)1 ExternalSessionExpires (com.tremolosecurity.proxy.ExternalSessionExpires)1 SessionManager (com.tremolosecurity.proxy.SessionManager)1 AuthController (com.tremolosecurity.proxy.auth.AuthController)1 AuthInfo (com.tremolosecurity.proxy.auth.AuthInfo)1 BigInteger (java.math.BigInteger)1 Cookie (javax.servlet.http.Cookie)1 HttpSession (javax.servlet.http.HttpSession)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 BasicHttpClientConnectionManager (org.apache.http.impl.conn.BasicHttpClientConnectionManager)1 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)1 DateTime (org.joda.time.DateTime)1