2019-02-25 20:54:40 +00:00
|
|
|
"""
|
|
|
|
|
Axes utility functions that are publicly available.
|
2019-02-13 12:50:50 +00:00
|
|
|
|
2019-02-25 20:54:40 +00:00
|
|
|
This module is separate for historical reasons
|
|
|
|
|
and offers a backwards compatible import path.
|
|
|
|
|
"""
|
2019-02-13 12:50:50 +00:00
|
|
|
|
2019-02-25 20:54:40 +00:00
|
|
|
from logging import getLogger
|
2020-08-09 05:58:03 +00:00
|
|
|
from typing import Optional
|
2019-02-17 21:56:48 +00:00
|
|
|
|
2020-07-08 05:43:45 +00:00
|
|
|
from django.http import HttpRequest
|
|
|
|
|
|
2019-07-11 12:56:28 +00:00
|
|
|
from axes.handlers.proxy import AxesProxyHandler
|
2023-05-04 09:49:25 +00:00
|
|
|
from axes.helpers import get_client_ip_address, get_lockout_parameters
|
2019-02-17 21:56:48 +00:00
|
|
|
|
2019-02-25 20:54:40 +00:00
|
|
|
log = getLogger(__name__)
|
2019-02-17 21:56:48 +00:00
|
|
|
|
|
|
|
|
|
2022-05-16 07:31:46 +00:00
|
|
|
def reset(
|
|
|
|
|
ip: Optional[str] = None, username: Optional[str] = None, ip_or_username=False
|
|
|
|
|
) -> int:
|
2019-02-17 21:56:48 +00:00
|
|
|
"""
|
2019-02-25 20:54:40 +00:00
|
|
|
Reset records that match IP or username, and return the count of removed attempts.
|
2019-02-17 21:56:48 +00:00
|
|
|
|
2019-02-25 20:54:40 +00:00
|
|
|
This utility method is meant to be used from the CLI or via Python API.
|
2019-02-17 21:56:48 +00:00
|
|
|
"""
|
|
|
|
|
|
2020-07-07 08:46:22 +00:00
|
|
|
return AxesProxyHandler.reset_attempts(
|
|
|
|
|
ip_address=ip, username=username, ip_or_username=ip_or_username
|
|
|
|
|
)
|
2020-07-07 17:55:46 +00:00
|
|
|
|
|
|
|
|
|
2020-07-08 05:43:45 +00:00
|
|
|
def reset_request(request: HttpRequest) -> int:
|
2020-07-07 17:55:46 +00:00
|
|
|
"""
|
|
|
|
|
Reset records that match IP or username, and return the count of removed attempts.
|
|
|
|
|
|
|
|
|
|
This utility method is meant to be used from the CLI or via Python API.
|
|
|
|
|
"""
|
2023-05-04 09:49:25 +00:00
|
|
|
lockout_paramaters = get_lockout_parameters(request)
|
2020-07-07 17:55:46 +00:00
|
|
|
|
2020-08-09 05:58:03 +00:00
|
|
|
ip: Optional[str] = get_client_ip_address(request)
|
2020-07-07 17:55:46 +00:00
|
|
|
username = request.GET.get("username", None)
|
|
|
|
|
|
2023-05-04 09:49:25 +00:00
|
|
|
ip_required = False
|
|
|
|
|
username_required = False
|
|
|
|
|
ip_and_username = False
|
|
|
|
|
|
|
|
|
|
for param in lockout_paramaters:
|
|
|
|
|
# hack: in works with all iterables, including strings
|
|
|
|
|
# so this checks works with separate parameters
|
|
|
|
|
# and with parameters combinations
|
|
|
|
|
if "username" in param and "ip_address" in param:
|
|
|
|
|
ip_and_username = True
|
|
|
|
|
ip_required = True
|
|
|
|
|
username_required = True
|
|
|
|
|
break
|
2023-05-10 17:07:11 +00:00
|
|
|
if "username" in param:
|
2023-05-04 09:49:25 +00:00
|
|
|
username_required = True
|
|
|
|
|
elif "ip_address" in param:
|
|
|
|
|
ip_required = True
|
|
|
|
|
|
|
|
|
|
ip_or_username = not ip_and_username and ip_required and username_required
|
|
|
|
|
if not ip_required:
|
2020-07-07 17:55:46 +00:00
|
|
|
ip = None
|
2023-05-04 09:49:25 +00:00
|
|
|
if not username_required:
|
2020-07-08 05:43:45 +00:00
|
|
|
username = None
|
|
|
|
|
|
|
|
|
|
if not ip and not username:
|
2020-08-09 05:58:03 +00:00
|
|
|
return 0
|
|
|
|
|
# We don't want to reset everything, if there is some wrong request parameter
|
2020-07-07 17:55:46 +00:00
|
|
|
|
|
|
|
|
# TODO: reset based on user_agent?
|
2020-07-07 08:46:22 +00:00
|
|
|
return reset(ip, username, ip_or_username)
|