From 4a02865697d3929c5c5c88da06a8dd6f14c90e35 Mon Sep 17 00:00:00 2001 From: Igor Rzegocki Date: Sat, 3 Jun 2023 10:41:04 +0200 Subject: [PATCH] fix broken "failed login" signal --- src/paperless/auth.py | 4 ++-- src/paperless/signals.py | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/paperless/auth.py b/src/paperless/auth.py index ec853743e..2285d0526 100644 --- a/src/paperless/auth.py +++ b/src/paperless/auth.py @@ -1,6 +1,6 @@ from django.conf import settings from django.contrib import auth -from django.contrib.auth.middleware import RemoteUserMiddleware +from django.contrib.auth.middleware import PersistentRemoteUserMiddleware from django.contrib.auth.models import User from django.utils.deprecation import MiddlewareMixin from rest_framework import authentication @@ -37,7 +37,7 @@ class AngularApiAuthenticationOverride(authentication.BaseAuthentication): return None -class HttpRemoteUserMiddleware(RemoteUserMiddleware): +class HttpRemoteUserMiddleware(PersistentRemoteUserMiddleware): """This class allows authentication via HTTP_REMOTE_USER which is set for example by certain SSO applications. """ diff --git a/src/paperless/signals.py b/src/paperless/signals.py index 83ba74193..9dc3383e3 100644 --- a/src/paperless/signals.py +++ b/src/paperless/signals.py @@ -12,22 +12,21 @@ def handle_failed_login(sender, credentials, request, **kwargs): client_ip, _ = ipware.get_client_ip( meta=request.META, ) + username = credentials.get("username") or "anonymous" if client_ip is None: logger.info( - f"Login failed for user `{credentials['username']}`." - " Unable to determine IP address.", + f"Login failed for user `{username}`. Unable to determine IP address.", ) else: if client_ip.is_global: # We got the client's IP address logger.info( - f"Login failed for user `{credentials['username']}`" - f" from IP `{client_ip}.`", + f"Login failed for user `{username}` from IP `{client_ip}.`", ) else: # The client's IP address is private logger.info( - f"Login failed for user `{credentials['username']}`" + f"Login failed for user `{username}`" f" from private IP `{client_ip}.`", )