zcode 0.1.29: a sign-in keeps the account's user id beside its name

wakkana on Discord reported WorkBuddy accounts of one name collapsing
into one. WorkBuddy's plugin already keeps a uid, which magpie's plugin
host tells same-name accounts apart by. ZCode names an account by email,
else by its name, and gave no uid, so two ZCode accounts with no email
and one name were one account in the host. Both sign-ins (browser and
the ZCode app's) now give uid: user_id. The app's entry also follows
ZCode only while ZCode is signed in to the same user id, not another
account of that name.

Run: HOME=<scratch> TMPDIR=<scratch> bun test in packages/zcode, 112
pass. Both new tests (own.test.mjs, team.test.mjs) fail with index.mjs
from HEAD (uid undefined). bun scripts/check.mjs green.
Not tried: a real ZCode sign-in; whether ZCode's poll user carries
user_id is read from who(), which already reads it.
This commit is contained in:
yetone
2026-10-25 17:37:19 +08:00
parent 36d8902601
commit 5c9b7a4137
4 changed files with 48 additions and 8 deletions
+11 -5
View File
@@ -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 }
}
}
+24
View File
@@ -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" })
})
+1 -1
View File
@@ -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",
+12 -2
View File
@@ -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")
})