Files
xenia-pos-local/tools/support_code.py
bonaminandClaude Opus 5.5 3a522e457d 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>
2026-11-13 10:52:16 +02:00

64 lines
2.4 KiB
Python

"""Support access codes for Xenia sites (APPS_PLATFORM D-16). Run on the BUILDER's machine.
python tools/support_code.py --init create the master key (once; never overwrites)
python tools/support_code.py --site-secret <SITE_ID> the SUPPORT_SECRET value for that site's .env
python tools/support_code.py <SITE_ID> <CHALLENGE> the response code for a challenge the site shows
The master key lives in %USERPROFILE%\\.xenia\\support_master_key.txt (XENIA_SUPPORT_MASTER_KEY_FILE
overrides the path). It is outside the repo: back it up. Losing it means every site's
SUPPORT_SECRET has to be replaced; leaking it gives support access to every site.
The derivation is local_backend/core/support_codes.py (loaded by path; stdlib only).
"""
from __future__ import annotations
import importlib.util
import os
import secrets
import sys
from pathlib import Path
_HERE = Path(__file__).resolve().parent
_spec = importlib.util.spec_from_file_location("support_codes", _HERE.parent / "local_backend" / "core" / "support_codes.py")
codes = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(codes)
def key_file() -> Path:
return Path(os.environ.get("XENIA_SUPPORT_MASTER_KEY_FILE") or Path.home() / ".xenia" / "support_master_key.txt")
def master_key() -> str:
p = key_file()
if not p.exists():
sys.exit(f"No master key at {p}. Create it once with: python tools/support_code.py --init")
key = p.read_text(encoding="utf-8").strip()
if len(key) < 32:
sys.exit(f"{p} looks wrong (too short).")
return key
def main(argv: list[str]) -> int:
if argv == ["--init"]:
p = key_file()
if p.exists():
print(f"Master key already exists at {p}; not touching it.")
return 0
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(secrets.token_hex(32) + "\n", encoding="utf-8")
print(f"Created {p}. Back it up somewhere safe.")
return 0
if len(argv) == 2 and argv[0] == "--site-secret":
print(codes.site_secret(master_key(), argv[1]))
return 0
if len(argv) == 2 and not argv[0].startswith("-"):
site_id, challenge = argv
secret = codes.site_secret(master_key(), site_id)
print(codes.group(codes.code_for(secret, site_id, challenge), 5))
return 0
print(__doc__)
return 2
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))