Search in sources :

Example 1 with ExternalSessionExpires

use of com.tremolosecurity.proxy.ExternalSessionExpires 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

ExternalSessionExpires (com.tremolosecurity.proxy.ExternalSessionExpires)1 TremoloHttpSession (com.tremolosecurity.proxy.TremoloHttpSession)1 AuthController (com.tremolosecurity.proxy.auth.AuthController)1 AuthInfo (com.tremolosecurity.proxy.auth.AuthInfo)1 Cookie (javax.servlet.http.Cookie)1 DateTime (org.joda.time.DateTime)1