zcode 0.1.31: ZCode 3.16's key location gives the coding plan its key (#410)
ZCode 3.16.3 on a Mac keeps no coding plan key in credentials.json or
config.json, only a project-key-location:v1 record
({organizationId, projectId, apiKeyId}, encrypted like the rest). With no
key, plansOf sent the account to the gift and the BigModel GLM Coding Plan
never showed (meraki7304).
ownSignIn now reads that record (the account's own: team null, coding
plan, the profile's user id, the family ZCode is on first), and located()
reads the named key's secret from api_keys/copy/{apiKeyId} with the OAuth
token, as ZCode lists keys (BigModel bare, Z.ai as a Bearer). Only GET:
nothing is created. The record's origin is used only when it is an https
z.ai / bigmodel.cn host, else the site's own API. A 404 leaves the sign-in
as before; another failure says sign in again. Request fetch and usage
resolve it before plansOf.
Record format and auth forms read from ZCode 3.16.3's own app.asar
(ProjectAccessTokenClient / project-key-location:v1).
Run: HOME=$(mktemp -d) bun test in packages/zcode, 119 pass;
bun scripts/check.mjs ok.
Red on old code: the new own.test.mjs case fails with the old index.mjs
(key "" and start true).
Not tried: a real ZCode 3.16 account (no vendor credentials used); the
reporter's read-only check of the same endpoints is the evidence.
This commit is contained in:
@@ -193,3 +193,61 @@ test("the app's sign-in keeps the user id, and follows ZCode by it", async () =>
|
||||
zcodeHas({ [KEY]: "id3.secret3", zcodejwttoken: "j3", "oauth:zai:user_info": as("u2") })
|
||||
expect(_internal.stateOf(auth)).toMatchObject({ key: "id1.secret1" })
|
||||
})
|
||||
|
||||
// meraki7304 (#410): ZCode 3.16.3 on a Mac keeps no plan key at all, in
|
||||
// credentials.json or config.json, only where the key is: a
|
||||
// project-key-location:v1 record. The sign-in reads that key, as ZCode lists
|
||||
// it, and the BigModel GLM Coding Plan is found, not only the gift.
|
||||
test("ZCode 3.16's key location gives the coding plan its key", async () => {
|
||||
const loc = (fam, account) =>
|
||||
"project-key-location:v1:" + encodeURIComponent(JSON.stringify(["https://bigmodel.cn", fam, account, null, 1, "non-team"]))
|
||||
zcodeHas(
|
||||
{
|
||||
zcodejwttoken: "Bearer j1",
|
||||
"oauth:bigmodel:access_token": "tok-bm",
|
||||
"oauth:bigmodel:user_info": JSON.stringify({ id: "u7", email: "q@example.com" }),
|
||||
[loc("bigmodel", "u7")]: JSON.stringify({ organizationId: "org7", projectId: "proj7", apiKeyId: "k7" }),
|
||||
// another account's location is left alone
|
||||
[loc("bigmodel", "u8")]: JSON.stringify({ organizationId: "org8", projectId: "proj8", apiKeyId: "k8" }),
|
||||
},
|
||||
{ providerFamilyDomain: "bigmodel" },
|
||||
)
|
||||
const sent = []
|
||||
globalThis.fetch = async (input, init = {}) => {
|
||||
const url = String(input)
|
||||
sent.push([init.method ?? "GET", url, new Headers(init.headers ?? {}).get("authorization")])
|
||||
if (url.includes("/api_keys/copy/k7")) return Response.json({ code: 200, data: { secretKey: "sec7" } })
|
||||
if (url.includes("/api/biz/subscription/list")) return Response.json({ code: 200, data: [{ productName: "GLM Coding Pro", status: "VALID" }] })
|
||||
if (url.includes("/zcode-plan/billing/balance")) return Response.json({ code: 0, data: { plans: [] } })
|
||||
return new Response("{}", { status: 404 })
|
||||
}
|
||||
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("success")
|
||||
const auth = { ...got, type: "oauth" }
|
||||
const s = await _internal.located(_internal.stateOf(auth))
|
||||
expect(s).toMatchObject({ key: "k7.sec7", site: "bigmodel", base: "https://open.bigmodel.cn/api/anthropic" })
|
||||
expect(sent[0]).toEqual(["GET", "https://bigmodel.cn/api/biz/v1/organization/org7/projects/proj7/api_keys/copy/k7", "tok-bm"])
|
||||
// nothing is made
|
||||
expect(sent.every(([method]) => method === "GET")).toBe(true)
|
||||
_internal.routes.clear()
|
||||
expect((await _internal.plansOf(s)).start).toBe(false)
|
||||
expect(sent.map(([, u]) => u).join(" ")).not.toContain("k8")
|
||||
|
||||
// a location whose key is gone leaves the sign-in as it was
|
||||
zcodeHas(
|
||||
{
|
||||
zcodejwttoken: "Bearer j1",
|
||||
"oauth:zai:access_token": "tok-z",
|
||||
"oauth:zai:user_info": JSON.stringify({ id: "u9", email: "z@example.com" }),
|
||||
["project-key-location:v1:" + encodeURIComponent(JSON.stringify(["https://evil.example", "zai", "u9", null, 1, "non-team"]))]: JSON.stringify({ organizationId: "o", projectId: "p", apiKeyId: "gone" }),
|
||||
},
|
||||
)
|
||||
sent.length = 0
|
||||
const z = await (await m.authorize()).callback()
|
||||
const zs = await _internal.located(_internal.stateOf({ ...z, type: "oauth" }))
|
||||
expect(zs.key).toBe("")
|
||||
// asked at the site's own API, never the record's foreign origin, as a Bearer
|
||||
expect(sent[0]).toEqual(["GET", "https://api.z.ai/api/biz/v1/organization/o/projects/p/api_keys/copy/gone", "Bearer tok-z"])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user