-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
147 lines (122 loc) · 4.86 KB
/
index.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
"use strict";
const {OAuth2Client} = require('google-auth-library');
const http = require('http');
const url = require('url');
const open = require('open');
const destroyer = require('server-destroy');
const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const clientSecretPath = process.env.CLIENT_SECRET || '/usr/src/config/client_secret.json';
let clientSecret;
const savedTokensPath = '/usr/src/config/tokens.json';
async function main() {
validateClientSecretFile();
clientSecret = require(clientSecretPath);
const oAuth2Client = await getAuthenticatedClient();
await oAuth2Client.getTokenInfo(oAuth2Client.credentials.access_token);
}
/**
* Create a new OAuth2Client, and go through the OAuth2 content
* workflow. Return the full client to the callback.
*/
function getAuthenticatedClient() {
const port = 3005;
const serverBasePath = `http://localhost:${port}`;
const callBackPath = '/oauth2callback';
const callBackUrl = `${serverBasePath}${callBackPath}`
return new Promise((resolve, reject) => {
// Create an oAuth client to authorize the API call.
const oAuth2Client = new OAuth2Client(clientSecret.installed.client_id,
clientSecret.installed.client_secret, callBackUrl);
// Generate the url that will be used for the consent dialog.
const authorizeUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/assistant-sdk-prototype',
});
const saveTokens = (tokens) => {
// Save retrieved tokens
mkdirp(path.dirname(savedTokensPath))
.then(() => {
fs.writeFile(savedTokensPath, JSON.stringify(tokens), () => {
console.info(`Tokens saved to ${savedTokensPath}.`)
});
})
.catch((error) => {
console.error('Error saving tokens:', error.message);
});
};
// Open a http server to accept the oauth callback.
// The only request to our webserver is to /oauth2callback?code=<code>
const server = http
.createServer(async (req, res) => {
try {
if (req.url.indexOf(callBackPath) > -1) {
// acquire the code from the querystring, and close the web server.
const qs = new url.URL(req.url, serverBasePath).searchParams;
const code = qs.get('code');
console.log(`Code is ${code}`);
// Inform user in their browser to return back to the console
res.end('Authentication successful! Please return to the console.');
// Destroy the http server
server.destroy();
// Now that we have the code, use that to acquire tokens.
const r = await oAuth2Client.getToken(code);
// Make sure to set the credentials on the OAuth2 client.
oAuth2Client.setCredentials(r.tokens);
console.info('Tokens acquired.');
saveTokens(r.tokens);
resolve(oAuth2Client);
}
} catch (e) {
reject(e);
}
})
.listen(port, () => {
// Open the browser to the authorize url to start the workflow
console.info('Go to this URL in your browser:', authorizeUrl);
open(authorizeUrl, {wait: false}).then(cp => cp.unref());
});
destroyer(server);
});
}
const checkFileExistsSync = (filepath) => {
let exists = true;
try {
fs.accessSync(filepath, fs.constants.F_OK);
} catch (e) {
exists = false;
}
return exists;
};
const exitAndLogError = (errorMsg) => {
console.log(`[ERROR] ${errorMsg}`)
process.exit(1)
}
const readJsonFile = (filepath) => {
try {
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
} catch (e) {
console.log(e);
exitAndLogError(`Failed reading JSON file at path ${filepath}`)
}
}
const validateClientSecretFile = () => {
if (!checkFileExistsSync(clientSecretPath)) {
exitAndLogError(
`Client Secret file at path '${clientSecretPath}' does not exist.`)
}
const secretFileContent = readJsonFile(clientSecretPath).installed;
if(!secretFileContent){
exitAndLogError(`The Client Secret file at path '${clientSecretPath}' is missing root-property 'installed'. Please see example of correct file content in README and follow steps as described in Prerequisites.`)
}
if (secretFileContent.token_uri !== "https://oauth2.googleapis.com/token") {
exitAndLogError(
`The Client Secret file at path '${clientSecretPath}' has invalid 'token_uri' value. Expecting value 'https://oauth2.googleapis.com/token', but was '${secretFileContent.token_uri}'. Please make sure you download OAuth client file from GCP Console / API & Services / Credentials.`)
}
if (!secretFileContent.redirect_uris) {
exitAndLogError(
`The Client Secret file at path '${clientSecretPath}' is missing 'redirect_uris' property. Please make sure you download OAuth client file from GCP Console / API & Services / Credentials.`)
}
}
main().catch(console.error);