diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index 438a546..1434c68 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -24,7 +24,8 @@ The package exports `WorkBuddyAuthPlugin` (`workbuddy`) and - Windows: `~/AppData/Local/CodeBuddyExtension/…` - Linux: `~/.local/share/CodeBuddyExtension/…` - A sign-in the app keeps encrypted can't be read, and this way fails. + WorkBuddy 5.6 and later keep the sign-in encrypted, which only the app + opens, so this way fails there and says so: use the browser sign-in. A browser sign-in is kept as an OpenCode `oauth` auth: the access and refresh tokens, their expiry, the user id and the domain; desktop's as a diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index d455aab..f8e1a55 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -931,6 +931,19 @@ function readDesktop(site) { } } +// notDesktop says why there is no desktop sign-in to use: WorkBuddy 5.6 +// and later seal its tokens at rest ({$wbEncrypted: 1, envelope}), which +// only the app itself opens (plugins#18), so the browser sign-in is the way; +// else the app isn't signed in. +function notDesktop(site) { + try { + const t = JSON.parse(readFileSync(desktopFile(site), "utf8"))?.auth ?? {} + if (t.accessToken?.$wbEncrypted || t.refreshToken?.$wbEncrypted) + return `${site.name} desktop keeps its sign-in encrypted (WorkBuddy 5.6 and later), which magpie can't read; sign in with "${site.name} account (browser)" instead` + } catch {} + return `${site.name} desktop isn't signed in` +} + // desktopSignIn uses the account WorkBuddy desktop is signed in to: nothing // is copied (a copied refresh token would be the app's and the plugin's // both, and the first to use it would sign the other out); its file is @@ -942,7 +955,7 @@ function desktopSignIn(site) { method: "auto", async callback() { const d = readDesktop(site) - if (!d) return { type: "failed", error: `${site.name} desktop isn't signed in` } + if (!d) return { type: "failed", error: notDesktop(site) } return { type: "success", refresh: "", access: "", expires: 0, source: "desktop", accountId: d.name || site.name, uid: d.uid } }, } @@ -957,7 +970,7 @@ const desktopHeld = new Map() async function current(site, client, auth) { if (auth?.source !== "desktop") return fresh(site, client, auth) const d = readDesktop(site) - if (!d) throw new Error(`${site.name} desktop isn't signed in`) + if (!d) throw new Error(notDesktop(site)) const k = site.id + "|" + d.uid const h = desktopHeld.get(k) const a = await fresh(site, null, h && h.expires >= d.expires ? h : d) @@ -1425,4 +1438,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf } +export const _internal = { notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 8dbb663..69b2cb5 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.17", + "version": "0.1.18", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs", diff --git a/packages/workbuddy/usage.test.mjs b/packages/workbuddy/usage.test.mjs index 6e194e9..85affb1 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -1,7 +1,7 @@ // auth.usage tells what magpie's built-in WorkBuddy usage told for the same // answers (internal/provider/workbuddy_test.go, workbuddy_ai_test.go). import { test, expect, afterAll, beforeAll, afterEach } from "bun:test" -import { mkdirSync, writeFileSync, readFileSync } from "node:fs" +import { mkdirSync, writeFileSync, readFileSync, rmSync } from "node:fs" import { homedir, tmpdir } from "node:os" import { realpathSync } from "node:fs" import { join } from "node:path" @@ -149,6 +149,29 @@ test("a desktop sign-in with the app signed out says why it failed", async () => expect(r).toEqual({ type: "failed", error: "WorkBuddy AI desktop isn't signed in" }) }) +// plugins#18 (seavenlin, WorkBuddy desktop 5.8.1): a sign-in the app seals +// at rest read as "isn't signed in", while the app was signed in +test("a desktop sign-in the app keeps encrypted says so, and that the browser sign-in is the way", async () => { + const dir = process.platform === "darwin" ? join(home, "Library", "Application Support", "CodeBuddyExtension") + : process.platform === "win32" ? join(home, "AppData", "Local", "CodeBuddyExtension") : join(home, ".local", "share", "CodeBuddyExtension") + mkdirSync(join(dir, "Data", "Public", "auth"), { recursive: true }) + const file = join(dir, "Data", "Public", "auth", "workbuddy-desktop-ai.info") + const sealed = { $wbEncrypted: 1, envelope: "AAAA" } + const info = JSON.stringify({ auth: { accessToken: sealed, refreshToken: sealed, expiresAt: Date.now() + 3600_000 }, account: { uid: "u9", nickname: "x" } }) + writeFileSync(file, info) + const want = 'WorkBuddy AI desktop keeps its sign-in encrypted (WorkBuddy 5.6 and later), which magpie can\'t read; sign in with "WorkBuddy AI account (browser)" instead' + const p = await WorkBuddyAIAuthPlugin({ client: {} }) + const m = p.auth.methods.find((x) => x.label.endsWith("desktop's sign-in")) + expect(await (await m.authorize()).callback()).toEqual({ type: "failed", error: want }) + // one signed in before the app sealed it: its usage and chats say the same + const auth = { type: "oauth", access: "", refresh: "", expires: 0, source: "desktop", uid: "u9" } + expect((await usageOf(WorkBuddyAIAuthPlugin, auth, { auth: { set: async () => {} } })).error).toBe(want) + const opts = await p.auth.loader(async () => auth) + await expect(opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: "{}" })).rejects.toThrow(want) + expect(readFileSync(file, "utf8")).toBe(info) + rmSync(file) +}) + // Each model carries the credits WorkBuddy's picker shows by it, as // magpie's built-in reads them (internal/provider/workbuddy_free_test.go, // TestWBCreditsRate): x0.03 is 0.03, X1.00 is 1; x0.00 is free and no