• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

Django’s Authentication System (django.contrib.auth) provides a complete user management framework including the User model (or custom user model), password hashing (PBKDF2 by default), session-based authentication, login/logout views, permission system (model-level and object-level), groups, and decorators/mixins for access control.

Explanation

The auth system solves the problem of securely managing user identity, credentials, and access control in web applications. It handles user registration, login, logout, password reset/change, session management, and authorization through permissions and groups. The system is built on a swappable User model (default: AbstractUser), allowing customization while maintaining compatibility with admin, forms, and third-party packages.

How It Works

  1. User modelAUTH_USER_MODEL points to user class (default auth.User or custom)
  2. Password hashingset_password() uses PBKDF2+SHA256; check_password() verifies
  3. Login flowauthenticate(username, password)login(request, user) → sets session
  4. Session storage — User ID stored in session; request.user populated by AuthenticationMiddleware
  5. Permission checksuser.has_perm('app.action_model') or @permission_required decorator
  6. Groups — Users inherit permissions from groups; user.groups.add(group)

Visual Explanation

auth_system UserModel User Model AbstractUser / Custom (username, email, password) PasswordHash PBKDF2+SHA256 set_password() / check_password() UserModel->PasswordHash 1. Store/verify Authenticate authenticate() → User or None UserModel->Authenticate 2. Credentials Groups Groups permissions inherited UserModel->Groups 6. Membership Login login(request, user) → Session['_auth_user_id'] Authenticate->Login 3. Success Middleware AuthenticationMiddleware request.user = LazyUser() Login->Middleware 4. Session set Permissions user.has_perm() user.has_perms() @permission_required Middleware->Permissions 5. request.user Groups->Permissions 7. Inherited perms

Semantic Network

semantic_auth_system THIS Authentication System PRE1 User Model (AbstractUser) THIS--PRE1 built from PRE2 Session Framework THIS--PRE2 built from PRE3 Password Hashers THIS--PRE3 built from PRE4 Permissions Framework THIS--PRE4 built from OUT1 Login/Logout Views THIS--OUT1 builds into OUT2 Password Reset/Change THIS--OUT2 builds into OUT3 Decorators /@login_required THIS--OUT3 builds into OUT4 Custom User Model THIS--OUT4 builds into OUT5 DRF Authentication THIS--OUT5 builds into CON1 Flask-Login (Extension) THIS--CON1 contrasts with CON2 FastAPI Dependencies THIS--CON2 contrasts with CON3 NextAuth.js (Client-side) THIS--CON3 contrasts with REL1 CSRF Protection THIS--REL1 related REL2 Admin Panel THIS--REL2 related

Key Properties

  • Swappable User model: AUTH_USER_MODEL = 'myapp.CustomUser' — must set before first migration
  • Password hashers: PASSWORD_HASHERS setting; PBKDF2 default; supports bcrypt, argon2, scrypt
  • Session backend: Database, cache, file, or signed cookies; SESSION_ENGINE setting
  • Permissions: add, change, delete, view auto-created per model; custom via Meta.permissions
  • Object-level permissions: Not built-in; use django-guardian or custom has_perm override
  • Auth backends: AUTHENTICATION_BACKENDSModelBackend default; can add LDAP, OAuth, etc.

Connections

Edge Cases & Gotchas

  • Custom user model timing: Must set AUTH_USER_MODEL before any migrations; changing later is extremely difficult
  • username vs email: Default User requires unique username; email not unique by default — customize for email-as-username
  • Session fixation: login() rotates session key; SESSION_COOKIE_HTTPONLY, SECURE should be True in prod
  • Permission caching: user.get_all_permissions() caches; user.has_perm() uses cache; user = User.objects.get(...) refreshes
  • is_active flag: Inactive users can’t login; authenticate() returns None for inactive users