Files
Kochbuch/accounts/tokens.py
T
Kyle DammannandClaude Opus 5.5 ef9deaeff7 Add phase 1: Docker setup, auth with invitations and roles
- Django 5.2 + PostgreSQL 17 via docker compose (app + db, volumes,
  healthcheck on /healthz, test profile running pytest against Postgres)
- Custom e-mail user model with user/admin roles
- Invitation-only registration with single-use, hashed, expiring links
- Admin area: invitations, role changes, deactivation, password reset
  via one-time link or temporary password (forced change on login)
- First admin created from ADMIN_EMAIL/ADMIN_PASSWORD on startup
- Login rate limiting per e-mail and IP (DB-backed, worker-safe)
- Proxy middleware honouring X-Forwarded-* only from TRUSTED_PROXIES,
  optional CLIENT_IP_HEADER (e.g. CF-Connecting-IP)
- Security headers (CSP, HSTS, Permissions-Policy), secure __Host- cookies
- Mobile-first German UI, CLAUDE.md, README, init-env script
- 65 tests for auth, permissions, proxy handling and rate limiting

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 21:24:59 +01:00

19 lines
443 B
Python

"""One-time tokens for invitation and password-reset links.
Only a SHA-256 hash is stored, so a database leak does not expose usable links.
The raw token exists only in the link shown to the admin once.
"""
import hashlib
import secrets
def new_token():
"""Return (raw_token, token_hash)."""
raw = secrets.token_urlsafe(32)
return raw, hash_token(raw)
def hash_token(raw):
return hashlib.sha256(raw.encode()).hexdigest()