// ServerBike Reseller Public API: BRIDGE-HMAC-SHA256-V1 signer and minimal client (Node.js 18+, no dependencies). // Usage: node serverbike_sign.mjs vectors ../vectors.json (checks the known-answer vectors) // SB_API_BASE=... SB_CREDENTIAL_ID=... SB_SECRET=... node serverbike_sign.mjs get /api/v1/auth/verify import { createHash, createHmac, randomBytes } from 'node:crypto'; import { readFileSync } from 'node:fs'; import https from 'node:https'; export function decodeSecret(wire) { if (!/^[A-Za-z0-9_-]{43}$/.test(wire)) throw new Error('secret must be 43 unpadded base64url characters'); const raw = Buffer.from(wire, 'base64url'); if (raw.length !== 32 || raw.toString('base64url') !== wire) throw new Error('non-canonical secret'); return raw; } export function canonicalMessage(timestamp, nonce, method, rawTarget, body) { const i = rawTarget.indexOf('?'); const path = i < 0 ? rawTarget : rawTarget.slice(0, i); const query = i < 0 ? '-' : '?' + rawTarget.slice(i + 1); const bodyHash = createHash('sha256').update(body).digest('hex'); return ['BRIDGE-HMAC-SHA256-V1', 'reseller-public', timestamp, nonce, method, path, query, bodyHash].join('\n'); } export function sign(secret, credentialId, method, rawTarget, body, timestamp, nonce) { timestamp = timestamp ?? String(Math.floor(Date.now() / 1000)); nonce = nonce ?? randomBytes(16).toString('base64url'); const mac = createHmac('sha256', secret).update(canonicalMessage(timestamp, nonce, method, rawTarget, body)).digest('hex'); return { 'X-Reseller-Credential-Id': credentialId, 'X-Request-Timestamp': timestamp, 'X-Request-Nonce': nonce, 'X-Request-Signature': 'v1=' + mac }; } export function request(method, rawTarget, bodyText = '') { const base = new URL(process.env.SB_API_BASE); const body = Buffer.from(bodyText, 'utf8'); const headers = { ...sign(decodeSecret(process.env.SB_SECRET), process.env.SB_CREDENTIAL_ID, method, rawTarget, body), Accept: 'application/json', 'Accept-Encoding': 'identity' }; if (body.length) headers['Content-Type'] = 'application/json'; return new Promise((resolve, reject) => { // path is passed verbatim: the signed raw target is exactly what goes on the wire. const req = https.request({ host: base.hostname, port: base.port || 443, method, path: rawTarget, headers, minVersion: 'TLSv1.2', timeout: 20000 }, (res) => { const chunks = []; res.on('data', (c) => chunks.push(c)); res.on('end', () => resolve({ status: res.statusCode, body: Buffer.concat(chunks).toString('utf8') })); }); req.on('timeout', () => req.destroy(new Error('timeout: outcome unknown for a POST, look it up'))); req.on('error', reject); if (body.length) req.write(body); req.end(); }); } if (process.argv[1] && process.argv[1].endsWith('serverbike_sign.mjs')) { const [cmd, a, b] = process.argv.slice(2); if (cmd === 'vectors') { let ok = 0; for (const v of JSON.parse(readFileSync(a, 'utf8'))) { const h = sign(decodeSecret(v.secret_wire), v.credential_id, v.method, v.target, Buffer.from(v.body, 'utf8'), v.timestamp, v.nonce); if (h['X-Request-Signature'] !== v.signature || canonicalMessage(v.timestamp, v.nonce, v.method, v.target, Buffer.from(v.body)) !== v.canonical_message) { console.error('MISMATCH', v.method, v.target); process.exit(1); } ok++; } console.log(`node: ${ok} vectors OK`); } else if (cmd === 'get') { console.log(await request('GET', a)); } else if (cmd === 'post') { console.log(await request('POST', a, b ?? '')); } else { console.error('usage: vectors | get | post '); process.exit(2); } }