Files
stackpol/iapswitch.js
T
martbost ffa0d74c70 My Stack POL: network sign-in through the hub + signed-in member area at /my (Marty approved 2026-11-19)
- networksso.js (shared file, same copy as IAP/LinkSpin/YPB): hub-only sign-in (HUB_SSO=on), silent check on /my, sign-out
- my.js: signed session cookie (keyed off HUB_SECRET), sign-outs in the my-signouts.json MySQL document; /my shows the page
  link + on/off, Pro, join links per program, share kit, own payout rows with Polygonscan links and totals from the ledger
- iapswitch.js: the page switch stays in IAP's stack-optin.json; Stack POL calls IAP POST /api/partner/stackpol/switch
  (signed, STACKPOL_SWITCH_KEY; spec in the file). Until IAP has it, /my links to IAP Promo tools
- tools/indexer.py: snapshot gains `my` (every paid member's rows, wallets, page flag, IAP switches, Pro list); public data unchanged
- header Sign in / My Stack POL entry; robots keeps /my and /sso/ out
- qa/my.test.mjs: 66 checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 06:57:11 -06:00

53 lines
3.9 KiB
JavaScript

// The Stack POL page switch, read and written from stackpol.com/my (Marty approved the My Stack POL area 2026-11-19).
//
// ONE PLACE FOR THE SWITCH. The switch lives in InstantAdPay: its document stack-optin.json { users: { username: true|false },
// pro: { username: true } } (an IAP MySQL document), written by IAP's Promo tools > Stack POL toggle (POST /api/my/stackpol). The
// Stack POL indexer reads that document every 5 minutes. Stack POL never keeps a second copy: a switch made here is sent to IAP, which
// writes the same document with the same code, so the two can never disagree.
//
// THE INTERFACE IAP NEEDS (not built in IAP yet; until it is, configured() is false and /my links to IAP's Promo tools instead):
// POST <IAP_URL>/api/partner/stackpol/switch
// Headers: Content-Type: application/json
// X-Stack-Ts: milliseconds since 1970 (IAP refuses anything more than 5 minutes from its own clock)
// X-Stack-Sig: hex HMAC-SHA256(STACKPOL_SWITCH_KEY, X-Stack-Ts + "\n" + raw body)
// Body: { "email": "<the member's email, from their network sign-in>" } read only
// { "email": "...", "on": true|false } switch it
// IAP does: verify the signature and the time (timingSafeEqual), find the account by email (accounts.byEmail), refuse when there is
// none (404) or it has no username (409), then run the SAME code as /api/my/stackpol: so.users[un] = !!on when `on` is given, refresh
// so.pro[un] from gamepass.stackProView(email), write stack-optin.json atomically (tmp + rename), and answer
// 200 { "on": so.users[un] !== false, "username": un, "pro": !!pv.pro }.
// The secret is a new env on BOTH apps, the same value: STACKPOL_SWITCH_KEY (32+ characters). IAP_URL on Stack POL is optional
// (default https://instantadpay.com).
// Only Stack POL can call it (the key), only for the member signed in on Stack POL (the email comes from their verified hub token),
// and it changes nothing but the page switch.
'use strict';
const crypto = require('crypto');
const KEY = () => String(process.env.STACKPOL_SWITCH_KEY || '').trim();
const BASE = () => String(process.env.IAP_URL || 'https://instantadpay.com').trim().replace(/\/+$/, '');
const ROUTE = '/api/partner/stackpol/switch';
const configured = () => KEY().length >= 32 && /^https?:\/\/[a-z0-9.:-]+$/i.test(BASE());
const sign = (ts, raw) => crypto.createHmac('sha256', KEY()).update(ts + '\n' + raw).digest('hex');
// -> { ok: true, on, username, pro } or { ok: false, reason }
async function call(body, ms) {
if (!configured()) return { ok: false, reason: 'off' };
if (typeof fetch !== 'function') return { ok: false, reason: 'no fetch' };
const raw = JSON.stringify(body); const ts = String(Date.now());
const ac = new AbortController(); const tm = setTimeout(() => ac.abort(), ms || 3000);
try {
const r = await fetch(BASE() + ROUTE, { method: 'POST', body: raw, signal: ac.signal, headers: { 'Content-Type': 'application/json', 'X-Stack-Ts': ts, 'X-Stack-Sig': sign(ts, raw), 'User-Agent': 'stackpol-my' } });
let j = null; try { j = await r.json(); } catch (e) {}
if (r.status !== 200 || !j || typeof j.on !== 'boolean') return { ok: false, reason: 'iap ' + r.status + (j && j.error ? ': ' + String(j.error).slice(0, 120) : '') };
const un = String(j.username || '').toLowerCase();
return { ok: true, on: j.on, username: /^[a-z0-9_]{3,30}$/.test(un) ? un : null, pro: !!j.pro };
} catch (e) { return { ok: false, reason: 'iap unreachable' }; }
finally { clearTimeout(tm); }
}
const get = email => call({ email: String(email || '').toLowerCase() }, 2500);
const set = (email, on) => call({ email: String(email || '').toLowerCase(), on: !!on }, 5000);
// the deep link to IAP's own toggle (always works, needs no key)
const promoUrl = () => BASE() + '/my#promo?s=stackpol';
module.exports = { configured, get, set, promoUrl, sign, ROUTE };