Search in sources :

Example 11 with CasEvent

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

the class MongoDbCasEventRepository method getEventsForPrincipal.

@Override
public Stream<? extends CasEvent> getEventsForPrincipal(final String id) {
    val query = new Query();
    query.addCriteria(Criteria.where(PRINCIPAL_ID_PARAM).is(id));
    return this.mongoTemplate.stream(query, CasEvent.class, this.collectionName).stream();
}
Also used : lombok.val(lombok.val) Query(org.springframework.data.mongodb.core.query.Query) CasEvent(org.apereo.cas.support.events.dao.CasEvent)

Example 12 with CasEvent

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

the class MongoDbCasEventRepository method getEventsForPrincipal.

@Override
public Stream<? extends CasEvent> getEventsForPrincipal(final String principal, final ZonedDateTime dateTime) {
    val query = new Query();
    query.addCriteria(Criteria.where(PRINCIPAL_ID_PARAM).is(principal).and(CREATION_TIME_PARAM).gte(dateTime.toString()));
    return this.mongoTemplate.stream(query, CasEvent.class, this.collectionName).stream();
}
Also used : lombok.val(lombok.val) Query(org.springframework.data.mongodb.core.query.Query) CasEvent(org.apereo.cas.support.events.dao.CasEvent)

Example 13 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 String user) {
    val ticket = new MockTicketGrantingTicket(user);
    val event = new CasTicketGrantingTicketCreatedEvent(this, ticket);
    val dto = new CasEvent();
    dto.setType(event.getClass().getCanonicalName());
    dto.putTimestamp(event.getTimestamp());
    dto.setCreationTime(event.getTicketGrantingTicket().getCreationTime().format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    dto.putEventId(event.getTicketGrantingTicket().getId());
    dto.putClientIpAddress("1.2.3.4");
    dto.putServerIpAddress("1.2.3.4");
    val location = new GeoLocationRequest(1234, 1234);
    location.setAccuracy("80");
    location.setTimestamp(String.valueOf(event.getTimestamp()));
    dto.putGeoLocation(location);
    dto.setPrincipalId(event.getTicketGrantingTicket().getAuthentication().getPrincipal().getId());
    return dto;
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) CasTicketGrantingTicketCreatedEvent(org.apereo.cas.support.events.ticket.CasTicketGrantingTicketCreatedEvent) CasEvent(org.apereo.cas.support.events.dao.CasEvent) GeoLocationRequest(org.apereo.cas.authentication.adaptive.geo.GeoLocationRequest)

Example 14 with CasEvent

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

the class CasAuthenticationAuthenticationEventListener method prepareCasEvent.

private static CasEvent prepareCasEvent(final AbstractCasEvent event) {
    val dto = new CasEvent();
    dto.setType(event.getClass().getCanonicalName());
    dto.putTimestamp(event.getTimestamp());
    val dt = DateTimeUtils.zonedDateTimeOf(Instant.ofEpochMilli(event.getTimestamp()));
    dto.setCreationTime(dt.toString());
    val clientInfo = ClientInfoHolder.getClientInfo();
    if (clientInfo != null) {
        dto.putClientIpAddress(clientInfo.getClientIpAddress());
        dto.putServerIpAddress(clientInfo.getServerIpAddress());
        dto.putAgent(clientInfo.getUserAgent());
        val location = HttpRequestUtils.getHttpServletRequestGeoLocation(clientInfo.getGeoLocation());
        dto.putGeoLocation(location);
    } else {
        LOGGER.trace("No client information is available. The final event cannot track client location, user agent or IP addresses");
    }
    return dto;
}
Also used : lombok.val(lombok.val) AbstractCasEvent(org.apereo.cas.support.events.AbstractCasEvent) CasEvent(org.apereo.cas.support.events.dao.CasEvent)

Example 15 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 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)

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