Search in sources :

Example 1 with Session

use of suite.http.Http.Session in project suite by stupidsing.

the class HttpHandleSessionAuth method getHandler.

public Handler getHandler(BiPredicate<String, String> authenticate, Handler protectedHandler) {
    return new Handler() {

        public Response handle(Request request) {
            var current = System.currentTimeMillis();
            var sessionIdOpt = // 
            request.headers.getOpt(// 
            "Cookie").map(cookie -> HttpHeaderUtil.getCookieAttrs(cookie).get("session"));
            var session = sessionIdOpt.map(sm::get).or(null);
            Response response;
            if (Equals.ab(request.paths, PerList.of("login"))) {
                var attrs = HttpHeaderUtil.getPostedAttrs(request.in);
                var username = attrs.get("username");
                var password = attrs.get("password");
                var paths = HttpHeaderUtil.getPaths(attrs.get("path"));
                if (authenticate.test(username, password)) {
                    var sessionId = getRandomSessionId();
                    sm.put(sessionId, session = new Session(username, current));
                    var request1 = new // 
                    Request(// 
                    request.method, // 
                    request.server, // 
                    paths, // 
                    request.query, // 
                    request.headers, request.in);
                    response = showProtectedPage(request1, sessionId);
                } else
                    response = showLoginPage(paths, true);
            } else if (Equals.ab(request.paths, PerList.of("logout"))) {
                sessionIdOpt.sink(sm::remove);
                response = showLoginPage(PerList.end(), false);
            } else if (session != null && current < session.lastRequestDt.value() + timeoutDuration) {
                session.lastRequestDt.update(current);
                response = showProtectedPage(request, sessionIdOpt.g());
            } else
                response = showLoginPage(request.paths, false);
            return response;
        }

        private Response showProtectedPage(Request request, String sessionId) {
            var r = protectedHandler.handle(request);
            var headers1 = r.headers.put("Set-Cookie", "session=" + sessionId + "; Path=/site");
            return new Response(r.status, headers1, r.body);
        }

        private Response showLoginPage(PerList<String> redirectPath, boolean isLoginFailed) {
            var redirectPath1 = redirectPath.streamlet().map(p -> "/" + p).toJoinedString();
            return Response.of(Pull.from(// 
            "<html>" + // 
            "<head><title>Login</title></head>" + // 
            "<body>" + // 
            "<font face=\"Monospac821 BT,Monaco,Consolas\">" + // 
            (isLoginFailed ? "<b>LOGIN FAILED</b><p/>" : "") + // 
            "<form name=\"login\" action=\"login\" method=\"post\">" + // 
            "Username <input type=\"text\" name=\"username\" autofocus /><br/>" + // 
            "Password <input type=\"password\" name=\"password\" /><br/>" + "<input type=\"hidden\" name=\"path\" value=\"" + htmlUtil.encode(redirectPath1) + // 
            "\" />" + // 
            "<input type=\"submit\" value=\"Login\">" + // 
            "</form>" + // 
            "</font>" + // 
            "</body>" + "</html>"));
        }

        private String getRandomSessionId() {
            var bytes = new byte[16];
            random.nextBytes(bytes);
            return Build.string(sb -> {
                for (var b : bytes) sb.append(String.format("%02x", b));
            });
        }
    };
}
Also used : Response(suite.http.Http.Response) Build(primal.Verbs.Build) Random(java.util.Random) Session(suite.http.Http.Session) SecureRandom(java.security.SecureRandom) BiPredicate(java.util.function.BiPredicate) Response(suite.http.Http.Response) Request(suite.http.Http.Request) SessionManager(suite.http.Http.SessionManager) Pull(primal.MoreVerbs.Pull) Equals(primal.Verbs.Equals) PerList(primal.persistent.PerList) Handler(suite.http.Http.Handler) HtmlUtil(suite.util.HtmlUtil) Request(suite.http.Http.Request) Handler(suite.http.Http.Handler) PerList(primal.persistent.PerList) Session(suite.http.Http.Session)

Aggregations

SecureRandom (java.security.SecureRandom)1 Random (java.util.Random)1 BiPredicate (java.util.function.BiPredicate)1 Pull (primal.MoreVerbs.Pull)1 Build (primal.Verbs.Build)1 Equals (primal.Verbs.Equals)1 PerList (primal.persistent.PerList)1 Handler (suite.http.Http.Handler)1 Request (suite.http.Http.Request)1 Response (suite.http.Http.Response)1 Session (suite.http.Http.Session)1 SessionManager (suite.http.Http.SessionManager)1 HtmlUtil (suite.util.HtmlUtil)1