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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user