opencode-anthropic-auth 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +182 -0
- package/package.json +11 -0
package/index.mjs
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { generatePKCE } from "@openauthjs/openauth/pkce";
|
|
2
|
+
|
|
3
|
+
const CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {"max" | "console"} mode
|
|
7
|
+
*/
|
|
8
|
+
async function authorize(mode) {
|
|
9
|
+
const pkce = await generatePKCE();
|
|
10
|
+
|
|
11
|
+
const url = new URL(
|
|
12
|
+
`https://${mode === "console" ? "console.anthropic.com" : "claude.ai"}/oauth/authorize`,
|
|
13
|
+
import.meta.url,
|
|
14
|
+
);
|
|
15
|
+
url.searchParams.set("code", "true");
|
|
16
|
+
url.searchParams.set("client_id", CLIENT_ID);
|
|
17
|
+
url.searchParams.set("response_type", "code");
|
|
18
|
+
url.searchParams.set(
|
|
19
|
+
"redirect_uri",
|
|
20
|
+
"https://console.anthropic.com/oauth/code/callback",
|
|
21
|
+
);
|
|
22
|
+
url.searchParams.set(
|
|
23
|
+
"scope",
|
|
24
|
+
"org:create_api_key user:profile user:inference",
|
|
25
|
+
);
|
|
26
|
+
url.searchParams.set("code_challenge", pkce.challenge);
|
|
27
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
28
|
+
url.searchParams.set("state", pkce.verifier);
|
|
29
|
+
return {
|
|
30
|
+
url: url.toString(),
|
|
31
|
+
verifier: pkce.verifier,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {string} code
|
|
37
|
+
* @param {string} verifier
|
|
38
|
+
*/
|
|
39
|
+
async function exchange(code, verifier) {
|
|
40
|
+
const splits = code.split("#");
|
|
41
|
+
const result = await fetch("https://console.anthropic.com/v1/oauth/token", {
|
|
42
|
+
method: "POST",
|
|
43
|
+
headers: {
|
|
44
|
+
"Content-Type": "application/json",
|
|
45
|
+
},
|
|
46
|
+
body: JSON.stringify({
|
|
47
|
+
code: splits[0],
|
|
48
|
+
state: splits[1],
|
|
49
|
+
grant_type: "authorization_code",
|
|
50
|
+
client_id: CLIENT_ID,
|
|
51
|
+
redirect_uri: "https://console.anthropic.com/oauth/code/callback",
|
|
52
|
+
code_verifier: verifier,
|
|
53
|
+
}),
|
|
54
|
+
});
|
|
55
|
+
if (!result.ok)
|
|
56
|
+
return {
|
|
57
|
+
type: "failed",
|
|
58
|
+
};
|
|
59
|
+
const json = await result.json();
|
|
60
|
+
return {
|
|
61
|
+
type: "success",
|
|
62
|
+
refresh: json.refresh_token,
|
|
63
|
+
access: json.access_token,
|
|
64
|
+
expires: Date.now() + json.expires_in * 1000,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @type {import('@opencode-ai/plugin').Plugin}
|
|
70
|
+
*/
|
|
71
|
+
export async function AnthropicAuthPlugin({ client }) {
|
|
72
|
+
return {
|
|
73
|
+
auth: {
|
|
74
|
+
provider: "anthropic",
|
|
75
|
+
async loader(getAuth, provider) {
|
|
76
|
+
const auth = await getAuth();
|
|
77
|
+
if (auth.type === "oauth") {
|
|
78
|
+
// zero out cost for max plan
|
|
79
|
+
for (const model of Object.values(provider.models)) {
|
|
80
|
+
model.cost = {
|
|
81
|
+
input: 0,
|
|
82
|
+
output: 0,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
apiKey: "",
|
|
87
|
+
/**
|
|
88
|
+
* @param {any} input
|
|
89
|
+
* @param {any} init
|
|
90
|
+
*/
|
|
91
|
+
async fetch(input, init) {
|
|
92
|
+
const auth = await getAuth();
|
|
93
|
+
if (auth.type !== "oauth") return fetch(input, init);
|
|
94
|
+
if (!auth.access || auth.expires < Date.now()) {
|
|
95
|
+
const response = await fetch(
|
|
96
|
+
"https://console.anthropic.com/v1/oauth/token",
|
|
97
|
+
{
|
|
98
|
+
method: "POST",
|
|
99
|
+
headers: {
|
|
100
|
+
"Content-Type": "application/json",
|
|
101
|
+
},
|
|
102
|
+
body: JSON.stringify({
|
|
103
|
+
grant_type: "refresh_token",
|
|
104
|
+
refresh_token: auth.refresh,
|
|
105
|
+
client_id: CLIENT_ID,
|
|
106
|
+
}),
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
if (!response.ok) return;
|
|
110
|
+
const json = await response.json();
|
|
111
|
+
await client.auth.set({
|
|
112
|
+
path: {
|
|
113
|
+
id: "anthropic",
|
|
114
|
+
},
|
|
115
|
+
body: {
|
|
116
|
+
type: "oauth",
|
|
117
|
+
refresh: json.refresh_token,
|
|
118
|
+
access: json.access_token,
|
|
119
|
+
expires: Date.now() + json.expires_in * 1000,
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
auth.access = json.access_token;
|
|
123
|
+
}
|
|
124
|
+
const headers = {
|
|
125
|
+
...init.headers,
|
|
126
|
+
authorization: `Bearer ${auth.access}`,
|
|
127
|
+
"anthropic-beta":
|
|
128
|
+
"oauth-2025-04-20,claude-code-20250219,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14",
|
|
129
|
+
};
|
|
130
|
+
delete headers["x-api-key"];
|
|
131
|
+
return fetch(input, {
|
|
132
|
+
...init,
|
|
133
|
+
headers,
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return {};
|
|
140
|
+
},
|
|
141
|
+
methods: [
|
|
142
|
+
{
|
|
143
|
+
label: "Claude Pro/Max",
|
|
144
|
+
type: "oauth",
|
|
145
|
+
authorize: async () => {
|
|
146
|
+
const { url, verifier } = await authorize("max");
|
|
147
|
+
return {
|
|
148
|
+
url: url,
|
|
149
|
+
instructions: "Paste the authorization code here: ",
|
|
150
|
+
method: "code",
|
|
151
|
+
callback: async (code) => {
|
|
152
|
+
const credentials = await exchange(code, verifier);
|
|
153
|
+
return credentials;
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
label: "Create an API Key",
|
|
160
|
+
type: "oauth",
|
|
161
|
+
authorize: async () => {
|
|
162
|
+
const { url, verifier } = await authorize("console");
|
|
163
|
+
return {
|
|
164
|
+
url: url,
|
|
165
|
+
instructions: "Paste the authorization code here: ",
|
|
166
|
+
method: "code",
|
|
167
|
+
callback: async (code) => {
|
|
168
|
+
const credentials = await exchange(code, verifier);
|
|
169
|
+
return credentials;
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
provider: "anthropic",
|
|
176
|
+
label: "Manually enter API Key",
|
|
177
|
+
type: "api",
|
|
178
|
+
},
|
|
179
|
+
],
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|