diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index e3ea6e8..29063cc 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -1804,7 +1804,11 @@ function ownSignIn() { else s = { ...s, jwt, base: s.base || ZAI_BASE, site: s.base === BIGMODEL_BASE ? "bigmodel" : "zai" } if (!s.key && !s.jwt && !isTeam(s)) return null let info = {} - for (const name of ["oauth:zai:user_info", "oauth:bigmodel:user_info"]) { + // The JWT belongs to the app's active sign-in. Cached profiles from + // another provider must not name it, and 3.18's China sign-in is zai_cn. + const active = decrypt(key, store["oauth:active_provider"]) + const families = [...new Set([active, "zai", "zai_cn", "bigmodel"].filter((x) => ["zai", "zai_cn", "bigmodel"].includes(x)))] + for (const name of families.map((x) => `oauth:${x}:user_info`)) { const v = decrypt(key, store[name]) if (v && !first(info.email, info.name, info.user_id)) { try { diff --git a/packages/zcode/own.test.mjs b/packages/zcode/own.test.mjs index 551476e..c244298 100644 --- a/packages/zcode/own.test.mjs +++ b/packages/zcode/own.test.mjs @@ -315,3 +315,43 @@ test("ZCode 3.16's key location lists the coding plan's models", async () => { _internal.routes.clear() await expect(hooks.provider.models({ models: {} }, { auth: { ...z, type: "oauth" } })).rejects.toThrow("coding plan key") }) + +// ZCode 3.18's China sign-in keeps its identity under zai_cn. Losing it +// makes every gift-only account "ZCode" and lets it follow a different +// account's session when the app switches users. +test("the app's China sign-in keeps its identity and follows only that user", async () => { + const as = (id) => JSON.stringify({ email: "ada@phone.local", user_id: id }) + zcodeHas({ + zcodejwttoken: "Bearer china-a", + "oauth:active_provider": "zai_cn", + "oauth:zai_cn:user_info": as("china-user-a"), + }) + const auth = await appSignIn() + expect(auth.accountId).toBe("ada") + expect(auth.uid).toBe("china-user-a") + + zcodeHas({ + zcodejwttoken: "Bearer china-renewed", + "oauth:active_provider": "zai_cn", + "oauth:zai_cn:user_info": as("china-user-a"), + }) + expect((await _internal.stateOf(auth)).jwt).toBe("china-renewed") + + // Two accounts may share the same display name; the uid guards them. + zcodeHas({ + zcodejwttoken: "Bearer china-b", + "oauth:active_provider": "zai_cn", + "oauth:zai_cn:user_info": as("china-user-b"), + }) + expect((await _internal.stateOf(auth)).jwt).toBe("china-a") +}) + +test.each(["zai_cn", "zai", "bigmodel"])("the app's active %s identity wins over another provider's cached profile", async (active) => { + const store = { zcodejwttoken: "Bearer active-session", "oauth:active_provider": active } + for (const family of ["zai", "zai_cn", "bigmodel"]) + store[`oauth:${family}:user_info`] = JSON.stringify({ email: `${family}@example.com`, user_id: `user-${family}` }) + zcodeHas(store) + const auth = await appSignIn() + expect(auth.accountId).toBe(`${active}@example.com`) + expect(auth.uid).toBe(`user-${active}`) +}) diff --git a/packages/zcode/package.json b/packages/zcode/package.json index b669eb1..7edc2cd 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.61", + "version": "0.1.63", "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan, and claims its gift plans daily", "type": "module", "main": "./opencode.mjs",