public void addAuthenticatedUser(Request request, User u) { request.session().attribute(USER_SESSION_ID, u); }
public void removeAuthenticatedUser(Request request) { request.session().removeAttribute(USER_SESSION_ID); }
/** * Returns the current session associated with this request, * or if the request does not have a session, creates one. * * @return the session associated with this request */ public Session session() { if (session == null || !validSession) { validSession(true); session = new Session(servletRequest.getSession(), this); } return session; }
public static String removeSessionAttrLoginRedirect(Request request) { String loginRedirect = request.session().attribute("loginRedirect"); request.session().removeAttribute("loginRedirect"); return loginRedirect; }
protected WebTextTerminal create(SessionHolder sessionHolder, String initData) { String sessionId = sessionHolder.sessionId; Session session = sessionHolder.session; logger.debug("Creating terminal for sessionId: {}", sessionId); WebTextTerminal terminal = termTemplate.createCopy(); terminal.setOnDispose(() -> { if(onDispose != null) { onDispose.accept(sessionId); } Executors.newSingleThreadScheduledExecutor().schedule(() -> session.removeAttribute(getSessionIdAttribute(sessionId)), 5, TimeUnit.SECONDS); }); terminal.setOnAbort(() -> { if(onAbort != null) { onAbort.accept(sessionId); } Executors.newSingleThreadScheduledExecutor().schedule(() -> session.removeAttribute(getSessionIdAttribute(sessionId)), 5, TimeUnit.SECONDS); }); session.attribute(getSessionIdAttribute(sessionId), terminal); if(maxInactiveSeconds != null) { session.maxInactiveInterval(maxInactiveSeconds); } TextIO textIO = new TextIO(terminal); RunnerData runnerData = createRunnerData(initData, sessionHolder); Thread thread = new Thread(() -> textIoRunner.accept(textIO, runnerData)); thread.setDaemon(true); thread.start(); return terminal; }
protected WebTextTerminal get(SessionHolder sessionHolder) { String sessionId = sessionHolder.sessionId; Session session = sessionHolder.session; WebTextTerminal terminal = session.attribute(getSessionIdAttribute(sessionId)); if(terminal == null) { throw new DataApiProviderException("Unknown session: " + sessionId); } logger.trace("Terminal found for sessionId: {} on session with attributes {}", sessionId, session.attributes()); return terminal; }
protected static String getId(Request request) { Session session = request.session(); String id = session.id(); String uuid = request.headers("uuid"); if(uuid != null) { id += "-" + uuid; } logger.trace("id: {}", id); return id; }
public static boolean removeSessionAttrLoggedOut(Request request) { Object loggedOut = request.session().attribute("loggedOut"); request.session().removeAttribute("loggedOut"); return loggedOut != null; }
public static boolean removeSessionAttrLoggedOut(Request request) { Object loggedOut = request.session().attribute("loggedOut"); request.session().removeAttribute("loggedOut"); return loggedOut != null; }
public User getAuthenticatedUser(Request request) { User theUser = request.session().attribute(USER_SESSION_ID); if(theUser == null) { String authHeader = request.headers("Authorization");
private void removeAuthenticatedUser(Request request) { request.session().removeAttribute(USER_SESSION_ID); }
/** * Returns the current session associated with this request, or if there is * no current session and <code>create</code> is true, returns a new session. * * @param create <code>true</code> to create a new session for this request if necessary; * <code>false</code> to return null if there's no current session * @return the session associated with this request or <code>null</code> if * <code>create</code> is <code>false</code> and the request has no valid session */ public Session session(boolean create) { if (session == null || !validSession) { HttpSession httpSession = servletRequest.getSession(create); if (httpSession != null) { validSession(true); session = new Session(httpSession, this); } else { session = null; } } return session; }
public static String removeSessionAttrLoginRedirect(Request request) { String loginRedirect = request.session().attribute("loginRedirect"); request.session().removeAttribute("loginRedirect"); return loginRedirect; }
private User getAuthenticatedUser(Request request) { return request.session().attribute(USER_SESSION_ID); } }
public void defineEndpoints() { /** * Settings page */ Spark.get("/settings", this::getSettingsPage, new HomeDashTemplateEngine()); Spark.post("/settings", this::saveSettings); /** * Login form */ Spark.get("/login", (req, res) -> new ModelAndView(new HashMap<String, String>(), "login"), new HomeDashTemplateEngine()); Spark.post("/login", this::login, new HomeDashTemplateEngine()); Spark.get("/export-config", this::exportConfig); /** * Loging out ! */ Spark.get("/logout", (req, res) -> { res.removeCookie(AUTH_KEY); req.session().removeAttribute(AUTH_KEY); res.redirect("/"); return null; }); }
/** * Returns the current session associated with this request, * or if the request does not have a session, creates one. * * @return the session associated with this request */ public Session session() { if(session == null) { session = new Session(servletRequest.getSession()); } return session; }
/** * Checks user cookies and seee if he is allowed to login * * @return ok, if he is allowed to see what he's trying to see */ public boolean checkSession(Request req, Response res) { try { Settings useAuth = get(Settings.USE_AUTH); if (useAuth != null && useAuth.getValue().equalsIgnoreCase("1")) { logger.info("Auth requested, checking if everything is alright;"); //checking cookie first if (req.cookies().containsKey(AUTH_KEY)) { return checkPassword(req.cookie(AUTH_KEY)); } else if (req.session().attribute(AUTH_KEY) != null) { return checkPassword(req.session().attribute(AUTH_KEY)); } else { res.removeCookie(AUTH_KEY); req.session().removeAttribute(AUTH_KEY); return false; } } return true; } catch (Exception e) { return false; } }
public static String getSessionLocale(Request request) { return request.session().attribute("locale"); }
/** * Returns the current session associated with this request, or if there is * no current session and <code>create</code> is true, returns a new * session. * * @param create <code>true</code> to create a new session for this request * if necessary; <code>false</code> to return null if there's no * current session * @return the session associated with this request or <code>null</code> if * <code>create</code> is <code>false</code> and the request has no * valid session */ public Session session(boolean create) { if(session == null) { HttpSession httpSession = servletRequest.getSession(create); if(httpSession != null) { session = new Session(httpSession); } } return session; }
public static String getSessionLocale(Request request) { return request.session().attribute("locale"); }