security: prevent re-use of refresh token (reduce surface for any stolen token)
Security: session hardening Refresh token rotation with theft detection (family_id) - Added family_id column to auth.sessions (migration 20260507000000_session_family.sql) grouping all tokens issued from the same login into a family - On refresh, the new session inherits the parent's family_id - If a revoked token is replayed (indicates the token was stolen after rotation), the entire family is immediately invalidated and a warning is logged — forcing re-authentication on all devices SameSite=Strict on refresh cookie - Access cookie stays SameSite=Lax (needed for top-level navigation) - Refresh cookie upgraded to SameSite=Strict — it is only ever used for explicit POST to /api/auth/refresh, never via cross-site navigation Refresh token TTL: 30 days → 7 days - With rotation, active sessions auto-renew and effectively never expire - Inactive sessions expire after 7 days instead of 30, reducing the theft window
This commit is contained in:
@@ -11,6 +11,9 @@ pub struct Session {
|
||||
user_agent: Option<String>,
|
||||
created_at: DateTime<Utc>,
|
||||
revoked: bool,
|
||||
/// Groups all tokens issued from the same original login.
|
||||
/// Replaying a revoked token from this family triggers full-family revocation.
|
||||
family_id: Uuid,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
@@ -20,6 +23,7 @@ impl Session {
|
||||
ip_address: Option<String>,
|
||||
user_agent: Option<String>,
|
||||
expires_in_days: i64,
|
||||
family_id: Uuid,
|
||||
) -> Self {
|
||||
if refresh_token.is_empty() {
|
||||
panic!("Session refresh_token cannot be empty");
|
||||
@@ -35,6 +39,7 @@ impl Session {
|
||||
user_agent,
|
||||
created_at: now,
|
||||
revoked: false,
|
||||
family_id,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +53,7 @@ impl Session {
|
||||
user_agent: Option<String>,
|
||||
created_at: DateTime<Utc>,
|
||||
revoked: bool,
|
||||
family_id: Uuid,
|
||||
) -> Self {
|
||||
Self {
|
||||
id,
|
||||
@@ -58,6 +64,7 @@ impl Session {
|
||||
user_agent,
|
||||
created_at,
|
||||
revoked,
|
||||
family_id,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,4 +108,8 @@ impl Session {
|
||||
pub fn revoke(&mut self) {
|
||||
self.revoked = true;
|
||||
}
|
||||
|
||||
pub fn family_id(&self) -> Uuid {
|
||||
self.family_id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,9 @@ pub trait SessionRepository: Send + Sync + 'static {
|
||||
/// Revokes all sessions for a user
|
||||
async fn revoke_all_user_sessions(&self, user_id: Uuid) -> SessionRepositoryResult<u64>;
|
||||
|
||||
/// Revokes all sessions in a token family (theft response)
|
||||
async fn revoke_session_family(&self, family_id: Uuid) -> SessionRepositoryResult<u64>;
|
||||
|
||||
/// Deletes expired sessions
|
||||
async fn delete_expired_sessions(&self) -> SessionRepositoryResult<u64>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user