private static Cookie makeDefaultSessionCookie(String sessionId, int maxAge) { DefaultCookie cookie = new DefaultCookie(SessionDao.SESSION_COOKIE, sessionId); cookie.setMaxAge(maxAge); return cookie; }
Cookie cookie() { cookie.setDomain(domain); cookie.setPath(path); cookie.setMaxAge(mergeMaxAgeAndExpires()); cookie.setSecure(secure); cookie.setHttpOnly(httpOnly); return cookie; }
/** * Converts the specified {@link ResponseCookie} to Netty's {@link Cookie} interface. */ static Cookie toNettyCookie(ResponseCookie resCookie) { final DefaultCookie cookie = new DefaultCookie(resCookie.getName(), resCookie.getValue()); cookie.setHttpOnly(resCookie.isHttpOnly()); cookie.setMaxAge(resCookie.getMaxAge().getSeconds()); cookie.setSecure(resCookie.isSecure()); // Domain and path are nullable, but the setters allow null as their input. cookie.setDomain(resCookie.getDomain()); cookie.setPath(resCookie.getPath()); return cookie; } }
private FullHttpResponse unauthorized(Optional<String> jsonp) { String encode = JsonHelper.jsonObject() .put("success", false) .put("message", UNAUTHORIZED.reasonPhrase()) .put("authentication", config.getAuthentication()) .put("googleApiKey", config.getGoogleClientId()).toString(); if (jsonp.isPresent()) { encode = jsonp.get() + "(" + encode + ")"; } DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, wrappedBuffer(encode.getBytes(CharsetUtil.UTF_8))); DefaultCookie cookie = new DefaultCookie("session", ""); cookie.setHttpOnly(true); cookie.setMaxAge(0); response.headers().add(SET_COOKIE, STRICT.encode(cookie)); response.headers().set(CONTENT_TYPE, "application/json; charset=utf-8"); return response; }
@Override protected final void channelRead0(ChannelHandlerContext ctx, T loginRequest) throws Exception { try { LOG.trace("Authenticating {}", loginRequest); Authentication auth = authenticate(ctx, loginRequest); LOG.trace("Authenticated {}", auth); String sessionId = URLEncoder.encode(UUID.randomUUID().toString(), StandardCharsets.UTF_8.name()); AuthCache.getCache().put(sessionId, auth); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.headers().set(Names.CONTENT_TYPE, Constants.JSON_TYPE); response.headers().set(Names.CONTENT_LENGTH, response.content().readableBytes()); DefaultCookie cookie = new DefaultCookie(Constants.COOKIE_NAME, sessionId); cookie.setDomain(domain); cookie.setMaxAge(maxAge); cookie.setPath("/"); cookie.setHttpOnly(true); cookie.setSecure(true); response.headers().set(Names.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie)); sendResponse(ctx, response); } catch (Exception e) { LOG.error("Login failure", e); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED); response.headers().set(Names.CONTENT_TYPE, Constants.JSON_TYPE); response.headers().set(Names.CONTENT_LENGTH, response.content().readableBytes()); sendResponse(ctx, response); } }
public Response addCookie(String name, String value, String domain, Boolean isHttpOnly, Long maxAge, String path, Boolean isSecured) { if(cookies == null) { cookies = Lists.newArrayList(); } final DefaultCookie defaultCookie = new DefaultCookie(name, value); if(domain != null) { defaultCookie.setDomain(domain); } if(isHttpOnly != null) { defaultCookie.setHttpOnly(isHttpOnly); } if(maxAge != null) { defaultCookie.setMaxAge(maxAge); } if(path != null) { defaultCookie.setPath(path); } if(isSecured != null) { defaultCookie.setSecure(isSecured); } cookies.add(defaultCookie); return this; }
Cookie cookie() { cookie.setDomain(domain); cookie.setPath(path); cookie.setMaxAge(mergeMaxAgeAndExpires()); cookie.setSecure(secure); cookie.setHttpOnly(httpOnly); return cookie; }
Cookie cookie() { cookie.setDomain(domain); cookie.setPath(path); cookie.setMaxAge(mergeMaxAgeAndExpires()); cookie.setSecure(secure); cookie.setHttpOnly(httpOnly); return cookie; }
Cookie cookie() { cookie.setDomain(domain); cookie.setPath(path); cookie.setMaxAge(mergeMaxAgeAndExpires()); cookie.setSecure(secure); cookie.setHttpOnly(httpOnly); return cookie; }
Cookie cookie() { cookie.setDomain(domain); cookie.setPath(path); cookie.setMaxAge(mergeMaxAgeAndExpires()); cookie.setSecure(secure); cookie.setHttpOnly(httpOnly); return cookie; }
private static Cookie convert(ResponseCookie cookie) { DefaultCookie nCookie = new DefaultCookie(cookie.name, cookie.value); nCookie.setDomain(cookie.domain); nCookie.setHttpOnly(cookie.httpOnly); nCookie.setSecure(cookie.secure); if (cookie.maxAge != null) { nCookie.setMaxAge(cookie.maxAge); } nCookie.setPath(cookie.path); return nCookie; }
@Override public void setCookie(Cookie cicadaCookie) { if (null == cicadaCookie){ throw new CicadaException("cookie is null!") ; } if (null == cicadaCookie.getName()){ throw new CicadaException("cookie.getName() is null!") ; } if (null == cicadaCookie.getValue()){ throw new CicadaException("cookie.getValue() is null!") ; } DefaultCookie cookie = new DefaultCookie(cicadaCookie.getName(), cicadaCookie.getValue()); cookie.setPath("/"); cookie.setMaxAge(cicadaCookie.getMaxAge()); cookies.add(cookie) ; }
@Override public void addResponseCookie(Cookie cookie) { final DefaultCookie newCookie = new DefaultCookie(cookie.getName(), cookie.getValue()); newCookie.setDomain(cookie.getDomain()); newCookie.setPath(cookie.getPath()); if (cookie.getMaxAge() >= 0) { newCookie.setMaxAge(cookie.getMaxAge()); } newCookie.setSecure(cookie.isSecure()); newCookie.setHttpOnly(cookie.isHttpOnly()); response.getCookies().add(newCookie); }
/** * Converts the Wisdom's cookie to a Netty's cookie. * * @param cookie the Wisdom's cookie * @return the Netty's cookie with the same metadata and content than the input cookie. */ public static DefaultCookie convertWisdomCookieToNettyCookie(Cookie cookie) { DefaultCookie nettyCookie = new DefaultCookie(cookie.name(), cookie.value()); nettyCookie.setMaxAge(cookie.maxAge()); // Comments are not supported anymore by netty. if (cookie.domain() != null) { nettyCookie.setDomain(cookie.domain()); } if (cookie.isSecure()) { nettyCookie.setSecure(true); } if (cookie.path() != null) { nettyCookie.setPath(cookie.path()); } if (cookie.isHttpOnly()) { nettyCookie.setHttpOnly(true); } return nettyCookie; }