2022-12-31 13:13:19 -08:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from django.conf import settings
|
2023-10-30 10:16:49 -07:00
|
|
|
from python_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-10-30 10:16:49 -07:00
|
|
|
ipware = IpWare(proxy_list=settings.TRUSTED_PROXIES)
|
2023-05-11 08:58:32 -07:00
|
|
|
client_ip, _ = ipware.get_client_ip(
|
|
|
|
|
meta=request.META,
|
2022-12-31 13:13:19 -08:00
|
|
|
)
|
2023-06-03 08:50:54 -07:00
|
|
|
username = credentials.get("username")
|
|
|
|
|
log_output = (
|
|
|
|
|
"No authentication provided"
|
|
|
|
|
if username is None
|
|
|
|
|
else f"Login failed for user `{username}`"
|
|
|
|
|
)
|
2023-05-11 08:58:32 -07:00
|
|
|
|
2022-12-31 13:13:19 -08:00
|
|
|
if client_ip is None:
|
2023-06-03 08:50:54 -07:00
|
|
|
log_output += ". 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
|
2023-06-30 15:11:14 +02:00
|
|
|
log_output += f" from IP `{client_ip}`."
|
2022-12-31 13:13:19 -08:00
|
|
|
else:
|
|
|
|
|
# The client's IP address is private
|
2023-06-30 15:11:14 +02:00
|
|
|
log_output += f" from private IP `{client_ip}`."
|
2023-06-03 08:50:54 -07:00
|
|
|
|
|
|
|
|
logger.info(log_output)
|
2025-02-24 09:23:20 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def handle_social_account_updated(sender, request, sociallogin, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Handle the social account update signal.
|
|
|
|
|
"""
|
|
|
|
|
from django.contrib.auth.models import Group
|
|
|
|
|
|
|
|
|
|
social_account_groups = sociallogin.account.extra_data.get(
|
|
|
|
|
"groups",
|
|
|
|
|
[],
|
|
|
|
|
) # None if not found
|
|
|
|
|
if settings.SOCIAL_ACCOUNT_SYNC_GROUPS and social_account_groups is not None:
|
|
|
|
|
groups = Group.objects.filter(name__in=social_account_groups)
|
|
|
|
|
logger.debug(
|
|
|
|
|
f"Syncing groups for user `{sociallogin.user}`: {social_account_groups}",
|
|
|
|
|
)
|
|
|
|
|
sociallogin.user.groups.set(groups, clear=True)
|