# Sessions 📖 Gestion des sessions utilisateur et tokens d'authentification OAuth2/OIDC ## Diagramme ```kroki-dbml Table users { id uuid [primary key] email varchar(255) [not null, unique] username varchar(50) [not null, unique] } Table devices { id uuid [primary key] user_id uuid [not null, ref: > users.id] device_name varchar(255) device_type varchar(50) } Table sessions { id uuid [primary key] user_id uuid [not null, ref: > users.id] device_id uuid [ref: > devices.id] access_token_hash varchar(64) [not null, note: 'SHA256 hash, never stored in clear'] refresh_token_hash varchar(64) [not null, note: 'SHA256 hash, auto-rotated'] access_token_expires_at timestamp [not null, note: 'Lifetime: 15 minutes'] refresh_token_expires_at timestamp [not null, note: 'Lifetime: 30 days (rolling)'] ip_address inet [not null] user_agent text [not null] city varchar(100) country_code char(2) created_at timestamp [not null, default: `now()`] last_activity_at timestamp [not null, default: `now()`] revoked_at timestamp [note: 'NULL if active, timestamp if manually revoked'] indexes { (user_id, revoked_at) [note: 'Find active sessions for user'] (device_id) (refresh_token_hash) [unique, note: 'Detect replay attacks'] (last_activity_at) [note: 'Auto-cleanup inactive sessions'] } } ``` ## Légende **Durées de vie** : - Access token : **15 minutes** - Refresh token : **30 jours** (rotatif) - Inactivité : Déconnexion automatique après **30 jours** **Sécurité** : - Tokens stockés en **SHA256** (jamais en clair) - Rotation automatique des refresh tokens - Détection replay attack **Multi-device** : - Sessions simultanées **illimitées** - Révocation individuelle ou globale possible - Alertes si connexion depuis nouveau device ou pays différent