diff --git a/server.js b/server.js index 25ee63a..ec72245 100644 --- a/server.js +++ b/server.js @@ -5570,6 +5570,27 @@ const server = http.createServer(async (req, res) => { if (req.method === 'POST' || !!so.pro[un] !== !!pv.pro || pv.pro) { so.updatedAt = Date.now(); const tmp = F + '.tmp'; fs.writeFileSync(tmp, JSON.stringify(so)); fs.renameSync(tmp, F); } return json(res, 200, { on: so.users[un] !== false, username: un, url: 'https://stackpol.com/' + un, wallet: !!(acct && acct.address), pro: pv, buy }); } + // the same switch read and written from stackpol.com/my (Marty 2026-11-19, My Stack POL): signed by Stack POL with STACKPOL_SWITCH_KEY + // (X-Stack-Ts + X-Stack-Sig = HMAC(ts + "\n" + body)), for the member signed in there (email from their hub token). Only the page switch. + if (p === '/api/partner/stackpol/switch' && req.method === 'POST') { + const key = String(process.env.STACKPOL_SWITCH_KEY || ''); + if (key.length < 32) return json(res, 503, { error: 'not configured' }); + let raw; try { raw = (await readRaw(req, 4096)).toString('utf8'); } catch (e) { return json(res, 413, { error: 'too large' }); } + const ts = String(req.headers['x-stack-ts'] || ''); const sig = String(req.headers['x-stack-sig'] || ''); + if (!/^\d{10,16}$/.test(ts) || Math.abs(Date.now() - Number(ts)) > 5 * 60000) return json(res, 401, { error: 'auth' }); + const want = crypto.createHmac('sha256', key).update(ts + '\n' + raw).digest('hex'); + if (sig.length !== want.length || !crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(want))) return json(res, 401, { error: 'auth' }); + let b = {}; try { b = JSON.parse(raw || '{}'); } catch (e) { return json(res, 400, { error: 'bad json' }); } + const acct = await accounts.byEmail(String(b.email || '').trim().toLowerCase()); + if (!acct) return json(res, 404, { error: 'no account' }); + const un = String(acct.username || '').toLowerCase(); + if (!un) return json(res, 409, { error: 'no username' }); + const F = path.join(DATA_DIR, 'stack-optin.json'); let so = { users: {} }; try { so = JSON.parse(fs.readFileSync(F, 'utf8')); if (!so.users) so.users = {}; } catch (e) {} + if (typeof b.on === 'boolean') so.users[un] = b.on; + so.pro = so.pro || {}; const pv = gamepass.stackProView(acct.email); if (pv.pro) so.pro[un] = true; else delete so.pro[un]; + if (typeof b.on === 'boolean' || !!so.pro[un] !== !!pv.pro || pv.pro) { so.updatedAt = Date.now(); const tmp = F + '.tmp'; fs.writeFileSync(tmp, JSON.stringify(so)); fs.renameSync(tmp, F); } + return json(res, 200, { on: so.users[un] !== false, username: un, pro: !!pv.pro }); + } if (p === '/api/my/inbox' && req.method === 'GET') { const s = await auth.fromRequest(req); if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });