GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(core): preserve OAuth method switching · anomalyco/opencode@9b73e65

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • core
      • src
        • integration.ts
      • test
        • integration.test.ts
        • plugin
          • provider-chatgpt.test.ts
    • tui/test/cli/tui
      • dialog-integration.test.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/core/src/integration.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/integration.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,8 @@ const layer = Layer.effect(
606606
return yield* new AuthorizationError({ cause: new Error("OAuth credential belongs to another integration") })
607607
if (saved.value.type !== "oauth")
608608
return yield* new AuthorizationError({ cause: new Error("Credential is not an OAuth credential") })
609-
if (saved.value.methodID !== input.methodID)
610-
return yield* new AuthorizationError({ cause: new Error("OAuth credential belongs to another method") })
609+
// Choosing another OAuth method starts a new connection without reusing the old registration.
610+
if (saved.value.methodID !== input.methodID) return undefined
611611
return saved.value
612612
})
613613
const answer = input.answer ?? {}
Collapse file

‎packages/core/test/integration.test.ts‎

Copy file name to clipboardExpand all lines: packages/core/test/integration.test.ts
+55-2Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ describe("Integration", () => {
326326
}),
327327
)
328328

329-
for (const target of ["missing", "other-integration", "key", "other-method"] as const) {
329+
for (const target of ["missing", "other-integration", "key"] as const) {
330330
it.effect(`rejects an OAuth ${target} target before authorization without changing credentials`, () =>
331331
Effect.gen(function* () {
332332
const integrations = yield* Integration.Service
@@ -356,7 +356,7 @@ describe("Integration", () => {
356356
? Credential.Key.make({ type: "key", key: "secret" })
357357
: Credential.OAuth.make({
358358
type: "oauth",
359-
methodID: target === "other-method" ? Integration.MethodID.make("other") : methodID,
359+
methodID,
360360
access: "access",
361361
refresh: "refresh",
362362
expires: 1,
@@ -381,6 +381,59 @@ describe("Integration", () => {
381381
)
382382
}
383383

384+
for (const sameMethod of [true, false]) {
385+
it.effect(`authorizes an OAuth target with ${sameMethod ? "the same" : "a different"} method`, () =>
386+
Effect.gen(function* () {
387+
const integrations = yield* Integration.Service
388+
const credentials = yield* Credential.Service
389+
const integrationID = Integration.ID.make("openai")
390+
const value = Credential.OAuth.make({
391+
type: "oauth",
392+
methodID: Integration.MethodID.make("original"),
393+
access: "old-access",
394+
refresh: "old-refresh",
395+
expires: 1,
396+
})
397+
const target = yield* credentials.create({ integrationID, value })
398+
const methodID = sameMethod ? value.methodID : Integration.MethodID.make("selected")
399+
const answer = { account: "work" }
400+
yield* integrations.transform((editor) =>
401+
editor.method.update({
402+
integrationID,
403+
method: {
404+
id: methodID,
405+
type: "oauth",
406+
label: "Selected",
407+
form: [{ type: "string", key: "account", title: "Account", required: true }],
408+
},
409+
authorize: (received, context) => {
410+
expect(received).toEqual(answer)
411+
expect(context?.credential).toEqual(sameMethod ? value : undefined)
412+
return Effect.succeed({
413+
mode: "code" as const,
414+
url: "https://example.com/authorize",
415+
instructions: "Paste the code",
416+
callback: (code: string) => Effect.succeed(Credential.OAuth.make({ ...value, methodID, access: code })),
417+
})
418+
},
419+
}),
420+
)
421+
const before = yield* credentials.list(integrationID)
422+
const active = yield* integrations.connection.active(integrationID)
423+
const attempt = yield* integrations.oauth.connect({ integrationID, methodID, credentialID: target.id, answer })
424+
expect(yield* credentials.list(integrationID)).toEqual(before)
425+
expect(yield* integrations.connection.active(integrationID)).toEqual(active)
426+
427+
yield* integrations.oauth.complete({ integrationID, attemptID: attempt.attemptID, code: "new-access" })
428+
const after = yield* credentials.list(integrationID)
429+
expect(after.find((entry) => entry.id === target.id)?.value).toEqual(value)
430+
expect(after.filter((entry) => entry.id !== target.id)).toEqual([
431+
expect.objectContaining({ value: Credential.OAuth.make({ ...value, methodID, access: "new-access" }) }),
432+
])
433+
}),
434+
)
435+
}
436+
384437
it.effect("keeps code attempts open when the code is missing and closes them on cancel", () =>
385438
Effect.gen(function* () {
386439
const integrations = yield* Integration.Service
Collapse file

‎packages/core/test/plugin/provider-chatgpt.test.ts‎

Copy file name to clipboardExpand all lines: packages/core/test/plugin/provider-chatgpt.test.ts
+35-131Lines changed: 35 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Session } from "@opencode/core/session"
44
import { OpenAIResponses } from "@opencode/ai/protocols/openai-responses"
55
import { AIError, HttpContext, LLM, Message, RateLimitError } from "@opencode/ai"
66
import { classifyProviderFailure } from "@opencode/ai/provider-error"
7-
import { fromPromise } from "@opencode/plugin/promise/adapter"
87
import { compileRequest } from "@opencode/ai/route/client"
98
import { describe, expect } from "bun:test"
109
// import { DateTime, Deferred, Effect, Schedule } from "effect"
@@ -99,136 +98,6 @@ const request = Effect.fn(function* (
9998
}
10099
})
101100

102-
describe("OAuth plugin authorization context", () => {
103-
for (const kind of ["effect", "promise"] as const) {
104-
it.effect(`passes the target credential and answers through the ${kind} plugin adapter`, () =>
105-
Effect.gen(function* () {
106-
const integrations = yield* Integration.Service
107-
const credentials = yield* Credential.Service
108-
const plugin = yield* Plugin.Service
109-
const host = yield* PluginHost.make(plugin)
110-
const integrationID = Integration.ID.make("context-fixture")
111-
const methodID = Integration.MethodID.make("oauth")
112-
const value = Credential.OAuth.make({
113-
type: "oauth",
114-
methodID,
115-
access: "target-access",
116-
refresh: "target-refresh",
117-
expires: 1,
118-
metadata: { clientID: "target-client" },
119-
})
120-
const target = yield* credentials.create({ integrationID, value })
121-
yield* credentials.create({
122-
integrationID,
123-
value: Credential.OAuth.make({ type: "oauth", methodID, access: "active", refresh: "active", expires: 1 }),
124-
})
125-
const method = {
126-
id: methodID,
127-
type: "oauth" as const,
128-
label: "Fixture",
129-
form: [{ type: "string" as const, key: "account", title: "Account", required: true }],
130-
} satisfies Integration.OAuthMethod
131-
const authorization = {
132-
mode: "code" as const,
133-
url: "https://example.com/authorize",
134-
instructions: "Paste the code",
135-
}
136-
if (kind === "effect") {
137-
yield* host.integration.transform((editor) =>
138-
editor.method.update({
139-
integrationID,
140-
method,
141-
authorize: (answer, context) => {
142-
expect(answer).toEqual({ account: "work" })
143-
expect(context?.credential).toEqual(value)
144-
return Effect.succeed({
145-
...authorization,
146-
callback: (code: string) => Effect.succeed(Credential.OAuth.make({ ...value, access: code })),
147-
})
148-
},
149-
}),
150-
)
151-
}
152-
if (kind === "promise") {
153-
yield* fromPromise({
154-
id: "oauth-context-fixture",
155-
setup: async (context) => {
156-
await context.integration.transform((editor) =>
157-
editor.method.update({
158-
integrationID,
159-
method,
160-
authorize: async (answer, context) => {
161-
expect(answer).toEqual({ account: "work" })
162-
expect(context?.credential).toEqual(value)
163-
return {
164-
...authorization,
165-
callback: async (code: string) => Credential.OAuth.make({ ...value, access: code }),
166-
}
167-
},
168-
}),
169-
)
170-
},
171-
}).effect(host)
172-
}
173-
174-
const attempt = yield* integrations.oauth.connect({
175-
integrationID,
176-
methodID,
177-
credentialID: target.id,
178-
answer: { account: "work" },
179-
})
180-
expect(attempt.url).toBe(authorization.url)
181-
yield* integrations.oauth.complete({ integrationID, attemptID: attempt.attemptID, code: "code" })
182-
expect((yield* credentials.list(integrationID)).map((entry) => entry.value)).toContainEqual(
183-
Credential.OAuth.make({ ...value, access: "code" }),
184-
)
185-
}),
186-
)
187-
}
188-
189-
it.effect("keeps single-argument Promise OAuth plugins compatible with explicit targets", () =>
190-
Effect.gen(function* () {
191-
const integrations = yield* Integration.Service
192-
const credentials = yield* Credential.Service
193-
const plugin = yield* Plugin.Service
194-
const host = yield* PluginHost.make(plugin)
195-
const integrationID = Integration.ID.make("legacy-fixture")
196-
const methodID = Integration.MethodID.make("oauth")
197-
const target = yield* credentials.create({
198-
integrationID,
199-
value: Credential.OAuth.make({ type: "oauth", methodID, access: "old", refresh: "old", expires: 1 }),
200-
})
201-
yield* fromPromise({
202-
id: "legacy-oauth-fixture",
203-
setup: async (context) => {
204-
await context.integration.transform((editor) =>
205-
editor.method.update({
206-
integrationID,
207-
method: { id: methodID, type: "oauth", label: "Legacy" },
208-
authorize: async (answer) => {
209-
expect(answer).toEqual({})
210-
return {
211-
mode: "code" as const,
212-
url: "https://example.com/legacy",
213-
instructions: "Paste the code",
214-
callback: async (code: string) =>
215-
Credential.OAuth.make({ type: "oauth", methodID, access: code, refresh: "new", expires: 2 }),
216-
}
217-
},
218-
}),
219-
)
220-
},
221-
}).effect(host)
222-
223-
const attempt = yield* integrations.oauth.connect({ integrationID, methodID, credentialID: target.id })
224-
yield* integrations.oauth.complete({ integrationID, attemptID: attempt.attemptID, code: "new-access" })
225-
expect((yield* credentials.list(integrationID)).map((entry) => entry.value)).toContainEqual(
226-
expect.objectContaining({ access: "new-access", refresh: "new" }),
227-
)
228-
}),
229-
)
230-
})
231-
232101
describe("ChatGPTPlugin", () => {
233102
it.live("loads only visible API-supported ChatGPT models", () =>
234103
Effect.gen(function* () {
@@ -614,6 +483,41 @@ describe("ChatGPTPlugin", () => {
614483
}),
615484
)
616485

486+
it.live("registers a new ChatGPT agent when switching a legacy target to token sharing", () =>
487+
Effect.gen(function* () {
488+
const credentials = yield* Credential.Service
489+
const integrationID = Integration.ID.make("openai")
490+
const legacy = yield* credentials.create({
491+
integrationID,
492+
value: Credential.OAuth.make({
493+
type: "oauth",
494+
methodID: Integration.MethodID.make("chatgpt-browser"),
495+
access: "legacy-access",
496+
refresh: "legacy-refresh",
497+
expires: Date.now() + 60 * 60_000,
498+
metadata: { accountID: "acct_legacy" },
499+
}),
500+
})
501+
yield* credentials.create({
502+
integrationID,
503+
value: Credential.OAuth.make({
504+
type: "oauth",
505+
methodID: Integration.MethodID.make("chatgpt-token-sharing"),
506+
access: "active-access",
507+
refresh: "active-refresh",
508+
expires: Date.now() + 60 * 60_000,
509+
metadata: { clientID: "oaiapp_active" },
510+
}),
511+
})
512+
yield* addPlugin()
513+
const url = yield* authorize(legacy.id)
514+
expect(Object.fromEntries(url.searchParams)).toMatchObject({
515+
client_id: "dynamic_agent_client",
516+
agent_name_hint: "OpenCode",
517+
})
518+
}),
519+
)
520+
617521
it.live("reauthorizes a non-active ChatGPT connection instead of the active one", () =>
618522
Effect.gen(function* () {
619523
const credentials = yield* Credential.Service
Collapse file

‎packages/tui/test/cli/tui/dialog-integration.test.tsx‎

Copy file name to clipboardExpand all lines: packages/tui/test/cli/tui/dialog-integration.test.tsx
+14-5Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ test("starts a new OAuth registration from add account without targeting an exis
7676
}
7777
})
7878

79+
// This HTTP fixture verifies UI request wiring, not core reauthentication or method-switch semantics.
7980
test.each([
80-
{ credentialID: "cred_personal", moves: 1 },
81-
{ credentialID: "cred_work", moves: 2 },
82-
])("reauthenticates $credentialID through method selection without activating it", async ({ credentialID, moves }) => {
81+
{ credentialID: "cred_personal", moves: 1, methodID: "browser" },
82+
{ credentialID: "cred_work", moves: 2, methodID: "browser" },
83+
{ credentialID: "cred_work", moves: 2, methodID: "device" },
84+
])("starts $methodID OAuth for $credentialID without activating it", async ({ credentialID, moves, methodID }) => {
8385
const fixture = await renderIntegration(undefined, undefined, { oauth: true })
8486

8587
try {
@@ -89,15 +91,17 @@ test.each([
8991
await fixture.app.waitForFrame(
9092
(frame) =>
9193
frame.includes("Browser login") &&
94+
frame.includes("Device login") &&
9295
frame.includes("API key") &&
9396
fixture.app.renderer.currentFocusedEditor instanceof InputRenderable,
9497
)
9598
expect(fixture.requests).toEqual([])
99+
if (methodID === "device") fixture.app.mockInput.pressArrow("down")
96100
fixture.app.mockInput.pressEnter()
97101
await fixture.app.waitFor(() => fixture.requests.length === 2)
98102

99103
expect(fixture.requests).toEqual([
100-
{ method: "POST", path: "/api/integration/openai/connect/oauth", body: { methodID: "browser", credentialID } },
104+
{ method: "POST", path: "/api/integration/openai/connect/oauth", body: { methodID, credentialID } },
101105
{ method: "GET", path: "/api/integration/openai/connect/oauth/attempt_test" },
102106
])
103107
expect(fixture.accounts.map((account) => account.id)).toEqual(["cred_personal", "cred_work"])
@@ -393,7 +397,12 @@ async function renderIntegration(
393397
id: "openai",
394398
name: "OpenAI",
395399
methods: [
396-
...(options?.oauth ? [{ type: "oauth", id: "browser", label: "Browser login" }] : []),
400+
...(options?.oauth
401+
? [
402+
{ type: "oauth", id: "browser", label: "Browser login" },
403+
{ type: "oauth", id: "device", label: "Device login" },
404+
]
405+
: []),
397406
{ type: "key", label: "API key", form },
398407
],
399408
connections: [...accounts, { type: "env", name: "OPENAI_API_KEY" }],

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.