Search in sources :

Example 1 with CookieRetrievingCookieGenerator

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

the class InitialFlowSetupCookieActionTests method initialize.

@BeforeEach
public void initialize() throws Exception {
    val warn = CookieGenerationContext.builder().name("warn").path(StringUtils.EMPTY).maxAge(2).domain(null).secure(false).httpOnly(false).comment("CAS Cookie").build();
    val tgt = CookieGenerationContext.builder().name("tgt").path(StringUtils.EMPTY).maxAge(2).domain(null).secure(false).httpOnly(false).comment("CAS Cookie").build();
    this.warnCookieGenerator = new CookieRetrievingCookieGenerator(warn);
    this.warnCookieGenerator.setCookiePath(StringUtils.EMPTY);
    this.tgtCookieGenerator = new CookieRetrievingCookieGenerator(tgt);
    this.tgtCookieGenerator.setCookiePath(StringUtils.EMPTY);
    val argExtractors = Collections.<ArgumentExtractor>singletonList(new DefaultArgumentExtractor(new WebApplicationServiceFactory()));
    val servicesManager = mock(ServicesManager.class);
    when(servicesManager.findServiceBy(any(Service.class))).thenReturn(RegisteredServiceTestUtils.getRegisteredService("test"));
    val sso = new SingleSignOnProperties().setCreateSsoCookieOnRenewAuthn(true).setRenewAuthnEnabled(true);
    this.action = new InitialFlowSetupAction(argExtractors, servicesManager, authenticationRequestServiceSelectionStrategies, tgtCookieGenerator, warnCookieGenerator, casProperties, authenticationEventExecutionPlan, new DefaultSingleSignOnParticipationStrategy(servicesManager, sso, mock(TicketRegistrySupport.class), mock(AuthenticationServiceSelectionPlan.class)), mock(TicketRegistrySupport.class));
    this.action.afterPropertiesSet();
}
Also used : lombok.val(lombok.val) ArgumentExtractor(org.apereo.cas.web.support.ArgumentExtractor) DefaultArgumentExtractor(org.apereo.cas.web.support.DefaultArgumentExtractor) DefaultArgumentExtractor(org.apereo.cas.web.support.DefaultArgumentExtractor) CookieRetrievingCookieGenerator(org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator) WebApplicationServiceFactory(org.apereo.cas.authentication.principal.WebApplicationServiceFactory) Service(org.apereo.cas.authentication.principal.Service) TicketRegistrySupport(org.apereo.cas.ticket.registry.TicketRegistrySupport) AuthenticationServiceSelectionPlan(org.apereo.cas.authentication.AuthenticationServiceSelectionPlan) InitialFlowSetupAction(org.apereo.cas.web.flow.login.InitialFlowSetupAction) SingleSignOnProperties(org.apereo.cas.configuration.model.core.sso.SingleSignOnProperties) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with CookieRetrievingCookieGenerator

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

the class CookieRetrievingCookieGeneratorTests method verifyCookiePathNotModified.

@Test
public void verifyCookiePathNotModified() {
    val request = new MockHttpServletRequest();
    var response = new MockHttpServletResponse();
    var gen1 = new CookieRetrievingCookieGenerator(getCookieGenerationContext("/custom/path/"));
    var cookie1 = gen1.addCookie(request, response, "some-value");
    assertEquals("/custom/path/", cookie1.getPath());
    gen1 = new CookieRetrievingCookieGenerator(getCookieGenerationContext(StringUtils.EMPTY));
    cookie1 = gen1.addCookie(request, response, "some-value");
    assertEquals("/", cookie1.getPath());
}
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)

Example 3 with CookieRetrievingCookieGenerator

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

the class CookieRetrievingCookieGeneratorTests method verifyOtherSetCookieHeaderIsNotDiscarded.

