From 8f19f2e13249d74075d43319c8a8364dfc121ea5 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 30 Sep 2026 20:58:51 +0800 Subject: [PATCH] =?UTF-8?q?workbuddy:=20desktop's=20sign-in=20is=20read=20?= =?UTF-8?q?where=20the=20app=20keeps=20it,=20not=20copied=20=E2=80=94=20a?= =?UTF-8?q?=20copied=20refresh=20token=20was=20the=20app's=20and=20the=20p?= =?UTF-8?q?lugin's=20both,=20and=20the=20first=20to=20use=20it=20signed=20?= =?UTF-8?q?the=20other=20out.=20The=20sign-in=20keeps=20a=20marker=20({sou?= =?UTF-8?q?rce:=20"desktop"});=20each=20request=20reads=20the=20app's=20.i?= =?UTF-8?q?nfo,=20and=20a=20token=20the=20app=20let=20lapse=20is=20renewed?= =?UTF-8?q?=20in=20memory=20only,=20as=20magpie's=20built-in=20does.=200.1?= =?UTF-8?q?.1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/workbuddy/README.md | 15 ++++--- packages/workbuddy/index.mjs | 75 ++++++++++++++++++++++----------- packages/workbuddy/package.json | 2 +- 3 files changed, 61 insertions(+), 31 deletions(-) diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index 5a60c3c..4540fce 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -17,22 +17,25 @@ The package exports `WorkBuddyAuthPlugin` (`workbuddy`) and one WorkBuddy's desktop app opens. The plugin then asks WorkBuddy for the token every second until you're signed in, for 5 minutes at most. Nothing needs to be pasted. -- **Desktop's sign-in.** Copies the account WorkBuddy desktop is signed in - to. The app's file is only read, never changed: +- **Desktop's sign-in.** Uses the account WorkBuddy desktop is signed in + to. Nothing is copied: the app's file is read on each request, and never + changed: - macOS: `~/Library/Application Support/CodeBuddyExtension/Data/Public/auth/workbuddy-desktop[-ai].info` - Windows: `~/AppData/Local/CodeBuddyExtension/…` - Linux: `~/.local/share/CodeBuddyExtension/…` A sign-in the app keeps encrypted can't be read, and this way fails. -The sign-in is kept as an OpenCode `oauth` auth: the access and refresh -tokens, their expiry, the user id and the domain. OpenCode keeps it 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 +marker, `{"type": "oauth", "source": "desktop", "accountId", "uid"}`. OpenCode keeps it in `auth.json`; magpie keeps it in `plugin-auth.json`. The access token is refreshed a minute before it ends, and the new one is saved. If the refresh fails, the old token is used while it lasts. -If you signed in with desktop's sign-in, the refresh can rotate the -desktop app's refresh token as well. The app then has to sign in again. +Desktop's sign-in is refreshed only when the app hasn't refreshed it +itself, and the new token is kept in memory, never written back, as magpie's +built-in WorkBuddy does. ## Requests diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 6885040..ea3d945 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -284,37 +284,64 @@ function desktopFile(site) { return join(base, "Data", "Public", "auth", site.authID + ".info") } -// desktopSignIn copies WorkBuddy desktop's sign-in (read only; the app's -// file is left as it is). Tokens the app keeps encrypted can't be read. +// readDesktop is WorkBuddy desktop's sign-in as the app keeps it now, null +// for none (or tokens the app keeps encrypted, which can't be read). +function readDesktop(site) { + try { + const f = JSON.parse(readFileSync(desktopFile(site), "utf8")) + const t = f?.auth ?? {} + if (typeof t.accessToken !== "string" || !t.accessToken || !f?.account?.uid) return null + const now = Date.now() + const acct = f.account + return { + access: t.accessToken, + refresh: typeof t.refreshToken === "string" ? t.refreshToken : "", + expires: t.expiresAt > 0 ? t.expiresAt : t.expiresIn > 0 ? now + t.expiresIn * 1000 : 0, + refreshExpiresAt: t.refreshExpiresAt > 0 ? t.refreshExpiresAt : t.refreshExpiresIn > 0 ? now + t.refreshExpiresIn * 1000 : 0, + domain: typeof t.domain === "string" ? t.domain : "", + tokenType: typeof t.tokenType === "string" ? t.tokenType : "", + uid: acct.uid, + name: acct.nickname || acct.phoneNumber || acct.uid, + } + } catch { + return null + } +} + +// 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 +// read each time, as the app keeps it. function desktopSignIn(site) { return { url: "", instructions: `Uses the account ${site.name} desktop is signed in to.`, method: "auto", async callback() { - try { - const f = JSON.parse(readFileSync(desktopFile(site), "utf8")) - const t = f?.auth ?? {} - if (typeof t.accessToken !== "string" || !t.accessToken) return { type: "failed" } - const now = Date.now() - const a = { - access: t.accessToken, - refresh: typeof t.refreshToken === "string" ? t.refreshToken : "", - expires: t.expiresAt > 0 ? t.expiresAt : t.expiresIn > 0 ? now + t.expiresIn * 1000 : 0, - refreshExpiresAt: t.refreshExpiresAt > 0 ? t.refreshExpiresAt : t.refreshExpiresIn > 0 ? now + t.refreshExpiresIn * 1000 : 0, - domain: typeof t.domain === "string" ? t.domain : "", - tokenType: typeof t.tokenType === "string" ? t.tokenType : "", - uid: f?.account?.uid ?? "", - } - const acct = f?.account ?? {} - return success(site, a, acct.nickname || acct.phoneNumber || acct.uid) - } catch { - return { type: "failed" } - } + const d = readDesktop(site) + if (!d) return { type: "failed" } + return { type: "success", refresh: "", access: "", expires: 0, source: "desktop", accountId: d.name || site.name, uid: d.uid } }, } } +// desktopHeld is what a desktop sign-in was renewed to here, by site and +// account: kept in memory only, never written where the app keeps it. +const desktopHeld = new Map() + +// current is the sign-in a stored auth names, fresh: WorkBuddy desktop's, +// read where it keeps it, or one kept here. +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`) + const k = site.id + "|" + d.uid + const h = desktopHeld.get(k) + const a = await fresh(site, null, h && h.expires >= d.expires ? h : d) + desktopHeld.set(k, a) + return a +} + // ---- models ----------------------------------------------------------------- // variants are the reasoning levels as OpenCode gives them to the AI SDK. @@ -422,9 +449,9 @@ function makePlugin(site) { // the account's own list, when it's signed in and WorkBuddy answers; // else the list above async models(provider, { auth }) { - if (auth?.type !== "oauth" || !auth.access) return provider.models + if (auth?.type !== "oauth" || !(auth.access || auth.source)) return provider.models try { - const a = await fresh(site, client, auth) + const a = await current(site, client, auth) const ms = await liveModels(site, a) if (!ms.length) return provider.models return Object.fromEntries(ms.map((m) => [m.id, modelOf(site, provider, m)])) @@ -444,7 +471,7 @@ function makePlugin(site) { async fetch(input, init) { let a = await getAuth() if (a?.type !== "oauth") throw new Error(`${site.name} isn't signed in`) - a = await fresh(site, client, a) + a = await current(site, client, a) const req = input instanceof Request ? input : null const headers = new Headers(init?.headers ?? req?.headers) headers.delete("authorization") diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index dd9209d..6005811 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.0", + "version": "0.1.1", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs",