-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleauth.js
88 lines (80 loc) · 2.75 KB
/
googleauth.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
var fs = require("fs");
const { google } = require("googleapis");
var TOKEN_STORE = "./.gdrive";
module.exports = function (api, ver, scopes, Wrapper) {
return function (info, callback) {
var self = this;
if (self._client) {
callback(self._client);
} else {
const oauth2Client = new google.auth.OAuth2(info.clientId, info.clientSecret, info.redirectUrl);
oauth2Client.on("tokens", (tokens) => {
storeToken(tokens);
});
acquireToken(oauth2Client, scopes, function () {
var client = api({ version: ver, auth: oauth2Client });
callback(self._client = new Wrapper(client));
});
}
};
};
function loadStoredToken() {
try {
if (fs.existsSync(TOKEN_STORE)) {
return JSON.parse(fs.readFileSync(TOKEN_STORE, "utf8"));
}
} catch (err) {
console.warn(err);
}
return null;
}
function storeToken(tokens) {
fs.writeFileSync(TOKEN_STORE, JSON.stringify(tokens));
}
function acquireToken(oauth2Client, scopes, callback) {
const tokens = loadStoredToken();
if (tokens) {
oauth2Client.setCredentials({
access_token: tokens.access_token,
refresh_token: tokens.refresh_token,
expiry_date: tokens.expiry_date
});
// bug https://github.com/google/google-api-nodejs-client/issues/260
var expiryDate = oauth2Client.credentials.expiry_date;
var isTokenExpired = expiryDate ? expiryDate <= (new Date()).getTime() : false;
if (isTokenExpired) {
oauth2Client.refreshAccessToken(function (err, tokens) {
console.log(err || tokens);
if (!err) {
fs.writeFileSync(TOKEN_STORE, JSON.stringify(tokens));
callback();
}
});
} else {
callback();
}
return;
}
var url = oauth2Client.generateAuthUrl({
access_type: "offline", // 'online' (default) or 'offline' (gets refresh_token)
scope: scopes // If you only need one scope you can pass it as string
});
console.log(url);
var readline = require("readline");
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("code? ", function (code) {
oauth2Client.getToken(code, function (err, tokens) {
// Now tokens contains an access_token and an optional refresh_token. Save them.
console.log(err || tokens);
if (!err) {
oauth2Client.setCredentials(tokens);
storeToken(tokens);
callback();
}
});
rl.close();
});
}