diff --git a/axes/attempts.py b/axes/attempts.py index 9d22f9c..83367f9 100644 --- a/axes/attempts.py +++ b/axes/attempts.py @@ -24,19 +24,3 @@ def get_cool_off_threshold(request: Optional[HttpRequest] = None) -> datetime: if attempt_time is None: return now() - cool_off return attempt_time - cool_off - -def get_individual_attempt_expiry(request: Optional[HttpRequest] = None) -> datetime: - """ - Get threshold for fetching access attempts from the database. - """ - - cool_off = get_cool_off(request) - if cool_off is None: - raise TypeError( - "Cool off threshold can not be calculated with settings.AXES_COOLOFF_TIME set to None" - ) - - attempt_time = request.axes_attempt_time - if attempt_time is None: - return now() + cool_off - return attempt_time + cool_off \ No newline at end of file diff --git a/axes/handlers/database.py b/axes/handlers/database.py index e71add3..cd2ca77 100644 --- a/axes/handlers/database.py +++ b/axes/handlers/database.py @@ -7,7 +7,7 @@ from django.db.models.functions import Concat from django.http import HttpRequest from django.utils import timezone -from axes.attempts import get_cool_off_threshold, get_individual_attempt_expiry +from axes.attempts import get_cool_off_threshold from axes.conf import settings from axes.handlers.base import AbstractAxesHandler, AxesBaseHandler from axes.helpers import ( @@ -19,6 +19,7 @@ from axes.helpers import ( get_failure_limit, get_lockout_parameters, get_query_str, + get_attempt_expiration, ) from axes.models import AccessAttempt, AccessAttemptExpiration, AccessFailureLog, AccessLog from axes.signals import user_locked_out @@ -226,11 +227,11 @@ class AxesDatabaseHandler(AbstractAxesHandler, AxesBaseHandler): ) attempt.expiration = AccessAttemptExpiration.objects.create( access_attempt=attempt, - expires_at=get_individual_attempt_expiry(request) + expires_at=get_attempt_expiration(request) ) else: attempt.expiration.expires_at = max( - get_individual_attempt_expiry(request), attempt.expiration.expires_at + get_attempt_expiration(request), attempt.expiration.expires_at ) attempt.expiration.save() diff --git a/axes/helpers.py b/axes/helpers.py index 198460f..5e1a5f9 100644 --- a/axes/helpers.py +++ b/axes/helpers.py @@ -1,4 +1,4 @@ -from datetime import timedelta +from datetime import timedelta, datetime from hashlib import sha256 from logging import getLogger from string import Template @@ -100,6 +100,21 @@ def get_cool_off_iso8601(delta: timedelta) -> str: return f"P{days_str}T{time_str}" return f"P{days_str}" +def get_attempt_expiration(request: Optional[HttpRequest] = None) -> datetime: + """ + Get threshold for fetching access attempts from the database. + """ + + cool_off = get_cool_off(request) + if cool_off is None: + raise TypeError( + "Cool off threshold can not be calculated with settings.AXES_COOLOFF_TIME set to None" + ) + + attempt_time = request.axes_attempt_time + if attempt_time is None: + return datetime.now() + cool_off + return attempt_time + cool_off def get_credentials(username: Optional[str] = None, **kwargs) -> dict: """