-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoGoogleAuth.js
More file actions
119 lines (100 loc) · 3.14 KB
/
DoGoogleAuth.js
File metadata and controls
119 lines (100 loc) · 3.14 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
"use strict"
var fs = require('fs');
var readline = require('readline');
var googleAuth = require('google-auth-library');
var scopes
, tokenFile
, tokenDir
, clientSecretFile
function doGoogleAuth(scopes,tokenFile,tokenDir,clientSecretFile) {
this.scopes = (Array.isArray(scopes))? scopes.join(" ") : scopes
this.tokenFile = tokenFile
this.tokenDir = tokenDir
this.clientSecretFile = clientSecretFile
}
var method = doGoogleAuth.prototype
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*
* @param {function} callback The callback to call with the authorized client.
*/
method.authorize = function (callback) {
var self = this
// Load client secrets from a local file.
var content = ""
try {
var content = fs.readFileSync(this.clientSecretFile, 'utf8')
} catch (err) {
console.log('Error loading client secret file: ' + err);
callback(err)
return null;
}
var credentials = JSON.parse(content)
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
var tokenPath = this.tokenDir + '/' + this.tokenFile;
// Check if we have previously stored a token.
fs.readFile(tokenPath, function(err, token) {
if (err) {
self.getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(null,oauth2Client);
}
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback to call with the authorized
* client.
*/
method.getNewToken = function (oauth2Client, callback) {
var self = this
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: this.scopes
});
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
callback(err)
return null;
}
oauth2Client.credentials = token;
self.storeToken(token);
callback(null,oauth2Client);
});
});
}
/**
* Store token to disk be used in later program executions.
*
* @param {Object} token The token to store to disk.
*/
method.storeToken = function (token) {
try {
fs.mkdirSync(this.tokenDir);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
var tokenPath = this.tokenDir + '/' + this.tokenFile;
fs.writeFileSync(tokenPath , JSON.stringify(token));
console.log('Token stored to ' + tokenPath);
}
module.exports = doGoogleAuth;