Adds testing for unauthenticated API calls, simplify logging logic

This commit is contained in:
shamoon 2023-06-03 08:50:54 -07:00
parent 4a02865697
commit ea14fa5251
2 changed files with 31 additions and 11 deletions

View file

@ -12,21 +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"
username = credentials.get("username")
log_output = (
"No authentication provided"
if username is None
else f"Login failed for user `{username}`"
)
if client_ip is None:
logger.info(
f"Login failed for user `{username}`. Unable to determine IP address.",
)
log_output += ". 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 `{username}` from IP `{client_ip}.`",
)
log_output += f" from IP `{client_ip}.`"
else:
# The client's IP address is private
logger.info(
f"Login failed for user `{username}`"
f" from private IP `{client_ip}.`",
)
log_output += f" from private IP `{client_ip}.`"
logger.info(log_output)