Skip to content

Commit 1471392

Browse files
JosXaJosXa
authored andcommitted
fix: allow user plugins to override built-in auth plugins (anomalyco#11058)
Co-authored-by: JosXa <info@josxa.dev>
1 parent a4eb49d commit 1471392

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

packages/opencode/src/cli/cmd/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export const AuthLoginCommand = cmd({
307307

308308
if (prompts.isCancel(provider)) throw new UI.CancelledError()
309309

310-
const plugin = await Plugin.list().then((x) => x.find((x) => x.auth?.provider === provider))
310+
const plugin = await Plugin.list().then((x) => x.findLast((x) => x.auth?.provider === provider))
311311
if (plugin && plugin.auth) {
312312
const handled = await handlePluginAuth({ auth: plugin.auth }, provider)
313313
if (handled) return
@@ -323,7 +323,7 @@ export const AuthLoginCommand = cmd({
323323
if (prompts.isCancel(provider)) throw new UI.CancelledError()
324324

325325
// Check if a plugin provides auth for this custom provider
326-
const customPlugin = await Plugin.list().then((x) => x.find((x) => x.auth?.provider === provider))
326+
const customPlugin = await Plugin.list().then((x) => x.findLast((x) => x.auth?.provider === provider))
327327
if (customPlugin && customPlugin.auth) {
328328
const handled = await handlePluginAuth({ auth: customPlugin.auth }, provider)
329329
if (handled) return
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, expect, test } from "bun:test"
2+
import path from "path"
3+
import fs from "fs/promises"
4+
import { tmpdir } from "../fixture/fixture"
5+
import { Instance } from "../../src/project/instance"
6+
import { ProviderAuth } from "../../src/provider/auth"
7+
8+
describe("plugin.auth-override", () => {
9+
test("user plugin overrides built-in github-copilot auth", async () => {
10+
await using tmp = await tmpdir({
11+
init: async (dir) => {
12+
const pluginDir = path.join(dir, ".opencode", "plugin")
13+
await fs.mkdir(pluginDir, { recursive: true })
14+
15+
await Bun.write(
16+
path.join(pluginDir, "custom-copilot-auth.ts"),
17+
[
18+
"export default async () => ({",
19+
" auth: {",
20+
' provider: "github-copilot",',
21+
" methods: [",
22+
' { type: "api", label: "Test Override Auth" },',
23+
" ],",
24+
" loader: async () => ({ access: 'test-token' }),",
25+
" },",
26+
"})",
27+
"",
28+
].join("\n"),
29+
)
30+
},
31+
})
32+
33+
await Instance.provide({
34+
directory: tmp.path,
35+
fn: async () => {
36+
const methods = await ProviderAuth.methods()
37+
const copilot = methods["github-copilot"]
38+
expect(copilot).toBeDefined()
39+
expect(copilot.length).toBe(1)
40+
expect(copilot[0].label).toBe("Test Override Auth")
41+
},
42+
})
43+
}, 30000) // Increased timeout for plugin installation
44+
})

0 commit comments

Comments
 (0)