authentik.events.context_processors.geoip
events GeoIP Reader
1"""events GeoIP Reader""" 2 3from typing import TYPE_CHECKING, TypedDict 4 5from django.http import HttpRequest 6from geoip2.errors import GeoIP2Error 7from geoip2.models import City 8 9from authentik.events.context_processors.mmdb import MMDBContextProcessor 10from authentik.lib.config import CONFIG 11from authentik.lib.tracing import active_tracer 12from authentik.root.middleware import ClientIPMiddleware 13 14if TYPE_CHECKING: 15 from authentik.api.v3.config import Capabilities 16 from authentik.events.models import Event 17 18 19class GeoIPDict(TypedDict): 20 """GeoIP Details""" 21 22 continent: str | None 23 country: str | None 24 lat: float | None 25 long: float | None 26 city: str 27 28 29class GeoIPContextProcessor(MMDBContextProcessor): 30 """Slim wrapper around GeoIP API""" 31 32 default_path = "/geoip/dbip-city-lite.mmdb" 33 legacy_path = "/geoip/GeoLite2-City.mmdb" 34 35 def capability(self) -> Capabilities | None: 36 from authentik.api.v3.config import Capabilities 37 38 return Capabilities.CAN_GEO_IP 39 40 def path(self) -> str | None: 41 return CONFIG.get("events.context_processors.geoip") 42 43 def enrich_event(self, event: Event): 44 city = self.city_dict(event.client_ip) 45 if not city: 46 return 47 event.context["geo"] = city 48 49 def enrich_context(self, request: HttpRequest) -> dict: 50 # Different key `geoip` vs `geo` for legacy reasons 51 return {"geoip": self.city_dict(ClientIPMiddleware.get_client_ip(request))} 52 53 def city(self, ip_address: str) -> City | None: 54 """Wrapper for Reader.city""" 55 with active_tracer().start_span( 56 op="authentik.events.geo.city", 57 name=ip_address, 58 ): 59 if not self.configured(): 60 return None 61 self.check_expired() 62 try: 63 return self.reader.city(ip_address) 64 except GeoIP2Error, ValueError: 65 return None 66 67 def city_to_dict(self, city: City | None) -> GeoIPDict | dict: 68 """Convert City to dict""" 69 if not city: 70 return {} 71 city_dict: GeoIPDict = { 72 "continent": city.continent.code, 73 "country": city.country.iso_code, 74 "lat": city.location.latitude, 75 "long": city.location.longitude, 76 "city": "", 77 } 78 if city.city.name: 79 city_dict["city"] = city.city.name 80 return city_dict 81 82 def city_dict(self, ip_address: str) -> GeoIPDict | None: 83 """Wrapper for self.city that returns a dict""" 84 city = self.city(ip_address) 85 if not city: 86 return None 87 return self.city_to_dict(city) 88 89 90GEOIP_CONTEXT_PROCESSOR = GeoIPContextProcessor()
class
GeoIPDict(typing.TypedDict):
20class GeoIPDict(TypedDict): 21 """GeoIP Details""" 22 23 continent: str | None 24 country: str | None 25 lat: float | None 26 long: float | None 27 city: str
GeoIP Details
30class GeoIPContextProcessor(MMDBContextProcessor): 31 """Slim wrapper around GeoIP API""" 32 33 default_path = "/geoip/dbip-city-lite.mmdb" 34 legacy_path = "/geoip/GeoLite2-City.mmdb" 35 36 def capability(self) -> Capabilities | None: 37 from authentik.api.v3.config import Capabilities 38 39 return Capabilities.CAN_GEO_IP 40 41 def path(self) -> str | None: 42 return CONFIG.get("events.context_processors.geoip") 43 44 def enrich_event(self, event: Event): 45 city = self.city_dict(event.client_ip) 46 if not city: 47 return 48 event.context["geo"] = city 49 50 def enrich_context(self, request: HttpRequest) -> dict: 51 # Different key `geoip` vs `geo` for legacy reasons 52 return {"geoip": self.city_dict(ClientIPMiddleware.get_client_ip(request))} 53 54 def city(self, ip_address: str) -> City | None: 55 """Wrapper for Reader.city""" 56 with active_tracer().start_span( 57 op="authentik.events.geo.city", 58 name=ip_address, 59 ): 60 if not self.configured(): 61 return None 62 self.check_expired() 63 try: 64 return self.reader.city(ip_address) 65 except GeoIP2Error, ValueError: 66 return None 67 68 def city_to_dict(self, city: City | None) -> GeoIPDict | dict: 69 """Convert City to dict""" 70 if not city: 71 return {} 72 city_dict: GeoIPDict = { 73 "continent": city.continent.code, 74 "country": city.country.iso_code, 75 "lat": city.location.latitude, 76 "long": city.location.longitude, 77 "city": "", 78 } 79 if city.city.name: 80 city_dict["city"] = city.city.name 81 return city_dict 82 83 def city_dict(self, ip_address: str) -> GeoIPDict | None: 84 """Wrapper for self.city that returns a dict""" 85 city = self.city(ip_address) 86 if not city: 87 return None 88 return self.city_to_dict(city)
Slim wrapper around GeoIP API
def
capability(unknown):
36 def capability(self) -> Capabilities | None: 37 from authentik.api.v3.config import Capabilities 38 39 return Capabilities.CAN_GEO_IP
Return the capability this context processor provides
def
enrich_event(unknown):
44 def enrich_event(self, event: Event): 45 city = self.city_dict(event.client_ip) 46 if not city: 47 return 48 event.context["geo"] = city
Modify event
def
enrich_context(self, request: django.http.request.HttpRequest) -> dict:
50 def enrich_context(self, request: HttpRequest) -> dict: 51 # Different key `geoip` vs `geo` for legacy reasons 52 return {"geoip": self.city_dict(ClientIPMiddleware.get_client_ip(request))}
Modify context
def
city(self, ip_address: str) -> geoip2.models.City | None:
54 def city(self, ip_address: str) -> City | None: 55 """Wrapper for Reader.city""" 56 with active_tracer().start_span( 57 op="authentik.events.geo.city", 58 name=ip_address, 59 ): 60 if not self.configured(): 61 return None 62 self.check_expired() 63 try: 64 return self.reader.city(ip_address) 65 except GeoIP2Error, ValueError: 66 return None
Wrapper for Reader.city
68 def city_to_dict(self, city: City | None) -> GeoIPDict | dict: 69 """Convert City to dict""" 70 if not city: 71 return {} 72 city_dict: GeoIPDict = { 73 "continent": city.continent.code, 74 "country": city.country.iso_code, 75 "lat": city.location.latitude, 76 "long": city.location.longitude, 77 "city": "", 78 } 79 if city.city.name: 80 city_dict["city"] = city.city.name 81 return city_dict
Convert City to dict
GEOIP_CONTEXT_PROCESSOR =
<GeoIPContextProcessor object>