Skip to content

Commit 1652026

Browse files
thdxrOpenCode
andcommitted
update auth and provider configuration
🤖 Generated with [OpenCode](https://opencode.ai) Co-Authored-By: OpenCode <noreply@opencode.ai>
1 parent 65b2cf7 commit 1652026

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

packages/opencode/src/auth/anthropic.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// Example: https://claude.ai/oauth/authorize?code=true&client_id=9d1c250a-e61b-44d9-88ed-5944d1962f5e&response_type=code&redirect_uri=https%3A%2F%2Fconsole.anthropic.com%2Foauth%2Fcode%2Fcallback&scope=org%3Acreate_api_key+user%3Aprofile+user%3Ainference&code_challenge=MdFtFgFap23AWDSN0oa3-eaKjQRFE4CaEhXx8M9fHZg&code_challenge_method=S256&state=rKLtaDzm88GSwekyEqdi0wXX-YqIr13tSzYymSzpvfs
2-
31
import { generatePKCE } from "@openauthjs/openauth/pkce"
42
import { Global } from "../global"
53
import path from "path"
64

75
export namespace AuthAnthropic {
6+
const CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
7+
88
export async function authorize() {
99
const pkce = await generatePKCE()
1010
const url = new URL("https://claude.ai/oauth/authorize", import.meta.url)
@@ -48,14 +48,30 @@ export namespace AuthAnthropic {
4848
await Bun.write(path.join(Global.Path.data, "anthropic.json"), result)
4949
}
5050

51-
export async function load() {
51+
export async function access() {
5252
const file = Bun.file(path.join(Global.Path.data, "anthropic.json"))
5353
if (!(await file.exists())) return
5454
const result = await file.json()
55-
return {
56-
accessToken: result.access_token as string,
57-
refreshToken: result.refresh_token as string,
58-
}
55+
const refresh = result.refresh_token
56+
const now = Date.now()
57+
const response = await fetch(
58+
"https://console.anthropic.com/v1/oauth/token",
59+
{
60+
method: "POST",
61+
headers: {
62+
"Content-Type": "application/json",
63+
},
64+
body: JSON.stringify({
65+
grant_type: "refresh_token",
66+
refresh_token: refresh,
67+
client_id: CLIENT_ID,
68+
}),
69+
},
70+
)
71+
if (!response.ok) return
72+
const json = await response.json()
73+
await Bun.write(file, JSON.stringify(json))
74+
return json.access_token as string
5975
}
6076

6177
export class ExchangeFailed extends Error {

packages/opencode/src/provider/provider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ export namespace Provider {
7676
(provider: Info) => Promise<Record<string, any> | false>
7777
> = {
7878
anthropic: async () => {
79-
const result = await AuthAnthropic.load()
80-
if (result)
79+
const access = await AuthAnthropic.access()
80+
if (access)
8181
return {
8282
apiKey: "",
8383
headers: {
84-
authorization: `Bearer ${result.accessToken}`,
84+
authorization: `Bearer ${access}`,
8585
"anthropic-beta": "oauth-2025-04-20",
8686
},
8787
}

0 commit comments

Comments
 (0)