| 35 | } |
| 36 | |
| 37 | class CliAuthManager { |
| 38 | /** |
| 39 | * 读取响应体 |
| 40 | * @param {Response} response - Fetch 响应对象 |
| 41 | * @returns {Promise<*>} 响应体 |
| 42 | */ |
| 43 | async readResponseBody(response) { |
| 44 | const contentType = response.headers.get('content-type') || '' |
| 45 | const rawText = await response.text() |
| 46 | |
| 47 | if (!rawText) { |
| 48 | return '' |
| 49 | } |
| 50 | |
| 51 | if (contentType.includes('application/json')) { |
| 52 | try { |
| 53 | return JSON.parse(rawText) |
| 54 | } catch (error) { |
| 55 | return rawText |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return rawText |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * 启动 OAuth 设备授权流程 |
| 64 | * @param {Object} [account] - Qwen 账户对象(用于解析账号级代理) |
| 65 | * @returns {Promise<Object>} 包含设备代码、验证URL和代码验证器的对象 |
| 66 | */ |
| 67 | async initiateDeviceFlow(account) { |
| 68 | // 生成 PKCE 代码验证器和挑战 |
| 69 | const { code_verifier, code_challenge } = generatePKCEPair() |
| 70 | |
| 71 | const bodyData = new URLSearchParams({ |
| 72 | client_id: "f0304373b74a44d2b584a3fb70ca9e56", |
| 73 | scope: "openid profile email model.completion", |
| 74 | code_challenge: code_challenge, |
| 75 | code_challenge_method: 'S256', |
| 76 | }) |
| 77 | |
| 78 | const chatBaseUrl = getChatBaseUrl() |
| 79 | |
| 80 | const fetchOptions = { |
| 81 | method: 'POST', |
| 82 | headers: { |
| 83 | 'Content-Type': 'application/x-www-form-urlencoded', |
| 84 | Accept: 'application/json', |
| 85 | }, |
| 86 | body: bodyData, |
| 87 | } |
| 88 | |
| 89 | applyProxyToFetchOptions(fetchOptions, account) |
| 90 | |
| 91 | try { |
| 92 | const response = await fetch(`${chatBaseUrl}/api/v1/oauth2/device/code`, fetchOptions) |
| 93 | |
| 94 | if (response.ok) { |
nothing calls this directly
no outgoing calls
no test coverage detected