Skip to content

Commit

Permalink
add reset_request helper function to internalize reset logic into dja…
Browse files Browse the repository at this point in the history
…ngo-axis
  • Loading branch information
PetrDlouhy authored and aleksihakli committed Aug 21, 2020
1 parent b855a92 commit c3c2786
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 23 additions & 0 deletions axes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

from logging import getLogger

from axes.conf import settings
from axes.handlers.proxy import AxesProxyHandler
from axes.helpers import get_client_ip_address

log = getLogger(__name__)

Expand All @@ -20,3 +22,24 @@ def reset(ip: str = None, username: str = None) -> int:
"""

return AxesProxyHandler.reset_attempts(ip_address=ip, username=username)


def reset_request(request) -> int:
"""
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.
"""

ip = None
ip = get_client_ip_address(request)
username = request.GET.get("username", None)

if settings.AXES_ONLY_USER_FAILURES:
ip = None
else:
username = None

# if settings.AXES_USE_USER_AGENT:
# TODO: reset based on user_agent?
return reset(ip, username)
6 changes: 2 additions & 4 deletions docs/6_integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ Axes supports Captcha with the Django Simple Captcha package in the following ma

``example/views.py``::

from axes.utils import reset
from axes.helpers import get_client_ip_address
from axes.utils import reset_request
from django.http.response import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse_lazy
Expand All @@ -174,8 +173,7 @@ Axes supports Captcha with the Django Simple Captcha package in the following ma
if request.POST:
form = AxesCaptchaForm(request.POST)
if form.is_valid():
ip = get_client_ip_address(request)
reset(ip=ip)
reset_request(request)
return HttpResponseRedirect(reverse_lazy('auth_login'))
else:
form = AxesCaptchaForm()
Expand Down

0 comments on commit c3c2786

Please sign in to comment.