feat(backend): support access by offline challenge codes replaces the master account

Platform v2 step 1.10 (APPS_PLATFORM D-16, section 6.5).
- POST /api/auth/support/challenge (public): an 8-character challenge, stored 15 min
  (support_challenges, revision 0009; at most 5 open). POST /api/auth/support/verify
  {challenge, code}: code = base32(HMAC(SUPPORT_SECRET, site_id + challenge))[:10];
  single use, 5 wrong codes burn the challenge. Opens a 4 h support session
  (client=support, synthetic superadmin) with a hard end: sessions.expires_at.
- core/support_codes.py (stdlib only) is the derivation; tools/support_code.py
  (--init, --site-secret <SITE_ID>, <SITE_ID> <CHALLENGE>) uses it on the builder's
  machine with the master key in %USERPROFILE%/.xenia/support_master_key.txt.
- MASTER_USERNAME/MASTER_PASSWORD removed (config, login, docker-compose.yml,
  .env.example); SUPPORT_SECRET added; install.sh asks for it.
- e2e: backends get SUPPORT_SECRET from a test master key and log in with challenge
  codes (lib/support.mjs, license_e2e.py); install.mjs expects the new prompt.
- Pack rule CS-15.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 10:52:16 +02:00
co-authored by Claude Opus 5.5
parent 650fee7873
commit 3a522e457d
23 changed files with 517 additions and 52 deletions
+9 -5
View File
@@ -44,6 +44,7 @@ class Active(NamedTuple):
"""What validate() knows about an active session."""
user_id: Optional[int] # None = the support account
device_id: Optional[int] # set for terminal sessions: the device they belong to
expires_at: Optional[object] = None # hard end (support sessions); None = none
_cache: dict[str, tuple[float, Active]] = {} # sid -> (checked_at monotonic, info) for ACTIVE sessions
@@ -61,8 +62,8 @@ def single_session_rule(db, client: str) -> str:
# ── lifecycle ────────────────────────────────────────────────────────────────
def start(db, user, client: str, request=None, *, legacy_manager: bool = False, device=None):
"""Create a session for user (None user_id for the support ghost). Commits.
def start(db, user, client: str, request=None, *, legacy_manager: bool = False, device=None, expires_at=None):
"""Create a session for user (None user_id for the support account). Commits.
`device`: the paired device of a terminal login; its previous sessions are replaced
(a shared screen shows one user at a time)."""
from models.session import LoginSession
@@ -86,7 +87,7 @@ def start(db, user, client: str, request=None, *, legacy_manager: bool = False,
_end_row(s, "replaced")
now = utcnow()
row = LoginSession(id=uuid.uuid4().hex, user_id=user_id, client=client, created_at=now, last_seen_at=now,
device_id=(device.id if device is not None else None),
device_id=(device.id if device is not None else None), expires_at=expires_at,
ip=(request.client.host if request is not None and request.client else None),
user_agent=(request.headers.get("user-agent", "")[:300] if request is not None else None))
db.add(row)
@@ -138,7 +139,7 @@ def validate(sid: str | None, db=None) -> Active:
now_m = time.monotonic()
with _lock:
hit = _cache.get(sid)
if hit and now_m - hit[0] < CACHE_TTL_S:
if hit and now_m - hit[0] < CACHE_TTL_S and not (hit[1].expires_at and hit[1].expires_at <= utcnow()):
return hit[1]
from models.session import LoginSession
own = db is None
@@ -147,11 +148,14 @@ def validate(sid: str | None, db=None) -> Active:
db = SessionLocal()
try:
row = db.get(LoginSession, sid)
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()
if row is None or row.ended_at is not None:
with _lock:
_cache.pop(sid, None)
raise XeniaError("auth.session_ended")
info = Active(row.user_id, row.device_id)
info = Active(row.user_id, row.device_id, row.expires_at)
stale = utcnow() - row.last_seen_at > timedelta(seconds=LAST_SEEN_EVERY_S)
finally:
if own: