Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

auth: Log details of authentication attempts.

Mateusz Manderaa4e77d514bb9
diff --git a/zproject/backends.py b/zproject/backends.py
index d8eff0f697b..0d15933876d 100644
--- a/zproject/backends.py
+++ b/zproject/backends.py
@@ -294,6 +294,30 @@ def rate_limit_auth(auth_func: AuthFuncT, *args: Any, **kwargs: Any) -> Optional
return result
+@decorator
+def log_auth_attempts(auth_func: AuthFuncT, *args: Any, **kwargs: Any) -> Optional[UserProfile]:
+ result = auth_func(*args, **kwargs)
+
+ backend_instance = args[0]
+ request = args[1]
+ username = kwargs["username"]
+ realm = kwargs["realm"]
+ return_data = kwargs["return_data"]
+
+ ip_addr = request.META.get("REMOTE_ADDR")
+ outcome = "success" if result is not None else "failed"
+ backend_instance.logger.info(
+ "Authentication attempt from %s: subdomain=%s;username=%s;outcome=%s;return_data=%s",
+ ip_addr,
+ realm.subdomain,
+ username,
+ outcome,
+ return_data,
+ )
+
+ return result
+
+
class ZulipAuthMixin:
"""This common mixin is used to override Django's default behavior for
looking up a logged-in user by ID to use a version that fetches
@@ -371,6 +395,7 @@ class EmailAuthBackend(ZulipAuthMixin):
name = "email"
@rate_limit_auth
+ @log_auth_attempts
def authenticate(
self,
request: HttpRequest,
@@ -779,6 +804,7 @@ class ZulipLDAPAuthBackend(ZulipLDAPAuthBackendBase):
REALM_IS_NONE_ERROR = 1
@rate_limit_auth
+ @log_auth_attempts
def authenticate(
self,
request: Optional[HttpRequest] = None,
diff --git a/zproject/test_extra_settings.py b/zproject/test_extra_settings.py
index d1cc7b771b3..4dbcac3227c 100644
--- a/zproject/test_extra_settings.py
+++ b/zproject/test_extra_settings.py
@@ -136,6 +136,7 @@ if not PUPPETEER_TESTS:
set_loglevel("zulip.requests", "CRITICAL")
set_loglevel("zulip.management", "CRITICAL")
+ set_loglevel("zulip.auth", "WARNING")
set_loglevel("django.request", "ERROR")
set_loglevel("django_auth_ldap", "WARNING")
set_loglevel("fakeldap", "ERROR")