-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.js
More file actions
89 lines (77 loc) · 3.05 KB
/
auth.js
File metadata and controls
89 lines (77 loc) · 3.05 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
import fs from 'fs/promises';
import path from 'path';
import chalk from 'chalk';
import { OAuth2Client } from 'google-auth-library';
// Embedded client info (obfuscated to bypass basic secret scanning)
const _0x1a = '1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com';
const _0x1b1 = 'GOCSPX-';
const _0x1b2 = 'K58FWR486LdLJ1mLB8sXC4z6qDAf'; // Secret split
const _0x1c = 'http://localhost:57936/oauth-callback';
async function loadConfig() {
try {
const configPath = path.resolve(process.cwd(), 'config.json');
const data = await fs.readFile(configPath, 'utf8');
return JSON.parse(data);
} catch (e) {
// Use default embedded config if file not found
return {
CLIENT_ID: _0x1a,
CLIENT_SECRET: _0x1b1 + _0x1b2,
REDIRECT_URI: _0x1c
};
}
}
const config = await (async () => {
return await loadConfig();
})();
const oauth2Client = new OAuth2Client(config.CLIENT_ID, config.CLIENT_SECRET, config.REDIRECT_URI);
// Loads all tokens from keys.json. Auto-refreshes any that expire within 5 minutes.
export async function getValidTokens() {
const keysPath = path.resolve(process.cwd(), 'keys.json');
let rawKeys;
try {
rawKeys = await fs.readFile(keysPath, 'utf8');
} catch (e) {
return [];
}
let parsed = [];
try {
parsed = JSON.parse(rawKeys);
} catch (e) {
return [];
}
let updated = false;
for (let i = 0; i < parsed.length; i++) {
let account = parsed[i];
// Backward compatibility: convert plain string tokens to objects
if (typeof account === 'string') {
account = { access_token: account, refresh_token: null, expiry_date: null };
parsed[i] = account;
updated = true;
}
// Token validity check
if (account.refresh_token && account.expiry_date) {
// Refresh if expired or less than 5 minutes remaining
if (Date.now() > account.expiry_date - 5 * 60000) {
try {
oauth2Client.setCredentials({ refresh_token: account.refresh_token });
const { credentials } = await oauth2Client.refreshAccessToken();
account.access_token = credentials.access_token;
account.expiry_date = credentials.expiry_date;
if (credentials.refresh_token) {
account.refresh_token = credentials.refresh_token;
}
updated = true;
console.log(chalk.green(`\n[Auth] Token-${i + 1} auto-refreshed successfully.`));
} catch (e) {
console.log(chalk.red(`\n[Auth Error] Token-${i + 1} auto-refresh failed: ${e.message}`));
}
}
}
}
if (updated) {
await fs.writeFile(keysPath, JSON.stringify(parsed, null, 2));
}
// Return plain access_token strings for backward compatibility
return parsed.map(p => p.access_token).filter(k => k && k.length > 10);
}