forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleauth_nodes.ts
More file actions
309 lines (298 loc) · 11.9 KB
/
Copy pathgoogleauth_nodes.ts
File metadata and controls
309 lines (298 loc) · 11.9 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
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
import * as RED from "node-red";
import { Red } from "node-red";
import { NoderedUtil } from "@openiap/openflow-api";
import { Config } from "../../Config";
const { GoogleAuth, OAuth2Client } = require('google-auth-library');
const fs = require("fs");
// const request = require('request');
const request = require("request");
function GetGoogleAuthClient(config: Igoogleauth_credentials): any {
const result = {
auth: null,
Client: null
}
if (config != null) {
if (typeof config.serviceaccount === "string" && !NoderedUtil.IsNullEmpty(config.serviceaccount)) {
config.serviceaccount = JSON.parse(config.serviceaccount);
}
if (!NoderedUtil.IsNullEmpty(config.serviceaccount)) {
result.auth = new GoogleAuth({
scopes: config.scopes.split(",").join(" "),
credentials: config.serviceaccount
});
}
if (!NoderedUtil.IsNullEmpty(config.clientid) || !NoderedUtil.IsNullEmpty(config.clientsecret) || !NoderedUtil.IsNullEmpty(config.redirecturi)) {
result.Client = new OAuth2Client(
config.clientid,
config.clientsecret,
config.redirecturi
);
if (!NoderedUtil.IsNullEmpty(config.tokens)) {
result.Client.setCredentials(JSON.parse(config.tokens));
}
}
}
return result;
}
export interface Igoogleauth_credentials {
name: string;
clientid: string;
clientsecret: string;
redirecturi: string;
scopes: string;
code: string;
tokens: string;
serviceaccount: string;
authtype: string;
apikey: string;
username: string;
password: string;
}
export class googleauth_credentials {
public node: Red = null;
public name: string = "";
public clientid: string = "";
public clientsecret: string = "";
public redirecturi: string = "";
public scopes: string = "";
public code: string = "";
public tokens: string = "";
public serviceaccount: string = "";
public apikey: string = "";
public authtype: string = "";
public username: string = "";
public password: string = "";
constructor(public config: Igoogleauth_credentials) {
RED.nodes.createNode(this, config);
this.node = this;
this.node.status({});
this.name = this.config.name;
this.authtype = this.config.authtype;
if (this.node.credentials && this.node.credentials.hasOwnProperty("clientid")) {
this.clientid = this.node.credentials.clientid;
}
if (this.node.credentials && this.node.credentials.hasOwnProperty("clientsecret")) {
this.clientsecret = this.node.credentials.clientsecret;
}
if (this.node.credentials && this.node.credentials.hasOwnProperty("redirecturi")) {
this.redirecturi = this.node.credentials.redirecturi;
}
if (this.node.credentials && this.node.credentials.hasOwnProperty("scopes")) {
this.scopes = this.node.credentials.scopes;
}
if (this.node.credentials && this.node.credentials.hasOwnProperty("code")) {
this.code = this.node.credentials.code;
}
if (this.node.credentials && this.node.credentials.hasOwnProperty("tokens")) {
this.tokens = this.node.credentials.tokens;
}
if (this.node.credentials && this.node.credentials.hasOwnProperty("serviceaccount")) {
this.serviceaccount = this.node.credentials.serviceaccount;
}
if (this.node.credentials && this.node.credentials.hasOwnProperty("apikey")) {
this.apikey = this.node.credentials.apikey;
}
if (this.node.credentials && this.node.credentials.hasOwnProperty("username")) {
this.username = this.node.credentials.username;
}
if (this.node.credentials && this.node.credentials.hasOwnProperty("password")) {
this.password = this.node.credentials.password;
}
this.init();
}
async init() {
if (NoderedUtil.IsNullEmpty(this.clientid) || NoderedUtil.IsNullEmpty(this.clientsecret) || NoderedUtil.IsNullEmpty(this.redirecturi)) return;
const me = this;
let oAuth2Client = new OAuth2Client(
this.clientid,
this.clientsecret,
this.redirecturi
);
if (!NoderedUtil.IsNullEmpty(this.tokens)) {
oAuth2Client.setCredentials(JSON.parse(this.tokens));
return;
}
let authorizeUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: this.scopes.split(",").join(" ")
});
const googleauthGetURL = function googleauthGetURL(req, res) {
try {
me.clientid = req.query.clientid;
me.redirecturi = req.query.redirecturi;
if (req.query.clientsecret != '__PWRD__') me.clientsecret = req.query.clientsecret;
//
oAuth2Client = new OAuth2Client(
me.clientid,
me.clientsecret,
me.redirecturi
);
authorizeUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: req.query.scopes.split(",").join(" ")
});
RED.httpAdmin.get('/googleauth-code', googleauthCode);
RED.httpAdmin.get('/googleauth-set-code', googleauthSetCode);
res.json({ url: authorizeUrl });
} catch (error) {
res.status(500).send(error);
}
};
const googleauthCode = function googleauthCode(req, res) {
const code = req.query.code;
res.json({ code: code });
};
const googleauthSetCode = async function googleauthSetCode(req, res) {
try {
// Now that we have the code, use that to acquire tokens.
const code = req.query.code;
const r = await oAuth2Client.getToken(code);
// Make sure to set the credentials on the OAuth2 client.
oAuth2Client.setCredentials(r.tokens);
res.json({ tokens: r.tokens });
} catch (error) {
res.status(500).send(error);
}
};
// We can find mappings by function name, so we start by cleaning up,
// and add mappings as needed
const removeRoute = function (route, i, routes) {
if (route.handle.name == "googleauthGetURL" || route.handle.name == "googleauthCode" || route.handle.name == "googleauthSetCode") {
routes.splice(i, 1);
}
if (route.route) route.route.stack.forEach(removeRoute);
};
RED.httpAdmin._router.stack.forEach(removeRoute);
RED.httpAdmin.get('/googleauth-get-' + this.node.id, googleauthGetURL);
}
}
export async function get_rpa_workflows(req, res) {
try {
const query: any = { _type: "workflow" };
const result: any[] = await NoderedUtil.Query({
collectionname: 'openrpa', query,
projection: { name: 1, projectandname: 1 }, orderby: { projectid: -1, name: -1 }, top: 1000, queryas: req.query.queue
})
res.json(result);
} catch (error) {
res.status(500).json(error);
}
}
export interface Igoogleauth_request {
config: string;
method: string;
url: string;
name: string;
ignoretls: boolean;
asjson: boolean;
}
export class googleauth_request {
public node: Red = null;
public name: string = "";
public _config: Igoogleauth_credentials;
public method: string = "";
public url: string = "";
public auth: any = null;
public Client: any = null;
public ignoretls: boolean;
public asjson: boolean;
constructor(public config: Igoogleauth_request) {
try {
RED.nodes.createNode(this, config);
this.node = this;
this.name = config.name;
this.node.status({ fill: "blue", shape: "dot", text: "Initializing" });
this._config = RED.nodes.getNode(this.config.config);
this.method = this.config.method;
this.url = this.config.url;
if (this.config.ignoretls != null) {
this.ignoretls = Config.parseBoolean(this.config.ignoretls);
} else {
this.ignoretls = false;
}
if (this.config.asjson != null) {
this.asjson = Config.parseBoolean(this.config.asjson);
} else {
this.asjson = true;
}
const cli = GetGoogleAuthClient(this._config);
this.Client = cli.Client;
this.auth = cli.auth;
this.node.on('input', this.oninput);
this.node.status({});
} catch (error) {
NoderedUtil.HandleError(this, error, null);
}
}
async oninput(msg: any, send: any, done: any) {
try {
this.node.status({ fill: "blue", shape: "dot", text: "Getting client" });
if (this.auth != null) {
this.Client = await this.auth.getClient();
}
if (!NoderedUtil.IsNullEmpty(msg.method)) this.method = msg.method;
if (!NoderedUtil.IsNullEmpty(msg.url)) this.url = msg.url;
if (NoderedUtil.IsNullEmpty(this.method)) this.method = msg.method;
if (NoderedUtil.IsNullEmpty(this.method)) this.method = "GET";
if (NoderedUtil.IsNullEmpty(this.url)) this.url = msg.url;
if (NoderedUtil.IsNullEmpty(this.url)) throw new Error("url is mandaotry");
let url = this.url;
if (!NoderedUtil.IsNullUndefinded(this._config) && this._config.authtype == "apikey" && !NoderedUtil.IsNullEmpty(this._config.apikey)) {
if (url.indexOf("?") > -1) {
url = url + "&key=" + this._config.apikey;
} else {
url = url + "?key=" + this._config.apikey;
}
}
const options: any = {
method: this.method,
url,
data: msg.payload,
rejectUnauthorized: (!this.ignoretls)
// data: {
// payload: msg.payload
// }
};
if (this.method == "GET") delete options.data;
this.node.status({ fill: "blue", shape: "dot", text: "Requesting" });
if (this.Client != null) {
const res: any = await this.Client.request(options);
msg.status = res.status;
msg.statusText = res.statusText;
msg.payload = res.data;
this.node.status({});
send(msg);
done();
} else {
if (!NoderedUtil.IsNullUndefinded(this._config) && this._config.authtype == "username") {
if (!NoderedUtil.IsNullEmpty(this._config.username)) {
options.headers = {};
options.headers["Authorization"] = "Basic " + new Buffer(this._config.username + ":" + this._config.password).toString("base64");
}
}
if (this.asjson) {
options.body = options.data;
options.json = true;
delete options.data;
}
request(options, (error, response, body) => {
if (error) {
NoderedUtil.HandleError(this, error, msg);
return done();
}
msg.payload = body;
msg.status = response.statusCode;
msg.statusText = response.statusText;
this.node.status({});
send(msg);
done();
});
}
} catch (error) {
done();
NoderedUtil.HandleError(this, error, msg);
}
}
onclose() {
}
}