diff --git a/authentik/events/geo.py b/authentik/events/geo.py index b5b75e93b..fd38a873d 100644 --- a/authentik/events/geo.py +++ b/authentik/events/geo.py @@ -76,11 +76,8 @@ class GeoIPReader: except (GeoIP2Error, ValueError): return None - def city_dict(self, ip_address: str) -> Optional[GeoIPDict]: - """Wrapper for self.city that returns a dict""" - city = self.city(ip_address) - if not city: - return None + def city_to_dict(self, city: City) -> GeoIPDict: + """Convert City to dict""" city_dict: GeoIPDict = { "continent": city.continent.code, "country": city.country.iso_code, @@ -92,5 +89,12 @@ class GeoIPReader: city_dict["city"] = city.city.name return city_dict + def city_dict(self, ip_address: str) -> Optional[GeoIPDict]: + """Wrapper for self.city that returns a dict""" + city = self.city(ip_address) + if not city: + return None + return self.city_to_dict(city) + GEOIP_READER = GeoIPReader() diff --git a/authentik/events/utils.py b/authentik/events/utils.py index 3fdad4a47..f23c6d533 100644 --- a/authentik/events/utils.py +++ b/authentik/events/utils.py @@ -10,9 +10,11 @@ from django.db import models from django.db.models.base import Model from django.http.request import HttpRequest from django.views.debug import SafeExceptionReporterFilter +from geoip2.models import City from guardian.utils import get_anonymous_user from authentik.core.models import User +from authentik.events.geo import GEOIP_READER from authentik.policies.types import PolicyRequest # Special keys which are *not* cleaned, even when the default filter @@ -93,6 +95,8 @@ def sanitize_dict(source: dict[Any, Any]) -> dict[Any, Any]: final_dict[key] = value.hex elif isinstance(value, (HttpRequest, WSGIRequest)): continue + elif isinstance(value, City): + final_dict[key] = GEOIP_READER.city_to_dict(value) elif isinstance(value, type): final_dict[key] = { "type": value.__name__,