From f20e845cf15ac65c7069b82a100155146971349e Mon Sep 17 00:00:00 2001 From: martbost Date: Tue, 10 Nov 2026 06:51:34 -0600 Subject: [PATCH] Earners Corner forum sign-in (Marty 2026-11-10): InstantAdPay is the DiscourseConnect identity provider for earnerscorner.com. /forum/sso verifies Discourse's HMAC-signed request (return URL must be on the forum), a signed-out visitor signs in on /my (request kept in a 30-minute cookie, my.js hands back via /api/my/forum), then a signed payload goes back: email, external id, username, Stack POL page as website, groups network_members (on-chain position) and wallet_linked. Suspended accounts are refused. Env FORUM_SSO_SECRET + FORUM_URL. qa/forumsso.mjs 9/9; member walk 0 bugs; my.js ?v=20261110forum. Co-Authored-By: Claude Opus 5.5 --- forumsso.js | 44 +++++++++++++++++++++++++++++++++ public/assets/my.js | 1 + public/my.html | 2 +- qa/forumsso.mjs | 59 +++++++++++++++++++++++++++++++++++++++++++++ server.js | 15 +++++++++++- 5 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 forumsso.js create mode 100644 qa/forumsso.mjs diff --git a/forumsso.js b/forumsso.js new file mode 100644 index 0000000..3913fdf --- /dev/null +++ b/forumsso.js @@ -0,0 +1,44 @@ +// Earners Corner forum sign-in (Marty 2026-11-10): earnerscorner.com runs Discourse with DiscourseConnect, and InstantAdPay is the +// identity provider, so every forum name is a real InstantAdPay account (no separate forum sign-ups to farm). Discourse sends +// /forum/sso?sso=&sig=; we check the signature, make sure the visitor is signed in here, and send +// back a signed payload with the member's email, username and groups. Secret: env FORUM_SSO_SECRET (the same value is set in +// Discourse as discourse_connect_secret). Groups: network_members (paying member: an on-chain position) and wallet_linked; in +// Discourse those groups grant the trust level that may post links, so link spam needs a real member behind it. +'use strict'; +const crypto = require('crypto'); + +const SECRET = () => String(process.env.FORUM_SSO_SECRET || '').trim(); +const FORUM_URL = () => String(process.env.FORUM_URL || 'https://earnerscorner.com').replace(/\/+$/, ''); +const sign = s => crypto.createHmac('sha256', SECRET()).update(s).digest('hex'); + +// -> { nonce, returnUrl } or { error } +function verify(sso, sig) { + if (SECRET().length < 32) return { error: 'The forum sign-in is not connected yet.' }; + sso = String(sso || ''); sig = String(sig || '').toLowerCase(); + if (!sso || !/^[0-9a-f]{64}$/.test(sig)) return { error: 'That forum sign-in link is incomplete.' }; + const want = Buffer.from(sign(sso), 'hex'), got = Buffer.from(sig, 'hex'); + if (want.length !== got.length || !crypto.timingSafeEqual(want, got)) return { error: 'That forum sign-in link is not valid.' }; + const q = new URLSearchParams(Buffer.from(sso, 'base64').toString('utf8')); + const nonce = q.get('nonce'), returnUrl = q.get('return_sso_url') || ''; + if (!nonce) return { error: 'That forum sign-in link is incomplete.' }; + if (!returnUrl.startsWith(FORUM_URL() + '/')) return { error: 'That forum sign-in link points somewhere else.' }; + return { nonce, returnUrl }; +} + +// the signed redirect back to the forum for this account +function redirectFor(v, acct, site) { + const email = String(acct.email || '').toLowerCase(); + const groups = []; + if (Number(acct.memberId) > 0) groups.push('network_members'); + if (acct.address) groups.push('wallet_linked'); + const p = new URLSearchParams({ nonce: v.nonce, email, external_id: email, require_activation: 'false', suppress_welcome_message: 'false' }); + if (acct.username) { p.set('username', acct.username); p.set('name', acct.username); } + if (acct.avatarUrl) p.set('avatar_url', /^https?:/.test(acct.avatarUrl) ? acct.avatarUrl : site + acct.avatarUrl); + if (acct.username) p.set('website', 'https://stackpol.com/' + acct.username); + if (groups.length) p.set('add_groups', groups.join(',')); + const remove = ['network_members', 'wallet_linked'].filter(g => !groups.includes(g)); if (remove.length) p.set('remove_groups', remove.join(',')); + const out = Buffer.from(p.toString()).toString('base64'); + return v.returnUrl + (v.returnUrl.includes('?') ? '&' : '?') + 'sso=' + encodeURIComponent(out) + '&sig=' + sign(out); +} + +module.exports = { verify, redirectFor, sign, SECRET, FORUM_URL }; diff --git a/public/assets/my.js b/public/assets/my.js index fe4c672..84e986f 100644 --- a/public/assets/my.js +++ b/public/assets/my.js @@ -965,6 +965,7 @@ function shareLink(ref) { return SHARE_DOMAIN && /^[A-Za-z0-9_]{1,40}$/.test(Str if (me.returnTo === 'gkshop' && !render.toGame) { render.toGame = true; location.href = '/gk/shop/resume'; return; } if (me.returnTo === 'spookydoors' && !render.toGame) { render.toGame = true; location.href = '/api/my/spookydoors'; return; } // Spooky Doors sent them here to sign in (2026-10-02) if (me.returnTo === 'polybreak' && !render.toGame) { render.toGame = true; location.href = '/api/my/polybreak'; return; } // PolyBreak sent them here to sign in (2026-10-13) + if (me.returnTo === 'forum' && !render.toGame) { render.toGame = true; location.href = '/api/my/forum'; return; } // Earners Corner forum sign-in (2026-11-10) if (me.returnTo === 'polminer' && !render.toGame) { render.toGame = true; location.href = '/api/my/polminer'; return; } // POL Miner sent them here to sign in (2026-10-28) if (me.returnTo === 'warpwares' && !render.toGame) { render.toGame = true; location.href = '/api/my/warpwares'; return; } // Warp & Wares sent them here to sign in (2026-10-29) if (me.returnTo === 'turkeydrop' && !render.toGame) { render.toGame = true; location.href = '/api/my/turkeydrop'; return; } // Turkey Drop sent them here to sign in (2026-10-28) diff --git a/public/my.html b/public/my.html index bb33a4d..593db4b 100644 --- a/public/my.html +++ b/public/my.html @@ -1291,7 +1291,7 @@ - + diff --git a/qa/forumsso.mjs b/qa/forumsso.mjs new file mode 100644 index 0000000..a9451e0 --- /dev/null +++ b/qa/forumsso.mjs @@ -0,0 +1,59 @@ +// 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'); +} 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); diff --git a/server.js b/server.js index b8af5f7..4519dd8 100644 --- a/server.js +++ b/server.js @@ -16,6 +16,7 @@ const { URL } = require('url'); const chain = require('./chain'); const auth = require('./auth'); const accounts = require('./accounts'); +const forumsso = require('./forumsso'); // Earners Corner forum sign-in (DiscourseConnect, 2026-11-10) const themehead = require('./themehead'); // 2026-11-01 light/dark theme head script for server-rendered pages + its CSP hash const ads = require('./ads'); const mailer = require('./mailer'); @@ -2969,7 +2970,7 @@ const server = http.createServer(async (req, res) => { if (tankHold) sponsorBlocked = 'tankHold'; else if (!sponsorId && memberId !== _defSpon) { sponsorId = _defSpon; sponsorFallback = true; } // orphan fallback → #1 (never self-sponsor) if (memberId && acct && acct.memberId !== memberId) accounts.setMemberId(acct.email, memberId).catch(() => {}); - const out = { signedIn: true, gkShop: !!parseCookies(req)['iap.gkshop'], sdShop: !!parseCookies(req)['iap.sdshop'], pwMint: !!parseCookies(req)['iap.pwmint'], returnTo: ['turkeydrop', 'polhunter', 'driprunner', 'passivedrip', 'gemkingdom', 'gkshop', 'spookydoors', 'sdshop', 'pwmint', 'polybreak', 'polminer', 'warpwares', 'shardsofvalor'].includes(parseCookies(req)['iap.return']) ? parseCookies(req)['iap.return'] : null, sponsorBlocked, sponsorRouted, sponsorName: spdMe.name || null, email: s.email || (acct && acct.email) || null, + const out = { signedIn: true, gkShop: !!parseCookies(req)['iap.gkshop'], sdShop: !!parseCookies(req)['iap.sdshop'], pwMint: !!parseCookies(req)['iap.pwmint'], returnTo: ['turkeydrop', 'polhunter', 'driprunner', 'passivedrip', 'gemkingdom', 'gkshop', 'spookydoors', 'sdshop', 'pwmint', 'polybreak', 'polminer', 'warpwares', 'shardsofvalor', 'forum'].includes(parseCookies(req)['iap.return']) ? parseCookies(req)['iap.return'] : null, sponsorBlocked, sponsorRouted, sponsorName: spdMe.name || null, email: s.email || (acct && acct.email) || null, address: s.address || (acct && acct.address) || null, memberId, username: (acct && acct.username) || null, theme: (acct && acct.theme) || null, inviteAsk: !memberId && acct ? tank.inviteAsk(acct) : null, // the Who invited you card (2026-11-07) @@ -3932,6 +3933,18 @@ const server = http.createServer(async (req, res) => { const tok = wwMint({ memberId: Number((acct && acct.memberId) || s.memberId || 0), email: String(s.email).toLowerCase(), username: (acct && acct.username) || null, wallet: (acct && acct.address) || null, sponsorRef: (acct && acct.sponsorRef) || null }); res.writeHead(302, { Location: WW_URL() + '/auth?t=' + tok, 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.return', '', 0) }); return res.end(); } + // -- Earners Corner forum sign-in (2026-11-10): Discourse sends /forum/sso; a signed-out visitor signs in on /my first (the request + // rides in a 30-minute cookie) and /api/my/forum finishes the hand-back. See forumsso.js. + if ((p === '/forum/sso' || p === '/api/my/forum') && req.method === 'GET') { + const raw = p === '/forum/sso' ? { sso: u.searchParams.get('sso'), sig: u.searchParams.get('sig') } : (() => { const c = String(parseCookies(req)['iap.forumsso'] || ''); const i = c.lastIndexOf('.'); return { sso: c.slice(0, i), sig: c.slice(i + 1) }; })(); + const v = forumsso.verify(raw.sso, raw.sig); + if (v.error) { res.writeHead(302, { Location: forumsso.FORUM_URL() + '/', 'Cache-Control': 'no-store', 'Set-Cookie': gkCookie('iap.forumsso', '', 0) }); return res.end(); } + const s = await auth.fromRequest(req); + if (!s || !s.email) { res.writeHead(302, { Location: '/my', 'Cache-Control': 'no-store', 'Set-Cookie': [gkCookie('iap.return', 'forum', 1800), gkCookie('iap.forumsso', raw.sso + '.' + raw.sig, 1800)] }); return res.end(); } + const acct = (await accounts.byEmail(s.email)) || { email: s.email }; + if (fraud.isSuspended(s.email)) { res.writeHead(302, { Location: forumsso.FORUM_URL() + '/', 'Cache-Control': 'no-store' }); return res.end(); } + res.writeHead(302, { Location: forumsso.redirectFor(v, Object.assign({ email: s.email }, acct), 'https://instantadpay.com'), 'Cache-Control': 'no-store', 'Set-Cookie': [gkCookie('iap.return', '', 0), gkCookie('iap.forumsso', '', 0)] }); return res.end(); + } if (p === '/api/my/polminer' && req.method === 'GET') { gamestats.handoff('polminer'); const s = await auth.fromRequest(req); if (!s || !s.email) { res.writeHead(302, { Location: '/my' + refQ(u), 'Set-Cookie': gkCookie('iap.return', 'polminer', 1800), 'Cache-Control': 'no-store' }); return res.end(); }