use of org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse in project cas by apereo.
the class GoogleMapsGeoLocationService method locate.
@Override
public GeoLocationResponse locate(final Double latitude, final Double longitude) {
if (latitude == null || longitude == null) {
LOGGER.debug("latitude/longitude must not be null in order for geolocation to proceed");
return null;
}
final GeoLocationResponse r = new GeoLocationResponse();
r.setLatitude(latitude);
r.setLongitude(longitude);
final LatLng latlng = new LatLng(latitude, longitude);
try {
final GeocodingResult[] results = GeocodingApi.reverseGeocode(this.context, latlng).await();
if (results != null && results.length > 0) {
Arrays.stream(results).map(result -> result.formattedAddress).forEach(r::addAddress);
return r;
}
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return r;
}
use of org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse in project cas by apereo.
the class DefaultAdaptiveAuthenticationPolicy method apply.
@Override
public boolean apply(final String userAgent, final GeoLocationRequest location) {
final ClientInfo clientInfo = ClientInfoHolder.getClientInfo();
if (clientInfo == null || StringUtils.isBlank(userAgent)) {
LOGGER.warn("No client IP or user-agent was provided. Skipping adaptive authentication policy...");
return true;
}
final String clientIp = clientInfo.getClientIpAddress();
LOGGER.debug("Located client IP address as [{}]", clientIp);
if (isClientIpAddressRejected(clientIp)) {
LOGGER.warn("Client IP [{}] is rejected for authentication", clientIp);
return false;
}
if (isUserAgentRejected(userAgent)) {
LOGGER.warn("User agent [{}] is rejected for authentication", userAgent);
return false;
}
LOGGER.debug("User agent [{}] is authorized to proceed", userAgent);
if (this.geoLocationService != null && location != null && StringUtils.isNotBlank(clientIp) && StringUtils.isNotBlank(this.adaptiveAuthenticationProperties.getRejectCountries())) {
final GeoLocationResponse loc = this.geoLocationService.locate(clientIp, location);
if (loc != null) {
LOGGER.debug("Determined geolocation to be [{}]", loc);
if (isGeoLocationCountryRejected(loc)) {
LOGGER.warn("Client [{}] is rejected for authentication", clientIp);
return false;
}
} else {
LOGGER.info("Could not determine geolocation for [{}]", clientIp);
}
}
LOGGER.debug("Adaptive authentication policy has authorized client [{}] to proceed.", clientIp);
return true;
}
use of org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse in project cas by apereo.
the class MaxmindDatabaseGeoLocationService method locate.
@Override
public GeoLocationResponse locate(final InetAddress address) {
try {
final GeoLocationResponse location = new GeoLocationResponse();
if (this.cityDatabaseReader != null) {
final CityResponse response = this.cityDatabaseReader.city(address);
location.addAddress(response.getCity().getName());
location.setLatitude(response.getLocation().getLatitude());
location.setLongitude(response.getLocation().getLongitude());
}
if (this.countryDatabaseReader != null) {
final CountryResponse response = this.countryDatabaseReader.country(address);
location.addAddress(response.getCountry().getName());
}
LOGGER.debug("Geo location for [{}] is calculated as [{}]", address, location);
return location;
} catch (final AddressNotFoundException e) {
LOGGER.info(e.getMessage(), e);
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
use of org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse in project cas by apereo.
the class AbstractGeoLocationService method locate.
@Override
public GeoLocationResponse locate(final String clientIp, final GeoLocationRequest location) {
LOGGER.debug("Attempting to find geolocation for [{}]", clientIp);
GeoLocationResponse loc = locate(clientIp);
if (loc == null && location != null) {
LOGGER.debug("Attempting to find geolocation for [{}]", location);
if (StringUtils.isNotBlank(location.getLatitude()) && StringUtils.isNotBlank(location.getLongitude())) {
loc = locate(Double.valueOf(location.getLatitude()), Double.valueOf(location.getLongitude()));
}
}
return loc;
}
use of org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse 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();
if (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());
} else {
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;
}
Aggregations