workbuddy: desktop's sign-in is read where the app keeps it, not copied — a copied refresh token was the app's and the plugin's both, and the first to use it signed the other out. The sign-in keeps a marker ({source: "desktop"}); each request reads the app's .info, and a token the app let lapse is renewed in memory only, as magpie's built-in does. 0.1.1.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user