Skip to content

Commit

Permalink
Only do the work in the middleware if axes is enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashokdelphia authored and aleksihakli committed Dec 2, 2020
1 parent 23ca162 commit 146d7a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
29 changes: 15 additions & 14 deletions axes/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,20 @@ def __init__(self, get_response: Callable):
def __call__(self, request):
response = self.get_response(request)

if "rest_framework" in settings.INSTALLED_APPS:
AxesProxyHandler.update_request(request)
username = get_client_username(request)
credentials = get_credentials(username)
failures_since_start = AxesProxyHandler.get_failures(request, credentials)
if (
settings.AXES_LOCK_OUT_AT_FAILURE
and failures_since_start >= get_failure_limit(request, credentials)
):

request.axes_locked_out = True

if getattr(request, "axes_locked_out", None):
response = get_lockout_response(request) # type: ignore
if settings.AXES_ENABLED:
if "rest_framework" in settings.INSTALLED_APPS:
AxesProxyHandler.update_request(request)
username = get_client_username(request)
credentials = get_credentials(username)
failures_since_start = AxesProxyHandler.get_failures(request, credentials)
if (
settings.AXES_LOCK_OUT_AT_FAILURE
and failures_since_start >= get_failure_limit(request, credentials)
):

request.axes_locked_out = True

if getattr(request, "axes_locked_out", None):
response = get_lockout_response(request) # type: ignore

return response
9 changes: 9 additions & 0 deletions axes/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def get_response(request):
response = AxesMiddleware(get_response)(self.request)
self.assertEqual(response.status_code, self.STATUS_LOCKOUT)

@override_settings(AXES_ENABLED=False)
def test_respects_enabled_switch(self):
def get_response(request):
request.axes_locked_out = True
return HttpResponse()

response = AxesMiddleware(get_response)(self.request)
self.assertEqual(response.status_code, self.STATUS_SUCCESS)

@mock.patch("django.conf.settings.INSTALLED_APPS", ["rest_framework"])
def test_response_contains_required_attrs_with_drf_integration(self):
def get_response(request):
Expand Down

0 comments on commit 146d7a2

Please sign in to comment.