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
-2
View File
@@ -18,8 +18,6 @@ mkdirSync(OUT, { recursive: true })
export const PYTHON = process.env.PYTHON || (process.platform === 'win32' ? 'python' : 'python3')
// Playwright browser: system Edge by default; BROWSER_CHANNEL= (empty) uses Playwright's own Chromium
export const BROWSER_CHANNEL = process.env.BROWSER_CHANNEL ?? 'msedge'
// Break-glass login the test backends are started with (never a real account)
export const MASTER = { username: 'e2e', password: 'e2e-pass-123' }
// The Android emulator reaches the host machine at this address
export const EMULATOR_HOST = '10.0.2.2'
+6 -9
View File
@@ -4,7 +4,8 @@
import { execFileSync, spawn } from 'node:child_process'
import { copyFileSync, existsSync, mkdirSync, mkdtempSync, openSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { BACKEND, CS, MASTER, OUT, PYTHON, WAITER, sleep } from './env.mjs'
import { BACKEND, CS, OUT, PYTHON, WAITER, sleep } from './env.mjs'
import { SUPPORT_MASTER, siteSecret, supportLogin } from './support.mjs'
/** Kill a process and its children (npx/shell wrappers on Windows). */
export function killTree(child) {
@@ -66,7 +67,7 @@ export async function startBackend({ port = 8055, siteId = 'test-site-1', siteKe
SITE_ID: siteId, SITE_KEY: siteKey, CLOUD_URL: cloudUrl, SECRET_KEY: 'e2e-secret',
// No cloud = a test backend: licensing off (every module). The license suite passes a cloudUrl.
LICENSE_MODE: cloudUrl ? 'enforced' : 'off',
MASTER_USERNAME: MASTER.username, MASTER_PASSWORD: MASTER.password, ...env,
SUPPORT_SECRET: siteSecret(SUPPORT_MASTER, siteId), ...env,
},
})
const url = `http://127.0.0.1:${port}`
@@ -74,12 +75,8 @@ export async function startBackend({ port = 8055, siteId = 'test-site-1', siteKe
return { url, dataDir, child, stop: async () => { killTree(child); await waitHttp(`${url}/api/system/health`, false).catch(() => {}) } }
}
export async function login(baseUrl) {
const r = await fetch(`${baseUrl}/api/auth/login`, {
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(MASTER),
})
return (await r.json()).access_token
}
/** A superadmin token for a test backend, through support access (challenge code). */
export const login = baseUrl => supportLogin(baseUrl)
/** nginx:alpine serving a waiter build (`dist`) with the waiter's own nginx.conf, /api → host backend. */
export async function serveWaiterBuild({ name, port, dist = path.join(WAITER, 'dist'), backendPort = 8055 }) {
@@ -121,7 +118,7 @@ export function composeStack({ project, version, siteId, http, https, manager, e
writeFileSync(envFile, Object.entries({
REGISTRY: 'e2e', VERSION: version, SITE_ID: siteId, SITE_KEY: 'x', CLOUD_URL: '', SECRET_KEY: 'e2e-secret',
LICENSE_MODE: 'off',
HOST_IP: '10.0.2.2', TLS_PORT: https, HTTP_PORT: http, MASTER_USERNAME: MASTER.username, MASTER_PASSWORD: MASTER.password,
HOST_IP: '10.0.2.2', TLS_PORT: https, HTTP_PORT: http, SUPPORT_SECRET: siteSecret(SUPPORT_MASTER, siteId),
DATA_PATH: fwd(path.join(dir, 'data')), LOGO_PATH: fwd(path.join(dir, 'logo.png')), FISCAL_PATH: fwd(path.join(dir, 'fiscal')),
...extraEnv,
}).map(([k, v]) => `${k}=${v}`).join('\n') + '\n')
+49
View File
@@ -0,0 +1,49 @@
// Support access for the test backends (APPS_PLATFORM D-16): the suites log in the way
// the builder would, with a challenge code. Same algorithm as
// local_backend/core/support_codes.py; local_backend/tests/test_support.py pins a known
// vector (master "test-master-key-0123456789abcdef", site "site-A", challenge
// "ABCD-EFGH" -> "E4MYPVWT3M"), checked here by selfCheck().
import { createHmac } from 'node:crypto'
// The test master key the e2e backends' SUPPORT_SECRET is derived from (never a real key)
export const SUPPORT_MASTER = 'e2e-support-master-key-0000000000'
const B32 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
function base32(buf) {
let bits = 0, value = 0, out = ''
for (const byte of buf) {
value = (value << 8) | byte
bits += 8
while (bits >= 5) { out += B32[(value >>> (bits - 5)) & 31]; bits -= 5 }
}
if (bits > 0) out += B32[(value << (5 - bits)) & 31]
return out
}
export const siteSecret = (master, siteId) =>
createHmac('sha256', master.trim()).update(`xenia-support-site:${siteId}`).digest('hex')
export function codeFor(secret, siteId, challenge) {
const ch = challenge.toUpperCase().replace(/[^A-Z0-9]/g, '')
return base32(createHmac('sha256', secret.trim()).update(`xenia-support:${siteId}:${ch}`).digest()).slice(0, 10)
}
export function selfCheck() {
const s = siteSecret('test-master-key-0123456789abcdef', 'site-A')
if (codeFor(s, 'site-A', 'ABCD-EFGH') !== 'E4MYPVWT3M') throw new Error('support code algorithm drifted from core/support_codes.py')
}
/** A support session on a test backend: challenge -> code -> token. */
export async function supportLogin(baseUrl, master = SUPPORT_MASTER) {
selfCheck()
const c = await (await fetch(`${baseUrl}/api/auth/support/challenge`, { method: 'POST' })).json()
if (!c.challenge) throw new Error(`support challenge failed: ${JSON.stringify(c)}`)
const code = codeFor(siteSecret(master, c.site_id), c.site_id, c.challenge)
const r = await fetch(`${baseUrl}/api/auth/support/verify`, {
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ challenge: c.challenge, code }),
})
const body = await r.json()
if (!body.access_token) throw new Error(`support verify failed: ${JSON.stringify(body)}`)
return body.access_token
}