List of usage examples for io.netty.handler.codec.http.cookie Cookie maxAge
long maxAge();
From source file:com.vmware.xenon.services.common.authn.TestBasicAuthenticationService.java
License:Open Source License
@Test public void testAuth() throws Throwable { this.host.resetAuthorizationContext(); URI authServiceUri = UriUtils.buildUri(this.host, BasicAuthenticationService.SELF_LINK); // send a request with no authentication header this.host.testStart(1); this.host.send(Operation.createPost(authServiceUri).setBody(new Object()).setCompletion((o, e) -> { if (e == null) { this.host.failIteration(new IllegalStateException("request should have failed")); return; }//w w w .j a v a 2s . c o m if (o.getStatusCode() != Operation.STATUS_CODE_UNAUTHORIZED) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } String authHeader = o.getResponseHeader(BasicAuthenticationUtils.WWW_AUTHENTICATE_HEADER_NAME); if (authHeader == null || !authHeader.equals(BasicAuthenticationUtils.WWW_AUTHENTICATE_HEADER_VALUE)) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } this.host.completeIteration(); })); this.host.testWait(); // send a request with an authentication header for an invalid user String headerVal = constructBasicAuth(INVALID_USER, PASSWORD); this.host.testStart(1); this.host.send(Operation.createPost(authServiceUri).setBody(new Object()) .addRequestHeader(Operation.AUTHORIZATION_HEADER, headerVal).setCompletion((o, e) -> { if (e == null) { this.host.failIteration(new IllegalStateException("request should have failed")); return; } if (o.getStatusCode() != Operation.STATUS_CODE_FORBIDDEN) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } this.host.completeIteration(); })); this.host.testWait(); // send a request with a malformed authentication header String userPassStr = new String(Base64.getEncoder().encode(new StringBuffer(USER).toString().getBytes())); headerVal = new StringBuffer(BASIC_AUTH_PREFIX).append(userPassStr).toString(); this.host.testStart(1); this.host.send(Operation.createPost(authServiceUri).setBody(new Object()) .addRequestHeader(Operation.AUTHORIZATION_HEADER, headerVal).setCompletion((o, e) -> { if (e == null) { this.host.failIteration(new IllegalStateException("request should have failed")); return; } if (o.getStatusCode() != Operation.STATUS_CODE_BAD_REQUEST) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } this.host.completeIteration(); })); this.host.testWait(); // send a request with an invalid password headerVal = constructBasicAuth(USER, INVALID_PASSWORD); this.host.testStart(1); this.host.send(Operation.createPost(authServiceUri).setBody(new Object()) .addRequestHeader(Operation.AUTHORIZATION_HEADER, headerVal).setCompletion((o, e) -> { if (e == null) { this.host.failIteration(new IllegalStateException("request should have failed")); return; } if (o.getStatusCode() != Operation.STATUS_CODE_FORBIDDEN) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } this.host.completeIteration(); })); this.host.testWait(); // Next send a valid request headerVal = constructBasicAuth(USER, PASSWORD); this.host.testStart(1); this.host.send(Operation.createPost(authServiceUri).setBody(new Object()) .addRequestHeader(Operation.AUTHORIZATION_HEADER, headerVal).setCompletion((o, e) -> { if (e != null) { this.host.failIteration(e); return; } if (o.getStatusCode() != Operation.STATUS_CODE_OK) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } if (o.getAuthorizationContext() == null) { this.host.failIteration(new IllegalStateException("Authorization context not set")); return; } // now issue a logout AuthenticationRequest request = new AuthenticationRequest(); request.requestType = AuthenticationRequestType.LOGOUT; Operation logoutOp = Operation.createPost(authServiceUri).setBody(request).forceRemote() .setCompletion((oo, ee) -> { if (ee != null) { this.host.failIteration(ee); return; } if (oo.getStatusCode() != Operation.STATUS_CODE_OK) { this.host.failIteration( new IllegalStateException("Invalid status code returned")); return; } String cookieHeader = oo.getResponseHeader(SET_COOKIE_HEADER); if (cookieHeader == null) { this.host.failIteration(new IllegalStateException("Cookie is null")); return; } Cookie cookie = ClientCookieDecoder.LAX.decode(cookieHeader); if (cookie.maxAge() != 0) { this.host.failIteration( new IllegalStateException("Max-Age for cookie is not zero")); return; } this.host.resetAuthorizationContext(); this.host.completeIteration(); }); this.host.setAuthorizationContext(o.getAuthorizationContext()); this.host.send(logoutOp); })); this.host.testWait(); // Finally, send a valid remote request, and validate the cookie & auth token this.host.testStart(1); this.host.send(Operation.createPost(authServiceUri).setBody(new Object()).forceRemote() .addRequestHeader(Operation.AUTHORIZATION_HEADER, headerVal).setCompletion((o, e) -> { if (e != null) { this.host.failIteration(e); return; } if (o.getStatusCode() != Operation.STATUS_CODE_OK) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } if (!validateAuthToken(o)) { return; } this.host.completeIteration(); })); this.host.testWait(); // delete the user and issue a remote request as the user // we should see a 200 response as xenon invokes this // request with the guest context this.host.setSystemAuthorizationContext(); this.host.sendAndWait(Operation .createDelete(UriUtils.buildUri(this.host, UriUtils.buildUriPath(UserService.FACTORY_LINK, USER))) .setCompletion((o, e) -> { if (e != null) { this.host.failIteration(e); return; } this.host.completeIteration(); })); this.host.resetSystemAuthorizationContext(); this.host.assumeIdentity(UriUtils.buildUriPath(UserService.FACTORY_LINK, USER)); this.host.testStart(1); this.host.send(Operation.createGet(UriUtils.buildUri(this.host, UserService.FACTORY_LINK)).forceRemote() .setCompletion((o, e) -> { if (e != null) { this.host.failIteration(e); return; } if (o.getStatusCode() != Operation.STATUS_CODE_OK) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } this.host.completeIteration(); })); this.host.testWait(); }
From source file:com.vmware.xenon.services.common.authn.TestBasicAuthenticationService.java
License:Open Source License
private void logout(URI authServiceUri, String[] authToken) { this.host.testStart(1); AuthenticationRequest request = new AuthenticationRequest(); request.requestType = AuthenticationRequestType.LOGOUT; this.host.send(Operation.createPost(authServiceUri).setBody(request).forceRemote() .addRequestHeader(Operation.REQUEST_AUTH_TOKEN_HEADER, authToken[0]).setCompletion((oo, ee) -> { if (ee != null) { this.host.failIteration(ee); return; }//from w ww.java 2 s. com if (oo.getStatusCode() != Operation.STATUS_CODE_OK) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } String cookieHeader = oo.getResponseHeader(SET_COOKIE_HEADER); if (cookieHeader == null) { this.host.failIteration(new IllegalStateException("Cookie is null")); return; } Cookie cookie = ClientCookieDecoder.LAX.decode(cookieHeader); if (cookie.maxAge() != 0) { this.host.failIteration(new IllegalStateException("Max-Age for cookie is not zero")); return; } this.host.completeIteration(); })); this.host.testWait(); }
From source file:com.vmware.xenon.services.common.authn.TestBasicAuthenticationService.java
License:Open Source License
private void doTestAuthWithUserInfo(boolean remote) throws Throwable { this.host.resetAuthorizationContext(); String userPassStr = new StringBuilder(USER).append(BASIC_AUTH_USER_SEPARATOR).append(PASSWORD).toString(); URI authServiceUri = UriUtils.buildUri(this.host, BasicAuthenticationService.SELF_LINK, null, userPassStr); this.host.testStart(1); Operation post = Operation.createPost(authServiceUri).setBody(new Object()).setCompletion((o, e) -> { if (e != null) { this.host.failIteration(e); return; }/*from w w w .j a v a2 s.c om*/ if (o.getStatusCode() != Operation.STATUS_CODE_OK) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } if (!o.isRemote() && o.getAuthorizationContext() == null) { this.host.failIteration(new IllegalStateException("Authorization context not set")); return; } // now issue a logout AuthenticationRequest request = new AuthenticationRequest(); request.requestType = AuthenticationRequestType.LOGOUT; Operation logoutOp = Operation.createPost(authServiceUri).setBody(request).forceRemote() .setCompletion((oo, ee) -> { if (ee != null) { this.host.failIteration(ee); return; } if (oo.getStatusCode() != Operation.STATUS_CODE_OK) { this.host.failIteration(new IllegalStateException("Invalid status code returned")); return; } String cookieHeader = oo.getResponseHeader(SET_COOKIE_HEADER); if (cookieHeader == null) { this.host.failIteration(new IllegalStateException("Cookie is null")); return; } Cookie cookie = ClientCookieDecoder.LAX.decode(cookieHeader); if (cookie.maxAge() != 0) { this.host.failIteration(new IllegalStateException("Max-Age for cookie is not zero")); return; } if (!cookie.isHttpOnly()) { this.host.failIteration(new IllegalStateException("Cookie is not HTTP-only")); return; } this.host.resetAuthorizationContext(); this.host.completeIteration(); }); this.host.setAuthorizationContext(o.getAuthorizationContext()); this.host.send(logoutOp); }); if (remote) { post.forceRemote(); } this.host.send(post); this.host.testWait(); }
From source file:org.asynchttpclient.cookie.ThreadSafeCookieStore.java
License:Open Source License
private boolean hasCookieExpired(Cookie cookie, long whenCreated) { // if not specify max-age, this cookie should be discarded when user agent is to be closed, but it is not expired. if (cookie.maxAge() == Cookie.UNDEFINED_MAX_AGE) return false; if (cookie.maxAge() <= 0) return true; if (whenCreated > 0) { long deltaSecond = (System.currentTimeMillis() - whenCreated) / 1000; return deltaSecond > cookie.maxAge(); } else//from ww w.jav a 2 s . com return false; }
From source file:org.asynchttpclient.cookie.ThreadSafeCookieStore.java
License:Open Source License
private void add(String requestDomain, String requestPath, Cookie cookie) { AbstractMap.SimpleEntry<String, Boolean> pair = cookieDomain(cookie.domain(), requestDomain); String keyDomain = pair.getKey(); boolean hostOnly = pair.getValue(); String keyPath = cookiePath(cookie.path(), requestPath); CookieKey key = new CookieKey(cookie.name().toLowerCase(), keyDomain, keyPath); if (hasCookieExpired(cookie, 0)) cookieJar.remove(key);/*from www. j av a 2 s. c o m*/ else cookieJar.put(key, new StoredCookie(cookie, hostOnly, cookie.maxAge() != Cookie.UNDEFINED_MAX_AGE)); }
From source file:org.atmosphere.nettosphere.BridgeRuntime.java
License:Apache License
private Set<javax.servlet.http.Cookie> getCookies(final HttpRequest request) { Set<javax.servlet.http.Cookie> result = new HashSet<javax.servlet.http.Cookie>(); String cookieHeader = request.headers().get("Cookie"); if (cookieHeader != null) { Set<io.netty.handler.codec.http.cookie.Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieHeader); for (io.netty.handler.codec.http.cookie.Cookie cookie : cookies) { javax.servlet.http.Cookie c = new javax.servlet.http.Cookie(cookie.name(), cookie.value()); if (cookie.domain() != null) { c.setDomain(cookie.domain()); }// w w w .ja v a 2s.c o m c.setHttpOnly(cookie.isHttpOnly()); c.setMaxAge((int) cookie.maxAge()); if (cookie.path() != null) { c.setPath(cookie.path()); } c.setSecure(cookie.isSecure()); result.add(c); } } return result; }
From source file:org.robotbrains.support.web.server.netty.NettyHttpRequest.java
License:Apache License
/** * Convert a Netty cookie to a Java HTTP cookie. * * @param cookie/* www . ja va2s . co m*/ * the Netty cookie * * @return the Java cookie */ private HttpCookie convertFromNettyCookie(Cookie cookie) { HttpCookie httpCookie = new HttpCookie(cookie.name(), cookie.value()); httpCookie.setDomain(cookie.domain()); httpCookie.setMaxAge(cookie.maxAge()); httpCookie.setPath(cookie.path()); httpCookie.setSecure(cookie.isSecure()); return httpCookie; }