Search in sources :

Example 6 with NewCookie

use of javax.ws.rs.core.NewCookie in project jersey by jersey.

the class FormResource method getForm.

/**
     * Produce a form from a static HTML file packaged with the compiled class
     * @return a stream from which the HTML form can be read.
     */
@GET
public Response getForm() {
    Date now = new Date();
    InputStream entity = this.getClass().getClassLoader().getResourceAsStream("form.html");
    return Response.ok(entity).cookie(new NewCookie("date", now.toString())).build();
}
Also used : InputStream(java.io.InputStream) Date(java.util.Date) NewCookie(javax.ws.rs.core.NewCookie) GET(javax.ws.rs.GET)

Example 7 with NewCookie

use of javax.ws.rs.core.NewCookie in project keywhiz by square.

the class SessionLoginResourceTest method goodCredentialsSetsCookie.

@Test
public void goodCredentialsSetsCookie() throws Exception {
    User user = User.named("goodUser");
    when(ldapAuthenticator.authenticate(goodCredentials)).thenReturn(Optional.of(user));
    Response response = sessionLoginResource.login(LoginRequest.from("good", "credentials".toCharArray()));
    assertThat(response.getStatus()).isEqualTo(SEE_OTHER.getStatusCode());
    Map<String, NewCookie> responseCookies = response.getCookies();
    assertThat(responseCookies).hasSize(2).containsOnlyKeys("session", "XSRF-TOKEN");
    User authUser = cookieAuthenticator.authenticate(responseCookies.get("session")).orElseThrow(RuntimeException::new);
    assertThat(authUser).isEqualTo(user);
}
Also used : Response(javax.ws.rs.core.Response) User(keywhiz.auth.User) NewCookie(javax.ws.rs.core.NewCookie) Test(org.junit.Test)

Example 8 with NewCookie

use of javax.ws.rs.core.NewCookie in project keywhiz by square.

the class SessionLogoutResourceIntegrationTest method sendsExpiredCookie.

@Test
public void sendsExpiredCookie() throws Exception {
    Request request = new Request.Builder().post(RequestBody.create(MediaType.parse("text/plain"), "")).url(testUrl("/admin/logout")).build();
    Response response = client.newCall(request).execute();
    assertThat(response.code()).isEqualTo(303);
    List<String> cookies = response.headers(HttpHeaders.SET_COOKIE);
    assertThat(cookies).hasSize(1);
    NewCookie cookie = NewCookie.valueOf(cookies.get(0));
    assertThat(cookie.getName()).isEqualTo("session");
    assertThat(cookie.getValue()).isEqualTo("expired");
    assertThat(cookie.getVersion()).isEqualTo(1);
    assertThat(cookie.getPath()).isEqualTo("/admin");
    assertThat(cookie.isSecure()).isTrue();
    assertThat(cookie.isHttpOnly()).isTrue();
    assertThat(cookie.getExpiry()).isEqualTo(new Date(0));
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) Date(java.util.Date) NewCookie(javax.ws.rs.core.NewCookie) Test(org.junit.Test)

Example 9 with NewCookie

use of javax.ws.rs.core.NewCookie in project ddf by codice.

the class IdpEndpoint method createCookie.

private NewCookie createCookie(HttpServletRequest request, org.opensaml.saml.saml2.core.Response response) {
    LOGGER.debug("Creating cookie for user.");
    if (response.getAssertions() != null && response.getAssertions().size() > 0) {
        Assertion assertion = response.getAssertions().get(0);
        if (assertion != null) {
            UUID uuid = UUID.randomUUID();
            cookieCache.cacheSamlAssertion(uuid.toString(), assertion.getDOM());
            URL url;
            try {
                url = new URL(request.getRequestURL().toString());
                LOGGER.debug("Returning new cookie for user.");
                return new NewCookie(COOKIE, uuid.toString(), SERVICES_IDP_PATH, url.getHost(), NewCookie.DEFAULT_VERSION, null, -1, null, true, true);
            } catch (MalformedURLException e) {
                LOGGER.info("Unable to create session cookie. Client will need to log in again.", e);
            }
        }
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) SecurityAssertion(ddf.security.assertion.SecurityAssertion) Assertion(org.opensaml.saml.saml2.core.Assertion) UUID(java.util.UUID) URL(java.net.URL) NewCookie(javax.ws.rs.core.NewCookie)

Example 10 with NewCookie

use of javax.ws.rs.core.NewCookie in project dropwizard by dropwizard.

the class FlashFactoryTest method passesInHttpSessions.

@Test
public void passesInHttpSessions() throws Exception {
    Response firstResponse = target("/flash").request(MediaType.TEXT_PLAIN).post(Entity.entity("Mr. Peeps", MediaType.TEXT_PLAIN));
    final Map<String, NewCookie> cookies = firstResponse.getCookies();
    firstResponse.close();
    Invocation.Builder builder = target("/flash").request().accept(MediaType.TEXT_PLAIN);
    for (NewCookie cookie : cookies.values()) {
        builder = builder.cookie(cookie);
    }
    final String secondResponse = builder.get(String.class);
    assertThat(secondResponse).isEqualTo("Mr. Peeps");
    Invocation.Builder anotherBuilder = target("/flash").request().accept(MediaType.TEXT_PLAIN);
    for (NewCookie cookie : cookies.values()) {
        anotherBuilder = anotherBuilder.cookie(cookie);
    }
    final String thirdResponse = anotherBuilder.get(String.class);
    assertThat(thirdResponse).isEqualTo("null");
}
Also used : Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) NewCookie(javax.ws.rs.core.NewCookie) AbstractJerseyTest(io.dropwizard.jersey.AbstractJerseyTest) Test(org.junit.Test)

Aggregations

NewCookie (javax.ws.rs.core.NewCookie)22 Test (org.junit.Test)11 Response (javax.ws.rs.core.Response)9 IOException (java.io.IOException)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Invocation (javax.ws.rs.client.Invocation)3 SimpleSign (ddf.security.samlp.SimpleSign)2 ValidationException (ddf.security.samlp.ValidationException)2 SecurityServiceException (ddf.security.service.SecurityServiceException)2 AbstractJerseyTest (io.dropwizard.jersey.AbstractJerseyTest)2 GET (javax.ws.rs.GET)2 WebTarget (javax.ws.rs.client.WebTarget)2 Cookie (javax.ws.rs.core.Cookie)2 User (keywhiz.auth.User)2 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)2 AuthnRequest (org.opensaml.saml.saml2.core.AuthnRequest)2 IncompatibleVersionException (com.datatorrent.stram.client.WebServicesVersionConversion.IncompatibleVersionException)1 WebServicesClient (com.datatorrent.stram.util.WebServicesClient)1 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)1