@Override public Optional<Session> ifSession() { return req.ifSession(); }
@Override public boolean destroySession(WebContext ctx) { req.ifSession().ifPresent(Session::destroy); return true; }
@Override public boolean renewSession(WebContext ctx) { req.ifSession().ifPresent(Session::renewId); return true; }
@Override public Object get(WebContext context, String key) { return req.ifSession() .map(session -> { String value = session.get(key).toOptional().orElse(null); return strToObject(value); }).orElse(null); }
static Provider profileProvider(AtomicReference<Registry> registry, Class profile, Function<Request, UserProfile> unauthenticated) { return () -> { Request req = registry.get().require(Request.class); ProfileManager pm = req.require(ProfileManager.class); Object result = pm.getAll(req.ifSession().isPresent()).stream() .filter(profile::isInstance) .findFirst() .orElse(null); if (result == null) { if (unauthenticated == null) { throw new Err(Status.FORBIDDEN, "Not found: " + profile.getSimpleName()); } result = unauthenticated.apply(req); } return result; }; } }
@Override public void set(WebContext context, String key, Object value) { if (value == null) { req.ifSession().ifPresent(session -> session.unset(key)); } else { req.session().set(key, objToStr(value)); } }
@SuppressWarnings("unchecked") @Override public void handle(final Request req, final Response rsp) throws Throwable { // DON'T create a session for JWT/param/header auth (a.k.a stateless) Optional<Session> ifSession = req.ifSession(); if (ifSession.isPresent()) { Session session = ifSession.get(); Optional<String> profileId = session.unset(Auth.ID).toOptional(); if (profileId.isPresent()) { Optional<CommonProfile> profile = req.require(AuthStore.class).unset(profileId.get()); log.debug("logout {}", profile); session.destroy(); } } else { log.debug("nothing to logout from session"); } String redirectTo = req.<String> ifGet("auth.logout.redirectTo").orElse(this.redirectTo); rsp.redirect(redirectTo); }
private Route.After saveCookie() { return (req, rsp, result) -> { req.ifSession().ifPresent(session -> { Optional<String> value = req.cookie(cookie.name().get()).toOptional(); Map<String, String> initial = value .map(this::attributes) .orElse(Collections.emptyMap()); Map<String, String> attributes = session.attributes(); // is dirty? boolean dirty = !initial.equals(attributes); log.debug("session dirty: {}", dirty); if (dirty) { log.debug("saving session cookie"); String encoded = Cookie.URL_ENCODER.apply(attributes); String signed = Cookie.Signature.sign(encoded, secret); rsp.cookie(new Cookie.Definition(cookie).value(signed)); } else if (timeout > 0) { // touch session value.ifPresent(raw -> rsp.cookie(new Cookie.Definition(cookie).value(raw))); } }); return result; }; }
@Override public void handle(Request req, Response rsp, Route.Chain chain) throws Throwable { try { WebContext context = req.require(WebContext.class); /** 1: don't save authentication urls: */ String existingRequestedUrl = (String) context .getSessionAttribute(Pac4jConstants.REQUESTED_URL); boolean resetRequestedUrl = excludes.stream() .filter(it -> !it.endsWith("/**") && req.matches(it)) .findFirst() .isPresent(); conf.getSecurityLogic() .perform(context, conf, new Pac4jGrantAccessAdapter(req, rsp, chain), conf.getHttpActionAdapter(), clients, authorizers, matchers, multiProfile); /** 2: don't save authentication urls: */ if (resetRequestedUrl && req.ifSession().isPresent()) { // log.info("ignoring {} by {}", ctx.g, existingRequestedUrl); context.setSessionAttribute(Pac4jConstants.REQUESTED_URL, existingRequestedUrl); } } catch (TechnicalException x) { Throwable cause = x.getCause(); if (!(cause instanceof Err)) { // Pac4j wrap everything as TechnicalException, it makes stacktrace ugly, so we rethrow // Err cause = x; } throw cause; } // } }
req.ifSession().ifPresent(s -> envdata.put("session", dump(s::attributes)));
@Override public Optional<Session> ifSession() { return req.ifSession(); }
@SuppressWarnings("unchecked") @Override public void handle(final Request req, final Response rsp) throws Throwable { // DON'T create a session for JWT/param/header auth (a.k.a stateless) Optional<Session> ifSession = req.ifSession(); if (ifSession.isPresent()) { Session session = ifSession.get(); Optional<String> profileId = session.unset(Auth.ID).toOptional(); if (profileId.isPresent()) { Optional<CommonProfile> profile = req.require(AuthStore.class).unset(profileId.get()); log.debug("logout {}", profile); session.destroy(); } } else { log.debug("nothing to logout from session"); } String redirectTo = req.<String> ifGet("auth.logout.redirectTo").orElse(this.redirectTo); rsp.redirect(redirectTo); }
private Route.After saveCookie() { return (req, rsp, result) -> { req.ifSession().ifPresent(session -> { Optional<String> value = req.cookie(cookie.name().get()).toOptional(); Map<String, String> initial = value .map(this::attributes) .orElse(Collections.emptyMap()); Map<String, String> attributes = session.attributes(); // is dirty? boolean dirty = !initial.equals(attributes); log.debug("session dirty: {}", dirty); if (dirty) { log.debug("saving session cookie"); String encoded = Cookie.URL_ENCODER.apply(attributes); String signed = Cookie.Signature.sign(encoded, secret); rsp.cookie(new Cookie.Definition(cookie).value(signed)); } else if (timeout > 0) { // touch session value.ifPresent(raw -> rsp.cookie(new Cookie.Definition(cookie).value(raw))); } }); return result; }; }
req.ifSession().ifPresent(s -> envdata.put("session", dump(s::attributes)));