-
-
Notifications
You must be signed in to change notification settings - Fork 361
feat: Add Retry-After HTTP header to lockout responses
#1401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
c9563bc
ef91ba9
a9e9704
359843c
0513231
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,7 @@ 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. | ||
|
|
@@ -116,6 +117,7 @@ def get_attempt_expiration(request: Optional[HttpRequest] = None) -> datetime: | |
| return datetime.now() + cool_off | ||
| return attempt_time + cool_off | ||
|
|
||
|
|
||
| def get_credentials(username: Optional[str] = None, **kwargs) -> dict: | ||
| """ | ||
| Calculate credentials for Axes to use internally from given username and kwargs. | ||
|
|
@@ -459,6 +461,12 @@ def get_lockout_message() -> str: | |
| return settings.AXES_PERMALOCK_MESSAGE | ||
|
|
||
|
|
||
| def _set_retry_after_header(response: HttpResponse, request: HttpRequest) -> None: | ||
| cool_off = get_cool_off(request) | ||
| if cool_off is not None: | ||
| response["Retry-After"] = str(int(cool_off.total_seconds())) | ||
|
||
|
|
||
|
|
||
| def get_lockout_response( | ||
| request: HttpRequest, | ||
| original_response: Optional[HttpResponse] = None, | ||
|
|
@@ -511,18 +519,25 @@ def get_lockout_response( | |
| json_response["Access-Control-Allow-Headers"] = ( | ||
| "Origin, Content-Type, Accept, Authorization, x-requested-with" | ||
| ) | ||
| _set_retry_after_header(json_response, request) | ||
| return json_response | ||
|
|
||
| if settings.AXES_LOCKOUT_TEMPLATE: | ||
| return render(request, settings.AXES_LOCKOUT_TEMPLATE, context, status=status) | ||
| response = render( | ||
| request, settings.AXES_LOCKOUT_TEMPLATE, context, status=status | ||
| ) | ||
| _set_retry_after_header(response, request) | ||
| return response | ||
|
|
||
| if settings.AXES_LOCKOUT_URL: | ||
| lockout_url = settings.AXES_LOCKOUT_URL | ||
| query_string = urlencode({"username": context["username"]}) | ||
| url = f"{lockout_url}?{query_string}" | ||
| return redirect(url) | ||
|
|
||
| return HttpResponse(get_lockout_message(), status=status) | ||
| response = HttpResponse(get_lockout_message(), status=status) | ||
| _set_retry_after_header(response, request) | ||
| return response | ||
|
|
||
|
|
||
| def is_ip_address_in_whitelist(ip_address: str) -> bool: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @rodrigobnogueira, would the Axes middleware be a good place for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @aleksihakli ,
moved Retry-After handling into AxesMiddleware so lockout response header logic is centralized there