mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
A locked user got django-axes' plain-text 429 reply, untranslated and with no way back; it is now a page in the panel's layout that says within how many minutes the lock ends, and leads back to the sign-in page with the destination kept (AXES_LOCKOUT_CALLABLE). A failed sign-in emptied the username; it is kept, the password is marked invalid and has the cursor (a refused username keeps it). A line under the password says when Caps Lock is on. The fields carry autocomplete (username, current-password, one-time-code) for password managers and phones, and the show-password button is reachable with Tab again. The lines under the forms are gone: "KVM / Libvirt" with a green dot looked like a status it did not check, and "2FA Protection Active" was shown to users without 2FA. A new setting, Sign-in notice (Settings, Login), is shown above both sign-in forms when set, e.g. planned maintenance or a terms-of-use line.
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
"""
|
|
Settings for django-axes, read from the Settings page (AppSettings) on each
|
|
use, so an administrator can change them without a restart.
|
|
"""
|
|
|
|
from datetime import timedelta
|
|
from ipaddress import ip_address, ip_network
|
|
|
|
from appsettings.models import AppSettings
|
|
from django.conf import settings
|
|
from django.shortcuts import render
|
|
|
|
DEFAULT_FAILURE_LIMIT = 5
|
|
DEFAULT_LOCKOUT_MINUTES = 15
|
|
|
|
|
|
def _positive_int(key, default):
|
|
try:
|
|
value = int(AppSettings.objects.get(key=key).value)
|
|
except (AppSettings.DoesNotExist, ValueError):
|
|
return default
|
|
return value if value > 0 else default
|
|
|
|
|
|
def failure_limit(request, credentials):
|
|
"""AXES_FAILURE_LIMIT: failed logins allowed before the lock."""
|
|
return _positive_int("LOGIN_FAILURE_LIMIT", DEFAULT_FAILURE_LIMIT)
|
|
|
|
|
|
def cooloff_time(request):
|
|
"""AXES_COOLOFF_TIME: how long a lock lasts."""
|
|
return timedelta(minutes=_positive_int("LOGIN_LOCKOUT_MINUTES", DEFAULT_LOCKOUT_MINUTES))
|
|
|
|
|
|
def _from_trusted_proxy(remote):
|
|
try:
|
|
address = ip_address(remote)
|
|
except ValueError:
|
|
return False
|
|
if address.is_loopback: # the bundled nginx
|
|
return True
|
|
return any(address in ip_network(net, strict=False) for net in getattr(settings, "LOGIN_TRUSTED_PROXIES", []))
|
|
|
|
|
|
def client_ip(request):
|
|
"""
|
|
AXES_CLIENT_IP_CALLABLE. Behind the bundled nginx every request comes from
|
|
127.0.0.1 and nginx sets X-Real-IP to the client's address (overwriting
|
|
any sent by the client). The header is used only for requests from that
|
|
proxy, or from the addresses in LOGIN_TRUSTED_PROXIES; from anyone else
|
|
it could be forged to escape a lock.
|
|
"""
|
|
remote = request.META.get("REMOTE_ADDR", "")
|
|
real_ip = request.META.get("HTTP_X_REAL_IP", "").strip()
|
|
return real_ip if real_ip and _from_trusted_proxy(remote) else remote
|
|
|
|
|
|
def lockout_response(request, original_response, credentials):
|
|
"""AXES_LOCKOUT_CALLABLE: the locked sign-in page, with the lock time."""
|
|
minutes = cooloff_time(request) // timedelta(minutes=1)
|
|
return render(request, "accounts/lockout.html", {"minutes": minutes}, status=429)
|