-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathoauth2-refresh-token-helper.js
462 lines (416 loc) · 12.5 KB
/
oauth2-refresh-token-helper.js
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env node
/**
* Google OAuth Refresh Token Generator
*
* A standalone script that helps obtain a refresh token for Google APIs
* without requiring npm install.
*
* NOTE: An alternative approach is to use the official google-auth-library:
* ---------------------------------------------------------------------
* If you're working in a Node.js environment where you can use npm packages,
* consider using the official google-auth-library instead:
*
* 1. Install the library: npm install google-auth-library
* 2. Then use it like this:
* ```
* import { OAuth2Client } from "google-auth-library";
*
* // Initialize OAuth client
* const REDIRECT_URI = "http://localhost:8080/oauth/callback";
* const oAuth2Client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
*
* // Generate authorization URL
* const authorizeUrl = oAuth2Client.generateAuthUrl({
* access_type: "offline",
* prompt: "consent",
* scope: ["https://mail.google.com/"], // Full Gmail access
* });
*
* // After receiving the code from the callback:
* const { tokens } = await oAuth2Client.getToken(code);
* const refreshToken = tokens.refresh_token;
* ```
*
* This approach integrates better with other Google services and handles
* token refresh automatically when using the library for API calls.
*/
// Core Node.js modules
const http = require("http");
const url = require("url");
const { exec } = require("child_process");
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const os = require("os");
// Configuration constants
const DEFAULT_PORT = 8080;
const DEFAULT_OUTPUT_FILE = "refresh_token.txt";
const AUTH_TIMEOUT_MS = 2 * 60 * 1000; // 2 minutes
const SERVER_SHUTDOWN_DELAY_MS = 3000;
/**
* Parses command line arguments
* @returns {Object} Parsed arguments
*/
function parseArguments() {
const args = process.argv.slice(2);
const config = {
port: DEFAULT_PORT,
clientId: "",
clientSecret: "",
outputFile: DEFAULT_OUTPUT_FILE,
showHelp: false,
};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case "--port":
case "-p":
config.port = parseInt(args[++i], 10) || DEFAULT_PORT;
break;
case "--id":
case "-i":
config.clientId = args[++i] || "";
break;
case "--secret":
case "-s":
config.clientSecret = args[++i] || "";
break;
case "--output":
case "-o":
config.outputFile = args[++i] || DEFAULT_OUTPUT_FILE;
break;
case "--help":
case "-h":
config.showHelp = true;
break;
}
}
return config;
}
/**
* Shows help information
*/
function displayHelp() {
console.log("Google OAuth Refresh Token Generator");
console.log("\nThis tool helps you obtain a refresh token for Google APIs.");
console.log(
"It starts a local web server, opens your browser for authentication,"
);
console.log("and saves the refresh token to a file.");
console.log("\nUsage:");
console.log(
" --port, -p Port to run the server on (default: 8080 or PORT env var)"
);
console.log(" --id, -i Google OAuth Client ID");
console.log(" --secret, -s Google OAuth Client Secret");
console.log(
" --output, -o Output file to save the refresh token (default: refresh_token.txt)"
);
console.log(" --help, -h Show this help information");
console.log("\nEnvironment Variables:");
console.log(
" PORT Port to run the server on (if --port not specified)"
);
console.log(
" CLIENT_ID Google OAuth Client ID (if --id not specified)"
);
console.log(
" CLIENT_SECRET Google OAuth Client Secret (if --secret not specified)"
);
}
/**
* Opens a browser with the specified URL
* @param {string} url - URL to open
*/
function openBrowser(url) {
console.log("Opening browser...");
const commands = {
win32: `start ${url}`,
darwin: `open ${url}`,
default: `xdg-open ${url}`,
};
const command = commands[os.platform()] || commands.default;
exec(command, (error) => {
if (error) {
console.log(`Error opening browser: ${error.message}`);
console.log(`Please open your browser and navigate to ${url}`);
}
});
}
/**
* Creates the HTTP server to handle OAuth flow
* @param {Object} options - Server configuration options
* @returns {Promise<string>} The refresh token
*/
function createAuthServer({
clientId,
clientSecret,
port,
redirectUrl,
outputFile,
}) {
return new Promise((resolve, reject) => {
let codeResolver;
const codePromise = new Promise((resolve) => {
codeResolver = resolve;
});
const server = http.createServer(async (req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathname = parsedUrl.pathname;
// Handle routes
if (pathname === "/") {
handleRootRoute(res, { clientId, redirectUrl });
} else if (pathname === "/oauth/callback") {
await handleCallbackRoute(req, res, {
clientId,
clientSecret,
redirectUrl,
outputFile,
codeResolver,
server,
resolve,
});
} else {
// Not found for all other routes
res.writeHead(404);
res.end("Not found");
}
});
// Set a timeout for the entire process
const timeout = setTimeout(() => {
console.log("Timeout waiting for authentication");
server.close();
reject(new Error("Authentication timeout"));
}, AUTH_TIMEOUT_MS);
// Start the server
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
openBrowser(`http://localhost:${port}`);
});
// Wait for the code and process it
codePromise
.then((code) => {
console.log("Exchanging code for tokens...");
clearTimeout(timeout);
})
.catch((error) => {
clearTimeout(timeout);
server.close();
reject(error);
});
});
}
/**
* Handles the root route - redirects to Google OAuth
*/
function handleRootRoute(res, { clientId, redirectUrl }) {
const authUrl =
`https://accounts.google.com/o/oauth2/v2/auth?` +
`client_id=${encodeURIComponent(clientId)}` +
`&redirect_uri=${encodeURIComponent(redirectUrl)}` +
`&response_type=code` +
`&scope=${encodeURIComponent("https://mail.google.com/")}` +
`&access_type=offline` +
`&prompt=consent`;
res.writeHead(302, { Location: authUrl });
res.end();
}
/**
* Handles the OAuth callback route
*/
async function handleCallbackRoute(req, res, options) {
const {
clientId,
clientSecret,
redirectUrl,
outputFile,
codeResolver,
server,
resolve,
} = options;
const parsedUrl = url.parse(req.url, true);
const code = parsedUrl.query.code;
if (!code) {
res.writeHead(400);
res.end("No code provided");
return;
}
try {
// Notify that we received the code
codeResolver(code);
// Exchange code for tokens
const tokenResponse = await fetch("https://oauth2.googleapis.com/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
code,
client_id: clientId,
client_secret: clientSecret,
redirect_uri: redirectUrl,
grant_type: "authorization_code",
}),
});
const tokens = await tokenResponse.json();
const refreshToken = tokens.refresh_token;
if (!refreshToken) {
sendErrorResponse(res);
return;
}
// Save the refresh token to file
const tokenFilePath = path.join(process.cwd(), outputFile);
fs.writeFileSync(tokenFilePath, refreshToken, { mode: 0o600 });
// Send success response
sendSuccessResponse(res, { refreshToken, outputFile });
console.log("\n✅ Successfully obtained refresh token!");
console.log(`\n✅ Saved refresh token to ${tokenFilePath}`);
// Wait a bit before shutting down
setTimeout(() => {
console.log("\nToken generation complete! Shutting down...");
server.close();
resolve(refreshToken);
}, SERVER_SHUTDOWN_DELAY_MS);
} catch (error) {
console.error("Error exchanging code for token:", error);
res.writeHead(500);
res.end("Error exchanging code for token");
}
}
/**
* Sends an error response when no refresh token is received
*/
function sendErrorResponse(res) {
res.writeHead(400, { "Content-Type": "text/html" });
res.end(`
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OAuth Error</title>
<style>
body { font-family: sans-serif; margin: 20px; max-width: 800px; line-height: 1.6; }
</style>
</head>
<body>
<h1>Error: No Refresh Token Received</h1>
<p>Make sure you've revoked previous access or forced consent.</p>
</body>
</html>
`);
}
/**
* Sends a success response with the refresh token
*/
function sendSuccessResponse(res, { refreshToken, outputFile }) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(`
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OAuth Refresh Token</title>
<style>
body { font-family: sans-serif; margin: 20px; max-width: 800px; line-height: 1.6; }
p { word-break: break-all; font-family: monospace; background: #f0f0f0; padding: 10px; }
.success { color: green; font-weight: bold; }
</style>
</head>
<body>
<h1>OAuth Successful!</h1>
<div class="success">✅ Token successfully generated</div>
<h2>Your OAuth Refresh Token:</h2>
<p>${refreshToken}</p>
<h3>Next Steps:</h3>
<ul>
<li>Your refresh token has been saved to <code>${outputFile}</code></li>
<li>Keep this token secure - it provides access to your Gmail account</li>
<li>You can now close this window and the program will exit automatically</li>
</ul>
</body>
</html>
`);
}
/**
* Main function to start the OAuth flow
*/
async function main() {
try {
// Initialize config
const config = parseArguments();
// Show help if requested
if (config.showHelp) {
displayHelp();
process.exit(0);
}
// Setup console display
console.log("====================================");
console.log("Google OAuth Refresh Token Generator");
console.log("====================================\n");
// Check environment variable if port not provided
if (!config.port) {
const portEnv = process.env.PORT;
if (portEnv) {
const parsedPort = parseInt(portEnv, 10);
if (!isNaN(parsedPort) && parsedPort > 0 && parsedPort <= 65535) {
config.port = parsedPort;
} else {
console.log(`Invalid PORT environment variable: ${portEnv}`);
config.port = DEFAULT_PORT;
}
} else {
config.port = DEFAULT_PORT;
}
}
// Create readline interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Get credentials
config.clientId = config.clientId || process.env.CLIENT_ID;
config.clientSecret = config.clientSecret || process.env.CLIENT_SECRET;
if (!config.clientId) {
config.clientId = await new Promise((resolve) => {
rl.question("Enter your Google OAuth Client ID: ", (answer) =>
resolve(answer)
);
});
}
if (!config.clientSecret) {
config.clientSecret = await new Promise((resolve) => {
rl.question("Enter your Google OAuth Client Secret: ", (answer) =>
resolve(answer)
);
});
}
if (!config.clientId || !config.clientSecret) {
console.error("Error: Client ID and Client Secret are required");
rl.close();
process.exit(1);
}
// Set up OAuth parameters
const redirectUrl = `http://localhost:${config.port}/oauth/callback`;
console.log(`\nUsing redirect URI: ${redirectUrl}`);
console.log(
"Make sure this exact URI is added to your OAuth consent screen redirects\n"
);
// Start the authentication server
await createAuthServer({
clientId: config.clientId,
clientSecret: config.clientSecret,
port: config.port,
redirectUrl,
outputFile: config.outputFile,
});
rl.close();
} catch (error) {
console.error("Error:", error);
process.exit(1);
}
}
// Run the main function
main().catch((error) => {
console.error("Unhandled error:", error);
process.exit(1);
});