Guest User

authtentik ntfy property mapping

a guest
Oct 11th, 2025
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. from datetime import datetime
  2. from zoneinfo import ZoneInfo
  3. import ipaddress
  4.  
  5. notification_data = notification.event.context
  6.  
  7. # Extract with safe defaults
  8. action = getattr(notification.event, "action", "N/A")
  9. client_ip = getattr(notification.event, "client_ip", "N/A")
  10.  
  11. # username: fallback, if not in data
  12. username = notification_data.get("username") or getattr(notification.event, "username", "unknown-user")
  13.  
  14. stage_model_name = notification_data.get("stage", {}).get("model_name", "N/A")
  15. method = notification_data.get("http_request", {}).get("method", "N/A")
  16. useragent = notification_data.get("http_request", {}).get("user_agent", "N/A")
  17.  
  18. # ASN info with fallbacks
  19. asn_data = notification_data.get("asn", {})
  20. as_org = asn_data.get("as_org", "N/A")
  21. network = asn_data.get("network", "N/A")
  22.  
  23. # Private IP check
  24. if client_ip != "N/A":
  25.     try:
  26.         client_ip_address = ipaddress.ip_address(client_ip)
  27.         private_ip_ranges = [
  28.             ipaddress.IPv4Network("10.0.0.0/8"),
  29.             ipaddress.IPv4Network("172.16.0.0/12"),
  30.             ipaddress.IPv4Network("192.168.0.0/16"),
  31.         ]
  32.         if any(client_ip_address in private_range for private_range in private_ip_ranges):
  33.             as_org = "N/A"
  34.             network = "N/A"
  35.     except ValueError:
  36.         # invalid IP in logs
  37.         pass
  38.  
  39. # Time handling
  40. local_now = datetime.now(tz=ZoneInfo("Europe/Zurich"))
  41. time_difference_hours = local_now.utcoffset().total_seconds() / 3600
  42. formatted_time = local_now.strftime("%H:%M %d.%m.%Y")
  43.  
  44. # Check if user exists
  45. user_exists = notification.event.user is not None
  46. user_status = "existing user" if user_exists else "nonexistent user"
  47.  
  48. # Build message
  49. formatted_message = (
  50.     f"User: {username}\n"
  51.     f"Action: {action}\n"
  52.     f"Stage: {stage_model_name}\n"
  53.     f"Client IP: {client_ip}\n"
  54.     f"Network: {network}\n"
  55.     f"AS Organization: {as_org}\n"
  56.     f"Time: {formatted_time} (UTC+{time_difference_hours:.0f})\n"
  57.     f"Method: {method}\n"
  58.     f"User-Agent: {useragent}\n"
  59. )
  60.  
  61. return {
  62.     "topic": "authentik",
  63.     "title": f"Authentik failed login ({user_status})",
  64.     "tags": ["rotating_light","closed_lock_with_key","camera_flash"],
  65.     "message": formatted_message
  66. }
Advertisement
Add Comment
Please, Sign In to add comment