Files
instantadpay/qa/forumsso.mjs
T

73 lines
8.1 KiB
JavaScript

// Earners Corner forum sign-in (DiscourseConnect, 2026-11-10), end to end against a LOCAL copy. node qa/forumsso.mjs
// Boots server.js on its own port with a throwaway DATA_DIR (JSON store, devCode sign-in). Nothing touches production.
// Pro is NEW MONEY only: "Get Pro" opens a waiting order; only a package of $10 or more, mined after the order, by the member's own
// position, fulfils it; one package fulfils one order (a pass and Pro never ride the same purchase); credits never count.
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { spawn } from 'node:child_process';
import crypto from 'node:crypto';
const SECRET = 'x'.repeat(40);
const sig = s => crypto.createHmac('sha256', SECRET).update(s).digest('hex');
const SITE = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..');
const PORT = Number(process.env.FS_PORT || 8804);
const B = 'http://127.0.0.1:' + PORT;
const DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'iap-forumsso-')); fs.mkdirSync(path.join(DATA, 'uploads'));
let fails = 0; const ok = (cond, label, extra) => { console.log((cond ? 'PASS ' : 'FAIL ') + label + (extra ? ' ' + extra : '')); if (!cond) fails++; };
const sleep = ms => new Promise(r => setTimeout(r, ms));
const srv = spawn(process.execPath, ['server.js'], { cwd: SITE, stdio: ['ignore', 'pipe', 'pipe'],
env: Object.assign({}, process.env, { PORT: String(PORT), DATA_DIR: DATA, NODE_ENV: 'development', ADMIN_EMAIL: 'qa-admin@example.com', ADMIN_PASSWORD: 'localtest', DEV_POL_USD: '0.10', FORUM_SSO_SECRET: SECRET, FORUM_URL: 'https://earnerscorner.com' }) });
let log = ''; srv.stdout.on('data', d => log += d); srv.stderr.on('data', d => log += d);
async function api(p, o, headers) {
const opts = { method: (o && 'data' in o) ? 'POST' : 'GET', headers: Object.assign({ 'Content-Type': 'application/json' }, headers || {}) };
if (o && 'data' in o) opts.body = JSON.stringify(o.data);
const r = await fetch(B + p, opts); let j = null; try { j = await r.json(); } catch (e) {} return { status: r.status, j };
}
async function signIn(email, username) {
const s = await api('/api/auth/email/start', { data: { email, fts: Date.now() - 5000 } });
const v = await fetch(B + '/api/auth/email/verify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, code: s.j && s.j.devCode }) });
const cookie = (v.headers.getSetCookie ? v.headers.getSetCookie() : [v.headers.get('set-cookie')]).map(c => String(c).split(';')[0]).join('; ');
if (username) await api('/api/my/profile', { data: { username } }, { Cookie: cookie });
return { Cookie: cookie };
}
const buy = (email, cents, extra) => api('/api/dev/gamepass-purchase', { data: Object.assign({ email, cents }, extra || {}) });
const optin = () => { try { return JSON.parse(fs.readFileSync(path.join(DATA, 'stack-optin.json'), 'utf8')); } catch (e) { return {}; } };
try {
for (let i = 0; i < 40; i++) { try { if ((await fetch(B + '/api/config')).ok) break; } catch (e) {} await sleep(500); }
const sso = Buffer.from('nonce=abc123&return_sso_url=' + encodeURIComponent('https://earnerscorner.com/session/sso_login')).toString('base64');
const get = (path, headers) => fetch(B + path, { redirect: 'manual', headers: headers || {} });
let r = await get('/forum/sso?sso=' + encodeURIComponent(sso) + '&sig=' + '0'.repeat(64)); ok(r.status === 302 && r.headers.get('location') === 'https://earnerscorner.com/', 'bad signature -> back to the forum, nothing signed');
const evil = Buffer.from('nonce=n&return_sso_url=' + encodeURIComponent('https://evil.example/x')).toString('base64');
r = await get('/forum/sso?sso=' + encodeURIComponent(evil) + '&sig=' + sig(evil)); ok(r.status === 302 && r.headers.get('location') === 'https://earnerscorner.com/', 'return URL off the forum is refused');
r = await get('/forum/sso?sso=' + encodeURIComponent(sso) + '&sig=' + sig(sso));
const ck = (r.headers.getSetCookie ? r.headers.getSetCookie() : []).map(c => c.split(';')[0]);
ok(r.status === 302 && r.headers.get('location') === '/my' && ck.some(c => c === 'iap.return=forum') && ck.some(c => c.startsWith('iap.forumsso=')), 'signed out -> /my with the request kept in a cookie');
const h = await signIn('fs-a@example.com', 'forumfan');
const me = await api('/api/me', null, { Cookie: h.Cookie + '; ' + ck.join('; ') }); ok(me.j && me.j.returnTo === 'forum', '/api/me tells /my to hand back to the forum', JSON.stringify(me.j && me.j.returnTo));
r = await get('/api/my/forum', { Cookie: h.Cookie + '; ' + ck.filter(c => c.startsWith('iap.forumsso=')).join('; ') });
const loc = r.headers.get('location') || ''; ok(r.status === 302 && loc.startsWith('https://earnerscorner.com/session/sso_login?sso='), 'signed in -> back to the forum with a payload', loc.slice(0, 80));
const q = new URL(loc).searchParams, out = q.get('sso');
ok(q.get('sig') === sig(out), 'payload is signed with the shared secret');
const pl = new URLSearchParams(Buffer.from(out, 'base64').toString('utf8'));
ok(pl.get('nonce') === 'abc123' && pl.get('email') === 'fs-a@example.com' && pl.get('external_id') === 'fs-a@example.com' && pl.get('username') === 'forumfan', 'payload: nonce, email, external id, username', pl.toString().slice(0, 200));
ok(pl.get('website') === 'https://stackpol.com/forumfan' && (pl.get('remove_groups') || '').includes('network_members'), 'free member: no network_members group, profile links their Stack POL page');
r = await get('/forum/sso?sso=' + encodeURIComponent(sso) + '&sig=' + sig(sso), { Cookie: h.Cookie }); ok(r.status === 302 && (r.headers.get('location') || '').startsWith('https://earnerscorner.com/session/sso_login?sso='), 'already signed in -> straight back');
// invite link + shared-thread credit (2026-11-10)
r = await get('/forum/join/forumfan?to=' + encodeURIComponent('/t/some-thread/23?u=forumfan'));
const ick = (r.headers.getSetCookie ? r.headers.getSetCookie() : []).map(c => c.split(';')[0]);
ok(r.status === 302 && r.headers.get('location') === 'https://earnerscorner.com/t/some-thread/23?u=forumfan' && ick.includes('iap.sponsor=forumfan'), 'invite link sets the member as sponsor and forwards to the thread', JSON.stringify(ick));
r = await get('/forum/join/nosuchperson'); ok(r.status === 302 && !(r.headers.getSetCookie ? r.headers.getSetCookie() : []).some(c => c.startsWith('iap.sponsor=')), 'unknown username: forwards, sets no sponsor');
r = await get('/forum/join/forumfan?to=' + encodeURIComponent('//evil.example/x')); ok(r.headers.get('location') === 'https://earnerscorner.com/', 'off-site ?to= is refused');
const inv = await fetch(B + '/forum/invite', { headers: { Cookie: h.Cookie } }); const ih = await inv.text();
ok(inv.status === 200 && ih.includes('https://instantadpay.com/forum/join/forumfan'), 'invite page shows the member their link');
const nb = await fetch(B + '/api/auth/email/start', { method: 'POST', headers: { 'Content-Type': 'application/json', Cookie: 'iap.sponsor=forumfan' }, body: JSON.stringify({ email: 'fs-new@example.com', fts: Date.now() - 5000 }) }); const nbj = await nb.json();
const nv = await fetch(B + '/api/auth/email/verify', { method: 'POST', headers: { 'Content-Type': 'application/json', Cookie: 'iap.sponsor=forumfan' }, body: JSON.stringify({ email: 'fs-new@example.com', code: nbj.devCode }) });
const nck = (nv.headers.getSetCookie ? nv.headers.getSetCookie() : []).map(c => c.split(';')[0]).join('; ');
const nme = await api('/api/me', null, { Cookie: nck + '; iap.sponsor=forumfan' });
ok(nme.j && (nme.j.sponsorName === '@forumfan' || String(nme.j.sponsorRef || '').toLowerCase() === 'forumfan' || JSON.stringify(nme.j).includes('forumfan')), 'a new sign-up through the forum invite lands under the inviter', JSON.stringify(nme.j && { sponsorName: nme.j.sponsorName, sponsorRef: nme.j.sponsorRef }));
} catch (e) { console.log('FAIL exception', e && e.stack || e); fails++; }
finally { try { srv.kill(); } catch (e) {} try { fs.rmSync(DATA, { recursive: true, force: true }); } catch (e) {} }
if (fails) console.log(log.split('\n').filter(l => /forum|error/i.test(l)).slice(-15).join('\n'));
console.log(fails ? '\n' + fails + ' FAILED' : '\nALL PASSED'); process.exit(fails ? 1 : 0);