// 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 }