Files
faunosteo/.claude/audits/2026-10-28-v1-2-1-m7/m7-read.mjs

67 lines
4.0 KiB
JavaScript

// M7 rehearsal, after M4+M5: reads the historical drawing object through the
// Storage API with real GoTrue tokens (sandbox only, ports 57xxx). An editor at
// aal2 must get the exact bytes; a catalogue reader and an anonymous caller get
// nothing (the historical sheet has no published version). Prints one JSON line.
// Keys come from the environment (the sandbox secrets file), never argv.
import { createHash, createHmac } from 'node:crypto'
import { execFileSync } from 'node:child_process'
const AUTH = 'http://127.0.0.1:57999'
const STORAGE = 'http://127.0.0.1:57500'
const KEY = process.argv[2]
const anon = process.env.SB_ANON_KEY
const service = process.env.SB_SERVICE_KEY
const psql = (script) => execFileSync('docker', ['exec', '-i', '-e', 'PGPASSWORD', 'faunosteo_dbsandbox_db', 'psql', '-X', '-q', '-t', '-A',
'-h', '127.0.0.1', '-U', 'postgres', '-d', 'postgres', '-v', 'ON_ERROR_STOP=1'],
{ input: script, env: { ...process.env, PGPASSWORD: process.env.SB_PW } }).toString().trim()
const json = async (res) => { const t = await res.text(); try { return JSON.parse(t) } catch { return t } }
const auth = (path, body, bearer = anon) => fetch(AUTH + path, { method: 'POST',
headers: { apikey: anon, Authorization: `Bearer ${bearer}`, 'Content-Type': 'application/json' }, body: JSON.stringify(body) })
function base32(text) {
const A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; const out = []; let buf = 0; let bits = 0
for (const c of text.replace(/=+$/, '').toUpperCase()) { buf = (buf << 5) | A.indexOf(c); bits += 5; if (bits >= 8) { bits -= 8; out.push((buf >> bits) & 0xff) } }
return Buffer.from(out)
}
function totp(secret) {
const counter = Buffer.alloc(8); counter.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 30000)))
const mac = createHmac('sha1', base32(secret)).update(counter).digest(); const o = mac[mac.length - 1] & 0x0f
return ((mac.readUInt32BE(o) & 0x7fffffff) % 1_000_000).toString().padStart(6, '0')
}
async function account(tag) {
const email = `m7-${tag}-${Date.now()}@sandbox.local`
const user = await json(await auth('/admin/users', { email, password: 'Sandbox-only-pw-1', email_confirm: true }, service))
const session = await json(await auth('/token?grant_type=password', { email, password: 'Sandbox-only-pw-1' }))
return { id: user.id, token: session.access_token }
}
async function stepUp(token) {
const call = (method, path, body) => fetch(AUTH + path, { method, headers: { apikey: anon, Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify(body) }).then(json)
const factor = await call('POST', '/factors', { factor_type: 'totp', friendly_name: 'm7' })
const challenge = await call('POST', `/factors/${factor.id}/challenge`, {})
const verified = await call('POST', `/factors/${factor.id}/verify`, { challenge_id: challenge.id, code: totp(factor.totp.secret) })
return verified.access_token
}
const read = async (token) => {
const res = await fetch(`${STORAGE}/object/authenticated/anatomical-sheet-assets/${KEY}`, { headers: token ? { Authorization: `Bearer ${token}` } : {} })
const bytes = Buffer.from(await res.arrayBuffer())
return { status: res.status, bytes: res.ok ? bytes.length : 0, sha256: res.ok ? createHash('sha256').update(bytes).digest('hex') : null }
}
const editor = await account('editor')
const editorAal2 = await stepUp(editor.token)
psql(`select private.assign_platform_role(null, '${editor.id}', 'admin');
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by)
values ('${editor.id}', 'sheet_editor', 'm7 rehearsal', 'sandbox', '${editor.id}');`)
const reader = await account('reader')
psql(`insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, granted_by)
values ('${reader.id}', 'catalog_reader', 'm7 rehearsal', '${editor.id}');`)
const result = {
editor_aal2: await read(editorAal2),
editor_aal1: await read(editor.token),
reader: await read(reader.token),
anonymous: await read(null),
}
console.log(JSON.stringify(result))