ctx.addCookie(Cookie.cookie("visits", "" + visits));
private void createNewSession(RoutingContext context) { Session session = sessionStore.createSession(sessionTimeout, minLength); context.setSession(session); Cookie cookie = Cookie.cookie(sessionCookieName, session.value()); cookie.setPath(sessionCookiePath); cookie.setSecure(sessionCookieSecure); cookie.setHttpOnly(sessionCookieHttpOnly); // Don't set max age - it's a session cookie context.addCookie(cookie); addStoreSessionHandler(context); } }
/** * Create a new cookie * @param name the name of the cookie * @param value the cookie value * @return the cookie */ public static io.vertx.rxjava.ext.web.Cookie cookie(String name, String value) { io.vertx.rxjava.ext.web.Cookie ret = io.vertx.rxjava.ext.web.Cookie.newInstance(io.vertx.ext.web.Cookie.cookie(name, value)); return ret; }
/** * Create a new cookie * @param name the name of the cookie * @param value the cookie value * @return the cookie */ public static io.vertx.rxjava.ext.web.Cookie cookie(String name, String value) { io.vertx.rxjava.ext.web.Cookie ret = io.vertx.rxjava.ext.web.Cookie.newInstance(io.vertx.ext.web.Cookie.cookie(name, value)); return ret; }
ctx.addCookie(Cookie.cookie(cookieName, token).setPath(cookiePath)); ctx.next(); break;
@Test public void testCookieFields() throws Exception { Cookie cookie = Cookie.cookie("foo", "bar"); assertEquals("foo", cookie.getName()); assertEquals("bar", cookie.getValue()); assertEquals("foo=bar", cookie.encode()); assertNull(cookie.getPath()); cookie.setPath("/somepath"); assertEquals("/somepath", cookie.getPath()); assertEquals("foo=bar; Path=/somepath", cookie.encode()); assertNull(cookie.getDomain()); cookie.setDomain("foo.com"); assertEquals("foo.com", cookie.getDomain()); assertEquals("foo=bar; Path=/somepath; Domain=foo.com", cookie.encode()); long maxAge = 30 * 60; cookie.setMaxAge(maxAge); long now = System.currentTimeMillis(); String encoded = cookie.encode(); int startPos = encoded.indexOf("Expires="); int endPos = encoded.indexOf(';', startPos); String expiresDate = encoded.substring(startPos + 8, endPos); Date d = dateTimeFormat.parse(expiresDate); assertTrue(d.getTime() - now >= maxAge); cookie.setMaxAge(Long.MIN_VALUE); cookie.setSecure(true); assertEquals("foo=bar; Path=/somepath; Domain=foo.com; Secure", cookie.encode()); cookie.setHttpOnly(true); assertEquals("foo=bar; Path=/somepath; Domain=foo.com; Secure; HTTPOnly", cookie.encode()); }
assertEquals("blibble", rc.getCookie("wibble").getValue()); assertNotNull(rc.getCookie("plop")); rc.addCookie(Cookie.cookie("fleeb", "floob")); assertEquals(4, rc.cookieCount()); assertNull(rc.removeCookie("blarb"));
@Override public void addResponseCookie(Cookie cookie) { routingContext.addCookie(io.vertx.ext.web.Cookie.cookie(cookie.getName(), cookie.getValue())); }
@Override public void addResponseCookie(Cookie cookie) { routingContext.addCookie(io.vertx.ext.web.Cookie.cookie(cookie.getName(), cookie.getValue())); }
@Override public void logout() { Session session = rc.session(); if (session != null) { session.destroy(); } rc.addCookie(Cookie.cookie(MeshJWTAuthProvider.TOKEN_COOKIE_KEY, "deleted").setMaxAge(0).setPath("/")); rc.clearUser(); }
/** * Handle the login action and set a token cookie if the credentials are valid. * * @param ac * Action context used to add token cookie * @param username * Username * @param password * Password */ public void login(InternalActionContext ac, String username, String password) { generateToken(username, password, rh -> { if (rh.failed()) { throw error(UNAUTHORIZED, "auth_login_failed", rh.cause()); } else { ac.addCookie(Cookie.cookie(MeshJWTAuthProvider.TOKEN_COOKIE_KEY, rh.result()) .setMaxAge(Mesh.mesh().getOptions().getAuthenticationOptions().getTokenExpirationTime()).setPath("/")); ac.send(new TokenResponse(rh.result()).toJson()); } }); }
public Handler<RoutingContext> loginStatusHandler() { return rc -> { AuthenticatedUser user = AuthenticatedUser.from(rc); if (user != null) { rc.response().end(new JsonObject() .put("authenticated", true) // TODO issuer; acting principal; authority sets .put("accountId", user.getAuthenticatedAs()) .put("userDisplayName", user.getFullDisplayName()).encode()); } else { QueryStringEncoder params = new QueryStringEncoder(""); params.addParam("client_id", clientId); params.addParam("response_type", "code"); params.addParam("scope", scope); params.addParam("redirect_uri", redirectUri(rc)); String state = new TokenGenerator(secureRandom).create(15); params.addParam("state", state); rc.response().headers().add(SET_COOKIE, io.vertx.ext.web.Cookie.cookie("state", state) .setHttpOnly(true) .setSecure(redirectUri(rc).startsWith("https")).encode()); rc.response().end(new JsonObject() .put("authenticated", false) .put("loginUrl", authUrl + params).encode()); } }; }
public Handler<RoutingContext> logoutHandler() { return rc -> { if ("yes".equals(rc.request().getParam("done"))) { rc.response().setStatusCode(302).putHeader("Location", VertxBase.absoluteContext(config::getString, rc)).end(); // rc.response().end("Logout complete"); return; } QueryStringEncoder fromEnc = new QueryStringEncoder(""); fromEnc.addParam("redirect_uri", VertxBase.absolutePath(config::getString, rc) + "?done=yes"); rc.response().headers() .add(SET_COOKIE, io.vertx.ext.web.Cookie.cookie("session_token", "").setMaxAge(0).encode()) .add(SET_COOKIE, io.vertx.ext.web.Cookie.cookie("XSRF-TOKEN", "").setMaxAge(0).encode()) .add("location", logoutUrl + fromEnc); rc.response().setStatusCode(302).end(); }; }
public Handler<RoutingContext> loginStatusHandler() { return rc -> { AuthenticatedUser user = AuthenticatedUser.from(rc); if (user != null) { rc.response().end(new JsonObject() .put("authenticated", true) // TODO issuer; acting principal; authority sets .put("accountId", user.getAuthenticatedAs()) .put("userDisplayName", user.getFullDisplayName()).encode()); } else { QueryStringEncoder params = new QueryStringEncoder(""); params.addParam("client_id", clientId); params.addParam("response_type", "code"); params.addParam("scope", scope); params.addParam("redirect_uri", redirectUri(rc)); String state = new TokenGenerator(secureRandom).create(15); params.addParam("state", state); rc.response().headers().add(SET_COOKIE, io.vertx.ext.web.Cookie.cookie("state", state) .setHttpOnly(true) .setSecure(redirectUri(rc).startsWith("https")).encode()); rc.response().end(new JsonObject() .put("authenticated", false) .put("loginUrl", authUrl + params).encode()); } }; }
public Handler<RoutingContext> logoutHandler() { return rc -> { if ("yes".equals(rc.request().getParam("done"))) { rc.response().setStatusCode(302).putHeader("Location", VertxBase.absoluteContext(config::getString, rc)).end(); // rc.response().end("Logout complete"); return; } QueryStringEncoder fromEnc = new QueryStringEncoder(""); fromEnc.addParam("redirect_uri", VertxBase.absolutePath(config::getString, rc) + "?done=yes"); rc.response().headers() .add(SET_COOKIE, io.vertx.ext.web.Cookie.cookie("session_token", "").setMaxAge(0).encode()) .add(SET_COOKIE, io.vertx.ext.web.Cookie.cookie("XSRF-TOKEN", "").setMaxAge(0).encode()) .add("location", logoutUrl + fromEnc); rc.response().setStatusCode(302).end(); }; }
private void createNewSession(RoutingContext context) { Session session = sessionStore.createSession(sessionTimeout, minLength); context.setSession(session); Cookie cookie = Cookie.cookie(sessionCookieName, session.value()); cookie.setPath(sessionCookiePath); cookie.setSecure(sessionCookieSecure); cookie.setHttpOnly(sessionCookieHttpOnly); // Don't set max age - it's a session cookie context.addCookie(cookie); addStoreSessionHandler(context); } }
private void createNewSession(RoutingContext context) { Session session = sessionStore.createSession(sessionTimeout, minLength); context.setSession(session); Cookie cookie = Cookie.cookie(sessionCookieName, session.id()); cookie.setPath(sessionCookiePath); cookie.setSecure(sessionCookieSecure); cookie.setHttpOnly(sessionCookieHttpOnly); // Don't set max age - it's a session cookie context.addCookie(cookie); addStoreSessionHandler(context); }
private void createNewSession(RoutingContext context) { Session session = sessionStore.createSession(sessionTimeout, minLength); context.setSession(session); Cookie cookie = Cookie.cookie(sessionCookieName, session.id()); cookie.setPath(sessionCookiePath); cookie.setSecure(sessionCookieSecure); cookie.setHttpOnly(sessionCookieHttpOnly); // Don't set max age - it's a session cookie context.addCookie(cookie); addStoreSessionHandler(context); }
ctx.addCookie(Cookie.cookie(cookieName, token).setPath(cookiePath)); ctx.next(); break;
@Test public void testCookieFields() throws Exception { Cookie cookie = Cookie.cookie("foo", "bar"); assertEquals("foo", cookie.getName()); assertEquals("bar", cookie.getValue()); assertEquals("foo=bar", cookie.encode()); assertNull(cookie.getPath()); cookie.setPath("/somepath"); assertEquals("/somepath", cookie.getPath()); assertEquals("foo=bar; Path=/somepath", cookie.encode()); assertNull(cookie.getDomain()); cookie.setDomain("foo.com"); assertEquals("foo.com", cookie.getDomain()); assertEquals("foo=bar; Path=/somepath; Domain=foo.com", cookie.encode()); long maxAge = 30 * 60; cookie.setMaxAge(maxAge); long now = System.currentTimeMillis(); String encoded = cookie.encode(); int startPos = encoded.indexOf("Expires="); int endPos = encoded.indexOf(';', startPos); String expiresDate = encoded.substring(startPos + 8, endPos); Date d = dateTimeFormat.parse(expiresDate); assertTrue(d.getTime() - now >= maxAge); cookie.setMaxAge(Long.MIN_VALUE); cookie.setSecure(true); assertEquals("foo=bar; Path=/somepath; Domain=foo.com; Secure", cookie.encode()); cookie.setHttpOnly(true); assertEquals("foo=bar; Path=/somepath; Domain=foo.com; Secure; HTTPOnly", cookie.encode()); }