File size: 8,639 Bytes
f120063
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const crypto = require('crypto')
const { getProxyAgent, getChatBaseUrl, applyProxyToFetchOptions } = require('./proxy-helper')

/**
 * 为 PKCE 生成随机代码验证器
 * @returns {string} 43-128个字符的随机字符串
 */
function generateCodeVerifier() {
    return crypto.randomBytes(32).toString('base64url')
}

/**
 * 使用 SHA-256 从代码验证器生成代码挑战
 * @param {string} codeVerifier - 代码验证器字符串
 * @returns {string} 代码挑战字符串
 */
function generateCodeChallenge(codeVerifier) {
    const hash = crypto.createHash('sha256')
    hash.update(codeVerifier)
    return hash.digest('base64url')
}

/**
 * 生成 PKCE 代码验证器和挑战对
 * @returns {Object} 包含 code_verifier 和 code_challenge 的对象
 */
function generatePKCEPair() {
    const codeVerifier = generateCodeVerifier()
    const codeChallenge = generateCodeChallenge(codeVerifier)
    return {
        code_verifier: codeVerifier,
        code_challenge: codeChallenge
    }
}

class CliAuthManager {
    /**
     * 启动 OAuth 设备授权流程
     * @returns {Promise<Object>} 包含设备代码、验证URL和代码验证器的对象
     */
    async initiateDeviceFlow() {
        // 生成 PKCE 代码验证器和挑战
        const { code_verifier, code_challenge } = generatePKCEPair()

        const bodyData = new URLSearchParams({
            client_id: "f0304373b74a44d2b584a3fb70ca9e56",
            scope: "openid profile email model.completion",
            code_challenge: code_challenge,
            code_challenge_method: 'S256',
        })

        const chatBaseUrl = getChatBaseUrl()
        const proxyAgent = getProxyAgent()

        const fetchOptions = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                Accept: 'application/json',
            },
            body: bodyData,
        }

        // 添加代理配置
        if (proxyAgent) {
            fetchOptions.agent = proxyAgent
        }

        try {
            const response = await fetch(`${chatBaseUrl}/api/v1/oauth2/device/code`, fetchOptions)

            if (response.ok) {
                const result = await response.json()
                return {
                    status: true,
                    ...result,
                    code_verifier: code_verifier
                }
            } else {
                throw new Error()
            }
        } catch (error) {
            return {
                status: false,
                device_code: null,
                user_code: null,
                verification_uri: null,
                verification_uri_complete: null,
                expires_in: null,
                code_verifier: null
            }
        }
    }

    /**
     * 授权登录
     * @param {string} user_code - 用户代码
     * @param {string} access_token - 访问令牌
     * @returns {Promise<boolean>} 是否授权成功
     */
    async authorizeLogin(user_code, access_token) {
        try {
            const chatBaseUrl = getChatBaseUrl()
            const proxyAgent = getProxyAgent()

            const fetchOptions = {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    "authorization": `Bearer ${access_token}`,
                },
                body: JSON.stringify({
                    "approved": true,
                    "user_code": user_code
                })
            }

            if (proxyAgent) {
                fetchOptions.agent = proxyAgent
            }

            const response = await fetch(`${chatBaseUrl}/api/v2/oauth2/authorize`, fetchOptions)

            if (response.ok) {
                return true
            } else {
                throw new Error()
            }
        } catch (error) {
            return false
        }
    }

    /**
     * 轮询获取访问令牌
     * @param {string} device_code - 设备代码
     * @param {string} code_verifier - 代码验证器
     * @returns {Promise<Object>} 访问令牌信息
     */
    async pollForToken(device_code, code_verifier) {
        let pollInterval = 5000
        const maxAttempts = 60
        const chatBaseUrl = getChatBaseUrl()
        const proxyAgent = getProxyAgent()

        for (let attempt = 0; attempt < maxAttempts; attempt++) {
            const bodyData = new URLSearchParams({
                grant_type: "urn:ietf:params:oauth:grant-type:device_code",
                client_id: "f0304373b74a44d2b584a3fb70ca9e56",
                device_code: device_code,
                code_verifier: code_verifier,
            })

            const fetchOptions = {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    Accept: 'application/json',
                },
                body: bodyData,
            }

            if (proxyAgent) {
                fetchOptions.agent = proxyAgent
            }

            try {
                const response = await fetch(`${chatBaseUrl}/api/v1/oauth2/token`, fetchOptions)

                if (response.ok) {


                    const tokenData = await response.json()

                    // 转换为凭据格式
                    const credentials = {
                        access_token: tokenData.access_token,
                        refresh_token: tokenData.refresh_token || undefined,
                        expiry_date: tokenData.expires_in ? Date.now() + tokenData.expires_in * 1000 : undefined,
                    }

                    return credentials
                }

                // 等待5秒, 然后继续轮询
                await new Promise(resolve => setTimeout(resolve, pollInterval))
            } catch (error) {
                // 等待5秒, 然后继续轮询
                await new Promise(resolve => setTimeout(resolve, pollInterval))
                console.log(`轮询尝试 ${attempt + 1}/${maxAttempts} 失败:`, error)
                continue
            }
        }

        return {
            status: false,
            access_token: null,
            refresh_token: null,
            expiry_date: null
        }
    }

    /**
     * 初始化 CLI 账户
     * @param {string} access_token - 访问令牌
     * @returns {Promise<Object>} 账户信息
     */
    async initCliAccount(access_token) {
        const deviceFlow = await this.initiateDeviceFlow()
        if (!deviceFlow.status || !await this.authorizeLogin(deviceFlow.user_code, access_token)) {
            return {
                status: false,
                access_token: null,
                refresh_token: null,
                expiry_date: null
            }
        }

        return await this.pollForToken(deviceFlow.device_code, deviceFlow.code_verifier)
    }

    /**
     * 刷新访问令牌
     * @param {Object} CliAccount - 账户信息
     * @returns {Promise<Object>} 账户信息
     */
    async refreshAccessToken(CliAccount) {
        try {

            if (!CliAccount || !CliAccount.refresh_token) {
                throw new Error()
            }

            const chatBaseUrl = getChatBaseUrl()
            const proxyAgent = getProxyAgent()

            const bodyData = new URLSearchParams({
                grant_type: 'refresh_token',
                refresh_token: CliAccount.refresh_token,
                client_id: "f0304373b74a44d2b584a3fb70ca9e56",
            })

            const fetchOptions = {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    Accept: 'application/json',
                },
                body: bodyData
            }

            if (proxyAgent) {
                fetchOptions.agent = proxyAgent
            }

            const response = await fetch(`${chatBaseUrl}/api/v1/oauth2/token`, fetchOptions)

            if (response.ok) {
                const tokenData = await response.json()

                return {
                    access_token: tokenData.access_token,
                    refresh_token: tokenData.refresh_token || CliAccount.refresh_token,
                    expiry_date: Date.now() + tokenData.expires_in * 1000,
                }
            }
        } catch (error) {
            return {
                status: false,
                access_token: null,
                refresh_token: null,
                expiry_date: null
            }
        }
    }

}

module.exports = new CliAuthManager()