Search in sources :

Example 1 with GoogleRecaptchaProperties

use of org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties in project cas by apereo.

the class ValidateCaptchaActionTests method verifyCaptchaFails.

@Test
public void verifyCaptchaFails() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    try (val webServer = new MockWebServer(9305, new ByteArrayResource(StringUtils.EMPTY.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        val props = new GoogleRecaptchaProperties().setVerifyUrl("http://localhost:9305");
        val validateAction = new ValidateCaptchaAction(new GoogleCaptchaV2Validator(props), captchaActivationStrategy);
        val result = validateAction.execute(context);
        assertNotNull(result);
        assertEquals(CasWebflowConstants.TRANSITION_ID_CAPTCHA_ERROR, result.getId());
    }
}
Also used : lombok.val(lombok.val) GoogleCaptchaV2Validator(org.apereo.cas.web.GoogleCaptchaV2Validator) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) MockWebServer(org.apereo.cas.util.MockWebServer) MockRequestContext(org.springframework.webflow.test.MockRequestContext) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with GoogleRecaptchaProperties

use of org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties in project cas by apereo.

the class DefaultCaptchaActivationStrategyTests method verifyByProps.

@Test
public void verifyByProps() {
    val strategy = new DefaultCaptchaActivationStrategy(mock(ServicesManager.class));
    val context = getRequestContext(new MockHttpServletRequest());
    val properties = new GoogleRecaptchaProperties().setEnabled(true);
    assertTrue(strategy.shouldActivate(context, properties).isPresent());
    properties.setEnabled(false);
    assertTrue(strategy.shouldActivate(context, properties).isEmpty());
}
Also used : lombok.val(lombok.val) ServicesManager(org.apereo.cas.services.ServicesManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) Test(org.junit.jupiter.api.Test)

Example 3 with GoogleRecaptchaProperties

use of org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties in project cas by apereo.

the class DefaultCaptchaActivationStrategyTests method verifyByIpPattern.

@Test
public void verifyByIpPattern() {
    val strategy = new DefaultCaptchaActivationStrategy(mock(ServicesManager.class));
    val request = new MockHttpServletRequest();
    val context = getRequestContext(request);
    val properties = new GoogleRecaptchaProperties().setEnabled(true).setActivateForIpAddressPattern("127.+");
    request.setRemoteAddr("185.86.151.11");
    request.setLocalAddr("195.88.151.11");
    ClientInfoHolder.setClientInfo(new ClientInfo(request));
    assertFalse(strategy.shouldActivate(context, properties).isPresent());
}
Also used : lombok.val(lombok.val) ServicesManager(org.apereo.cas.services.ServicesManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) Test(org.junit.jupiter.api.Test)

Example 4 with GoogleRecaptchaProperties

use of org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties in project cas by apereo.

the class DefaultCaptchaActivationStrategyTests method verifyByService.

@Test
public void verifyByService() {
    val servicesManager = mock(ServicesManager.class);
    val strategy = new DefaultCaptchaActivationStrategy(servicesManager);
    val context = getRequestContext(new MockHttpServletRequest());
    val service = RegisteredServiceTestUtils.getService(UUID.randomUUID().toString());
    val registeredService = RegisteredServiceTestUtils.getRegisteredService(service.getId());
    registeredService.getProperties().put(RegisteredServiceProperty.RegisteredServiceProperties.CAPTCHA_ENABLED.getPropertyName(), new DefaultRegisteredServiceProperty("true"));
    when(servicesManager.findServiceBy(any(Service.class))).thenReturn(registeredService);
    WebUtils.putServiceIntoFlowScope(context, service);
    val properties = new GoogleRecaptchaProperties().setEnabled(false);
    assertTrue(strategy.shouldActivate(context, properties).isPresent());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) Service(org.apereo.cas.authentication.principal.Service) DefaultRegisteredServiceProperty(org.apereo.cas.services.DefaultRegisteredServiceProperty) Test(org.junit.jupiter.api.Test)

Example 5 with GoogleRecaptchaProperties

use of org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties in project cas by apereo.

the class CaptchaValidatorTests method verifyLowScore.

@Test
public void verifyLowScore() throws Exception {
    val secret = UUID.randomUUID().toString();
    val props = new GoogleRecaptchaProperties().setScore(1).setSecret(secret).setVerifyUrl("http://localhost:8812");
    val validator = new GoogleCaptchaV2Validator(props);
    val entity = MAPPER.writeValueAsString(Map.of("score", .5));
    try (val webServer = new MockWebServer(8812, new ByteArrayResource(entity.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val response = UUID.randomUUID().toString();
        assertFalse(validator.validate(response, "Mozilla/5.0"));
    }
}
Also used : lombok.val(lombok.val) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)12 GoogleRecaptchaProperties (org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties)12 Test (org.junit.jupiter.api.Test)12 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)9 MockWebServer (org.apereo.cas.util.MockWebServer)4 ByteArrayResource (org.springframework.core.io.ByteArrayResource)4 Service (org.apereo.cas.authentication.principal.Service)2 DefaultRegisteredServiceProperty (org.apereo.cas.services.DefaultRegisteredServiceProperty)2 ServicesManager (org.apereo.cas.services.ServicesManager)2 ClientInfo (org.apereo.inspektr.common.web.ClientInfo)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)2 MockRequestContext (org.springframework.webflow.test.MockRequestContext)2 HashMap (java.util.HashMap)1 OneTimeTokenCredential (org.apereo.cas.authentication.credential.OneTimeTokenCredential)1 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)1 MockServletContext (org.apereo.cas.util.MockServletContext)1 GoogleCaptchaV2Validator (org.apereo.cas.web.GoogleCaptchaV2Validator)1 Executable (org.junit.jupiter.api.function.Executable)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1