| 1 |
# pylint: disable=arguments-differ |
| 2 |
# pylint generates false negatives from proxy class method overrides |
| 3 |
|
| 4 |
from logging import getLogger |
| 5 |
from typing import Optional |
| 6 |
|
| 7 |
from django.utils.module_loading import import_string |
| 8 |
from django.utils.timezone import now |
| 9 |
|
| 10 |
from axes.conf import settings |
| 11 |
from axes.handlers.base import AxesBaseHandler, AbstractAxesHandler, AxesHandler |
| 12 |
from axes.helpers import ( |
| 13 |
get_client_ip_address, |
| 14 |
get_client_user_agent, |
| 15 |
get_client_path_info, |
| 16 |
get_client_http_accept, |
| 17 |
toggleable, |
| 18 |
) |
| 19 |
|
| 20 |
log = getLogger(__name__) |
| 21 |
|
| 22 |
|
| 23 |
class AxesProxyHandler(AbstractAxesHandler, AxesBaseHandler): |
| 24 |
""" |
| 25 |
Proxy interface for configurable Axes signal handler class. |
| 26 |
|
| 27 |
If you wish to implement a custom version of this handler, |
| 28 |
you can override the settings.AXES_HANDLER configuration string |
| 29 |
with a class that implements a compatible interface and methods. |
| 30 |
|
| 31 |
Defaults to using axes.handlers.proxy.AxesProxyHandler if not overridden. |
| 32 |
Refer to axes.handlers.proxy.AxesProxyHandler for default implementation. |
| 33 |
""" |
| 34 |
|
| 35 |
implementation = None # type: AxesHandler |
| 36 |
|
| 37 |
@classmethod |
| 38 |
def get_implementation(cls, force: bool = False) -> AxesHandler: |
| 39 |
""" |
| 40 |
Fetch and initialize configured handler implementation and memoize it to avoid reinitialization. |
| 41 |
|
| 42 |
This method is re-entrant and can be called multiple times from e.g. Django application loader. |
| 43 |
""" |
| 44 |
|
| 45 |
if force or not cls.implementation: |
| 46 |
cls.implementation = import_string(settings.AXES_HANDLER)() |
| 47 |
return cls.implementation |
| 48 |
|
| 49 |
@classmethod |
| 50 |
def reset_attempts( |
| 51 |
cls, |
| 52 |
*, |
| 53 |
ip_address: Optional[str] = None, |
| 54 |
username: Optional[str] = None, |
| 55 |
ip_or_username: bool = False, |
| 56 |
) -> int: |
| 57 |
return cls.get_implementation().reset_attempts( |
| 58 |
ip_address=ip_address, username=username, ip_or_username=ip_or_username |
| 59 |
) |
| 60 |
|
| 61 |
@classmethod |
| 62 |
def reset_logs(cls, *, age_days: Optional[int] = None) -> int: |
| 63 |
return cls.get_implementation().reset_logs(age_days=age_days) |
| 64 |
|
| 65 |
@classmethod |
| 66 |
def reset_failure_logs(cls, *, age_days: Optional[int] = None) -> int: |
| 67 |
return cls.get_implementation().reset_failure_logs(age_days=age_days) |
| 68 |
|
| 69 |
@classmethod |
| 70 |
def remove_out_of_limit_failure_logs( |
| 71 |
cls, *, username: str, limit: Optional[int] = None |
| 72 |
) -> int: |
| 73 |
return cls.get_implementation().remove_out_of_limit_failure_logs( |
| 74 |
username=username |
| 75 |
) |
| 76 |
|
| 77 |
@staticmethod |
| 78 |
def update_request(request): |
| 79 |
""" |
| 80 |
Update request attributes before passing them into the selected handler class. |
| 81 |
""" |
| 82 |
|
| 83 |
if request is None: |
| 84 |
log.error( |
| 85 |
"AXES: AxesProxyHandler.update_request can not set request attributes to a None request" |
| 86 |
) |
| 87 |
return |
| 88 |
if not hasattr(request, "axes_updated"): |
| 89 |
if not hasattr(request, "axes_locked_out"): |
| 90 |
request.axes_locked_out = False |
| 91 |
request.axes_attempt_time = now() |
| 92 |
request.axes_ip_address = get_client_ip_address(request) |
| 93 |
request.axes_user_agent = get_client_user_agent(request) |
| 94 |
request.axes_path_info = get_client_path_info(request) |
| 95 |
request.axes_http_accept = get_client_http_accept(request) |
| 96 |
request.axes_failures_since_start = None |
| 97 |
request.axes_updated = True |
| 98 |
request.axes_credentials = None |
| 99 |
|
| 100 |
@classmethod |
| 101 |
def is_locked(cls, request, credentials: Optional[dict] = None) -> bool: |
| 102 |
cls.update_request(request) |
| 103 |
return cls.get_implementation().is_locked(request, credentials) |
| 104 |
|
| 105 |
@classmethod |
| 106 |
def is_allowed(cls, request, credentials: Optional[dict] = None) -> bool: |
| 107 |
cls.update_request(request) |
| 108 |
return cls.get_implementation().is_allowed(request, credentials) |
| 109 |
|
| 110 |
@classmethod |
| 111 |
def get_failures(cls, request, credentials: Optional[dict] = None) -> int: |
| 112 |
cls.update_request(request) |
| 113 |
return cls.get_implementation().get_failures(request, credentials) |
| 114 |
|
| 115 |
@classmethod |
| 116 |
@toggleable |
| 117 |
def user_login_failed(cls, sender, credentials: dict, request=None, **kwargs): |
| 118 |
cls.update_request(request) |
| 119 |
return cls.get_implementation().user_login_failed( |
| 120 |
sender, credentials, request, **kwargs |
| 121 |
) |
| 122 |
|
| 123 |
@classmethod |
| 124 |
@toggleable |
| 125 |
def user_logged_in(cls, sender, request, user, **kwargs): |
| 126 |
cls.update_request(request) |
| 127 |
return cls.get_implementation().user_logged_in(sender, request, user, **kwargs) |
| 128 |
|
| 129 |
@classmethod |
| 130 |
@toggleable |
| 131 |
def user_logged_out(cls, sender, request, user, **kwargs): |
| 132 |
cls.update_request(request) |
| 133 |
return cls.get_implementation().user_logged_out(sender, request, user, **kwargs) |
| 134 |
|
| 135 |
@classmethod |
| 136 |
@toggleable |
| 137 |
def post_save_access_attempt(cls, instance, **kwargs): |
| 138 |
return cls.get_implementation().post_save_access_attempt(instance, **kwargs) |
| 139 |
|
| 140 |
@classmethod |
| 141 |
@toggleable |
| 142 |
def post_delete_access_attempt(cls, instance, **kwargs): |
| 143 |
return cls.get_implementation().post_delete_access_attempt(instance, **kwargs) |