String cookieValue = someCookie.getValue(); try { visits = Long.parseLong(cookieValue);
private boolean validateToken(String header, Cookie cookie) { // both the header and the cookie must be present, not null and equal if (header == null || cookie == null || !header.equals(cookie.getValue())) { return false; } String[] tokens = header.split("\\."); if (tokens.length != 3) { return false; } String saltPlusToken = tokens[0] + "." + tokens[1]; String signature = BASE64.encodeToString(mac.doFinal(saltPlusToken.getBytes())); if(!signature.equals(tokens[2])) { return false; } try { // validate validity return !(System.currentTimeMillis() > Long.parseLong(tokens[1]) + timeout); } catch (NumberFormatException e) { return false; } }
@Override public Cookie[] getCookies() { if (cookies == null) { Set<io.vertx.ext.web.Cookie> vertxCookies = context.cookies(); Cookie tmpCookies[] = new Cookie[vertxCookies.size()]; int idx = 0; for (io.vertx.ext.web.Cookie oneVertxCookie : vertxCookies) { Cookie cookie = new Cookie(oneVertxCookie.getName(), oneVertxCookie.getValue()); tmpCookies[idx] = cookie; idx++; } cookies = tmpCookies; } return cookies; }
@Override public String getFormattedItem(AccessLogParam<RoutingContext> accessLogParam) { Set<Cookie> cookieSet = accessLogParam.getContextData().cookies(); if (null == cookieSet) { return RESULT_NOT_FOUND; } String result = null; for (Cookie cookie : cookieSet) { if (varName.equals(cookie.getName())) { result = cookie.getValue(); } } if (null == result) { return RESULT_NOT_FOUND; } return result; }
if (cookie != null) { String sessionID = cookie.getValue(); if (sessionID != null && sessionID.length() > minLength) {
/** * @return the value of this cookie */ public String getValue() { String ret = delegate.getValue(); return ret; }
/** * @return the value of this cookie */ public String getValue() { String ret = delegate.getValue(); return ret; }
@Test public void testSimpleCookie() throws Exception { router.route().handler(rc -> { assertEquals(1, rc.cookieCount()); Cookie cookie = rc.getCookie("foo"); assertNotNull(cookie); assertEquals("bar", cookie.getValue()); rc.response().end(); }); testRequestWithCookies(HttpMethod.GET, "/", "foo=bar", 200, "OK"); }
@Override public Object apply(final String name, final Class<?> paramType, final RoutingContext context) { final Cookie cookie = context.getCookie(name); return cookie.getValue(); } }
@Override public Object apply(final String name, final Class<?> paramType, final RoutingContext context) { final Cookie cookie = context.getCookie(name); return cookie.getValue(); } }
router.route().handler(rc -> { assertEquals(3, rc.cookieCount()); assertEquals("bar", rc.getCookie("foo").getValue()); assertEquals("blibble", rc.getCookie("wibble").getValue()); assertEquals("flop", rc.getCookie("plop").getValue()); rc.removeCookie("plop"); }); router.route().handler(rc -> { assertEquals("bar", rc.getCookie("foo").getValue()); assertEquals("blibble", rc.getCookie("wibble").getValue()); assertNotNull(rc.getCookie("plop")); rc.addCookie(Cookie.cookie("fleeb", "floob"));
private boolean validateToken(String header, Cookie cookie) { // both the header and the cookie must be present, not null and equal if (header == null || cookie == null || !header.equals(cookie.getValue())) { return false; } String[] tokens = header.split("\\."); if (tokens.length != 3) { return false; } String saltPlusToken = tokens[0] + "." + tokens[1]; String signature = BASE64.encodeToString(mac.doFinal(saltPlusToken.getBytes())); if(!signature.equals(tokens[2])) { return false; } try { // validate validity return !(System.currentTimeMillis() > Long.parseLong(tokens[1]) + timeout); } catch (NumberFormatException e) { return false; } }
@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()); }
@Override public Cookie[] getCookies() { if (cookies == null) { Set<io.vertx.ext.web.Cookie> vertxCookies = context.cookies(); Cookie tmpCookies[] = new Cookie[vertxCookies.size()]; int idx = 0; for (io.vertx.ext.web.Cookie oneVertxCookie : vertxCookies) { Cookie cookie = new Cookie(oneVertxCookie.getName(), oneVertxCookie.getValue()); tmpCookies[idx] = cookie; idx++; } cookies = tmpCookies; } return cookies; }
@Override public Cookie[] getCookies() { if (cookies == null) { Set<io.vertx.ext.web.Cookie> vertxCookies = context.cookies(); Cookie tmpCookies[] = new Cookie[vertxCookies.size()]; int idx = 0; for (io.vertx.ext.web.Cookie oneVertxCookie : vertxCookies) { Cookie cookie = new Cookie(oneVertxCookie.getName(), oneVertxCookie.getValue()); tmpCookies[idx] = cookie; idx++; } cookies = tmpCookies; } return cookies; }
public static AuthSession validateCookie(final RoutingContext context) { final Cookie cookie = context.getCookie(Constants.SESSIONCOOKIE); if (cookie == null) { return null; } final UUID sessioncookie = UUID.fromString(cookie.getValue()); final AuthSession session = AuthSessionManager.getSession(sessioncookie); if (session == null) { return null; } final String id = session.get("id"); if (id != null) { return session; } return null; } }
@Override public String getFormattedItem(AccessLogParam<RoutingContext> accessLogParam) { Set<Cookie> cookieSet = accessLogParam.getContextData().cookies(); if (null == cookieSet) { return RESULT_NOT_FOUND; } String result = null; for (Cookie cookie : cookieSet) { if (varName.equals(cookie.getName())) { result = cookie.getValue(); } } if (null == result) { return RESULT_NOT_FOUND; } return result; }
@Override public Collection<Cookie> getRequestCookies() { return routingContext.cookies().stream().map(cookie -> { final Cookie p4jCookie = new Cookie(cookie.getName(), cookie.getValue()); p4jCookie.setDomain(cookie.getDomain()); p4jCookie.setPath(cookie.getPath()); return p4jCookie; }).collect(Collectors.toList()); }
@Override public Collection<Cookie> getRequestCookies() { return routingContext.cookies().stream().map(cookie -> { final Cookie p4jCookie = new Cookie(cookie.getName(), cookie.getValue()); p4jCookie.setDomain(cookie.getDomain()); p4jCookie.setPath(cookie.getPath()); return p4jCookie; }).collect(Collectors.toList()); }
@Test public void testSimpleCookie() throws Exception { router.route().handler(rc -> { assertEquals(1, rc.cookieCount()); Cookie cookie = rc.getCookie("foo"); assertNotNull(cookie); assertEquals("bar", cookie.getValue()); rc.response().end(); }); testRequestWithCookies(HttpMethod.GET, "/", "foo=bar", 200, "OK"); }