feat(backend): sign people out after N days without use (auth.idle_days, default 14)
Platform-v2 step 2.99d, owner decision 2026-11-14. - New core setting auth.idle_days (int, default 14, 0 = never). A manager, waiter or paired-device session unused for that long ends with reason "idle" on its next request (sessions.validate) or when someone opens "Signed in now" (sessions.end_idle in GET /api/auth/sessions). Support keeps its own 4 h expiry. - Manager: Settings -> Sign-in & security -> "Sign people out after" (7/14/30/60/90 days / Never; a value set elsewhere is kept); "Signed in now" names the reason. Texts en/el. - Tests: backend 284 (+ 14-day default, list drops idle ones, setting incl. 0); manager e2e checks the default and saving 30. The support-access check that failed once in ~8 runs now saves a screenshot and the page's alert text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,9 @@ Sessions
|
||||
Results are cached for CACHE_TTL_S; end()/end_for_user() drop the cache entry at once,
|
||||
so an ended session is refused on its very next request.
|
||||
- last_seen_at is written at most every LAST_SEEN_EVERY_S, in its own DB session.
|
||||
- Idle sessions (owner decision 2026-11-14, plan 2.99d): a session unused for
|
||||
auth.idle_days days (default 14, 0 = never) ends with reason "idle" on its next request
|
||||
(validate) or when someone lists sessions (end_idle). Support sessions use expires_at.
|
||||
|
||||
Login modes (settings, declared in modules/core/manifest.py)
|
||||
- auth.manager_login_mode: password | pin (manager in a browser: never tap/none, D-8)
|
||||
@@ -128,6 +131,33 @@ def end_for_device(db, device_id: int, reason: str) -> int:
|
||||
return len(rows)
|
||||
|
||||
|
||||
def idle_days(db) -> int:
|
||||
try:
|
||||
return max(0, int(_setting(db, "auth.idle_days", "14")))
|
||||
except ValueError:
|
||||
return 14
|
||||
|
||||
|
||||
def _idle(row, days: int, now) -> bool:
|
||||
return days > 0 and row.client != "support" and now - row.last_seen_at > timedelta(days=days)
|
||||
|
||||
|
||||
def end_idle(db) -> int:
|
||||
"""End every active session unused for auth.idle_days. Returns how many."""
|
||||
from models.session import LoginSession
|
||||
days = idle_days(db)
|
||||
if not days:
|
||||
return 0
|
||||
now = utcnow()
|
||||
rows = db.query(LoginSession).filter(LoginSession.ended_at.is_(None), LoginSession.client != "support",
|
||||
LoginSession.last_seen_at < now - timedelta(days=days)).all()
|
||||
for r in rows:
|
||||
_end_row(r, "idle")
|
||||
if rows:
|
||||
db.commit()
|
||||
return len(rows)
|
||||
|
||||
|
||||
def validate(sid: str | None, db=None) -> Active:
|
||||
"""(user_id, device_id) of an active session. Raises auth.session_ended."""
|
||||
if not sid:
|
||||
@@ -147,6 +177,9 @@ def validate(sid: str | None, db=None) -> Active:
|
||||
if row is not None and row.ended_at is None and row.expires_at is not None and row.expires_at <= utcnow():
|
||||
_end_row(row, "expired")
|
||||
db.commit()
|
||||
elif row is not None and row.ended_at is None and _idle(row, idle_days(db), utcnow()):
|
||||
_end_row(row, "idle")
|
||||
db.commit()
|
||||
if row is None or row.ended_at is not None:
|
||||
with _lock:
|
||||
_cache.pop(sid, None)
|
||||
|
||||
Reference in New Issue
Block a user