Skip to content

Commit 421d9e7

Browse files
committed
Log auth details
Signed-off-by: Kipchirchir Sigei <[email protected]>
1 parent 0cf521e commit 421d9e7

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

onadata/libs/authentication.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"""
55
from __future__ import unicode_literals
66

7+
import logging
8+
79
from datetime import datetime
810
from typing import Optional, Tuple
911

@@ -35,8 +37,13 @@
3537
from onadata.apps.api.tasks import send_account_lockout_email
3638
from onadata.libs.utils.cache_tools import LOCKOUT_IP, LOGIN_ATTEMPTS, cache, safe_key
3739
from onadata.libs.utils.common_tags import API_TOKEN
40+
from onadata.libs.utils.common_tools import report_exception
3841
from onadata.libs.utils.email import get_account_lockout_email_data
3942

43+
logger = logging.getLogger("console_logger")
44+
logger.addHandler(logging.StreamHandler())
45+
logger.setLevel(logging.INFO)
46+
4047
ENKETO_AUTH_COOKIE = getattr(settings, "ENKETO_AUTH_COOKIE", "__enketo")
4148
TEMP_TOKEN_EXPIRY_TIME = getattr(
4249
settings, "DEFAULT_TEMP_TOKEN_EXPIRY_TIME", 60 * 60 * 6
@@ -114,14 +121,23 @@ def authenticate(self, request):
114121
remaining_attempts = getattr(settings, "MAX_LOGIN_ATTEMPTS", 10) - attempts
115122
# pylint: disable=unused-variable
116123
lockout_time = getattr(settings, "LOCKOUT_TIME", 1800) // 60 # noqa
117-
raise AuthenticationFailed(
118-
_(
119-
"Invalid username/password. "
120-
f"For security reasons, after {remaining_attempts} more failed "
121-
f"login attempts you'll have to wait {lockout_time} minutes "
122-
"before trying again."
123-
)
124+
ip_address, username = retrieve_user_identification(request)
125+
user_agent = request.META.get("HTTP_USER_AGENT", None)
126+
info_str = (
127+
f"IP: {ip_address}, USERNAME: {username}, "
128+
f"REMAINING_ATTEMPTS: {remaining_attempts}, USER_AGENT: {user_agent}"
124129
)
130+
logger.info(info_str)
131+
error_str = _(
132+
"Invalid username/password. "
133+
f"For security reasons, after {remaining_attempts} more failed "
134+
f"login attempts you'll have to wait {lockout_time} minutes "
135+
"before trying again."
136+
)
137+
# log to sentry
138+
report_exception("Authentication Failure", error_str)
139+
raise AuthenticationFailed(error_str)
140+
125141
except (AttributeError, ValueError, DataError) as e:
126142
raise AuthenticationFailed(e) from e
127143

0 commit comments

Comments
 (0)