Registration (case-insensitive unique names, 8-256 char passwords, no email), sign-in with a generic error and a fresh session, internal-only ?next=, POST-only sign-out, password change that closes the other sessions. The failed-login backoff, the registration quota and the password-change guard live in the database so every web process shares them; the client IP is the last X-Forwarded-For hop. One responsive stylesheet with light/dark theme, labelled fields, alerts and no inline scripts or styles; Italian error pages and a readable CSRF failure. Mutation checks: open redirects, uncounted failures and a missing CSRF middleware are all caught. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
21 lines
842 B
Python
21 lines
842 B
Python
from django.contrib.auth import get_user_model
|
|
from django.contrib.auth.backends import ModelBackend
|
|
|
|
|
|
class CaseInsensitiveBackend(ModelBackend):
|
|
"""Usernames are one case-insensitive namespace (ADR-38): `Mario` and `mario` log in as the same account."""
|
|
|
|
def authenticate(self, request, username=None, password=None, **kwargs):
|
|
User = get_user_model()
|
|
if username is None or password is None:
|
|
return None
|
|
try:
|
|
user = User.objects.get(username__iexact=username)
|
|
except User.DoesNotExist:
|
|
# Run the hasher anyway, so an unknown name costs as much as a wrong password (SEC-19).
|
|
User().set_password(password)
|
|
return None
|
|
if user.check_password(password) and self.user_can_authenticate(user):
|
|
return user
|
|
return None
|