Search in sources :

Example 26 with MockWebServer

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

the class SwivelAuthenticationHandlerTests method verifyAuthnFails.

@Test
public void verifyAuthnFails() throws Exception {
    val data = "<?xml version=\"1.0\" ?>" + "<SASResponse secret=\"MyAdminAgent\" version=\"3.4\">" + "<Version>3.6</Version>\n" + "<Result>FAIL</Result>\n" + "<SessionID>c7379ef1b41f90a4900548a75e13f62a</SessionID>" + "</SASResponse>";
    try (val webServer = new MockWebServer(9191, new ByteArrayResource(data.getBytes(UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val c = new SwivelTokenCredential("123456");
        val context = new MockRequestContext();
        val request = new MockHttpServletRequest();
        val response = new MockHttpServletResponse();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
        setRequestContext(context);
        ExternalContextHolder.setExternalContext(context.getExternalContext());
        WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication(), context);
        setRequestContext(context);
        assertThrows(FailedLoginException.class, () -> swivelAuthenticationHandler.authenticate(c));
    }
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 27 with MockWebServer

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

the class SwivelAuthenticationHandlerTests method verifyAuthn.

@Test
public void verifyAuthn() throws Exception {
    val data = "<?xml version=\"1.0\" ?>" + "<SASResponse secret=\"MyAdminAgent\" version=\"3.4\">" + "<Version>3.6</Version>\n" + "<Result>PASS</Result>\n" + "<SessionID>c7379ef1b41f90a4900548a75e13f62a</SessionID>" + "</SASResponse>";
    try (val webServer = new MockWebServer(9191, new ByteArrayResource(data.getBytes(UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val c = new SwivelTokenCredential("123456");
        val context = new MockRequestContext();
        val request = new MockHttpServletRequest();
        val response = new MockHttpServletResponse();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
        setRequestContext(context);
        ExternalContextHolder.setExternalContext(context.getExternalContext());
        WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication(), context);
        setRequestContext(context);
        assertNotNull(swivelAuthenticationHandler.authenticate(c));
    }
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 28 with MockWebServer

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

the class CRLDistributionPointRevocationCheckerTests method checkCertificate.

@ParameterizedTest
@MethodSource("getTestParameters")
public void checkCertificate(final CRLDistributionPointRevocationChecker checker, final String[] certFiles, final String crlFile, final GeneralSecurityException expected) throws IOException, InterruptedException {
    val file = new File(FileUtils.getTempDirectory(), "ca.crl");
    val out = new FileOutputStream(file);
    IOUtils.copy(new ClassPathResource(crlFile).getInputStream(), out);
    this.webServer = new MockWebServer(8085, new FileSystemResource(file), "text/plain");
    this.webServer.start();
    LOGGER.debug("Web server listening on port 8085 serving file [{}]", crlFile);
    Thread.sleep(500);
    BaseCRLRevocationCheckerTests.checkCertificate(checker, certFiles, expected);
    checker.close();
}
Also used : lombok.val(lombok.val) FileOutputStream(java.io.FileOutputStream) MockWebServer(org.apereo.cas.util.MockWebServer) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) ClassPathResource(org.springframework.core.io.ClassPathResource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 29 with MockWebServer

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

the class AccepttoMultifactorDetermineUserAccountStatusActionTests method verifyOperationApprove.

@Test
public void verifyOperationApprove(@Autowired final CasConfigurationProperties casProperties) throws Exception {
    val context = prepareRequestContext();
    val keyGen = KeyPairGenerator.getInstance("RSA");
    val pair = keyGen.generateKeyPair();
    val priv = pair.getPrivate();
    val pub = pair.getPublic();
    val payload = MAPPER.writeValueAsString(Map.of("success", "true", "status", "OK", "eguardian_user_id", "cas-user", "channel", UUID.randomUUID().toString(), "response_code", "approved"));
    val jwt = EncodingUtils.signJwsRSASha512(priv, payload.getBytes(StandardCharsets.UTF_8), Map.of());
    val data = MAPPER.writeValueAsString(Map.of("content", new String(jwt, StandardCharsets.UTF_8)));
    casProperties.getAuthn().getMfa().getAcceptto().setRegistrationApiUrl("http://localhost:5015");
    try (val webServer = new MockWebServer(5015, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        val action = new AccepttoMultifactorDetermineUserAccountStatusAction(casProperties, pub);
        val principal = CoreAuthenticationTestUtils.getPrincipal(Map.of("email", List.of("cas@example.org"), "group", List.of("staff")));
        val authentication = CoreAuthenticationTestUtils.getAuthentication(principal);
        WebUtils.putAuthentication(authentication, context);
        RequestContextHolder.setRequestContext(context);
        val result = action.doExecute(context);
        assertEquals(result.getId(), CasWebflowConstants.TRANSITION_ID_APPROVE);
    }
}
Also used : lombok.val(lombok.val) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 30 with MockWebServer

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

the class AccepttoMultifactorDetermineUserAccountStatusActionTests method verifyOperationSuccess.

@Test
public void verifyOperationSuccess(@Autowired final CasConfigurationProperties casProperties) throws Exception {
    val context = prepareRequestContext();
    val keyGen = KeyPairGenerator.getInstance("RSA");
    val pair = keyGen.generateKeyPair();
    val priv = pair.getPrivate();
    val pub = pair.getPublic();
    val payload = MAPPER.writeValueAsString(Map.of("success", "true", "status", "OK", "eguardian_user_id", "cas-user", "response_code", "success", "channel", UUID.randomUUID().toString()));
    val jwt = EncodingUtils.signJwsRSASha512(priv, payload.getBytes(StandardCharsets.UTF_8), Map.of());
    val data = MAPPER.writeValueAsString(Map.of("content", new String(jwt, StandardCharsets.UTF_8)));
    casProperties.getAuthn().getMfa().getAcceptto().setRegistrationApiUrl("http://localhost:5017");
    try (val webServer = new MockWebServer(5017, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        val action = new AccepttoMultifactorDetermineUserAccountStatusAction(casProperties, pub);
        val principal = CoreAuthenticationTestUtils.getPrincipal(Map.of("email", List.of("cas@example.org"), "group", List.of("staff")));
        val authentication = CoreAuthenticationTestUtils.getAuthentication(principal);
        WebUtils.putAuthentication(authentication, context);
        RequestContextHolder.setRequestContext(context);
        val result = action.doExecute(context);
        assertEquals(result.getId(), CasWebflowConstants.TRANSITION_ID_SUCCESS);
    }
}
Also used : lombok.val(lombok.val) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

MockWebServer (org.apereo.cas.util.MockWebServer)175 lombok.val (lombok.val)173 Test (org.junit.jupiter.api.Test)157 ByteArrayResource (org.springframework.core.io.ByteArrayResource)151 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)82 MockRequestContext (org.springframework.webflow.test.MockRequestContext)36 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)32 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)31 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)26 MockServletContext (org.springframework.mock.web.MockServletContext)25 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)15 ClassPathResource (org.springframework.core.io.ClassPathResource)15 ClientInfo (org.apereo.inspektr.common.web.ClientInfo)13 GoogleAuthenticatorMultifactorProperties (org.apereo.cas.configuration.model.support.mfa.gauth.GoogleAuthenticatorMultifactorProperties)10 AdaptiveAuthenticationProperties (org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties)9 Executable (org.junit.jupiter.api.function.Executable)8 RestEndpointProperties (org.apereo.cas.configuration.model.RestEndpointProperties)7 SamlRegisteredService (org.apereo.cas.support.saml.services.SamlRegisteredService)7 Service (org.apereo.cas.authentication.principal.Service)4 GoogleRecaptchaProperties (org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties)4