Search in sources :

Example 16 with CasEvent

use of org.apereo.cas.support.events.dao.CasEvent in project cas by apereo.

the class DateTimeAuthenticationRequestRiskCalculator method calculateScore.

@Override
protected BigDecimal calculateScore(final HttpServletRequest request, final Authentication authentication, final RegisteredService service, final Supplier<Stream<? extends CasEvent>> events) {
    val windowInHours = casProperties.getAuthn().getAdaptive().getRisk().getDateTime().getWindowInHours();
    val timestamp = ZonedDateTime.now(ZoneOffset.UTC);
    LOGGER.debug("Filtering authentication events for timestamp [{}]", timestamp);
    val hoursFromNow = timestamp.plusHours(windowInHours).getHour();
    val hoursBeforeNow = timestamp.minusHours(windowInHours).getHour();
    val count = events.get().map(time -> {
        val dt = DateTimeUtils.convertToZonedDateTime(time.getCreationTime());
        val instant = ChronoZonedDateTime.from(dt).toInstant();
        val zdt = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        return zdt.getHour();
    }).filter(hour -> hoursBeforeNow <= hoursFromNow ? (hour >= hoursBeforeNow && hour <= hoursFromNow) : (hour >= hoursBeforeNow || hour <= hoursFromNow)).count();
    LOGGER.debug("Total authentication events found for [{}] in a [{}]h window: [{}]", timestamp, windowInHours, count);
    return calculateScoreBasedOnEventsCount(authentication, events, count);
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) CasEventRepository(org.apereo.cas.support.events.CasEventRepository) DateTimeUtils(org.apereo.cas.util.DateTimeUtils) ZonedDateTime(java.time.ZonedDateTime) lombok.val(lombok.val) 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) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) Stream(java.util.stream.Stream) Authentication(org.apereo.cas.authentication.Authentication) ZoneOffset(java.time.ZoneOffset) CasEvent(org.apereo.cas.support.events.dao.CasEvent)

Example 17 with CasEvent

use of org.apereo.cas.support.events.dao.CasEvent in project cas by apereo.

the class DefaultCasEventListener method handleCasAuthenticationPolicyFailureEvent.

/**
 * Handle cas authentication policy failure event.
 *
 * @param event the event
 */
@EventListener
public void handleCasAuthenticationPolicyFailureEvent(final CasAuthenticationPolicyFailureEvent event) {
    if (this.casEventRepository != null) {
        final CasEvent dto = prepareCasEvent(event);
        dto.setPrincipalId(event.getAuthentication().getPrincipal().getId());
        dto.putId(CasAuthenticationPolicyFailureEvent.class.getSimpleName());
        this.casEventRepository.save(dto);
    }
}
Also used : AbstractCasEvent(org.apereo.cas.support.events.AbstractCasEvent) CasEvent(org.apereo.cas.support.events.dao.CasEvent) CasAuthenticationPolicyFailureEvent(org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent) EventListener(org.springframework.context.event.EventListener)

Example 18 with CasEvent

use of org.apereo.cas.support.events.dao.CasEvent in project cas by apereo.

the class DefaultCasEventListener method handleCasTicketGrantingTicketCreatedEvent.

/**
 * Handle TGT creation event.
 *
 * @param event the event
 */
@EventListener
public void handleCasTicketGrantingTicketCreatedEvent(final CasTicketGrantingTicketCreatedEvent event) {
    if (this.casEventRepository != null) {
        final CasEvent dto = prepareCasEvent(event);
        dto.setCreationTime(event.getTicketGrantingTicket().getCreationTime().toString());
        dto.putId(TicketIdSanitizationUtils.sanitize(event.getTicketGrantingTicket().getId()));
        dto.setPrincipalId(event.getTicketGrantingTicket().getAuthentication().getPrincipal().getId());
        this.casEventRepository.save(dto);
    }
}
Also used : AbstractCasEvent(org.apereo.cas.support.events.AbstractCasEvent) CasEvent(org.apereo.cas.support.events.dao.CasEvent) EventListener(org.springframework.context.event.EventListener)

Example 19 with CasEvent

use of org.apereo.cas.support.events.dao.CasEvent in project cas by apereo.

the class AbstractCasEventRepositoryTests method getCasEvent.

private CasEvent getCasEvent() {
    final TicketGrantingTicket ticket = new MockTicketGrantingTicket("casuser");
    final CasTicketGrantingTicketCreatedEvent event = new CasTicketGrantingTicketCreatedEvent(this, ticket);
    final CasEvent dto = new CasEvent();
    dto.setType(event.getClass().getCanonicalName());
    dto.putTimestamp(event.getTimestamp());
    dto.setCreationTime(event.getTicketGrantingTicket().getCreationTime().toString());
    dto.putId(event.getTicketGrantingTicket().getId());
    dto.setPrincipalId(event.getTicketGrantingTicket().getAuthentication().getPrincipal().getId());
    return dto;
}
Also used : MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) CasTicketGrantingTicketCreatedEvent(org.apereo.cas.support.events.ticket.CasTicketGrantingTicketCreatedEvent) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) CasEvent(org.apereo.cas.support.events.dao.CasEvent)

Example 20 with CasEvent

use of org.apereo.cas.support.events.dao.CasEvent 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

CasEvent (org.apereo.cas.support.events.dao.CasEvent)24 lombok.val (lombok.val)15 Query (org.springframework.data.mongodb.core.query.Query)7 BigDecimal (java.math.BigDecimal)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 Slf4j (lombok.extern.slf4j.Slf4j)6 Authentication (org.apereo.cas.authentication.Authentication)6 RegisteredService (org.apereo.cas.services.RegisteredService)6 AbstractCasEvent (org.apereo.cas.support.events.AbstractCasEvent)6 CasEventRepository (org.apereo.cas.support.events.CasEventRepository)6 Supplier (java.util.function.Supplier)4 Stream (java.util.stream.Stream)4 GeoLocationRequest (org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest)4 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)4 CasTicketGrantingTicketCreatedEvent (org.apereo.cas.support.events.ticket.CasTicketGrantingTicketCreatedEvent)4 EventListener (org.springframework.context.event.EventListener)4 ClientInfoHolder (org.apereo.inspektr.common.web.ClientInfoHolder)3 ZoneOffset (java.time.ZoneOffset)2 ZonedDateTime (java.time.ZonedDateTime)2 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)2