diff --git a/networksso.js b/networksso.js index a446dfa..f523b57 100644 --- a/networksso.js +++ b/networksso.js @@ -34,6 +34,8 @@ const enabled = () => SECRET().length >= 32; const TTL = 60 * 1000; // token life const SKEW = 30 * 1000; // clock skew allowed between the two servers const STATE_AGE = 300; // seconds: the state cookie +const GO_STATE_AGE = 1200; // seconds: the state cookie on a deliberate tap (time to sign in on the provider first, 2026-11-14) +const BACK_AGE = 1200; // seconds: the provider remembers an unfinished deliberate check while the member signs in 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 @@ -160,9 +162,15 @@ async function handle(req, res, u) { 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)]; + const set = [ck('nsso.st', st, mode === 'go' ? GO_STATE_AGE : 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, (from ? from() : C.peerBase()) + '/sso/check?st=' + st + (C.from ? '&r=' + C.self : '') + '&to=' + encodeURIComponent(to), set); + return go(res, (from ? from() : C.peerBase()) + '/sso/check?st=' + st + (C.from ? '&r=' + C.self : '') + '&to=' + encodeURIComponent(to) + (mode === 'go' ? '&m=go' : ''), set); + } + if (p === '/sso/resume') { // signed in on the provider after a deliberate tap: finish the check it was asked for (2026-11-14) + const q = new URLSearchParams(cookies(req)['nsso.back'] || ''); + const clear = [ck('nsso.back', '', 0, true)]; + if (!/^[a-f0-9]{32}$/.test(q.get('st') || '')) return go(res, C.defaultPath, clear); + return go(res, '/sso/check?st=' + q.get('st') + (q.get('r') ? '&r=' + encodeURIComponent(q.get('r')) : '') + '&to=' + encodeURIComponent(safePath(q.get('to'), '/')), clear); } if (p === '/sso/check') { const st = String(u.searchParams.get('st') || ''); @@ -173,7 +181,14 @@ async function handle(req, res, u) { 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 ' + forName); return go(res, none); } + if (!s || !s.email) { + if (u.searchParams.get('m') === 'go' && !isBot(req)) { // a deliberate tap: sign in here first, then /sso/resume finishes the trip + log('check: not signed in here, asking to sign in', null, 'for ' + forName); + const back = new URLSearchParams({ st, to }); if (r) back.set('r', r); + return go(res, C.signinPath(C.defaultPath), [ck('nsso.back', back.toString(), BACK_AGE, true)]); + } + 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); @@ -209,4 +224,7 @@ async function handle(req, res, u) { return false; } -module.exports = { init, handle, enabled, mint, verify, spend, safePath, isBot, offCookie, mask, TTL, NAMES }; +// true when this browser came here from a deliberate network tap and still has to finish it after signing in +const pendingBack = req => !!cookies(req)['nsso.back']; + +module.exports = { init, handle, enabled, pendingBack, mint, verify, spend, safePath, isBot, offCookie, mask, TTL, NAMES }; diff --git a/public/assets/my.js b/public/assets/my.js index 13be270..f1e8984 100644 --- a/public/assets/my.js +++ b/public/assets/my.js @@ -714,6 +714,7 @@ else if (!render.waiting) { render.waiting = true; window.addEventListener('ls:session', () => { render.waiting = false; render(); }, { once: true }); } } if (!signedIn) return; + if (me.nssoBack && me.username && !render.toNsso) { render.toNsso = true; location.href = '/sso/resume'; return; } // tapped "Sign in with LinkSpin" on another network site while signed out here (2026-11-14) TANK_HOLD = (!me.memberId && me.tankHold) || null; if ($('adminLink')) $('adminLink').hidden = !me.isAdmin; // admin portal link, only for ADMIN_EMAIL // REQUIRED first step (Marty, 2026-09-12): no username, nothing else. The modal cannot be diff --git a/public/my.html b/public/my.html index e78cdd0..2ecbe80 100644 --- a/public/my.html +++ b/public/my.html @@ -1020,7 +1020,7 @@ - + diff --git a/qa/networksso.test.cjs b/qa/networksso.test.cjs index 1df0262..f5bbea6 100644 --- a/qa/networksso.test.cjs +++ b/qa/networksso.test.cjs @@ -82,7 +82,7 @@ const b64u = b => Buffer.from(b).toString('base64').replace(/=+$/, '').replace(/ r = await call(R('/sso/start?to=/my', 'nsso.off=1')); ok(r.loc === '/my', 'after a sign-out here: no silent check'); r = await call(R('/sso/start?mode=go&to=/my', 'nsso.off=1')); - ok(/^https:\/\/ls\.example\/sso\/check\?st=[a-f0-9]{32}&to=%2Fmy$/.test(r.loc) && r.set.some(c => /^nsso\.off=;/.test(c)), 'a deliberate link from the other site still signs in after a sign-out (and lifts the 12-hour pause)'); + ok(/^https:\/\/ls\.example\/sso\/check\?st=[a-f0-9]{32}&to=%2Fmy&m=go$/.test(r.loc) && r.set.some(c => /^nsso\.off=;/.test(c)), 'a deliberate link from the other site still signs in after a sign-out (and lifts the 12-hour pause)'); r = await call(R('/sso/start?to=https://evil.example')); const stc = (r.set.find(c => c.startsWith('nsso.st=')) || '').split(';')[0].split('=')[1]; ok(/^[a-f0-9]{32}$/.test(stc) && r.set.some(c => /^nsso\.tried=1;.*Max-Age=1800/.test(c)) && r.set.every(c => /HttpOnly/.test(c) && /Secure/.test(c)) && r.loc.endsWith('&to=%2Fmy'), '/sso/start: HttpOnly state + 30-minute guard, off-site path replaced', r.loc); diff --git a/server.js b/server.js index 569ff14..160da46 100644 --- a/server.js +++ b/server.js @@ -2103,7 +2103,7 @@ const server = http.createServer(async (req, res) => { if (!sponsorId && memberId !== _defSpon) sponsorId = _defSpon; // orphan fallback → #1 (never self-sponsor) if (sponsorId) { try { if (sponsorId > await chain.memberCount()) sponsorId = 0; } catch (e) {} } // fresh contract: no catch position exists yet, the first member becomes #1 if (memberId && acct && acct.memberId !== memberId) accounts.setMemberId(acct.email, memberId).catch(() => {}); - const out = { signedIn: true, sponsorBlocked, sponsorName: spdMe.name || null, email: s.email || (acct && acct.email) || null, + const out = { signedIn: true, nssoBack: networksso.pendingBack(req), sponsorBlocked, 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)