Search in sources :

Example 1 with CasEvent

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

the class DefaultCasEventListener method handleCasAuthenticationTransactionFailureEvent.

/**
 * Handle cas authentication policy failure event.
 *
 * @param event the event
 */
@EventListener
public void handleCasAuthenticationTransactionFailureEvent(final CasAuthenticationTransactionFailureEvent event) {
    if (this.casEventRepository != null) {
        final CasEvent dto = prepareCasEvent(event);
        dto.setPrincipalId(event.getCredential().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 2 with CasEvent

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

the class DefaultCasEventListener method handleCasRiskyAuthenticationDetectedEvent.

/**
 * Handle cas risky authentication detected event.
 *
 * @param event the event
 */
@EventListener
public void handleCasRiskyAuthenticationDetectedEvent(final CasRiskyAuthenticationDetectedEvent event) {
    if (this.casEventRepository != null) {
        final CasEvent dto = prepareCasEvent(event);
        dto.putId(event.getService().getName());
        dto.setPrincipalId(event.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 3 with CasEvent

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

the class DefaultCasEventListener method prepareCasEvent.

private static CasEvent prepareCasEvent(final AbstractCasEvent event) {
    final CasEvent dto = new CasEvent();
    dto.setType(event.getClass().getCanonicalName());
    dto.putTimestamp(event.getTimestamp());
    dto.setCreationTime(DateTimeUtils.zonedDateTimeOf(event.getTimestamp()).toString());
    final ClientInfo clientInfo = ClientInfoHolder.getClientInfo();
    dto.putClientIpAddress(clientInfo.getClientIpAddress());
    dto.putServerIpAddress(clientInfo.getServerIpAddress());
    dto.putAgent(WebUtils.getHttpServletRequestUserAgentFromRequestContext());
    final GeoLocationRequest location = WebUtils.getHttpServletRequestGeoLocationFromRequestContext();
    if (location != null) {
        dto.putGeoLocation(location);
    }
    return dto;
}
Also used : AbstractCasEvent(org.apereo.cas.support.events.AbstractCasEvent) CasEvent(org.apereo.cas.support.events.dao.CasEvent) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest)

Example 4 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 Collection<CasEvent> events) {
    final ZonedDateTime timestamp = ZonedDateTime.now(ZoneOffset.UTC);
    LOGGER.debug("Filtering authentication events for timestamp [{}]", timestamp);
    final int hoursFromNow = timestamp.plusHours(windowInHours).getHour();
    final int hoursBeforeNow = timestamp.minusHours(windowInHours).getHour();
    final long count = events.stream().map(time -> {
        final Instant instant = ChronoZonedDateTime.from(time.getCreationTime()).toInstant();
        final ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        return zdt.getHour();
    }).filter(hour -> hour <= hoursFromNow && hour >= hoursBeforeNow).count();
    LOGGER.debug("Total authentication events found for [{}] in a [{}]h window: [{}]", timestamp, windowInHours, count);
    if (count == events.size()) {
        LOGGER.debug("Principal [{}] has always authenticated from [{}]", authentication.getPrincipal(), timestamp);
        return LOWEST_RISK_SCORE;
    }
    return getFinalAveragedScore(count, events.size());
}
Also used : CasEventRepository(org.apereo.cas.support.events.CasEventRepository) BigDecimal(java.math.BigDecimal) Slf4j(lombok.extern.slf4j.Slf4j) HttpServletRequest(javax.servlet.http.HttpServletRequest) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) Authentication(org.apereo.cas.authentication.Authentication) ZonedDateTime(java.time.ZonedDateTime) Collection(java.util.Collection) ZoneOffset(java.time.ZoneOffset) Instant(java.time.Instant) RegisteredService(org.apereo.cas.services.RegisteredService) CasEvent(org.apereo.cas.support.events.dao.CasEvent) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) ZonedDateTime(java.time.ZonedDateTime) Instant(java.time.Instant)

Example 5 with CasEvent

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

the class MockTicketGrantingTicketCreatedEventProducer method createEvent.

private static void createEvent(final int i, final CasEventRepository casEventRepository) {
    final CasEvent dto = new CasEvent();
    dto.setType(CasTicketGrantingTicketCreatedEvent.class.getName());
    dto.putTimestamp(new Date().getTime());
    dto.setCreationTime(ZonedDateTime.now(ZoneOffset.UTC).minusDays(5).toString());
    dto.putId(TicketIdSanitizationUtils.sanitize("TGT-" + i + "-" + RandomStringUtils.randomAlphanumeric(16)));
    dto.setPrincipalId("casuser");
    dto.putClientIpAddress(getMockClientIpAddress());
    dto.putServerIpAddress("127.0.0.1");
    dto.putAgent(getMockUserAgent());
    dto.putGeoLocation(getMockGeoLocation());
    casEventRepository.save(dto);
}
Also used : CasTicketGrantingTicketCreatedEvent(org.apereo.cas.support.events.ticket.CasTicketGrantingTicketCreatedEvent) CasEvent(org.apereo.cas.support.events.dao.CasEvent) Date(java.util.Date)

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