GeezerVison is showing github.com — fix(core): preserve OAuth method switching · anomalyco/opencode@9b73e65
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/integration.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -606,8 +606,8 @@ const layer = Layer.effect( | 606 | 606 | return yield* new AuthorizationError({ cause: new Error("OAuth credential belongs to another integration") }) | 607 | 607 | if (saved.value.type !== "oauth") | 608 | 608 | 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 | 611 | 611 | return saved.value | 612 | 612 | }) | 613 | 613 | const answer = input.answer ?? {} | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/integration.test.ts +55-2Lines changed: 55 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -326,7 +326,7 @@ describe("Integration", () => { | 326 | 326 | }), | 327 | 327 | ) | 328 | 328 |
| 329 | | - for (const target of ["missing", "other-integration", "key", "other-method"] as const) { | | 329 | + for (const target of ["missing", "other-integration", "key"] as const) { | 330 | 330 | it.effect(`rejects an OAuth ${target} target before authorization without changing credentials`, () => | 331 | 331 | Effect.gen(function* () { | 332 | 332 | const integrations = yield* Integration.Service | @@ -356,7 +356,7 @@ describe("Integration", () => { | 356 | 356 | ? Credential.Key.make({ type: "key", key: "secret" }) | 357 | 357 | : Credential.OAuth.make({ | 358 | 358 | type: "oauth", | 359 | | - methodID: target === "other-method" ? Integration.MethodID.make("other") : methodID, | | 359 | + methodID, | 360 | 360 | access: "access", | 361 | 361 | refresh: "refresh", | 362 | 362 | expires: 1, | @@ -381,6 +381,59 @@ describe("Integration", () => { | 381 | 381 | ) | 382 | 382 | } | 383 | 383 |
| | 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 | + | 384 | 437 | it.effect("keeps code attempts open when the code is missing and closes them on cancel", () => | 385 | 438 | Effect.gen(function* () { | 386 | 439 | const integrations = yield* Integration.Service | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -4,7 +4,6 @@ import { Session } from "@opencode/core/session" | 4 | 4 | import { OpenAIResponses } from "@opencode/ai/protocols/openai-responses" | 5 | 5 | import { AIError, HttpContext, LLM, Message, RateLimitError } from "@opencode/ai" | 6 | 6 | import { classifyProviderFailure } from "@opencode/ai/provider-error" | 7 | | -import { fromPromise } from "@opencode/plugin/promise/adapter" | 8 | 7 | import { compileRequest } from "@opencode/ai/route/client" | 9 | 8 | import { describe, expect } from "bun:test" | 10 | 9 | // import { DateTime, Deferred, Effect, Schedule } from "effect" | @@ -99,136 +98,6 @@ const request = Effect.fn(function* ( | 99 | 98 | } | 100 | 99 | }) | 101 | 100 |
| 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 | | - | 232 | 101 | describe("ChatGPTPlugin", () => { | 233 | 102 | it.live("loads only visible API-supported ChatGPT models", () => | 234 | 103 | Effect.gen(function* () { | @@ -614,6 +483,41 @@ describe("ChatGPTPlugin", () => { | 614 | 483 | }), | 615 | 484 | ) | 616 | 485 |
| | 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 | + | 617 | 521 | it.live("reauthorizes a non-active ChatGPT connection instead of the active one", () => | 618 | 522 | Effect.gen(function* () { | 619 | 523 | const credentials = yield* Credential.Service | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -76,10 +76,12 @@ test("starts a new OAuth registration from add account without targeting an exis | 76 | 76 | } | 77 | 77 | }) | 78 | 78 |
| | 79 | +// This HTTP fixture verifies UI request wiring, not core reauthentication or method-switch semantics. | 79 | 80 | 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 }) => { | 83 | 85 | const fixture = await renderIntegration(undefined, undefined, { oauth: true }) | 84 | 86 |
| 85 | 87 | try { | @@ -89,15 +91,17 @@ test.each([ | 89 | 91 | await fixture.app.waitForFrame( | 90 | 92 | (frame) => | 91 | 93 | frame.includes("Browser login") && | | 94 | + frame.includes("Device login") && | 92 | 95 | frame.includes("API key") && | 93 | 96 | fixture.app.renderer.currentFocusedEditor instanceof InputRenderable, | 94 | 97 | ) | 95 | 98 | expect(fixture.requests).toEqual([]) | | 99 | + if (methodID === "device") fixture.app.mockInput.pressArrow("down") | 96 | 100 | fixture.app.mockInput.pressEnter() | 97 | 101 | await fixture.app.waitFor(() => fixture.requests.length === 2) | 98 | 102 |
| 99 | 103 | 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 } }, | 101 | 105 | { method: "GET", path: "/api/integration/openai/connect/oauth/attempt_test" }, | 102 | 106 | ]) | 103 | 107 | expect(fixture.accounts.map((account) => account.id)).toEqual(["cred_personal", "cred_work"]) | @@ -393,7 +397,12 @@ async function renderIntegration( | 393 | 397 | id: "openai", | 394 | 398 | name: "OpenAI", | 395 | 399 | 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 | + : []), | 397 | 406 | { type: "key", label: "API key", form }, | 398 | 407 | ], | 399 | 408 | connections: [...accounts, { type: "env", name: "OPENAI_API_KEY" }], | |
|