Search in sources :

Example 6 with GoogleRecaptchaProperties

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

the class CaptchaValidatorTests method verifySuccess.

@Test
public void verifySuccess() throws Exception {
    val props = new GoogleRecaptchaProperties().setScore(.1).setSecret(UUID.randomUUID().toString()).setVerifyUrl("http://localhost:8812");
    val validator = new GoogleCaptchaV2Validator(props);
    val entity = MAPPER.writeValueAsString(Map.of("score", .5, "success", true));
    try (val webServer = new MockWebServer(8812, new ByteArrayResource(entity.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val response = UUID.randomUUID().toString();
        assertTrue(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)

Example 7 with GoogleRecaptchaProperties

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

the class GoogleCaptchaV2ValidatorTests method verifyOperation.

@Test
public void verifyOperation() {
    val props = new GoogleRecaptchaProperties().setScore(.1).setSecret(UUID.randomUUID().toString()).setVerifyUrl("http://localhost:8812");
    val validator = new GoogleCaptchaV2Validator(props);
    val request = new MockHttpServletRequest();
    request.addParameter(GoogleCaptchaV2Validator.REQUEST_PARAM_RECAPTCHA_RESPONSE, UUID.randomUUID().toString());
    assertNotNull(validator.getRecaptchaResponse(request));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) Test(org.junit.jupiter.api.Test)

Example 8 with GoogleRecaptchaProperties

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

the class HCaptchaValidatorTests method verifyOperation.

@Test
public void verifyOperation() {
    val props = new GoogleRecaptchaProperties().setScore(.1).setSecret(UUID.randomUUID().toString()).setVerifyUrl("http://localhost:8812");
    val validator = new HCaptchaValidator(props);
    val request = new MockHttpServletRequest();
    request.addParameter(HCaptchaValidator.REQUEST_PARAM_HCAPTCHA_RESPONSE, UUID.randomUUID().toString());
    assertNotNull(validator.getRecaptchaResponse(request));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) Test(org.junit.jupiter.api.Test)

Example 9 with GoogleRecaptchaProperties

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

the class WebUtilsTests 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));
    val flow = new Flow("mockFlow");
    val flowSession = new MockFlowSession(flow);
    flowSession.setParent(new MockFlowSession(flow));
    val mockExecutionContext = new MockFlowExecutionContext(flowSession);
    context.setFlowExecutionContext(mockExecutionContext);
    WebUtils.putLogoutRedirectUrl(context, URL);
    assertNotNull(WebUtils.getLogoutRedirectUrl(context, String.class));
    WebUtils.removeLogoutRedirectUrl(context);
    assertNull(WebUtils.getLogoutRedirectUrl(context, String.class));
    assertNull(WebUtils.getHttpServletRequestUserAgentFromRequestContext(context));
    assertNull(WebUtils.getHttpServletRequestUserAgentFromRequestContext(request));
    assertNull(WebUtils.getAuthenticationResult(context));
    assertNull(WebUtils.getHttpServletRequestGeoLocationFromRequestContext());
    assertNull(WebUtils.getAcceptableUsagePolicyTermsFromFlowScope(context, Object.class));
    assertFalse(WebUtils.hasSurrogateAuthenticationRequest(context));
    assertNotNull(WebUtils.produceUnauthorizedErrorView(new RuntimeException()));
    assertNotNull(WebUtils.produceErrorView(new IllegalArgumentException()));
    assertNotNull(WebUtils.produceErrorView("error-view", new IllegalArgumentException()));
    assertNotNull(WebUtils.getHttpRequestFullUrl(context));
    request.setQueryString("param=value");
    assertNotNull(WebUtils.getHttpRequestFullUrl(request));
    assertFalse(WebUtils.isGraphicalUserAuthenticationEnabled(context));
    assertTrue(WebUtils.getDelegatedAuthenticationProviderConfigurations(context).isEmpty());
    assertNull(WebUtils.getAvailableAuthenticationHandleNames(context));
    assertDoesNotThrow(new Executable() {

        @Override
        public void execute() {
            WebUtils.putYubiKeyMultipleDeviceRegistrationEnabled(context, true);
            WebUtils.putInitialHttpRequestPostParameters(context);
            WebUtils.putExistingSingleSignOnSessionAvailable(context, true);
            WebUtils.putExistingSingleSignOnSessionPrincipal(context, CoreAuthenticationTestUtils.getPrincipal());
            WebUtils.putAvailableAuthenticationHandleNames(context, List.of());
            WebUtils.putPasswordManagementEnabled(context, true);
            WebUtils.putRecaptchaPropertiesFlowScope(context, new GoogleRecaptchaProperties().setEnabled(true));
            WebUtils.putLogoutUrls(context, Map.of());
            val ac = OneTimeTokenAccount.builder().validationCode(123456).username("casuser").name("Example").build();
            WebUtils.putOneTimeTokenAccount(context, ac);
            assertNotNull(WebUtils.getOneTimeTokenAccount(context, OneTimeTokenAccount.class));
            WebUtils.putOneTimeTokenAccounts(context, List.of(ac));
            WebUtils.putWarnCookieIfRequestParameterPresent(null, context);
            WebUtils.putTicketGrantingTicketInScopes(context, "ticket-id");
        }
    });
    WebUtils.putCredential(context, new UsernamePasswordCredential("casuser", "password"));
    assertThrows(ClassCastException.class, () -> WebUtils.getCredential(context, OneTimeTokenCredential.class));
    WebUtils.putTicketGrantingTicketInScopes(context, StringUtils.EMPTY);
    WebUtils.putTicketGrantingTicketInScopes(context, (TicketGrantingTicket) null);
    WebUtils.putTicketGrantingTicketInScopes(context, (String) null);
    assertNull(WebUtils.getTicketGrantingTicket(context));
    assertThrows(IllegalArgumentException.class, () -> WebUtils.getPrincipalFromRequestContext(context, null));
    request.addParameter(WebUtils.PUBLIC_WORKSTATION_ATTRIBUTE, "true");
    WebUtils.putPublicWorkstationToFlowIfRequestParameterPresent(context);
    assertTrue(WebUtils.isAuthenticatingAtPublicWorkstation(context));
    val ticketRegistrySupport = mock(TicketRegistrySupport.class);
    WebUtils.putTicketGrantingTicketInScopes(context, "TGT-XYZ123");
    assertNull(WebUtils.getPrincipalFromRequestContext(context, ticketRegistrySupport));
    WebUtils.putLogoutPostUrl(context, URL);
    assertEquals(URL, WebUtils.getLogoutPostUrl(context));
    val data = new HashMap<String, Object>();
    data.put("SAMLResponse", "xxx");
    WebUtils.putLogoutPostData(context, data);
    assertEquals(data, WebUtils.getLogoutPostData(context));
}
Also used : lombok.val(lombok.val) MockFlowSession(org.springframework.webflow.test.MockFlowSession) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.apereo.cas.util.MockServletContext) OneTimeTokenCredential(org.apereo.cas.authentication.credential.OneTimeTokenCredential) Flow(org.springframework.webflow.engine.Flow) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) Executable(org.junit.jupiter.api.function.Executable) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 10 with GoogleRecaptchaProperties

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

