Help desk: signed partner API for LinkSpin tickets (LS- refs on the one shared desk)

POST /api/partner/support/{create,list,view,reply,token,token-reply}, signed exactly
like the registry rows (LS_SYNC_SECRET, X-LS-Ts + X-LS-Sig, now one readLsSigned helper
shared with /api/partner/registry). Only platform 'linkspin' tickets are reachable there.

LinkSpin tickets: requester mail, links and footer say LinkSpin support and point at
LS_URL (/support/t/<token>, /help#t=<ref>); no InstantAdPay in-app notice is sent for
them. Telegram ping, admin mail and the support bot's thread/list views mark them
LinkSpin. The member Help pane here lists InstantAdPay tickets only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-16 18:04:52 -05:00
parent 80885705d2
commit 524ce843dc
3 changed files with 81 additions and 35 deletions
+40 -9
View File
@@ -1047,6 +1047,17 @@ function supportGuard(req, b, email) {
supportGlobal.n += 1; supportMark('ip:' + ip); supportMark('em:' + email);
return null;
}
// LinkSpin partner calls (registry rows, help desk): one secret both ways, LS_SYNC_SECRET, as X-LS-Ts (ms) + X-LS-Sig =
// hex HMAC-SHA256(ts + newline + raw body), 10-minute window. Resolves { body } or { status, error }.
async function readLsSigned(req, maxBytes) {
const secret = String(process.env.LS_SYNC_SECRET || '').trim(); if (secret.length < 32) return { status: 503, error: 'sync not configured' };
const raw = await new Promise((ok, no) => { let d = ''; req.on('data', c => { d += c; if (d.length > maxBytes) { req.destroy(); no(new Error('too big')); } }); req.on('end', () => ok(d)); req.on('error', no); });
const ts = String(req.headers['x-ls-ts'] || ''), sig = String(req.headers['x-ls-sig'] || '');
if (!/^\d{13}$/.test(ts) || Math.abs(Date.now() - Number(ts)) > 10 * 60000) return { status: 401, error: 'stale' };
const want = crypto.createHmac('sha256', secret).update(ts + '\n' + raw).digest('hex');
if (want.length !== sig.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(sig))) return { status: 401, error: 'bad signature' };
try { return { body: JSON.parse(raw || '{}') }; } catch (e) { return { status: 400, error: 'bad json' }; }
}
function fraudAlert(email, fc, spAcct, blocked) {
try {
const sc = siteConfig();
@@ -2968,16 +2979,36 @@ const server = http.createServer(async (req, res) => {
// over ts + newline + raw body, 10-minute window). New people become waiting accounts with their LinkSpin line; existing
// accounts keep their sponsor and only get empty fields filled. No mail, no notices, no chain (lsinbound.js).
if (p === '/api/partner/registry' && req.method === 'POST') {
const secret = String(process.env.LS_SYNC_SECRET || '').trim(); if (secret.length < 32) return json(res, 503, { error: 'sync not configured' });
const raw = await new Promise((ok, no) => { let d = ''; req.on('data', c => { d += c; if (d.length > 2e6) { req.destroy(); no(new Error('too big')); } }); req.on('end', () => ok(d)); req.on('error', no); });
const ts = String(req.headers['x-ls-ts'] || ''), sig = String(req.headers['x-ls-sig'] || '');
if (!/^\d{13}$/.test(ts) || Math.abs(Date.now() - Number(ts)) > 10 * 60000) return json(res, 401, { error: 'stale' });
const want = crypto.createHmac('sha256', secret).update(ts + '\n' + raw).digest('hex');
if (want.length !== sig.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(sig))) return json(res, 401, { error: 'bad signature' });
let b; try { b = JSON.parse(raw || '{}'); } catch (e) { return json(res, 400, { error: 'bad json' }); }
const sg = await readLsSigned(req, 2e6); if (!sg.body) return json(res, sg.status, { error: sg.error });
const b = sg.body;
const rows = Array.isArray(b.rows) ? b.rows.slice(0, 500) : [];
return json(res, 200, Object.assign({ ok: true }, await lsinbound.receive(rows)));
}
// -- help desk for LinkSpin (2026-10-16): LinkSpin has no desk of its own; its Help screen, /support page, private ticket
// link and chat hand-off all proxy here, signed like the registry rows. LinkSpin runs the person-facing guards (sign-in,
// honeypot, form age, icon check) and vouches for the email; this side only ever touches platform 'linkspin' tickets
// (LS- refs). Replies to the requester point at LinkSpin (tickets.js BRAND), never an InstantAdPay notice.
m = /^\/api\/partner\/support\/(create|list|view|reply|token|token-reply)$/.exec(p);
if (m && req.method === 'POST') {
const sg = await readLsSigned(req, 64000); if (!sg.body) return json(res, sg.status, { error: sg.error });
const b = sg.body, op = m[1];
const email = String(b.email || '').trim().toLowerCase();
const mine = async () => { const t = await tickets.byRef(b.ref); return t && t.platform === 'linkspin' && t.email === email ? t : null; };
const byTok = async () => { const t = await tickets.byToken(b.token); return t && t.platform === 'linkspin' ? t : null; };
if (op === 'create') {
if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(email)) return json(res, 400, { error: 'That email address does not look right.' });
if (supportFlood('ls:' + email, 8, 3600000)) return json(res, 429, { error: 'You have sent several tickets in the last hour. Add to one of those instead.' }); // backstop behind LinkSpin's own limits
const r = await tickets.create({ platform: 'linkspin', source: ['form', 'public', 'chatbot'].includes(b.source) ? b.source : 'public', email, name: b.name, username: b.username, subject: b.subject, category: b.category, body: b.body });
return json(res, r.error ? 400 : 200, r.error ? r : { ok: true, ref: r.ticket.ref, token: r.token });
}
if (op === 'list') return json(res, 200, { tickets: await tickets.memberList(email, 'linkspin'), unread: await tickets.memberUnread(email, 'linkspin'), categories: tickets.CATEGORIES });
const t = op === 'view' || op === 'reply' ? await mine() : await byTok();
if (!t) return json(res, 404, { error: op.startsWith('token') ? 'This ticket link is not valid. Open the newest email about your ticket, or send a new one.' : 'Ticket not found.' });
if (op === 'view' || op === 'token') return json(res, 200, await tickets.memberView(t));
if (supportFlood('tk:' + t.id, 20, 3600000)) return json(res, 429, { error: 'That is a lot of messages in an hour. Give Marty a moment to catch up.' });
const r = await tickets.addMessage(t.id, { author: 'member', body: b.body, via: op === 'token-reply' ? 'link' : 'web' });
return json(res, r.error ? 400 : 200, r.error ? r : await tickets.memberView(r.ticket));
}
if (p === '/api/admin/lssync/run' && req.method === 'POST') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); const b = await readBody(req); return json(res, 200, await lssync.tick(!!(b && b.force))); }
if (p === '/api/admin/walletnudge/run' && req.method === 'POST') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, await walletnudge.tick()); }
if (p === '/api/admin/tank' && req.method === 'GET') {
@@ -3902,7 +3933,7 @@ const server = http.createServer(async (req, res) => {
if (p === '/api/my/support' && (req.method === 'GET' || req.method === 'POST')) {
const s = await auth.fromRequest(req);
if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
if (req.method === 'GET') return json(res, 200, { tickets: await tickets.memberList(s.email), unread: await tickets.memberUnread(s.email), categories: tickets.CATEGORIES });
if (req.method === 'GET') return json(res, 200, { tickets: await tickets.memberList(s.email, 'iap'), unread: await tickets.memberUnread(s.email, 'iap'), categories: tickets.CATEGORIES }); // LinkSpin tickets live in LinkSpin's Help
const b = await readBody(req);
if (supportFlood('em:' + String(s.email).toLowerCase(), 5, 3600000)) return json(res, 429, { error: 'You have opened several tickets in the last hour. Add to one of those instead.' });
const a = await accounts.byEmail(s.email);
@@ -3914,7 +3945,7 @@ const server = http.createServer(async (req, res) => {
const s = await auth.fromRequest(req);
if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
const t = await tickets.byRef(m[1]);
if (!t || t.email !== String(s.email).toLowerCase()) return json(res, 404, { error: 'Ticket not found.' });
if (!t || t.email !== String(s.email).toLowerCase() || t.platform !== 'iap') return json(res, 404, { error: 'Ticket not found.' });
if (req.method === 'GET') return json(res, 200, await tickets.memberView(t));
const b = await readBody(req);
if (supportFlood('tk:' + t.id, 20, 3600000)) return json(res, 429, { error: 'That is a lot of messages in an hour. Give Marty a moment to catch up.' });