diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 890dd94..e1f3ab1 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -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 } diff --git a/packages/zcode/own.test.mjs b/packages/zcode/own.test.mjs index c244298..ee2de27 100644 --- a/packages/zcode/own.test.mjs +++ b/packages/zcode/own.test.mjs @@ -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") +}) diff --git a/packages/zcode/package.json b/packages/zcode/package.json index f7618ff..bee7e2c 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -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",