the class DefaultCaptchaActivationStrategyTests method verifyByIpPatternPerService.

@Test
public void verifyByIpPatternPerService() {
    val servicesManager = mock(ServicesManager.class);
    val strategy = new DefaultCaptchaActivationStrategy(servicesManager);
    val request = new MockHttpServletRequest();
    val context = getRequestContext(request);
    request.setRemoteAddr("185.86.151.99");
    request.setLocalAddr("195.88.151.11");
    ClientInfoHolder.setClientInfo(new ClientInfo(request));
    val service = RegisteredServiceTestUtils.getService(UUID.randomUUID().toString());
    val registeredService = RegisteredServiceTestUtils.getRegisteredService(service.getId());
    registeredService.getProperties().put(RegisteredServiceProperty.RegisteredServiceProperties.CAPTCHA_ENABLED.getPropertyName(), new DefaultRegisteredServiceProperty("true"));
    registeredService.getProperties().put(RegisteredServiceProperty.RegisteredServiceProperties.CAPTCHA_IP_ADDRESS_PATTERN.getPropertyName(), new DefaultRegisteredServiceProperty("no-match1", "no-match2", "\\d\\d\\.8.+\\.99"));
    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) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) DefaultRegisteredServiceProperty(org.apereo.cas.services.DefaultRegisteredServiceProperty) 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