| """ |
| IP address and request utilities. |
| |
| Used across all phases for login history, rate limiting, security events. |
| """ |
|
|
|
|
| def get_client_ip(request) -> str: |
| """Extract the real client IP from the request, respecting proxy headers.""" |
| x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR") |
| if x_forwarded_for: |
| return x_forwarded_for.split(",")[0].strip() |
| return request.META.get("REMOTE_ADDR", "") |
|
|
|
|
| def get_user_agent(request) -> str: |
| """Extract User-Agent header from request.""" |
| return request.META.get("HTTP_USER_AGENT", "") |
|
|
|
|
| def get_device_fingerprint(request) -> str: |
| """ |
| Generate a simple device fingerprint from request headers. |
| |
| Combines IP + User-Agent + Accept-Language for basic fingerprinting. |
| """ |
| parts = [ |
| get_client_ip(request), |
| get_user_agent(request), |
| request.META.get("HTTP_ACCEPT_LANGUAGE", ""), |
| ] |
| import hashlib |
| raw = "|".join(parts) |
| return hashlib.sha256(raw.encode()).hexdigest()[:32] |
|
|