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
+10 -9
View File
@@ -17,15 +17,16 @@ bearer = HTTPBearer()
TOKEN_EXPIRY_HOURS = 8
MASTER_USER_ID = 0 # sentinel — never a real DB row
SUPPORT_USER_ID = 0 # sentinel — never a real DB row
def _make_ghost_superadmin():
"""Synthetic in-memory superadmin — plain namespace, never touches SQLAlchemy."""
def make_support_user():
"""The support account (D-16): a synthetic superadmin, never a DB row. Its sessions have
client="support" and user_id NULL; it only exists through POST /api/auth/support/verify."""
from types import SimpleNamespace
return SimpleNamespace(
id=MASTER_USER_ID,
username=settings.MASTER_USERNAME,
id=SUPPORT_USER_ID,
username="support",
full_name="Xenia Support",
role="superadmin",
is_active=True,
@@ -44,14 +45,14 @@ def _make_ghost_superadmin():
)
def make_token(user: User, sid: str) -> str:
def make_token(user: User, sid: str, hours: float = TOKEN_EXPIRY_HOURS) -> str:
"""JWT for an existing session row (core.sessions.start). `sid` is what makes it revocable."""
payload = {
"sub": str(user.id),
"sid": sid,
"username": user.username,
"role": user.role,
"exp": utcnow() + timedelta(hours=TOKEN_EXPIRY_HOURS),
"exp": utcnow() + timedelta(hours=hours),
}
return jwt.encode(payload, settings.SECRET_KEY, algorithm="HS256")
@@ -87,9 +88,9 @@ def user_from_token(token: str, request: Request, db: Session):
dev = current_device(request, db)
if dev is None or dev.id != info.device_id:
raise XeniaError("device.required")
if int(payload["sub"]) == MASTER_USER_ID and payload.get("role") == "superadmin":
if info.user_id is None: # only support sessions have no user (core/sessions.py)
set_log_context(user="support")
return _make_ghost_superadmin(), payload.get("sid")
return make_support_user(), payload.get("sid")
user = db.query(User).filter(User.id == int(payload["sub"]), User.is_active == True).first()
if not user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found")