zcode 0.1.67: ZCode 3.18's enc:v2 sign-in is named as unreadable, not as signed out (#1196)

hupeak (#1196): ZCode 3.18 writes credentials.json's values as
"enc:v2:os:<…>:<…>". decrypt reads enc:v1 alone, so the jwt read as
null and the card said "this sign-in has no ZCode session: sign in
again", which signing in again can't fix: ZCode writes enc:v2 again.
The new form looks bound to the system's own key store (the "os"
part); this change doesn't try to read it.

- A ZCode app sign-in that finds values in an enc: form other than v1
  fails with "ZCode 3.18 keeps its sign-in in a new form this plugin
  can't read yet: add the account with a Z.ai or BigModel sign-in",
  instead of "ZCode isn't signed in on this computer".
- An account signed in "as ZCode" before has the same words on its
  card's gift line. A browser sign-in, or enc:v1, keeps the old words.

Run (mote, HOME=$(mktemp -d)):
- bun test packages/zcode: 272 pass, 0 fail.
- bun scripts/check.mjs zcode: ok.

Broken by hand: origin/main's index.mjs (with giftSignIn exported) and
the new test: 1 fail, the sign-in says "ZCode isn't signed in on this
computer".

Not tried: ZCode 3.18 itself (not installed here); the stored values
are the shape the reporter showed. Reading enc:v2 is not done; #1196
stays open for it.
This commit is contained in:
yetone
2026-11-16 11:27:21 +08:00
parent 4e0cdabbba
commit a96714544e
3 changed files with 47 additions and 4 deletions
+21 -3
View File
@@ -1099,7 +1099,11 @@ async function claimDaily(s) {
// translate it; why goes in the display. Set aside: it stops nothing.
const GIFT_SIGN_IN = "ZCode gift plans (-Trial models) need ZCode's sign-in"
function giftSignIn(s, key) {
if (!s.jwt) return { name: GIFT_SIGN_IN, used: 0, aside: true, display: key ? "not read with an API key: add the account with a ZCode sign-in" : "this sign-in has no ZCode session: sign in again" }
if (!s.jwt) {
if (key) return { name: GIFT_SIGN_IN, used: 0, aside: true, display: "not read with an API key: add the account with a ZCode sign-in" }
if (s.source === "zcode" && ownStoreUnreadable()) return { name: GIFT_SIGN_IN, used: 0, aside: true, display: UNREADABLE_SIGN_IN }
return { name: GIFT_SIGN_IN, used: 0, aside: true, display: "this sign-in has no ZCode session: sign in again" }
}
if (jwtExpired(s.jwt)) return { name: GIFT_SIGN_IN, used: 0, aside: true, display: "ZCode's sign-in has expired: sign in again" }
return null
}
@@ -1610,6 +1614,20 @@ function decrypt(key, v) {
}
}
// UNREADABLE_SIGN_IN says why a sign-in "as ZCode" has no session to
// read: ZCode 3.18 writes credentials.json's values as "enc:v2:os:…",
// which looks bound to the system's own key store, and this plugin reads
// enc:v1 alone (#1196).
const UNREADABLE_SIGN_IN = "ZCode 3.18 keeps its sign-in in a new form this plugin can't read yet: add the account with a Z.ai or BigModel sign-in"
// ownStoreUnreadable says whether ZCode's credentials.json holds values
// in a form decrypt can't read.
function ownStoreUnreadable() {
const store = readJSON(join(zcodeDir(), "credentials.json"))
if (!store || typeof store !== "object") return false
return Object.values(store).some((v) => typeof v === "string" && v.startsWith("enc:") && !v.startsWith("enc:v1:"))
}
function readJSON(path) {
try {
return JSON.parse(readFileSync(path, "utf8"))
@@ -1876,7 +1894,7 @@ function asZCode() {
method: "auto",
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" }
if (!own) return { type: "failed", error: ownStoreUnreadable() ? UNREADABLE_SIGN_IN : "ZCode isn't signed in on this computer; sign in to ZCode, or sign in to Z.ai or BigModel here" }
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, ...(uid ? { uid } : {}) }
@@ -3570,4 +3588,4 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
// config sets what zcodeConfig has read, for tests: null is ZCode's config
// out of reach, undefined forgets it
const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
export const _internal = { wallAnswer, unreached, planFull, notePlan, planFullUntil, locatedKeys, lastKeys, edge, EDGE_HOLD, busyHold, startGate, NARROW_FOR, limitOf, BUSY_N, BUSY_WINDOW, BUSY_HOLD, busyCap, busyHeld, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, unclaimed, unclaimedSeen, aloneGift, aloneSeen, aloneNames, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, queryOf, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS }
export const _internal = { wallAnswer, unreached, planFull, notePlan, planFullUntil, locatedKeys, lastKeys, edge, EDGE_HOLD, busyHold, startGate, NARROW_FOR, limitOf, BUSY_N, BUSY_WINDOW, BUSY_HOLD, busyCap, busyHeld, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, unclaimed, unclaimedSeen, aloneGift, aloneSeen, aloneNames, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, giftSignIn, located, dress, queryOf, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS }
+25
View File
@@ -355,3 +355,28 @@ test.each(["zai_cn", "zai", "bigmodel"])("the app's active %s identity wins over
expect(auth.accountId).toBe(`${active}@example.com`)
expect(auth.uid).toBe(`user-${active}`)
})
// ZCode 3.18 writes credentials.json's values as "enc:v2:os:…", which this
// plugin can't read: the sign-in and the card say so, never "sign in again"
// (#1196). The values are the shape the reporter showed, not real ones.
test("ZCode 3.18's enc:v2 credentials are named as unreadable, not as signed out", async () => {
mkdirSync(dir(), { recursive: true })
const v2 = "enc:v2:os:" + "A".repeat(24) + ":" + "B".repeat(312)
writeFileSync(join(dir(), "credentials.json"), JSON.stringify({ zcodejwttoken: v2, "oauth:bigmodel:access_token": v2, "oauth:active_provider": v2 }))
writeFileSync(join(dir(), "setting.json"), "{}")
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.type).toBe("failed")
expect(got.error).toMatch(/ZCode 3\.18 keeps its sign-in in a new form/)
// an account signed in before: its card's gift line says why, too
const line = _internal.giftSignIn({ source: "zcode", key: "id1.secret1" }, false)
expect(line.display).toMatch(/ZCode 3\.18 keeps its sign-in in a new form/)
// a browser sign-in with no session is still asked to sign in again
expect(_internal.giftSignIn({ key: "id1.secret1" }, false).display).toBe("this sign-in has no ZCode session: sign in again")
// enc:v1 again: back to the old words
zcodeHas({})
expect(_internal.giftSignIn({ source: "zcode", key: "id1.secret1" }, false).display).toBe("this sign-in has no ZCode session: sign in again")
})
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zcode-auth",
"version": "0.1.66",
"version": "0.1.67",
"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",