diff --git a/networksso.js b/networksso.js index 6f08e6c..5b48882 100644 --- a/networksso.js +++ b/networksso.js @@ -17,7 +17,13 @@ // stops a second bounce for 30 minutes, signing out sets nsso.off (12 hours, cleared by the next deliberate cross-site link), // bots and non-page fetches never bounce. // Token: "v1..", JSON = { iss, aud, email, -// st, to, iat, exp, n }. The token grants a plain member session and nothing else: no wallet action, payout or purchase rides on it. +// st, to, iat, exp, n[, wallet, name] }. The token grants a plain member session and nothing else: no wallet action, payout or purchase rides on it. +// +// Wallet sites (Downline Banners, 2026-11-13, Marty: "sign in once and be logged into all these sites"). Downline Banners has no email +// accounts; its members are wallets. IAP and LinkSpin list it under `wallets: { bannerline: base }` and vouch one way: /go/bannerline +// and /sso/check?r=bannerline mint a token that also carries the member's wallet (the one they signed in with, else the main wallet +// their account linked by signature) and username. Downline Banners runs this file with `from: { iap, linkspin }`, accepts a token from +// either, and makes a session for that wallet. /sso/start?via= picks which site to ask; old copies ignore the new parameters. 'use strict'; const crypto = require('crypto'); const fs = require('fs'); @@ -31,9 +37,11 @@ const STATE_AGE = 300; // seconds: the state cookie const TRIED_AGE = 1800; // seconds: one silent check per browser per 30 minutes const OFF_AGE = 12 * 3600; // seconds: no silent check after a sign-out on this site const HINT_AGE = 300; // seconds: the email prefill when the member is not here yet -const NAMES = { iap: 'InstantAdPay', linkspin: 'LinkSpin' }; +const NAMES = { iap: 'InstantAdPay', linkspin: 'LinkSpin', bannerline: 'Downline Banners' }; +const WALLET_RE = /^0x[0-9a-f]{40}$/; -let C = null; // { self, peer, peerBase(), isProd, dataDir, db, member(req), accept(email), signIn(email, req), landing(to), signinPath(to), defaultPath } +let C = null; // { self, peer, peerBase(), isProd, dataDir, db, member(req), accept(email, req, v), signIn(email, req, v), landing(to), signinPath(to), defaultPath, + // wallets?: { site: base() } + wallet(session) -> { address, name }, from?: { site: base() } } const b64u = buf => Buffer.from(buf).toString('base64').replace(/=+$/, '').replace(/\+/g, '-').replace(/\//g, '_'); const unb64u = s => Buffer.from(String(s).replace(/-/g, '+').replace(/_/g, '/'), 'base64'); @@ -51,7 +59,10 @@ function safePath(to, dflt) { function mint(o) { const now = o.now || Date.now(); - const body = b64u(JSON.stringify({ iss: o.iss, aud: o.aud, email: String(o.email || '').trim().toLowerCase(), st: o.st, to: safePath(o.to), iat: now, exp: now + TTL, n: crypto.randomBytes(16).toString('hex') })); + const t = { iss: o.iss, aud: o.aud, email: String(o.email || '').trim().toLowerCase(), st: o.st, to: safePath(o.to), iat: now, exp: now + TTL, n: crypto.randomBytes(16).toString('hex') }; + if (o.wallet) t.wallet = String(o.wallet).toLowerCase(); + if (o.name) t.name = String(o.name).slice(0, 40); + const body = b64u(JSON.stringify(t)); return 'v1.' + body + '.' + b64u(mac(body)); } @@ -64,13 +75,15 @@ function verify(tok, o) { if (want.length !== got.length || !crypto.timingSafeEqual(want, got)) return { error: 'signature' }; let p; try { p = JSON.parse(unb64u(m[1]).toString('utf8')); } catch (e) { return { error: 'malformed' }; } const now = (o && o.now) || Date.now(); - if (!p || p.aud !== o.aud || p.iss !== o.iss) return { error: 'audience' }; + if (!p || p.aud !== o.aud || ![].concat(o.iss).includes(p.iss)) return { error: 'audience' }; if (!(Number(p.exp) > now) || Number(p.iat) > now + SKEW || Number(p.exp) - Number(p.iat) > TTL) return { error: 'expired' }; if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(String(p.email || ''))) return { error: 'email' }; if (!/^[a-f0-9]{32}$/.test(String(p.n || ''))) return { error: 'malformed' }; const st = String((o && o.st) || ''); if (!/^[a-f0-9]{32}$/.test(st) || !/^[a-f0-9]{32}$/.test(String(p.st || '')) || !crypto.timingSafeEqual(Buffer.from(st), Buffer.from(p.st))) return { error: 'state' }; - return { ok: true, email: String(p.email).toLowerCase(), to: safePath(p.to), n: p.n, exp: Number(p.exp) }; + if (p.wallet != null && !WALLET_RE.test(String(p.wallet))) return { error: 'wallet' }; + const name = p.name != null && /^[A-Za-z0-9_.-]{1,40}$/.test(String(p.name)) ? String(p.name) : null; + return { ok: true, iss: p.iss, email: String(p.email).toLowerCase(), wallet: p.wallet || null, name, to: safePath(p.to), n: p.n, exp: Number(p.exp) }; } // single use: true the first time a nonce is spent. MySQL when the site runs on it (two app copies during a deploy share it), @@ -124,60 +137,71 @@ async function handle(req, res, u) { if (!C || req.method !== 'GET') return false; const p = u.pathname; const peerName = NAMES[C.peer] || C.peer; - if (p === '/go/' + C.peer) { + const goTo = p.startsWith('/go/') ? p.slice(4) : ''; + const walletBase = x => (C.wallets && Object.prototype.hasOwnProperty.call(C.wallets, x) ? C.wallets[x] : null); + if (goTo && (goTo === C.peer || walletBase(goTo))) { + const base = goTo === C.peer ? C.peerBase() : walletBase(goTo)(); const to = safePath(u.searchParams.get('to'), '/'); - const plain = C.peerBase() + to; + const plain = base + to; if (!enabled() || isBot(req)) return go(res, plain); let s = null; try { s = await C.member(req); } catch (e) {} if (!s || !s.email) return go(res, plain); - return go(res, C.peerBase() + '/sso/start?mode=go&to=' + encodeURIComponent(to)); + if (goTo !== C.peer) { let w = null; try { w = await C.wallet(s); } catch (e) {} if (!w || !w.address) return go(res, plain); } // no wallet to vouch for: a plain link + return go(res, base + '/sso/start?mode=go' + (goTo === C.peer ? '' : '&via=' + C.self) + '&to=' + encodeURIComponent(to)); } if (p === '/sso/start') { const to = safePath(u.searchParams.get('to'), C.defaultPath); const mode = u.searchParams.get('mode') === 'go' ? 'go' : 'check'; + const via = String(u.searchParams.get('via') || ''); + const from = C.from && Object.prototype.hasOwnProperty.call(C.from, via) ? C.from[via] : null; // a wallet site may ask either provider if (!enabled() || isBot(req) || notPage(req)) return go(res, to); let s = null; try { s = await C.member(req); } catch (e) {} - if (s && s.email) return go(res, to); // already signed in here + if (s && (s.email || s.address)) return go(res, to); // already signed in here const c = cookies(req); if (mode === 'check' && (c['nsso.tried'] || c['nsso.off'])) return go(res, to); const st = crypto.randomBytes(16).toString('hex'); const set = [ck('nsso.st', st, STATE_AGE, true), ck('nsso.tried', '1', TRIED_AGE, true)]; if (mode === 'go' && c['nsso.off']) set.push(ck('nsso.off', '', 0, true)); - return go(res, C.peerBase() + '/sso/check?st=' + st + '&to=' + encodeURIComponent(to), set); + return go(res, (from ? from() : C.peerBase()) + '/sso/check?st=' + st + (C.from ? '&r=' + C.self : '') + '&to=' + encodeURIComponent(to), set); } if (p === '/sso/check') { const st = String(u.searchParams.get('st') || ''); const to = safePath(u.searchParams.get('to'), '/'); - const none = C.peerBase() + '/sso/in?none=1&to=' + encodeURIComponent(to); + const r = String(u.searchParams.get('r') || ''); + const wb = r && r !== C.peer ? walletBase(r) : null; // a wallet site asking: the token carries the wallet + const base = wb ? wb() : C.peerBase(), aud = wb ? r : C.peer, forName = NAMES[aud] || aud; + const none = base + '/sso/in?none=1&to=' + encodeURIComponent(to); if (!enabled() || !/^[a-f0-9]{32}$/.test(st)) return go(res, none); let s = null; try { s = await C.member(req); } catch (e) {} - if (!s || !s.email) { log('check: not signed in here', null, 'for ' + peerName); return go(res, none); } - log('token issued', s.email, 'for ' + peerName); - return go(res, C.peerBase() + '/sso/in?t=' + mint({ iss: C.self, aud: C.peer, email: s.email, st, to })); + if (!s || !s.email) { log('check: not signed in here', null, 'for ' + forName); return go(res, none); } + let w = null; + if (wb) { try { w = await C.wallet(s); } catch (e) {} if (!w || !WALLET_RE.test(String(w.address || '').toLowerCase())) { log('check: no wallet on the account', s.email, 'for ' + forName); return go(res, none); } } + log('token issued', s.email, 'for ' + forName); + return go(res, base + '/sso/in?t=' + mint({ iss: C.self, aud, email: s.email, wallet: w && w.address, name: w && w.name, st, to })); } if (p === '/sso/in') { const c = cookies(req); const set = [ck('nsso.st', '', 0, true)]; if (u.searchParams.get('none')) { log('none: not signed in on ' + peerName); return go(res, C.signinPath(safePath(u.searchParams.get('to'), C.defaultPath)), set); } - const v = verify(u.searchParams.get('t'), { iss: C.peer, aud: C.self, st: c['nsso.st'] }); + const v = verify(u.searchParams.get('t'), { iss: [C.peer].concat(Object.keys(C.from || {})), aud: C.self, st: c['nsso.st'] }); if (v.error) { log('refused: ' + v.error); return go(res, C.signinPath(C.defaultPath), set); } if (!(await spend(v.n, v.exp))) { log('refused: token already used', v.email); return go(res, C.signinPath(v.to), set); } - let a = null; try { a = await C.accept(v.email, req); } catch (e) { a = { fallback: 'error ' + e.message }; } + let a = null; try { a = await C.accept(v.email, req, v); } catch (e) { a = { fallback: 'error ' + e.message }; } if (!a || !a.ok) { // not a member here yet (or not allowed to sign in): the normal email-code sign-in, email filled in log('fallback to email code: ' + ((a && a.fallback) || 'not accepted'), v.email); set.push(ck('nsso.hint', v.email, HINT_AGE, true)); return go(res, C.signinPath(v.to), set); } - let cookie = null; try { cookie = await C.signIn(v.email, req); } catch (e) { console.error('network-sso sign-in', e.message); } + let cookie = null; try { cookie = await C.signIn(v.email, req, v); } catch (e) { console.error('network-sso sign-in', e.message); } if (!cookie) { log('fallback to email code: session not made', v.email); return go(res, C.signinPath(v.to), set); } set.push(cookie); if (c['nsso.off']) set.push(ck('nsso.off', '', 0, true)); - log('signed in from ' + peerName, v.email); + log('signed in from ' + (NAMES[v.iss] || peerName), v.email); return go(res, C.landing(v.to === '/' ? C.defaultPath : v.to), set); // no page named: the dashboard, not the public home page (2026-11-13) } if (p === '/api/sso/status') { const c = cookies(req); const set = []; let s = null; try { s = await C.member(req); } catch (e) {} - const signedIn = !!(s && s.email); + const signedIn = !!(s && (s.email || s.address)); const out = { peer: peerName, check: enabled() && !signedIn && !isBot(req) && !c['nsso.tried'] && !c['nsso.off'] }; if (c['nsso.hint']) { if (!signedIn) out.hint = c['nsso.hint']; set.push(ck('nsso.hint', '', 0, true)); } return sendJson(res, out, set); diff --git a/server.js b/server.js index c7349a0..7f4c226 100644 --- a/server.js +++ b/server.js @@ -637,6 +637,8 @@ async function boot() { // sessions only (the admin portal is never shared). An email that is not here yet (the 2-minute sync has not run) or is not // let in (LS_TESTERS) gets the normal email-code sign-in with the email filled in; nothing is created by the network sign-in. networksso.init({ self: 'linkspin', peer: 'iap', peerBase: IAP_BASE, isProd: IS_PROD, dataDir: DATA_DIR, db, defaultPath: '/ads', + wallets: { bannerline: () => String(process.env.BL_URL || 'https://new.downlinebanners.com').replace(/\/+$/, '') }, // Downline Banners by wallet (2026-11-13) + wallet: async s => { const a = await accounts.byEmail(s.email); const addr = String(s.address || (a && a.address) || '').toLowerCase(); return addr ? { address: addr, name: '' } : null; }, member: async req => { // a wallet sign-in carries the address, not the email: find the account that owns the wallet (2026-11-13) const s = await auth.fromRequest(req); if (!s) return null; let email = s.email || null; if (!email && s.address) { try { const a = await accounts.byAddress(s.address); if (a && a.email) email = a.email; } catch (e) {} }