opencode-codebuddy-external-auth 1.0.0 → 1.0.2
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/dist/plugin.js +19 -29
- package/package.json +1 -1
package/dist/plugin.js
CHANGED
|
@@ -8,11 +8,7 @@ const PROVIDER_ID = "codebuddy-external";
|
|
|
8
8
|
const CONFIG = {
|
|
9
9
|
// IOA 版本使用 copilot.tencent.com
|
|
10
10
|
serverUrl: "https://copilot.tencent.com",
|
|
11
|
-
//
|
|
12
|
-
authId: "Tencent-Cloud.coding-copilot",
|
|
13
|
-
// API 端点
|
|
14
|
-
// 注意:Anthropic SDK 会自动追加 /messages,所以 baseURL 需要以 /v1 结尾
|
|
15
|
-
// 完整路径: https://copilot.tencent.com/plugin/v1/messages
|
|
11
|
+
// API 端点 - Anthropic SDK 会自动追加 /messages
|
|
16
12
|
apiBaseUrl: "https://copilot.tencent.com/plugin/v1",
|
|
17
13
|
// 平台标识
|
|
18
14
|
platform: "CLI",
|
|
@@ -24,6 +20,9 @@ const CONFIG = {
|
|
|
24
20
|
function sleep(ms) {
|
|
25
21
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
26
22
|
}
|
|
23
|
+
function generateState() {
|
|
24
|
+
return crypto.randomUUID();
|
|
25
|
+
}
|
|
27
26
|
/**
|
|
28
27
|
* Creates an authenticated fetch function with CodeBuddy headers
|
|
29
28
|
*/
|
|
@@ -44,30 +43,18 @@ function createAuthenticatedFetch(accessToken, userId) {
|
|
|
44
43
|
};
|
|
45
44
|
}
|
|
46
45
|
// ============================================================================
|
|
47
|
-
// OAuth Flow Implementation (
|
|
46
|
+
// OAuth Flow Implementation (IOA Login)
|
|
48
47
|
// ============================================================================
|
|
49
48
|
/**
|
|
50
|
-
*
|
|
49
|
+
* Build the login URL for IOA authentication
|
|
51
50
|
*/
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
},
|
|
51
|
+
function buildLoginUrl(state) {
|
|
52
|
+
const params = new URLSearchParams({
|
|
53
|
+
platform: CONFIG.platform,
|
|
54
|
+
state: state,
|
|
55
|
+
ioa: "1", // IOA version flag
|
|
58
56
|
});
|
|
59
|
-
|
|
60
|
-
throw new Error(`Auth state request failed: ${response.status}`);
|
|
61
|
-
}
|
|
62
|
-
const data = await response.json();
|
|
63
|
-
if (!data.data?.state || !data.data?.url) {
|
|
64
|
-
throw new Error("Invalid auth state response");
|
|
65
|
-
}
|
|
66
|
-
return {
|
|
67
|
-
state: data.data.state,
|
|
68
|
-
url: data.data.url,
|
|
69
|
-
expiresAt: Date.now() + 10 * 60 * 1000, // 10 minutes
|
|
70
|
-
};
|
|
57
|
+
return `${CONFIG.serverUrl}/login?${params.toString()}`;
|
|
71
58
|
}
|
|
72
59
|
/**
|
|
73
60
|
* Poll for token after user completes browser authentication
|
|
@@ -84,6 +71,7 @@ async function pollForToken(state, expiresAt, signal) {
|
|
|
84
71
|
method: "GET",
|
|
85
72
|
headers: {
|
|
86
73
|
"Content-Type": "application/json",
|
|
74
|
+
"Accept": "application/json",
|
|
87
75
|
},
|
|
88
76
|
signal,
|
|
89
77
|
});
|
|
@@ -167,15 +155,17 @@ const CodeBuddyExternalAuthPlugin = async (_input) => {
|
|
|
167
155
|
label: "IOA 登录 (浏览器)",
|
|
168
156
|
type: "oauth",
|
|
169
157
|
async authorize() {
|
|
170
|
-
//
|
|
171
|
-
const
|
|
158
|
+
// Generate state for CSRF protection
|
|
159
|
+
const state = generateState();
|
|
160
|
+
const loginUrl = buildLoginUrl(state);
|
|
161
|
+
const expiresAt = Date.now() + 10 * 60 * 1000; // 10 minutes
|
|
172
162
|
return {
|
|
173
|
-
url:
|
|
163
|
+
url: loginUrl,
|
|
174
164
|
instructions: `请在浏览器中完成 IOA 登录`,
|
|
175
165
|
method: "auto",
|
|
176
166
|
async callback() {
|
|
177
167
|
// Poll for token
|
|
178
|
-
const tokenData = await pollForToken(
|
|
168
|
+
const tokenData = await pollForToken(state, expiresAt);
|
|
179
169
|
if (!tokenData) {
|
|
180
170
|
return { type: "failed" };
|
|
181
171
|
}
|