Files
instantadpay/forumsso.js
T

56 lines
3.9 KiB
JavaScript

// 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=<base64 payload>&sig=<hex HMAC-SHA256>; 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);
// no website on sign-in (Marty 2026-11-14): Discourse holds a new user who "entered profile information without reading" for
// approval, and the stackpol.com link we filled in put every new member in the review queue. Members add their own.
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);
}
// server-to-server: the forum's reward service asks "is this email an activated member, and where are payouts sent?" The request is
// signed with the same shared secret: header x-forum-sig = hex HMAC-SHA256 of "<email>.<unix seconds>", x-forum-ts = those seconds,
// valid for 5 minutes.
function verifyServer(email, ts, sig) {
if (SECRET().length < 32) return false;
const t = Number(ts); if (!(t > 0) || Math.abs(Date.now() / 1000 - t) > 300) return false;
const want = Buffer.from(sign(String(email || '').toLowerCase() + '.' + t), 'hex'), got = Buffer.from(String(sig || ''), 'hex');
return want.length === got.length && crypto.timingSafeEqual(want, got);
}
module.exports = { verify, verifyServer, redirectFor, sign, SECRET, FORUM_URL };