Skip to content

Commit e70d984

Browse files
committed
tui: enable password authentication for remote session attachment
Allow users to authenticate when attaching to a remote OpenCode session by supporting basic auth via a password flag or OPENCODE_SERVER_PASSWORD environment variable
1 parent da7c874 commit e70d984

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

packages/opencode/src/cli/cmd/tui/app.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export function tui(input: {
104104
args: Args
105105
directory?: string
106106
fetch?: typeof fetch
107+
headers?: RequestInit["headers"]
107108
events?: EventSource
108109
onExit?: () => Promise<void>
109110
}) {
@@ -130,6 +131,7 @@ export function tui(input: {
130131
url={input.url}
131132
directory={input.directory}
132133
fetch={input.fetch}
134+
headers={input.headers}
133135
events={input.events}
134136
>
135137
<SyncProvider>

packages/opencode/src/cli/cmd/tui/attach.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,34 @@ export const AttachCommand = cmd({
1919
alias: ["s"],
2020
type: "string",
2121
describe: "session id to continue",
22+
})
23+
.option("password", {
24+
alias: ["p"],
25+
type: "string",
26+
describe: "basic auth password (defaults to OPENCODE_SERVER_PASSWORD)",
2227
}),
2328
handler: async (args) => {
24-
let directory = args.dir
25-
if (args.dir) {
29+
const directory = (() => {
30+
if (!args.dir) return undefined
2631
try {
2732
process.chdir(args.dir)
28-
directory = process.cwd()
33+
return process.cwd()
2934
} catch {
3035
// If the directory doesn't exist locally (remote attach), pass it through.
36+
return args.dir
3137
}
32-
}
38+
})()
39+
const headers = (() => {
40+
const password = args.password ?? process.env.OPENCODE_SERVER_PASSWORD
41+
if (!password) return undefined
42+
const auth = `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}`
43+
return { Authorization: auth }
44+
})()
3345
await tui({
3446
url: args.url,
3547
args: { sessionID: args.session },
3648
directory,
49+
headers,
3750
})
3851
},
3952
})

packages/opencode/src/cli/cmd/tui/context/sdk.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ export type EventSource = {
99

1010
export const { use: useSDK, provider: SDKProvider } = createSimpleContext({
1111
name: "SDK",
12-
init: (props: { url: string; directory?: string; fetch?: typeof fetch; events?: EventSource }) => {
12+
init: (props: {
13+
url: string
14+
directory?: string
15+
fetch?: typeof fetch
16+
headers?: RequestInit["headers"]
17+
events?: EventSource
18+
}) => {
1319
const abort = new AbortController()
1420
const sdk = createOpencodeClient({
1521
baseUrl: props.url,
1622
signal: abort.signal,
1723
directory: props.directory,
1824
fetch: props.fetch,
25+
headers: props.headers,
1926
})
2027

2128
const emitter = createGlobalEmitter<{

0 commit comments

Comments
 (0)