events: fix error when attempting to create event with GeoIP City in context

closes #2709

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-06-05 00:15:01 +02:00
parent e30103aa9f
commit b26111fb42
2 changed files with 13 additions and 5 deletions

View File

@ -76,11 +76,8 @@ class GeoIPReader:
except (GeoIP2Error, ValueError): except (GeoIP2Error, ValueError):
return None return None
def city_dict(self, ip_address: str) -> Optional[GeoIPDict]: def city_to_dict(self, city: City) -> GeoIPDict:
"""Wrapper for self.city that returns a dict""" """Convert City to dict"""
city = self.city(ip_address)
if not city:
return None
city_dict: GeoIPDict = { city_dict: GeoIPDict = {
"continent": city.continent.code, "continent": city.continent.code,
"country": city.country.iso_code, "country": city.country.iso_code,
@ -92,5 +89,12 @@ class GeoIPReader:
city_dict["city"] = city.city.name city_dict["city"] = city.city.name
return city_dict 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() GEOIP_READER = GeoIPReader()

View File

@ -10,9 +10,11 @@ from django.db import models
from django.db.models.base import Model from django.db.models.base import Model
from django.http.request import HttpRequest from django.http.request import HttpRequest
from django.views.debug import SafeExceptionReporterFilter from django.views.debug import SafeExceptionReporterFilter
from geoip2.models import City
from guardian.utils import get_anonymous_user from guardian.utils import get_anonymous_user
from authentik.core.models import User from authentik.core.models import User
from authentik.events.geo import GEOIP_READER
from authentik.policies.types import PolicyRequest from authentik.policies.types import PolicyRequest
# Special keys which are *not* cleaned, even when the default filter # 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 final_dict[key] = value.hex
elif isinstance(value, (HttpRequest, WSGIRequest)): elif isinstance(value, (HttpRequest, WSGIRequest)):
continue continue
elif isinstance(value, City):
final_dict[key] = GEOIP_READER.city_to_dict(value)
elif isinstance(value, type): elif isinstance(value, type):
final_dict[key] = { final_dict[key] = {
"type": value.__name__, "type": value.__name__,