2022-12-31 13:13:19 -08:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from django.conf import settings
|
2023-05-11 08:58:32 -07:00
|
|
|
from ipware import IpWare
|
2022-12-31 13:13:19 -08:00
|
|
|
|
|
|
|
|
logger = logging.getLogger("paperless.auth")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# https://docs.djangoproject.com/en/4.1/ref/contrib/auth/#django.contrib.auth.signals.user_login_failed
|
|
|
|
|
def handle_failed_login(sender, credentials, request, **kwargs):
|
2023-05-11 08:58:32 -07:00
|
|
|
ipware = IpWare(proxy_trusted_list=settings.TRUSTED_PROXIES)
|
|
|
|
|
client_ip, _ = ipware.get_client_ip(
|
|
|
|
|
meta=request.META,
|
2022-12-31 13:13:19 -08:00
|
|
|
)
|
2023-06-03 10:41:04 +02:00
|
|
|
username = credentials.get("username") or "anonymous"
|
2023-05-11 08:58:32 -07:00
|
|
|
|
2022-12-31 13:13:19 -08:00
|
|
|
if client_ip is None:
|
|
|
|
|
logger.info(
|
2023-06-03 10:41:04 +02:00
|
|
|
f"Login failed for user `{username}`. Unable to determine IP address.",
|
2022-12-31 13:13:19 -08:00
|
|
|
)
|
|
|
|
|
else:
|
2023-05-11 08:58:32 -07:00
|
|
|
if client_ip.is_global:
|
2022-12-31 13:13:19 -08:00
|
|
|
# We got the client's IP address
|
|
|
|
|
logger.info(
|
2023-06-03 10:41:04 +02:00
|
|
|
f"Login failed for user `{username}` from IP `{client_ip}.`",
|
2022-12-31 13:13:19 -08:00
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
# The client's IP address is private
|
|
|
|
|
logger.info(
|
2023-06-03 10:41:04 +02:00
|
|
|
f"Login failed for user `{username}`"
|
2023-03-28 09:39:30 -07:00
|
|
|
f" from private IP `{client_ip}.`",
|
2022-12-31 13:13:19 -08:00
|
|
|
)
|