Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions packages/opencode/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Global } from "../global"
import fs from "fs/promises"
import z from "zod"
import { Lock } from "../util/lock"
import * as crypto from "crypto"
import { Log } from "../util/log"

export const OAUTH_DUMMY_KEY = "opencode-oauth-dummy-key"

Expand Down Expand Up @@ -47,15 +49,22 @@ export namespace Auth {
const release = await Lock.read("auth")
try {
const file = Bun.file(filepath)

if (!(await file.exists())) return {}

const data = await file.json()


let data: unknown
try {
data = await file.json()
} catch (error) {
Log.Default.warn("auth.json corrupted or unreadable, returning empty", { error })
return {}
}

if (typeof data !== "object" || data === null) {
throw new Error("auth.json contains invalid data")
Log.Default.warn("auth.json contains invalid data, returning empty")
return {}
}

return Object.entries(data).reduce(
(acc, [key, value]) => {
const parsed = Info.safeParse(value)
Expand Down Expand Up @@ -86,8 +95,10 @@ export namespace Auth {
}

data[key] = info
await Bun.write(file, JSON.stringify(data, null, 2))
await fs.chmod(filepath, 0o600)
const temp = filepath + ".tmp." + crypto.randomUUID()
await Bun.write(temp, JSON.stringify(data, null, 2))
await fs.chmod(temp, 0o600)
await fs.rename(temp, filepath)
} finally {
release[Symbol.dispose]()
}
Expand All @@ -109,8 +120,10 @@ export namespace Auth {
}

delete data[key]
await Bun.write(file, JSON.stringify(data, null, 2))
await fs.chmod(filepath, 0o600)
const temp = filepath + ".tmp." + crypto.randomUUID()
await Bun.write(temp, JSON.stringify(data, null, 2))
await fs.chmod(temp, 0o600)
await fs.rename(temp, filepath)
} finally {
release[Symbol.dispose]()
}
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/plugin/codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export async function CodexAuthPlugin(input: PluginInput): Promise<Hooks> {
provider: "openai",
async loader(getAuth, provider) {
const auth = await getAuth()
if (auth.type !== "oauth") return {}
if (!auth || auth.type !== "oauth") return {}

// Filter models to only allowed Codex models for OAuth
const allowedModels = new Set([
Expand Down
Loading