Search in sources :

Example 11 with GeoLocationRequest

use of org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest in project cas by apereo.

the class GeoLocationAuthenticationRequestRiskCalculator method calculateScore.

@Override
protected BigDecimal calculateScore(final HttpServletRequest request, final Authentication authentication, final RegisteredService service, final Supplier<Stream<? extends CasEvent>> events) {
    val loc = WebUtils.getHttpServletRequestGeoLocation(request);
    if (loc != null && loc.isValid()) {
        LOGGER.debug("Filtering authentication events for geolocation [{}]", loc);
        val count = events.get().filter(e -> e.getGeoLocation().equals(loc)).count();
        LOGGER.debug("Total authentication events found for [{}]: [{}]", loc, count);
        return calculateScoreBasedOnEventsCount(authentication, events, count);
    }
    val remoteAddr = ClientInfoHolder.getClientInfo().getClientIpAddress();
    LOGGER.debug("Filtering authentication events for location based on ip [{}]", remoteAddr);
    val response = this.geoLocationService.locate(remoteAddr);
    if (response != null) {
        val count = events.get().filter(e -> e.getGeoLocation().equals(new GeoLocationRequest(response.getLatitude(), response.getLongitude()))).count();
        LOGGER.debug("Total authentication events found for location of [{}]: [{}]", remoteAddr, count);
        return calculateScoreBasedOnEventsCount(authentication, events, count);
    }
    LOGGER.debug("Request does not contain enough geolocation data");
    return HIGHEST_RISK_SCORE;
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) CasEventRepository(org.apereo.cas.support.events.CasEventRepository) GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest) lombok.val(lombok.val) GeoLocationService(org.apereo.cas.authentication.adaptive.geo.GeoLocationService) Supplier(java.util.function.Supplier) RegisteredService(org.apereo.cas.services.RegisteredService) BigDecimal(java.math.BigDecimal) Slf4j(lombok.extern.slf4j.Slf4j) HttpServletRequest(javax.servlet.http.HttpServletRequest) Stream(java.util.stream.Stream) Authentication(org.apereo.cas.authentication.Authentication) ClientInfoHolder(org.apereo.inspektr.common.web.ClientInfoHolder) WebUtils(org.apereo.cas.web.support.WebUtils) CasEvent(org.apereo.cas.support.events.dao.CasEvent) GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest)

Example 12 with GeoLocationRequest

use of org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest in project cas by apereo.

the class AdaptiveMultifactorAuthenticationPolicyEventResolver method checkRequestGeoLocation.

private boolean checkRequestGeoLocation(final String clientIp, final String mfaMethod, final String pattern) {
    if (this.geoLocationService != null) {
        final GeoLocationRequest location = WebUtils.getHttpServletRequestGeoLocation();
        final GeoLocationResponse loc = this.geoLocationService.locate(clientIp, location);
        if (loc != null) {
            final String address = loc.build();
            if (address.matches(pattern)) {
                LOGGER.debug("Current address [{}] at [{}] matches the provided pattern [{}] for " + "adaptive authentication and is required to use [{}]", address, clientIp, pattern, mfaMethod);
                return true;
            }
        }
    }
    return false;
}
Also used : GeoLocationResponse(org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse) GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest)

Example 13 with GeoLocationRequest

use of org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest in project cas by apereo.

the class WebUtils method getHttpServletRequestGeoLocation.

/**
     * Gets http servlet request geo location.
     *
     * @param request the request
     * @return the http servlet request geo location
     */
public static GeoLocationRequest getHttpServletRequestGeoLocation(final HttpServletRequest request) {
    final int latIndex = 0;
    final int longIndex = 1;
    final int accuracyIndex = 2;
    final int timeIndex = 3;
    final GeoLocationRequest loc = new GeoLocationRequest();
    if (request != null) {
        final String geoLocationParam = request.getParameter("geolocation");
        if (StringUtils.isNotBlank(geoLocationParam)) {
            final String[] geoLocation = geoLocationParam.split(",");
            loc.setLatitude(geoLocation[latIndex]);
            loc.setLongitude(geoLocation[longIndex]);
            loc.setAccuracy(geoLocation[accuracyIndex]);
            loc.setTimestamp(geoLocation[timeIndex]);
        }
    }
    return loc;
}
Also used : GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest)

Example 14 with GeoLocationRequest

use of org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest in project cas by apereo.

the class AbstractAuthenticationAction method doExecute.

