forked from 74Labs/node-red-contrib-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.js
184 lines (155 loc) · 6.74 KB
/
google.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
module.exports = function (RED) {
"use strict";
function encodeAPI(name, version) {
return name + ':' + version;
}
function decodeAPI(api) {
var a = api.split(':', 2);
return {
name: a[0],
version: a[1]
};
}
function getOAuth2Client(configNode) {
if (configNode == null) return null;
if (configNode.oauth2Client == null) {
configNode.updatedTokensFile = `${RED.settings.userDir}/${configNode.id}.json`;
if (!fs.existsSync(configNode.updatedTokensFile)) {
configNode.log(`Create GoogleNode tokens file ${configNode.updatedTokensFile}`);
fs.writeFileSync(configNode.updatedTokensFile, '{}');
}
if (fs.existsSync(configNode.updatedTokensFile)) {
var lastTokens = JSON.parse(fs.readFileSync(configNode.updatedTokensFile));
configNode.credentials.accessToken = lastTokens?.accessToken ?? configNode.credentials.accessToken;
configNode.credentials.refreshToken = lastTokens?.refreshToken ?? configNode.credentials.refreshToken;
configNode.scopes = lastTokens?.scopes ?? configNode.scopes;
configNode.credentials.tokenType = lastTokens?.tokenType ?? configNode.credentials.tokenType;
configNode.credentials.expireTime = lastTokens?.expireTime ?? configNode.credentials.expireTime;
configNode.log(`GoogleNode tokens restored from ${configNode.updatedTokensFile}`);
}
configNode.oauth2Client = new google.auth.OAuth2(
configNode.credentials.clientId,
configNode.credentials.clientSecret
);
configNode.oauth2Client.setCredentials({
access_token: configNode.credentials.accessToken,
refresh_token: configNode.credentials.refreshToken,
scope: configNode.scopes.replace(/\n/g, " "),
token_type: configNode.credentials.tokenType,
expiry_date: configNode.credentials.expireTime
});
configNode.oauth2Client.on('tokens', (tokens) => {
configNode.credentials.accessToken = tokens.access_token ?? configNode.credentials.accessToken;
configNode.credentials.refreshToken = tokens.refresh_token ?? configNode.credentials.refreshToken;
configNode.scopes = tokens.scope?.replace(/ /g, "\n") ?? configNode.credentials.scopes;
configNode.credentials.tokenType = tokens.token_type ?? configNode.credentials.tokenType;
configNode.credentials.expireTime = tokens.expiry_date ?? configNode.credentials.expireTime;
RED.nodes.addCredentials(configNode.id, configNode.credentials);
if (configNode.id != null) {
var lastTokens = {
accessToken: configNode.credentials.accessToken,
refreshToken: configNode.credentials.refreshToken,
scopes: configNode.scopes,
tokenType: configNode.credentials.tokenType,
expireTime: configNode.credentials.expireTime,
};
fs.writeFileSync(configNode.updatedTokensFile, JSON.stringify(lastTokens, null, 2));
configNode.log(`GoogleNode tokens updated and saved in ${configNode.updatedTokensFile}`);
}
});
}
return configNode.oauth2Client;
}
const fs = require("fs");
var {google} = require('googleapis');
var discovery = google.discovery({version: 'v1'});
RED.httpAdmin.get('/google/apis', function (req, res) {
discovery.apis.list({
fields: "items(name,version)"
}, function (err, data) {
var response = [];
data.data.items.forEach(function (v) {
response.push(encodeAPI(v.name, v.version));
});
response.sort();
res.json(response);
});
});
RED.httpAdmin.get('/google/apis/:api/info', function (req, res) {
var api = decodeAPI(req.params.api);
discovery.apis.getRest({
api: api.name,
version: api.version,
fields: "auth,methods,resources"
}, function (err, data) {
if (err) {
return res.status(500).json(err);
}
var response = {
operations: [],
scopes: []
};
function processResources(d, parent) {
var prefix = parent ? parent + '.' : '';
if (d.methods) {
Object.keys(d.methods).forEach(function (k) {
response.operations.push(prefix + k);
});
}
if (d.resources) {
Object.keys(d.resources).forEach(function (k) {
processResources(d.resources[k], prefix + k);
});
}
}
processResources(data.data);
response.operations.sort();
response.scopes = Object.keys(data.data.auth.oauth2.scopes);
res.json(response);
});
});
function GoogleNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.config = RED.nodes.getNode(config.google);
node.api = config.api;
node.operation = config.operation;
node.scopes = config.scopes;
node.on('input', function (msg) {
node.status({
fill: 'blue',
shape: 'dot',
text: 'pending'
});
var api = decodeAPI(node.api);
api = google[api.name]({
version: api.version,
auth: getOAuth2Client(node.config),
});
var props = (node.operation || msg.operation).split('.');
var operation = api;
props.forEach(function (val) {
operation = operation[val];
});
operation.bind(api)(msg.payload, function (err, res) {
if (err) {
node.status({
fill: 'red',
shape: 'dot',
text: 'error'
});
node.error(err,msg);
return;
}
node.status({
fill: 'yellow',
shape: 'dot',
text: 'success'
});
msg.payload = res.data;
node.send(msg);
});
});
}
RED.nodes.registerType("google", GoogleNode);
};