Search in sources :

Example 6 with CookieRetrievingCookieGenerator

use of org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator in project cas by apereo.

the class CookieRetrievingCookieGeneratorTests method verifyCookieForRememberMeByAuthnRequest.

@Test
public void verifyCookieForRememberMeByAuthnRequest() {
    val ctx = getCookieGenerationContext();
    val gen = new CookieRetrievingCookieGenerator(ctx);
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    request.addParameter(RememberMeCredential.REQUEST_PARAMETER_REMEMBER_ME, "true");
    WebUtils.putRememberMeAuthenticationEnabled(context, Boolean.TRUE);
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    gen.addCookie(request, response, CookieRetrievingCookieGenerator.isRememberMeAuthentication(context), "CAS-Cookie-Value");
    val cookie = response.getCookie(ctx.getName());
    assertNotNull(cookie);
    assertEquals(ctx.getRememberMeMaxAge(), cookie.getMaxAge());
}
Also used : lombok.val(lombok.val) CookieRetrievingCookieGenerator(org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 7 with CookieRetrievingCookieGenerator

use of org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator in project cas by apereo.

the class CookieRetrievingCookieGeneratorTests method verifyCookieForRememberMeByRequestContext.

@Test
public void verifyCookieForRememberMeByRequestContext() {
    val ctx = getCookieGenerationContext();
    val gen = new CookieRetrievingCookieGenerator(ctx);
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val authn = CoreAuthenticationTestUtils.getAuthentication("casuser", CollectionUtils.wrap(RememberMeCredential.AUTHENTICATION_ATTRIBUTE_REMEMBER_ME, CollectionUtils.wrap(Boolean.TRUE)));
    WebUtils.putAuthentication(authn, context);
    WebUtils.putRememberMeAuthenticationEnabled(context, Boolean.TRUE);
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    gen.addCookie(request, response, CookieRetrievingCookieGenerator.isRememberMeAuthentication(context), "CAS-Cookie-Value");
    val cookie = response.getCookie(ctx.getName());
    assertNotNull(cookie);
    assertEquals(ctx.getRememberMeMaxAge(), cookie.getMaxAge());
}
Also used : lombok.val(lombok.val) CookieRetrievingCookieGenerator(org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 8 with CookieRetrievingCookieGenerator

use of org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator in project cas by apereo.

the class CookieRetrievingCookieGeneratorTests method verifyCookieValueMissing.

@Test
public void verifyCookieValueMissing() {
    val context = getCookieGenerationContext();
    context.setName(StringUtils.EMPTY);
    val gen = new CookieRetrievingCookieGenerator(context);
    val request = new MockHttpServletRequest();
    request.addHeader(context.getName(), "CAS-Cookie-Value");
    val cookie = gen.retrieveCookieValue(request);
    assertNull(cookie);
}
Also used : lombok.val(lombok.val) CookieRetrievingCookieGenerator(org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 9 with CookieRetrievingCookieGenerator

use of org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator in project cas by apereo.

the class CookieRetrievingCookieGeneratorTests method verifyCookieSameSiteLax.

@Test
public void verifyCookieSameSiteLax() {
    val ctx = getCookieGenerationContext();
    ctx.setSameSitePolicy("lax");
    val gen = new CookieRetrievingCookieGenerator(ctx);
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    gen.addCookie(request, response, false, "CAS-Cookie-Value");
    val cookie = (MockCookie) response.getCookie(ctx.getName());
    assertNotNull(cookie);
    assertEquals("Lax", cookie.getSameSite());
}
Also used : lombok.val(lombok.val) CookieRetrievingCookieGenerator(org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockCookie(org.springframework.mock.web.MockCookie) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 10 with CookieRetrievingCookieGenerator

use of org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator in project cas by apereo.

the class CookieRetrievingCookieGeneratorTests method verifyExistingCookieInResponse.

@Test
public void verifyExistingCookieInResponse() {
    val context = getCookieGenerationContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val gen = new CookieRetrievingCookieGenerator(context);
    var cookie = gen.addCookie(request, response, "some-value");
    assertNotNull(cookie);
    var headers = response.getHeaders("Set-Cookie");
    assertEquals(1, headers.size());
    assertTrue(headers.get(0).contains(context.getName() + '=' + cookie.getValue()));
    cookie = gen.addCookie(request, response, "updated-value");
    assertNotNull(cookie);
    headers = response.getHeaders("Set-Cookie");
    assertEquals(1, headers.size());
    assertTrue(headers.get(0).contains(context.getName() + '=' + cookie.getValue()));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CookieRetrievingCookieGenerator(org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)10 CookieRetrievingCookieGenerator (org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator)10 Test (org.junit.jupiter.api.Test)9 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)9 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 MockServletContext (org.springframework.mock.web.MockServletContext)5 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)5 MockRequestContext (org.springframework.webflow.test.MockRequestContext)5 MockCookie (org.springframework.mock.web.MockCookie)3 Arrays (java.util.Arrays)2 Collectors (java.util.stream.Collectors)2 StringUtils (org.apache.commons.lang3.StringUtils)2 CoreAuthenticationTestUtils (org.apereo.cas.authentication.CoreAuthenticationTestUtils)2 RememberMeCredential (org.apereo.cas.authentication.RememberMeCredential)2 CollectionUtils (org.apereo.cas.util.CollectionUtils)2 CookieGenerationContext (org.apereo.cas.web.cookie.CookieGenerationContext)2 Assertions (org.junit.jupiter.api.Assertions)2 Tag (org.junit.jupiter.api.Tag)2 AuthenticationServiceSelectionPlan (org.apereo.cas.authentication.AuthenticationServiceSelectionPlan)1 Service (org.apereo.cas.authentication.principal.Service)1