@Override
protected Event doExecute(final RequestContext requestContext) throws Exception {
    final String agent = WebUtils.getHttpServletRequestUserAgent();
    final GeoLocationRequest geoLocation = WebUtils.getHttpServletRequestGeoLocation();
    if (!adaptiveAuthenticationPolicy.apply(agent, geoLocation)) {
        final String msg = "Adaptive authentication policy does not allow this request for " + agent + " and " + geoLocation;
        final Map<String, Class<? extends Exception>> map = Collections.singletonMap(UnauthorizedAuthenticationException.class.getSimpleName(), UnauthorizedAuthenticationException.class);
        final AuthenticationException error = new AuthenticationException(msg, map, Collections.emptyMap());
        return new Event(this, CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, new LocalAttributeMap(CasWebflowConstants.TRANSITION_ID_ERROR, error));
    }
    final Event serviceTicketEvent = this.serviceTicketRequestWebflowEventResolver.resolveSingle(requestContext);
    if (serviceTicketEvent != null) {
        fireEventHooks(serviceTicketEvent, requestContext);
        return serviceTicketEvent;
    }
    final Event finalEvent = this.initialAuthenticationAttemptWebflowEventResolver.resolveSingle(requestContext);
    fireEventHooks(finalEvent, requestContext);
    return finalEvent;
}
Also used : LocalAttributeMap(org.springframework.webflow.core.collection.LocalAttributeMap) UnauthorizedAuthenticationException(org.apereo.cas.authentication.adaptive.UnauthorizedAuthenticationException) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) UnauthorizedAuthenticationException(org.apereo.cas.authentication.adaptive.UnauthorizedAuthenticationException) Event(org.springframework.webflow.execution.Event) GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) UnauthorizedAuthenticationException(org.apereo.cas.authentication.adaptive.UnauthorizedAuthenticationException)

Example 15 with GeoLocationRequest

use of org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest in project cas by apereo.

the class GeoLocationAuthenticationRequestRiskCalculator method calculateScore.

@Override
protected BigDecimal calculateScore(final HttpServletRequest request, final Authentication authentication, final RegisteredService service, final Collection<CasEvent> events) {
    final GeoLocationRequest loc = WebUtils.getHttpServletRequestGeoLocation(request);
    if (loc != null && loc.isValid()) {
        LOGGER.debug("Filtering authentication events for geolocation [{}]", loc);
        final long count = events.stream().filter(e -> e.getGeoLocation().equals(loc)).count();
        LOGGER.debug("Total authentication events found for [{}]: [{}]", loc, count);
        if (count == events.size()) {
            LOGGER.debug("Principal [{}] has always authenticated from [{}]", authentication.getPrincipal(), loc);
            return LOWEST_RISK_SCORE;
        }
        return getFinalAveragedScore(count, events.size());
    }
    final String remoteAddr = ClientInfoHolder.getClientInfo().getClientIpAddress();
    LOGGER.debug("Filtering authentication events for location based on ip [{}]", remoteAddr);
    final GeoLocationResponse response = this.geoLocationService.locate(remoteAddr);
    if (response != null) {
        final long count = events.stream().filter(e -> e.getGeoLocation().equals(new GeoLocationRequest(response.getLatitude(), response.getLongitude()))).count();
        LOGGER.debug("Total authentication events found for location of [{}]: [{}]", remoteAddr, count);
        if (count == events.size()) {
            LOGGER.debug("Principal [{}] has always authenticated from [{}]", authentication.getPrincipal(), loc);
            return LOWEST_RISK_SCORE;
        }
        return getFinalAveragedScore(count, events.size());
    }
    LOGGER.debug("Request does not contain enough geolocation data");
    return HIGHEST_RISK_SCORE;
}
Also used : CasEventRepository(org.apereo.cas.support.events.CasEventRepository) Collection(java.util.Collection) GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest) GeoLocationService(org.apereo.cas.authentication.adaptive.geo.GeoLocationService) Autowired(org.springframework.beans.factory.annotation.Autowired) RegisteredService(org.apereo.cas.services.RegisteredService) BigDecimal(java.math.BigDecimal) Slf4j(lombok.extern.slf4j.Slf4j) GeoLocationResponse(org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) Authentication(org.apereo.cas.authentication.Authentication) ClientInfoHolder(org.apereo.inspektr.common.web.ClientInfoHolder) Qualifier(org.springframework.beans.factory.annotation.Qualifier) WebUtils(org.apereo.cas.web.support.WebUtils) CasEvent(org.apereo.cas.support.events.dao.CasEvent) GeoLocationResponse(org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse) GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest)

Aggregations

GeoLocationRequest (org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest)20 lombok.val (lombok.val)13 GeoLocationResponse (org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse)7 Test (org.junit.jupiter.api.Test)7 AdaptiveAuthenticationProperties (org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties)5 ClientInfo (org.apereo.inspektr.common.web.ClientInfo)5 MockRequestContext (org.springframework.webflow.test.MockRequestContext)5 CasEvent (org.apereo.cas.support.events.dao.CasEvent)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 GeoLocationService (org.apereo.cas.authentication.adaptive.geo.GeoLocationService)3 ClientInfoHolder (org.apereo.inspektr.common.web.ClientInfoHolder)3 BigDecimal (java.math.BigDecimal)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Authentication (org.apereo.cas.authentication.Authentication)2 RegisteredService (org.apereo.cas.services.RegisteredService)2 CasEventRepository (org.apereo.cas.support.events.CasEventRepository)2 WebUtils (org.apereo.cas.web.support.WebUtils)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 Collection (java.util.Collection)1