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>
32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
import re
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.core.validators import RegexValidator
|
|
|
|
MIN_PASSWORD_LENGTH = 8 # SEC-17
|
|
MAX_PASSWORD_LENGTH = 256 # bounds the hashing work an anonymous caller can ask for (SEC-17)
|
|
|
|
username_validator = RegexValidator(
|
|
re.compile(r"^[A-Za-z0-9_.-]+$"), "Usa solo lettere, numeri, punto, trattino e trattino basso."
|
|
)
|
|
|
|
|
|
class MinimumLengthValidator:
|
|
def validate(self, password, user=None):
|
|
if len(password) < MIN_PASSWORD_LENGTH:
|
|
raise ValidationError(f"La password deve avere almeno {MIN_PASSWORD_LENGTH} caratteri.",
|
|
code="password_too_short")
|
|
|
|
def get_help_text(self):
|
|
return f"Almeno {MIN_PASSWORD_LENGTH} caratteri."
|
|
|
|
|
|
class MaximumLengthValidator:
|
|
def validate(self, password, user=None):
|
|
if len(password) > MAX_PASSWORD_LENGTH:
|
|
raise ValidationError(f"La password può avere al massimo {MAX_PASSWORD_LENGTH} caratteri.",
|
|
code="password_too_long")
|
|
|
|
def get_help_text(self):
|
|
return f"Al massimo {MAX_PASSWORD_LENGTH} caratteri."
|