Search in sources :

Example 51 with ClientInfo

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

the class BasePasswordManagementService method createToken.

@Override
public String createToken(final String to) {
    try {
        final String token = UUID.randomUUID().toString();
        final JwtClaims claims = new JwtClaims();
        claims.setJwtId(token);
        claims.setIssuer(issuer);
        claims.setAudience(issuer);
        claims.setExpirationTimeMinutesInTheFuture(properties.getReset().getExpirationMinutes());
        claims.setIssuedAtToNow();
        final ClientInfo holder = ClientInfoHolder.getClientInfo();
        claims.setStringClaim("origin", holder.getServerIpAddress());
        claims.setStringClaim("client", holder.getClientIpAddress());
        claims.setSubject(to);
        final String json = claims.toJson();
        return this.cipherExecutor.encode(json);
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
Also used : JwtClaims(org.jose4j.jwt.JwtClaims) ClientInfo(org.apereo.inspektr.common.web.ClientInfo)

Example 52 with ClientInfo

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

the class JdbcThrottledSubmissionHandlerInterceptorAdapterTests method loginUnsuccessfully.

@Override
protected MockHttpServletResponse loginUnsuccessfully(final String username, final String fromAddress) throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    request.setMethod("POST");
    request.setParameter("username", username);
    request.setRemoteAddr(fromAddress);
    request.setRequestURI("/cas/login");
    final MockRequestContext context = new MockRequestContext();
    context.setCurrentEvent(new Event(StringUtils.EMPTY, "error"));
    request.setAttribute("flowRequestContext", context);
    ClientInfoHolder.setClientInfo(new ClientInfo(request));
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    throttle.preHandle(request, response, null);
    try {
        authenticationManager.authenticate(AuthenticationTransaction.of(CoreAuthenticationTestUtils.getService(), badCredentials(username)));
    } catch (final AuthenticationException e) {
        throttle.postHandle(request, response, null, null);
        return response;
    }
    throw new AssertionError("Expected AbstractAuthenticationException");
}
Also used : AuthenticationException(org.apereo.cas.authentication.AuthenticationException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Event(org.springframework.webflow.execution.Event) MockRequestContext(org.springframework.webflow.test.MockRequestContext) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 53 with ClientInfo

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

the class AbstractThrottledSubmissionHandlerInterceptorAdapterTests method setUp.

@Before
public void setUp() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr(IP_ADDRESS);
    request.setLocalAddr(IP_ADDRESS);
    ClientInfoHolder.setClientInfo(new ClientInfo(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) Before(org.junit.Before)

Example 54 with ClientInfo

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

Example 55 with ClientInfo

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

the class DefaultCaptchaActivationStrategy method shouldActivate.

@Override
public Optional<GoogleRecaptchaProperties> shouldActivate(final RequestContext requestContext, final GoogleRecaptchaProperties properties) {
    val service = WebUtils.getService(requestContext);
    val registeredService = servicesManager.findServiceBy(service);
    if (RegisteredServiceProperty.RegisteredServiceProperties.CAPTCHA_ENABLED.isAssignedTo(registeredService)) {
        LOGGER.trace("Checking for activation of captcha defined for service [{}]", registeredService);
        if (RegisteredServiceProperty.RegisteredServiceProperties.CAPTCHA_IP_ADDRESS_PATTERN.isAssignedTo(registeredService)) {
            val ip = Optional.ofNullable(ClientInfoHolder.getClientInfo()).map(ClientInfo::getClientIpAddress).orElse(StringUtils.EMPTY).trim();
            LOGGER.trace("Checking for activation of captcha defined for service [{}] based on IP address [{}]", registeredService, ip);
            val ipPattern = RegisteredServiceProperty.RegisteredServiceProperties.CAPTCHA_IP_ADDRESS_PATTERN.getPropertyValues(registeredService, Set.class);
            val result = ipPattern.stream().anyMatch(pattern -> RegexUtils.find(pattern.toString().trim(), ip));
            return evaluateResult(result, properties);
        }
        val result = RegisteredServiceProperty.RegisteredServiceProperties.CAPTCHA_ENABLED.getPropertyBooleanValue(registeredService);
        return evaluateResult(result, properties);
    }
    if (StringUtils.isNotBlank(properties.getActivateForIpAddressPattern())) {
        val ip = Optional.ofNullable(ClientInfoHolder.getClientInfo()).map(ClientInfo::getClientIpAddress).orElse(StringUtils.EMPTY);
        LOGGER.debug("Remote IP address [{}] will be checked against [{}]", ip, properties.getActivateForIpAddressPattern());
        val activate = RegexUtils.find(properties.getActivateForIpAddressPattern(), ip);
        return evaluateResult(activate, properties);
    }
    LOGGER.trace("Checking for activation of captcha defined under site key [{}]", properties.getSiteKey());
    return evaluateResult(properties.isEnabled(), properties);
}
Also used : lombok.val(lombok.val) ClientInfo(org.apereo.inspektr.common.web.ClientInfo)

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