@Test
public void verifyOtherSetCookieHeaderIsNotDiscarded() {
    val context = getCookieGenerationContext();
    val gen = new CookieRetrievingCookieGenerator(context);
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    response.addHeader("Set-Cookie", gen.getCookieName() + "=some-cookie-value");
    response.addHeader("Set-Cookie", "OtherCookie=other-cookie-value");
    var headers = response.getHeaders("Set-Cookie");
    assertEquals(2, headers.size());
    val cookie = gen.addCookie(request, response, "some-value");
    assertNotNull(cookie);
    assertEquals("some-value", cookie.getValue());
    var headersAfter = response.getHeaders("Set-Cookie");
    assertEquals(2, headersAfter.size());
    val headerValuesAfter = response.getHeaderValues("Set-Cookie").stream().map(String.class::cast).map(header -> Arrays.stream(header.split(";")).iterator().next()).collect(Collectors.toSet());
    assertEquals(headerValuesAfter, CollectionUtils.wrapSet(cookie.getName() + "=some-value", "OtherCookie=other-cookie-value"));
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) lombok.val(lombok.val) MockRequestContext(org.springframework.webflow.test.MockRequestContext) RememberMeCredential(org.apereo.cas.authentication.RememberMeCredential) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CookieRetrievingCookieGenerator(org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) MockCookie(org.springframework.mock.web.MockCookie) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) CollectionUtils(org.apereo.cas.util.CollectionUtils) Assertions(org.junit.jupiter.api.Assertions) Tag(org.junit.jupiter.api.Tag) CookieGenerationContext(org.apereo.cas.web.cookie.CookieGenerationContext) MockServletContext(org.springframework.mock.web.MockServletContext) CoreAuthenticationTestUtils(org.apereo.cas.authentication.CoreAuthenticationTestUtils) CookieRetrievingCookieGenerator(org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with CookieRetrievingCookieGenerator

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

the class CookieRetrievingCookieGeneratorTests method verifyCookieValueByHeader.

@Test
public void verifyCookieValueByHeader() {
    val context = getCookieGenerationContext();
    val gen = new CookieRetrievingCookieGenerator(context);
    val request = new MockHttpServletRequest();
    request.addHeader(context.getName(), "CAS-Cookie-Value");
    val cookie = gen.retrieveCookieValue(request);
    assertNotNull(cookie);
    assertEquals("CAS-Cookie-Value", 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 5 with CookieRetrievingCookieGenerator

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

the class CookieRetrievingCookieGeneratorTests method verifyRemoveAllCookiesByName.

@Test
public void verifyRemoveAllCookiesByName() {
    val request = new MockHttpServletRequest();
    var response = new MockHttpServletResponse();
    val gen1 = new CookieRetrievingCookieGenerator(getCookieGenerationContext());
    val cookie1 = gen1.addCookie(request, response, "some-value");
    val gen2 = new CookieRetrievingCookieGenerator(getCookieGenerationContext("/cas"));
    val cookie2 = gen2.addCookie(request, response, "some-value");
    val gen3 = new CookieRetrievingCookieGenerator(getCookieGenerationContext("/cas/"));
    val cookie3 = gen3.addCookie(request, response, "some-value");
    request.setCookies(cookie1, cookie2, cookie3);
    response = new MockHttpServletResponse();
    gen1.removeAll(request, response);
    assertEquals(9, response.getCookies().length);
    assertTrue(Arrays.stream(response.getCookies()).allMatch(c -> c.getMaxAge() == 0));
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) lombok.val(lombok.val) MockRequestContext(org.springframework.webflow.test.MockRequestContext) RememberMeCredential(org.apereo.cas.authentication.RememberMeCredential) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CookieRetrievingCookieGenerator(org.apereo.cas.web.support.gen.CookieRetrievingCookieGenerator) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) MockCookie(org.springframework.mock.web.MockCookie) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) CollectionUtils(org.apereo.cas.util.CollectionUtils) Assertions(org.junit.jupiter.api.Assertions) Tag(org.junit.jupiter.api.Tag) CookieGenerationContext(org.apereo.cas.web.cookie.CookieGenerationContext) MockServletContext(org.springframework.mock.web.MockServletContext) CoreAuthenticationTestUtils(org.apereo.cas.authentication.CoreAuthenticationTestUtils) 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