Search in sources :

Example 21 with ClientInfo

use of org.apereo.inspektr.common.web.ClientInfo in project cas by apereo.

the class AccepttoQRCodeValidateWebSocketChannelActionTests method verifyForbidden.

@Test
public void verifyForbidden() throws Exception {
    val httpRequest = new MockHttpServletRequest();
    httpRequest.setRemoteAddr("185.86.151.11");
    httpRequest.setLocalAddr("185.88.151.11");
    httpRequest.addParameter("channel", "test-channel");
    ClientInfoHolder.setClientInfo(new ClientInfo(httpRequest));
    val data = MAPPER.writeValueAsString(CollectionUtils.wrap("success", "false", "user_email", "cas@example.org"));
    try (val webServer = new MockWebServer(5012, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.FORBIDDEN)) {
        webServer.start();
        val action = new AccepttoQRCodeValidateWebSocketChannelAction(casProperties, mfaAccepttoDistributedSessionStore);
        val context = new MockRequestContext();
        val response = new MockHttpServletResponse();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), httpRequest, response));
        WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication("casuser"), context);
        RequestContextHolder.setRequestContext(context);
        AccepttoWebflowUtils.setChannel(context, "test-channel");
        val result = action.doExecute(context);
        assertEquals(CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, result.getId());
    }
}
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) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) 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 22 with ClientInfo

use of org.apereo.inspektr.common.web.ClientInfo in project cas by apereo.

the class AccepttoQRCodeValidateWebSocketChannelActionTests method verifyUnauthz.

@Test
public void verifyUnauthz() throws Exception {
    val httpRequest = new MockHttpServletRequest();
    httpRequest.setRemoteAddr("185.86.151.11");
    httpRequest.setLocalAddr("185.88.151.11");
    httpRequest.addParameter("channel", "test-channel");
    ClientInfoHolder.setClientInfo(new ClientInfo(httpRequest));
    val data = MAPPER.writeValueAsString(CollectionUtils.wrap("success", "false", "user_email", "cas@example.org"));
    try (val webServer = new MockWebServer(5012, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.UNAUTHORIZED)) {
        webServer.start();
        val action = new AccepttoQRCodeValidateWebSocketChannelAction(casProperties, mfaAccepttoDistributedSessionStore);
        val context = new MockRequestContext();
        val response = new MockHttpServletResponse();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), httpRequest, response));
        WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication("casuser"), context);
        RequestContextHolder.setRequestContext(context);
        AccepttoWebflowUtils.setChannel(context, "test-channel");
        val result = action.doExecute(context);
        assertEquals(CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, result.getId());
    }
}
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) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) 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 23 with ClientInfo

use of org.apereo.inspektr.common.web.ClientInfo in project cas by apereo.

the class AccepttoQRCodeValidateWebSocketChannelActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val httpRequest = new MockHttpServletRequest();
    httpRequest.setRemoteAddr("185.86.151.11");
    httpRequest.setLocalAddr("185.88.151.11");
    httpRequest.addParameter("channel", "test-channel");
    ClientInfoHolder.setClientInfo(new ClientInfo(httpRequest));
    val data = MAPPER.writeValueAsString(CollectionUtils.wrap("success", "true", "user_email", "cas@example.org"));
    try (val webServer = new MockWebServer(5012, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val action = new AccepttoQRCodeValidateWebSocketChannelAction(casProperties, mfaAccepttoDistributedSessionStore);
        val context = new MockRequestContext();
        val response = new MockHttpServletResponse();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), httpRequest, response));
        WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication("casuser"), context);
        RequestContextHolder.setRequestContext(context);
        AccepttoWebflowUtils.setChannel(context, "test-channel");
        val result = action.doExecute(context);
        assertEquals(CasWebflowConstants.TRANSITION_ID_FINALIZE, result.getId());
        assertTrue(WebUtils.getCredential(context) instanceof AccepttoEmailCredential);
    }
}
Also used : lombok.val(lombok.val) AccepttoEmailCredential(org.apereo.cas.mfa.accepto.AccepttoEmailCredential) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockWebServer(org.apereo.cas.util.MockWebServer) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) 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 24 with ClientInfo

use of org.apereo.inspektr.common.web.ClientInfo 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 25 with ClientInfo

use of org.apereo.inspektr.common.web.ClientInfo in project cas by apereo.

the class DefaultConsentEngineTests method beforeAll.

@BeforeAll
public static void beforeAll() {
    val request = new MockHttpServletRequest();
    request.setRemoteAddr("127.0.0.1");
    request.setLocalAddr("127.0.0.1");
    ClientInfoHolder.setClientInfo(new ClientInfo(request));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

ClientInfo (org.apereo.inspektr.common.web.ClientInfo)82 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)69 lombok.val (lombok.val)65 Test (org.junit.jupiter.api.Test)42 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)36 MockRequestContext (org.springframework.webflow.test.MockRequestContext)35 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)31 MockServletContext (org.springframework.mock.web.MockServletContext)29 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)14 MockWebServer (org.apereo.cas.util.MockWebServer)13 ByteArrayResource (org.springframework.core.io.ByteArrayResource)13 BeforeEach (org.junit.jupiter.api.BeforeEach)12 BeforeAll (org.junit.jupiter.api.BeforeAll)6 AuthenticationException (org.apereo.cas.authentication.AuthenticationException)5 GeoLocationRequest (org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest)5 ClassPathResource (org.springframework.core.io.ClassPathResource)5 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)4 Cookie (javax.servlet.http.Cookie)4 GeoLocationResponse (org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse)4 AdaptiveAuthenticationProperties (org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties)4