diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 4cf0150..cf69602 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -1001,6 +1001,10 @@ async function signedIn(site, token, jwt, device, onTeam = false) { // TEAM marks a team seat's account name beside the account's own. const TEAM = "team" const who = (u) => first(String(u?.email ?? "").replace(/@phone\.local$/, ""), u?.name, u?.user_id, "ZCode") +// uidOf is the account's user id, kept beside its name: magpie tells two +// accounts of one name apart by it, a name being no one's own (wakkana on +// Discord, WorkBuddy's) +const uidOf = (u) => (u?.user_id === undefined || u?.user_id === null ? "" : String(u.user_id).trim()) const sleep = (ms) => new Promise((r) => setTimeout(r, ms)) // authorize opens ZCode's sign-in flow on site; its callback polls it. @@ -1059,6 +1063,7 @@ async function authorize(site, log, onTeam = false) { // the key doesn't run out; ZCode's token, when it is all there is, does expires: s.key || isTeam(s) ? 0 : jwtExpiry(s.jwt), accountId: onTeam ? `${who(got.user)} (${TEAM})` : who(got.user), + ...(uidOf(got.user) ? { uid: uidOf(got.user) } : {}), } } }, @@ -1220,7 +1225,7 @@ function ownSignIn() { } catch {} } } - return { ...s, user: who(info) } + return { ...s, user: who(info), uid: uidOf(info) } } // asZCode is the sign-in "as ZCode": what it reads is ZCode's own. @@ -1232,9 +1237,9 @@ function asZCode() { async callback() { const own = ownSignIn() if (!own) return { type: "failed", error: "ZCode isn't signed in on this computer; sign in to ZCode, or sign in to Z.ai or BigModel here" } - const { user, ...s } = own + const { user, uid, ...s } = own const state = { source: "zcode", device: anyDevice, ...s } - return { type: "success", refresh: JSON.stringify(state), access: s.key || s.jwt || "", expires: 0, accountId: user } + return { type: "success", refresh: JSON.stringify(state), access: s.key || s.jwt || "", expires: 0, accountId: user, ...(uid ? { uid } : {}) } }, } } @@ -1253,8 +1258,9 @@ function stateOf(auth) { // send this account's requests as that one, and an expired session // says sign in again (EXPIRED) rather than borrowing another's. const own = ownSignIn() - if (own && own.user === String(auth.accountId ?? "")) { - const { user, ...now } = own + // (by user id where both have one: another account may share the name) + if (own && own.user === String(auth.accountId ?? "") && (!own.uid || !auth.uid || own.uid === String(auth.uid))) { + const { user, uid: _uid, ...now } = own s = { source: "zcode", device: s.device, plan: s.plan, ...now } } } diff --git a/packages/zcode/own.test.mjs b/packages/zcode/own.test.mjs index 7a53276..a84f955 100644 --- a/packages/zcode/own.test.mjs +++ b/packages/zcode/own.test.mjs @@ -169,3 +169,27 @@ test("ZCode 3.15's plan key is read from its config.json", async () => { expect(_internal.stateOf(auth)).toMatchObject({ key: "id1.secret1", base: "https://api.z.ai/api/anthropic" }) config({}) }) + +// wakkana on Discord (WorkBuddy's, the same here): two accounts of one +// name are two accounts. The sign-in keeps ZCode's user id beside the name, +// for magpie to tell them apart by, and an entry follows ZCode only while +// ZCode is signed in to that same user id, not another of its name. +test("the app's sign-in keeps the user id, and follows ZCode by it", async () => { + const as = (id) => JSON.stringify({ name: "Ada", user_id: id }) + zcodeHas({ [KEY]: "id1.secret1", zcodejwttoken: "j1", "oauth:zai:user_info": as("u1") }) + const hooks = await ZCodeAuthPlugin({ client: {} }) + const m = hooks.auth.methods.find((x) => x.label === "ZCode app's sign-in") + const got = await (await m.authorize()).callback() + expect(got.accountId).toBe("Ada") + expect(got.uid).toBe("u1") + expect(JSON.parse(got.refresh).uid).toBeUndefined() + const auth = { ...got, type: "oauth" } + + zcodeHas({ [KEY]: "id2.secret2", zcodejwttoken: "j2", "oauth:zai:user_info": as("u1") }) + expect(_internal.stateOf(auth)).toMatchObject({ key: "id2.secret2", jwt: "j2" }) + expect(_internal.stateOf(auth).uid).toBeUndefined() + + // another account named Ada: A's entry keeps what it saved + zcodeHas({ [KEY]: "id3.secret3", zcodejwttoken: "j3", "oauth:zai:user_info": as("u2") }) + expect(_internal.stateOf(auth)).toMatchObject({ key: "id1.secret1" }) +}) diff --git a/packages/zcode/package.json b/packages/zcode/package.json index c040af6..c69ffee 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.28", + "version": "0.1.29", "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", "type": "module", "main": "./index.mjs", diff --git a/packages/zcode/team.test.mjs b/packages/zcode/team.test.mjs index cdaad7c..6a4b934 100644 --- a/packages/zcode/team.test.mjs +++ b/packages/zcode/team.test.mjs @@ -18,7 +18,7 @@ afterEach(() => (globalThis.fetch = real)) const ok = (data) => Response.json({ code: 200, success: true, data }) // a BigModel account with its own Pro plan in 默认项目 and a seat on team t1 -function vendor({ seat = "VALID", teams = true } = {}) { +function vendor({ seat = "VALID", teams = true, user = { email: "ada@example.com" } } = {}) { const seen = [] globalThis.fetch = async (input, init = {}) => { const u = new URL(typeof input === "string" ? input : input.url) @@ -28,7 +28,7 @@ function vendor({ seat = "VALID", teams = true } = {}) { seen.push({ method: init.method ?? "GET", path: u.pathname, org, proj }) const p = u.pathname if (p === "/api/v1/oauth/cli/init") return ok({ flow_id: "f1", authorize_url: "https://open.bigmodel.cn/login", poll_interval_sec: 0, expires_at: 0 }) - if (p === "/api/v1/oauth/cli/poll/f1") return ok({ status: "ready", bigmodel: { access_token: "bm-token" }, token: "", user: { email: "ada@example.com" } }) + if (p === "/api/v1/oauth/cli/poll/f1") return ok({ status: "ready", bigmodel: { access_token: "bm-token" }, token: "", user }) if (p === "/api/biz/customer/getCustomerInfo") return ok({ organizations: [ @@ -105,3 +105,13 @@ test("the team sign-in asks which site, as the key sign-in does", async () => { expect(m.type).toBe("oauth") expect(m.prompts.map((p) => [p.key, p.type, p.options.map((o) => o.value)])).toEqual([["site", "select", ["zai", "bigmodel"]]]) }) + +// wakkana on Discord (WorkBuddy's, the same here): the browser sign-in +// keeps the account's user id beside its name, for magpie to tell two +// accounts of one name apart by +test("the plan sign-in keeps the account's user id", async () => { + vendor({ user: { name: "Ada", user_id: 42 } }) + const r = await signIn("ZCode: BigModel (智谱) GLM Coding Plan") + expect(r.accountId).toBe("Ada") + expect(r.uid).toBe("42") +})