-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathauthenticate.js
More file actions
356 lines (302 loc) · 12.1 KB
/
authenticate.js
File metadata and controls
356 lines (302 loc) · 12.1 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env node
const crypto = require('crypto');
const fs = require('fs').promises;
const path = require('path');
const qrcode = require('qrcode-terminal');
const open = require('open');
class QwenAuthManager {
constructor() {
this.qwenDir = path.join(process.cwd(), '.qwen');
// Qwen OAuth Configuration (from working oai-proxy)
this.oauthBaseUrl = 'https://chat.qwen.ai';
this.deviceCodeEndpoint = `${this.oauthBaseUrl}/api/v1/oauth2/device/code`;
this.tokenEndpoint = `${this.oauthBaseUrl}/api/v1/oauth2/token`;
this.clientId = 'f0304373b74a44d2b584a3fb70ca9e56';
this.scope = 'openid profile email model.completion';
this.grantType = 'urn:ietf:params:oauth:grant-type:device_code';
}
async ensureDir() {
try {
await fs.mkdir(this.qwenDir, { recursive: true });
} catch (error) {
if (error.code !== 'EEXIST') throw error;
}
}
generateCodeVerifier(length = 128) {
return crypto.randomBytes(length).toString('base64url');
}
generateCodeChallenge(verifier) {
return crypto.createHash('sha256').update(verifier).digest('base64url');
}
async initiateDeviceFlow() {
const codeVerifier = this.generateCodeVerifier();
const codeChallenge = this.generateCodeChallenge(codeVerifier);
const response = await fetch(this.deviceCodeEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
body: new URLSearchParams({
client_id: this.clientId,
scope: this.scope,
code_challenge: codeChallenge,
code_challenge_method: 'S256',
}),
});
if (!response.ok) {
const errorData = await response.text();
throw new Error(`Device flow initiation failed: ${response.status} ${response.statusText}. Response: ${errorData}`);
}
const data = await response.json();
if (!data.device_code) {
throw new Error(`Device authorization failed: ${data.error || 'Unknown error'} - ${data.error_description || 'No details provided'}`);
}
return {
...data,
code_verifier: codeVerifier,
verification_uri_complete: data.verification_uri_complete || data.verification_uri,
};
}
async pollForToken(deviceCode, codeVerifier, accountId = null) {
let pollInterval = 5000; // 5 seconds
const maxAttempts = 60; // 5 minutes max
for (let attempt = 0; attempt < maxAttempts; attempt++) {
const bodyData = new URLSearchParams({
grant_type: this.grantType,
client_id: this.clientId,
device_code: deviceCode,
code_verifier: codeVerifier,
});
try {
const response = await fetch(this.tokenEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
body: bodyData,
});
if (!response.ok) {
// Parse the response as JSON to check for OAuth RFC 8628 standard errors
try {
const errorData = await response.json();
// According to OAuth RFC 8628, handle standard polling responses
if (response.status === 400 && errorData.error === 'authorization_pending') {
// User has not yet approved the authorization request. Continue polling.
console.log(`Polling attempt ${attempt + 1}/${maxAttempts}...`);
await new Promise(resolve => setTimeout(resolve, pollInterval));
continue;
}
if (response.status === 400 && errorData.error === 'slow_down') {
// Client is polling too frequently. Increase poll interval.
pollInterval = Math.min(pollInterval * 1.5, 10000); // Increase by 50%, max 10 seconds
console.log(`Server requested to slow down, increasing poll interval to ${pollInterval}ms`);
await new Promise(resolve => setTimeout(resolve, pollInterval));
continue;
}
if (response.status === 400 && errorData.error === 'expired_token') {
throw new Error('Device code expired. Please restart the authentication process.');
}
if (response.status === 400 && errorData.error === 'access_denied') {
throw new Error('Authorization denied by user. Please restart the authentication process.');
}
// For other errors, throw with proper error information
throw new Error(`Device token poll failed: ${errorData.error || 'Unknown error'} - ${errorData.error_description || 'No details provided'}`);
} catch (_parseError) {
// If JSON parsing fails, fall back to text response
const errorData = await response.text();
throw new Error(`Device token poll failed: ${response.status} ${response.statusText}. Response: ${errorData}`);
}
}
const tokenData = await response.json();
// Convert to credentials format and save
const credentials = {
access_token: tokenData.access_token,
refresh_token: tokenData.refresh_token || undefined,
token_type: tokenData.token_type,
resource_url: tokenData.resource_url || tokenData.endpoint,
expiry_date: tokenData.expires_in ? Date.now() + tokenData.expires_in * 1000 : undefined,
};
// Save credentials
if (accountId) {
await this.saveAccountCredentials(accountId, credentials);
} else {
await this.saveCredentials(credentials);
}
return credentials;
} catch (error) {
// Handle specific error cases
const errorMessage = error instanceof Error ? error.message : String(error);
// If we get a specific OAuth error that should stop polling, throw it
if (errorMessage.includes('expired_token') ||
errorMessage.includes('access_denied') ||
errorMessage.includes('Device authorization failed')) {
throw error;
}
// For other errors, continue polling
console.log(`Polling attempt ${attempt + 1}/${maxAttempts} failed:`, errorMessage);
await new Promise(resolve => setTimeout(resolve, pollInterval));
}
}
throw new Error('Authentication timeout. Please restart the authentication process.');
}
async saveCredentials(credentials) {
await this.ensureDir();
const filePath = path.join(this.qwenDir, 'oauth_creds.json');
await fs.writeFile(filePath, JSON.stringify(credentials, null, 2));
}
async saveAccountCredentials(accountId, credentials) {
await this.ensureDir();
const filePath = path.join(this.qwenDir, `oauth_creds_${accountId}.json`);
await fs.writeFile(filePath, JSON.stringify(credentials, null, 2));
}
async loadCredentials() {
try {
const filePath = path.join(this.qwenDir, 'oauth_creds.json');
const data = await fs.readFile(filePath, 'utf8');
return JSON.parse(data);
} catch (error) {
if (error.code === 'ENOENT') return null;
throw error;
}
}
async loadAccountCredentials(accountId) {
try {
const filePath = path.join(this.qwenDir, `oauth_creds_${accountId}.json`);
const data = await fs.readFile(filePath, 'utf8');
return JSON.parse(data);
} catch (error) {
if (error.code === 'ENOENT') return null;
throw error;
}
}
async loadAllAccounts() {
await this.ensureDir();
try {
const files = await fs.readdir(this.qwenDir);
const accountFiles = files.filter(file =>
file.startsWith('oauth_creds_') && file.endsWith('.json')
);
return accountFiles.map(file =>
file.replace('oauth_creds_', '').replace('.json', '')
);
} catch (error) {
return [];
}
}
isTokenValid(credentials) {
return credentials && credentials.expiry_date > Date.now();
}
async removeAccount(accountId) {
const filePath = path.join(this.qwenDir, `oauth_creds_${accountId}.json`);
try {
await fs.unlink(filePath);
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(`Account ${accountId} not found`);
}
throw error;
}
}
}
async function listAccounts() {
console.log('Listing all Qwen accounts...');
try {
const authManager = new QwenAuthManager();
const accountIds = await authManager.loadAllAccounts();
if (accountIds.length === 0) {
console.log('No accounts found in ./.qwen/ directory.');
return;
}
console.log(`\nFound ${accountIds.length} account(s):\n`);
for (const accountId of accountIds) {
const credentials = await authManager.loadAccountCredentials(accountId);
const isValid = authManager.isTokenValid(credentials);
console.log(`Account ID: ${accountId}`);
console.log(` Status: ${isValid ? '✅ Valid' : '❌ Invalid/Expired'}`);
if (credentials && credentials.expiry_date) {
const expiry = new Date(credentials.expiry_date);
console.log(` Expires: ${expiry.toLocaleString()}`);
}
console.log('');
}
} catch (error) {
console.error('Failed to list accounts:', error.message);
process.exit(1);
}
}
async function addAccount(accountId) {
console.log(`Adding new Qwen account with ID: ${accountId}...`);
try {
const authManager = new QwenAuthManager();
// Initiate device flow
console.log('\nInitiating device flow...');
const deviceFlow = await authManager.initiateDeviceFlow();
// Display verification URI and user code
console.log('\n=== Qwen OAuth Device Authorization ===');
console.log('Please visit the following URL to authenticate:');
console.log(`\n${deviceFlow.verification_uri_complete}\n`);
// Generate and display QR code
console.log('Or scan the QR code below:');
qrcode.generate(deviceFlow.verification_uri_complete, { small: true }, (qrCode) => {
console.log(qrCode);
});
console.log('User code:', deviceFlow.user_code);
console.log('(Press Ctrl+C to cancel)');
// Try to open the URL in the browser
try {
await open(deviceFlow.verification_uri_complete);
console.log('\nBrowser opened automatically. If not, please visit the URL above.');
} catch (openError) {
console.log('\nPlease visit the URL above in your browser to authenticate.');
}
// Poll for token and save to specific account
console.log('\nWaiting for authentication...');
const token = await authManager.pollForToken(deviceFlow.device_code, deviceFlow.code_verifier, accountId);
console.log(`\n🎉 Authentication successful for account ${accountId}!`);
console.log(`Access token saved to ./.qwen/oauth_creds_${accountId}.json`);
} catch (error) {
console.error('Authentication failed:', error.message);
process.exit(1);
}
}
async function removeAccount(accountId) {
console.log(`Removing Qwen account with ID: ${accountId}...`);
try {
const authManager = new QwenAuthManager();
await authManager.removeAccount(accountId);
console.log(`\n✅ Account ${accountId} removed successfully!`);
} catch (error) {
console.error('Failed to remove account:', error.message);
process.exit(1);
}
}
// Parse command line arguments
const args = process.argv.slice(2);
const command = args[0];
switch (command) {
case 'list':
listAccounts();
break;
case 'add':
if (!args[1]) {
console.error('Please provide an account ID: node authenticate.js add <account-id>');
process.exit(1);
}
addAccount(args[1]);
break;
case 'remove':
if (!args[1]) {
console.error('Please provide an account ID: node authenticate.js remove <account-id>');
process.exit(1);
}
removeAccount(args[1]);
break;
default:
console.log('Usage: node authenticate.js [list|add <account-id>|remove <account-id>]');
console.log(' list - List all accounts');
console.log(' add <account-id> - Add a new account with the specified ID');
console.log(' remove <account-id> - Remove an existing account with the specified ID');
process.exit(1);
}