|
4 | 4 | """ |
5 | 5 | from __future__ import unicode_literals |
6 | 6 |
|
| 7 | +import logging |
| 8 | + |
7 | 9 | from datetime import datetime |
8 | 10 | from typing import Optional, Tuple |
9 | 11 |
|
|
35 | 37 | from onadata.apps.api.tasks import send_account_lockout_email |
36 | 38 | from onadata.libs.utils.cache_tools import LOCKOUT_IP, LOGIN_ATTEMPTS, cache, safe_key |
37 | 39 | from onadata.libs.utils.common_tags import API_TOKEN |
| 40 | +from onadata.libs.utils.common_tools import report_exception |
38 | 41 | from onadata.libs.utils.email import get_account_lockout_email_data |
39 | 42 |
|
| 43 | +logger = logging.getLogger("console_logger") |
| 44 | +logger.addHandler(logging.StreamHandler()) |
| 45 | +logger.setLevel(logging.INFO) |
| 46 | + |
40 | 47 | ENKETO_AUTH_COOKIE = getattr(settings, "ENKETO_AUTH_COOKIE", "__enketo") |
41 | 48 | TEMP_TOKEN_EXPIRY_TIME = getattr( |
42 | 49 | settings, "DEFAULT_TEMP_TOKEN_EXPIRY_TIME", 60 * 60 * 6 |
@@ -114,14 +121,23 @@ def authenticate(self, request): |
114 | 121 | remaining_attempts = getattr(settings, "MAX_LOGIN_ATTEMPTS", 10) - attempts |
115 | 122 | # pylint: disable=unused-variable |
116 | 123 | 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}" |
124 | 129 | ) |
| 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 | + |
125 | 141 | except (AttributeError, ValueError, DataError) as e: |
126 | 142 | raise AuthenticationFailed(e) from e |
127 | 143 |
|
|
0 commit comments