diff --git a/docs/releases/v1.19.0.md b/docs/releases/v1.19.0.md index 1c5a4b0..43c388f 100644 --- a/docs/releases/v1.19.0.md +++ b/docs/releases/v1.19.0.md @@ -5,7 +5,8 @@ Log in to your CoreScope account from the app, and every companion you connect i ## What's new - **CoreScope account card (Settings).** It appears only when the CoreScope this app - uploads to has user management on (`/api/config/client` → `userManagement.enabled`). + uploads to has user management with companion linking on (`/api/config/client` → + `userManagement.enabled` and `userManagement.companionLinking`). Log in with your CoreScope email and password; the app keeps a device token, never the password, and the token is never shown or logged. The card shows who is logged in and the companions linked to the account, with ● on the one connected now. **Log out** @@ -43,7 +44,8 @@ Log in to your CoreScope account from the app, and every companion you connect i origin to CoreScope's `corsAllowedOrigins`. Without it the debug log says `account: off — … unreachable … corsAllowedOrigins …` and the app behaves as before. - **Needs CoreScope with companion linking** (user management sub-project F) and - `userManagement.enabled`. Against an older CoreScope the card simply stays hidden. + `userManagement.enabled`. Against an older CoreScope, also one that already has + accounts, the card stays hidden and uploading works as before. - **Upgrade every RX client before an admin turns on `requireLinkedCompanion`.** Older versions keep publishing, and CoreScope then drops their data. - Tests: 378 pass (+95). diff --git a/src/account.js b/src/account.js index 162a263..cb0097f 100644 --- a/src/account.js +++ b/src/account.js @@ -222,8 +222,8 @@ export function createAccount({ if (h && typeof h.unref === 'function') h.unref(); // node: never keeps a process alive } - // discover fetches /api/config/client once. Only userManagement.enabled === true turns - // the feature on; every other outcome is off, with one log line saying why. Any parsed + // discover fetches /api/config/client once. Only userManagement.enabled === true with + // userManagement.companionLinking === true turns the feature on; every other outcome is off, with one log line saying why. Any parsed // answer is also a fresh reading of the hold flag; no answer keeps the last known one. async function discover() { let json = null; @@ -244,6 +244,9 @@ export function createAccount({ const um = json && json.userManagement; if (!um || typeof um !== 'object') reason = 'has no userManagement block (older CoreScope, or user management off)'; else if (um.enabled !== true) reason = 'has userManagement.enabled off'; + // A CoreScope with accounts but without companion linking answers the + // device-token POST with its SPA page: a login there could only fail. + else if (um.companionLinking !== true) reason = 'has user management but no companion linking (CoreScope older than companion linking)'; } st.enabled = !reason; if (st.discovered) stopRetry('discover'); diff --git a/test/account-invariants.test.mjs b/test/account-invariants.test.mjs index a3dd32f..62d13ca 100644 --- a/test/account-invariants.test.mjs +++ b/test/account-invariants.test.mjs @@ -43,7 +43,7 @@ async function accountWith({ requireLinked = false, loggedIn = false, linked = f const storage = memStorage(loggedIn ? { [TOKEN_KEY]: JSON.stringify({ origin: ORIGIN, token: 'tok-secret-123', displayName: 'Erwin Account', companions: linked ? [{ pubkey: PK, name: 'obs' }] : [] }), } : {}); - const fetch = async () => res(200, { userManagement: { enabled: true }, ...(requireLinked ? { clientRxRequireLinkedCompanion: true } : {}) }); + const fetch = async () => res(200, { userManagement: { enabled: true, companionLinking: true }, ...(requireLinked ? { clientRxRequireLinkedCompanion: true } : {}) }); const account = createAccount({ baseUrl: ORIGIN, fetch, storage }); await account.discover(); return account; diff --git a/test/account.test.mjs b/test/account.test.mjs index a879ed1..fd96b67 100644 --- a/test/account.test.mjs +++ b/test/account.test.mjs @@ -57,7 +57,7 @@ function make({ routes = {}, storage = memStorage(), ...rest } = {}) { return { account, server, storage, logs, changes: () => changes }; } -const ENABLED = { 'GET /api/config/client': () => res(200, { userManagement: { enabled: true } }) }; +const ENABLED = { 'GET /api/config/client': () => res(200, { userManagement: { enabled: true, companionLinking: true } }) }; const session = (extra = {}) => ({ [TOKEN_KEY]: JSON.stringify({ origin: ORIGIN, token: 'tok-secret', displayName: 'Erwin', companions: [], ...extra }), }); @@ -119,6 +119,14 @@ test('discovery: a missing userManagement block means off', async () => { assert.match(t.logs[0], /no userManagement/); }); +test('discovery: user management without companion linking (an older CoreScope) means off', async () => { + // Such a server answers POST /api/auth/device-token with its SPA page (HTTP 200, + // HTML), so a login could only ever fail. + const t = make({ routes: { 'GET /api/config/client': () => res(200, { userManagement: { enabled: true, channelProposals: true } }) } }); + assert.strictEqual((await t.account.discover()).enabled, false); + assert.match(t.logs[0], /companion linking/); +}); + test('discovery: enabled false means off', async () => { const t = make({ routes: { 'GET /api/config/client': () => res(200, { userManagement: { enabled: false } }) } }); assert.strictEqual((await t.account.discover()).enabled, false); @@ -153,7 +161,7 @@ test('discovery: a timeout means off', async () => { // --- the hold flag: clientRxRequireLinkedCompanion --------------------------- test('discovery reads clientRxRequireLinkedCompanion and remembers it for this origin', async () => { - const t = make({ routes: { 'GET /api/config/client': () => res(200, { userManagement: { enabled: true }, clientRxRequireLinkedCompanion: true }) } }); + const t = make({ routes: { 'GET /api/config/client': () => res(200, { userManagement: { enabled: true, companionLinking: true }, clientRxRequireLinkedCompanion: true }) } }); await t.account.discover(); assert.strictEqual(t.account.requireLinked, true); assert.strictEqual(loadRequireLinked(t.storage, ORIGIN), true); @@ -187,7 +195,7 @@ test('an answer without the field clears a remembered flag', async () => { }); test('shouldHold only when the flag is on and the companion is not in the linked cache', async () => { - const flagOn = { 'GET /api/config/client': () => res(200, { userManagement: { enabled: true }, clientRxRequireLinkedCompanion: true }) }; + const flagOn = { 'GET /api/config/client': () => res(200, { userManagement: { enabled: true, companionLinking: true }, clientRxRequireLinkedCompanion: true }) }; const linked = make({ routes: flagOn, storage: memStorage(session({ companions: [{ pubkey: PK, name: 'obs' }] })) }); await linked.account.discover(); assert.strictEqual(linked.account.shouldHold(PK), false); @@ -336,7 +344,7 @@ function signer(impl) { async function linkable({ companions = [], challenge, link, requireLinked = false } = {}) { let n = 0; const routes = { - 'GET /api/config/client': () => res(200, { userManagement: { enabled: true }, ...(requireLinked ? { clientRxRequireLinkedCompanion: true } : {}) }), + 'GET /api/config/client': () => res(200, { userManagement: { enabled: true, companionLinking: true }, ...(requireLinked ? { clientRxRequireLinkedCompanion: true } : {}) }), 'GET /api/account/companions': () => res(200, companions), 'POST /api/account/companions/challenge': challenge || (() => { n++; return res(200, { challenge: 'c' + n, expiresAt: 0 }); }), 'POST /api/account/companions': link || ((req) => res(200, { pubkey: req.body.pubkey, name: req.body.name, linkedAt: 1, myNodes: 'added' })), @@ -516,7 +524,7 @@ test('a fresh link holds the queue until the ingestor can have seen it, then lif setTimeout: (fn, ms) => { timers.push({ fn, at: clock + ms }); return timers.length; }, clearTimeout: () => {}, routes: { - 'GET /api/config/client': () => res(200, { userManagement: { enabled: true }, clientRxRequireLinkedCompanion: true }), + 'GET /api/config/client': () => res(200, { userManagement: { enabled: true, companionLinking: true }, clientRxRequireLinkedCompanion: true }), 'GET /api/account/companions': () => res(200, []), 'POST /api/account/companions/challenge': () => res(200, { challenge: 'c1' }), 'POST /api/account/companions': (req) => res(200, { pubkey: req.body.pubkey, name: 'obs', myNodes: 'added' }), @@ -540,7 +548,7 @@ test('a companion already on the server list is not held for the settle time', a const t = make({ storage: memStorage(session()), routes: { - 'GET /api/config/client': () => res(200, { userManagement: { enabled: true }, clientRxRequireLinkedCompanion: true }), + 'GET /api/config/client': () => res(200, { userManagement: { enabled: true, companionLinking: true }, clientRxRequireLinkedCompanion: true }), 'GET /api/account/companions': () => res(200, [{ pubkey: PK, name: 'obs' }]), }, }); @@ -608,7 +616,7 @@ test('a discovery without an answer is retried with a growing delay, and a succe let up = false; const t = make({ setTimeout: box.setTimeout, clearTimeout: box.clearTimeout, onRetry: (w) => retries.push(w), - routes: { 'GET /api/config/client': () => { if (!up) throw new TypeError('Failed to fetch'); return res(200, { userManagement: { enabled: true } }); } }, + routes: { 'GET /api/config/client': () => { if (!up) throw new TypeError('Failed to fetch'); return res(200, { userManagement: { enabled: true, companionLinking: true } }); } }, }); // discover's own 5 s timeout timer is also in the box; only the retry timers count. await t.account.discover();