Search in sources :

Example 1 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class AccepttoMultifactorDetermineUserAccountStatusActionTests method prepareRequestContext.

private static MockRequestContext prepareRequestContext() {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    request.setRemoteAddr("185.86.151.11");
    request.setLocalAddr("185.88.151.11");
    request.setCookies(new Cookie("jwt", UUID.randomUUID().toString()));
    ClientInfoHolder.setClientInfo(new ClientInfo(request));
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    AccepttoWebflowUtils.setEGuardianUserId(context, "eguardian-userid");
    WebUtils.putCredential(context, new AccepttoEmailCredential("cas@example.org"));
    return context;
}
Also used : lombok.val(lombok.val) Cookie(javax.servlet.http.Cookie) AccepttoEmailCredential(org.apereo.cas.mfa.accepto.AccepttoEmailCredential) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.apereo.cas.util.MockServletContext)

Example 2 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class AccepttoApiUtilsTests method verifyAuthenticate.

@Test
public void verifyAuthenticate() throws Exception {
    val properties = new AccepttoMultifactorAuthenticationProperties();
    properties.setGroupAttribute("group");
    properties.setEmailAttribute("email");
    properties.setApplicationId("appid");
    properties.setSecret("p@$$w0rd");
    properties.setRegistrationApiUrl("http://localhost:9285");
    properties.setOrganizationId("org-id");
    properties.setOrganizationSecret("255724611137f7eb0280dd76b0546eea4bca1c7ba1");
    val principal = CoreAuthenticationTestUtils.getPrincipal(Map.of("email", List.of("cas@example.org"), "group", List.of("staff")));
    val authentication = CoreAuthenticationTestUtils.getAuthentication(principal);
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    request.setRemoteAddr("185.86.151.11");
    request.setLocalAddr("185.88.151.11");
    request.setCookies(new Cookie("jwt", UUID.randomUUID().toString()));
    ClientInfoHolder.setClientInfo(new ClientInfo(request));
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    AccepttoWebflowUtils.setEGuardianUserId(context, "eguardian-userid");
    WebUtils.putCredential(context, new AccepttoEmailCredential("cas@example.org"));
    val keyGen = KeyPairGenerator.getInstance("RSA");
    val pair = keyGen.generateKeyPair();
    val priv = pair.getPrivate();
    val pub = pair.getPublic();
    val payload = MAPPER.writeValueAsString(Map.of("uid", "casuser"));
    val jwt = EncodingUtils.signJwsRSASha512(priv, payload.getBytes(StandardCharsets.UTF_8), Map.of());
    var data = MAPPER.writeValueAsString(Map.of("content", new String(jwt, StandardCharsets.UTF_8)));
    try (val webServer = new MockWebServer(9285, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        val results = AccepttoApiUtils.authenticate(authentication, properties, context, pub);
        assertNotNull(results);
    }
    try (val webServer = new MockWebServer(9285, new ByteArrayResource(StringUtils.EMPTY.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        val results = AccepttoApiUtils.authenticate(authentication, properties, context, pub);
        assertTrue(results.isEmpty());
    }
}
Also used : lombok.val(lombok.val) Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) AccepttoMultifactorAuthenticationProperties(org.apereo.cas.configuration.model.support.mfa.AccepttoMultifactorAuthenticationProperties) MockRequestContext(org.springframework.webflow.test.MockRequestContext) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockServletContext(org.apereo.cas.util.MockServletContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockWebServer(org.apereo.cas.util.MockWebServer) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class U2FAccountSaveRegistrationActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val id = UUID.randomUUID().toString();
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication(id), context);
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putMultifactorAuthenticationProviderIdIntoFlowScope(context, u2fMultifactorAuthenticationProvider);
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, u2fStartRegistrationAction.execute(context).getId());
    val u2fReg = context.getFlowScope().get("u2fReg", U2FRegistration.class);
    deviceRepository.requestDeviceRegistration("fb36YebRZyTcR_p4SWrSI7UIJvB6FP82tkcg5AGHhJM", id, u2fReg.getJsonData());
    val tokenResponse = "{\"registrationData\":\"BQQkG0HfMv4hzlfHpaWySCKdvio0-w1zi6s" + "uSu0_QJiL4vE-DwggXFWxuWn7rkae1ZAasYl_YnQny7ldxYoGA9riQL2ast4dMon" + "mEtZ80VYKltXyWevhxR11OwF07GLZUcETjPlNGE8WFuanXXQo6-6cujXTZ6yltF8iME66jb8I3" + "hEwggEbMIHCoAMCAQICCjgtyGkJIYxviTAwCgYIKoZIzj0EAwIwFTETMBEGA1" + "UEAxMKVTJGIElzc3VlcjAaFwswMDAxMDEwMDAwWhcLMDAwMTAxMDAwMFowFTETMBEGA1UEAxMKV" + "TJGIERldmljZTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABE0eSLFD2Bg3YsE--2pDCrvhQ7_5Liwn" + "ssiRSdzgHQHo2RLtnhVJ_EDZkuuzxCeQoSkZBGJkARxjO_bcCitAjDwwCgYIKoZ" + "Izj0EAwIDSAAwRQIhAMGjpo4vFqchRicFf2K7coyeA-ehumLQRlJORW0sLz9zAiALX3jlEaoYEp9vI2" + "2SEyJ9krTmft9T6BbfsF2dyLkP3jBEAiB9VOA7XifeRUNKa9TefQog57-ojDsdWcDrjKTb5GQf0" + "QIgJzxbUiQam_olH10kq3y2FIosS8PTc2LhcotPH7_3uHU\",\"version\":\"U2F_V2\"," + "\"challenge\":\"fb36YebRZyTcR_p4SWrSI7UIJvB6FP82tkcg5AGHhJM\",\"" + "clientData\":\"eyJjaGFsbGVuZ2UiOiJmYjM2WWViUlp5VGNSX3A0U1dyU0k" + "3VUlKdkI2RlA4MnRrY2c1QUdIaEpNIiwib3J" + "pZ2luIjoiaHR0cHM6Ly9tbW9heXllZC51bmljb24ubmV0Ojg0NDMiLCJ0eXAiO" + "iJuYXZpZ2F0b3IuaWQuZmluaXNoRW5yb2xsbWVudCJ9\"}";
    request.addParameter("tokenResponse", tokenResponse);
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, u2fSaveAccountRegistrationAction.execute(context).getId());
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, u2fCheckAccountRegistrationAction.execute(context).getId());
}
Also used : lombok.val(lombok.val) 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.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class U2FAccountCheckRegistrationActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val id = UUID.randomUUID().toString();
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication(id), context);
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putMultifactorAuthenticationProviderIdIntoFlowScope(context, u2fMultifactorAuthenticationProvider);
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    assertEquals(CasWebflowConstants.TRANSITION_ID_REGISTER, u2fCheckAccountRegistrationAction.execute(context).getId());
}
Also used : lombok.val(lombok.val) 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.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class U2FStartRegistrationActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val id = UUID.randomUUID().toString();
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication(id), context);
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putMultifactorAuthenticationProviderIdIntoFlowScope(context, u2fMultifactorAuthenticationProvider);
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, u2fStartRegistrationAction.execute(context).getId());
    assertNotNull(context.getFlowScope().get("u2fReg"));
}
Also used : lombok.val(lombok.val) 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.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

lombok.val (lombok.val)50 MockServletContext (org.apereo.cas.util.MockServletContext)50 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)46 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)46 MockRequestContext (org.springframework.webflow.test.MockRequestContext)46 Test (org.junit.jupiter.api.Test)45 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)44 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)30 JEEContext (org.pac4j.core.context.JEEContext)10 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)7 Executable (org.junit.jupiter.api.function.Executable)5 DefaultRegisteredServiceAccessStrategy (org.apereo.cas.services.DefaultRegisteredServiceAccessStrategy)4 SAML2Client (org.pac4j.saml.client.SAML2Client)4 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)4 Flow (org.springframework.webflow.engine.Flow)3 Cookie (javax.servlet.http.Cookie)2 SneakyThrows (lombok.SneakyThrows)2 AuthenticationServiceSelectionPlan (org.apereo.cas.authentication.AuthenticationServiceSelectionPlan)2 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)2 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)2