Search in sources :

Example 1 with AdaptiveAuthenticationProperties

use of org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties in project cas by apereo.

the class DefaultAdaptiveAuthenticationPolicyTests method verifyActionGeoLocationRejected.

@Test
public void verifyActionGeoLocationRejected() {
    val request = new MockHttpServletRequest();
    request.setRemoteAddr("185.86.151.11");
    request.setLocalAddr("185.88.151.11");
    request.addHeader(HttpRequestUtils.USER_AGENT_HEADER, USER_AGENT);
    ClientInfoHolder.setClientInfo(new ClientInfo(request));
    val geoRequest = new GeoLocationRequest(51.5, -0.118);
    val props = new AdaptiveAuthenticationProperties();
    props.getPolicy().setRejectCountries("UK");
    val service = mock(GeoLocationService.class);
    val response = new GeoLocationResponse();
    response.addAddress("UK");
    response.setLatitude(Double.parseDouble(geoRequest.getLatitude()));
    response.setLongitude(Double.parseDouble(geoRequest.getLongitude()));
    when(service.locate(anyString(), any())).thenReturn(response);
    val p = new DefaultAdaptiveAuthenticationPolicy(service, IPAddressIntelligenceService.allowed(), props);
    assertFalse(p.apply(new MockRequestContext(), USER_AGENT, geoRequest));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GeoLocationResponse(org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse) AdaptiveAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) MockRequestContext(org.springframework.webflow.test.MockRequestContext) GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest) Test(org.junit.jupiter.api.Test)

Example 2 with AdaptiveAuthenticationProperties

use of org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties in project cas by apereo.

the class DefaultAdaptiveAuthenticationPolicyTests method verifyActionWithNoClientInfo.

@Test
public void verifyActionWithNoClientInfo() {
    val props = new AdaptiveAuthenticationProperties();
    val service = mock(GeoLocationService.class);
    val p = new DefaultAdaptiveAuthenticationPolicy(service, IPAddressIntelligenceService.allowed(), props);
    assertTrue(p.apply(new MockRequestContext(), "something", new GeoLocationRequest()));
}
Also used : lombok.val(lombok.val) AdaptiveAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties) MockRequestContext(org.springframework.webflow.test.MockRequestContext) GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest) Test(org.junit.jupiter.api.Test)

Example 3 with AdaptiveAuthenticationProperties

use of org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties in project cas by apereo.

the class DefaultIPAddressIntelligenceServiceTests method verifyOperation.

@Test
public void verifyOperation() {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    val input = new DefaultIPAddressIntelligenceService(new AdaptiveAuthenticationProperties());
    assertTrue(input.examine(context, "1.2.3.4").isAllowed());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) AdaptiveAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties) 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 4 with AdaptiveAuthenticationProperties

use of org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties in project cas by apereo.

the class RestfulIPAddressIntelligenceServiceTests method verifyBannedOperation.

@Test
public void verifyBannedOperation() {
    try (val webServer = new MockWebServer(9304, new ByteArrayResource(StringUtils.EMPTY.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.FORBIDDEN)) {
        webServer.start();
        val props = new AdaptiveAuthenticationProperties();
        props.getIpIntel().getRest().setUrl("http://localhost:9304");
        val service = new RestfulIPAddressIntelligenceService(props);
        val result = service.examine(new MockRequestContext(), "1.2.3.4");
        assertNotNull(result);
        assertTrue(result.isBanned());
    }
}
Also used : lombok.val(lombok.val) MockWebServer(org.apereo.cas.util.MockWebServer) AdaptiveAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockRequestContext(org.springframework.webflow.test.MockRequestContext) Test(org.junit.jupiter.api.Test)

Example 5 with AdaptiveAuthenticationProperties

use of org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties in project cas by apereo.

the class RestfulIPAddressIntelligenceServiceTests method verifyRankedOperation.

@Test
public void verifyRankedOperation() {
    try (val webServer = new MockWebServer(9306, new ByteArrayResource("12.435".getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.PRECONDITION_REQUIRED)) {
        webServer.start();
        val props = new AdaptiveAuthenticationProperties();
        props.getIpIntel().getRest().setUrl("http://localhost:9306");
        var service = new RestfulIPAddressIntelligenceService(props);
        var result = service.examine(new MockRequestContext(), "1.2.3.4");
        assertNotNull(result);
        assertTrue(result.isRanked());
        props.getPolicy().setRejectIpAddresses("123\\..*");
        service = new RestfulIPAddressIntelligenceService(props);
        result = service.examine(new MockRequestContext(), "123.1.2.3");
        assertNotNull(result);
        assertTrue(result.isBanned());
    }
}
Also used : lombok.val(lombok.val) MockWebServer(org.apereo.cas.util.MockWebServer) AdaptiveAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockRequestContext(org.springframework.webflow.test.MockRequestContext) Test(org.junit.jupiter.api.Test)

Aggregations

AdaptiveAuthenticationProperties (org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties)17 Test (org.junit.jupiter.api.Test)17 lombok.val (lombok.val)16 MockRequestContext (org.springframework.webflow.test.MockRequestContext)16 MockWebServer (org.apereo.cas.util.MockWebServer)9 ByteArrayResource (org.springframework.core.io.ByteArrayResource)9 GeoLocationRequest (org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 ClientInfo (org.apereo.inspektr.common.web.ClientInfo)4 GeoLocationResponse (org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse)3 ClassPathResource (org.springframework.core.io.ClassPathResource)2 GeoLocationService (org.apereo.cas.authentication.adaptive.geo.GeoLocationService)1 IPAddressIntelligenceResponse (org.apereo.cas.authentication.adaptive.intel.IPAddressIntelligenceResponse)1 IPAddressIntelligenceService (org.apereo.cas.authentication.adaptive.intel.IPAddressIntelligenceService)1 HttpRequestUtils (org.apereo.cas.util.HttpRequestUtils)1 ClientInfoHolder (org.apereo.inspektr.common.web.ClientInfoHolder)1 Assertions (org.junit.jupiter.api.Assertions)1 Tag (org.junit.jupiter.api.Tag)1 Mockito (org.mockito.Mockito)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1