-
Notifications
You must be signed in to change notification settings - Fork 0
/
snap4city-utility.js
274 lines (260 loc) · 12.8 KB
/
snap4city-utility.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
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
module.exports = {
getLogger: function (RED, node) {
const {
createLogger,
format,
transports
} = require('winston');
return createLogger({
level: (RED.settings.s4cLogLevel ? RED.settings.s4cLogLevel : "info"),
format: format.combine(
format.label({
label: node.type + ":" + node.id
}),
format.timestamp(),
format.printf(({
level,
message,
label,
timestamp
}) => {
return `${timestamp} [${level}][${label}] : ${message}`;
})
),
transports: [
new transports.Console()
],
});
},
eventLog: function (RED, inPayload, outPayload, config, _agent, _motivation, _ipext, _modcom) {
var os = require('os');
var ifaces = os.networkInterfaces();
var uri = (RED.settings.eventLogUri ? RED.settings.eventLogUri : null);
if (uri != null) {
var pidlocal = RED.settings.APPID;
var iplocal = null;
Object.keys(ifaces).forEach(function (ifname) {
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
iplocal = iface.address;
});
});
iplocal = iplocal + ":" + RED.settings.uiPort;
var timestamp = new Date().getTime();
var modcom = _modcom;
var ipext = _ipext;
var payloadsize = 0;
try {
payloadsize = JSON.stringify(outPayload).length / 1000;
} catch (err) {
try {
payloadsize = JSON.stringify(outPayload.payload).length / 1000;
} catch (error) {
//Catched
}
}
var agent = _agent;
var motivation = _motivation;
var lang = (inPayload.lang ? inPayload.lang : (config ? config.lang : ""));
var lat = (inPayload.lat ? inPayload.lat : (config ? config.lat : ""));
var lon = (inPayload.lon ? inPayload.lon : (config ? config.lon : ""));
var serviceuri = (inPayload.serviceuri ? inPayload.serviceuri : (config ? config.serviceuri : ""));
var message = (inPayload.message ? inPayload.message : (config ? config.message : ""));
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xmlHttp = new XMLHttpRequest();
console.log(encodeURI(uri + "/?p=log" + "&pid=" + pidlocal + "&tmstmp=" + timestamp + "&modCom=" + modcom + "&IP_local=" + iplocal + "&IP_ext=" + ipext +
"&payloadSize=" + payloadsize + "&agent=" + agent + "&motivation=" + motivation + "&lang=" + lang + "&lat=" + (typeof lat != "undefined" ? lat : 0.0) + "&lon=" + (typeof lon != "undefined" ? lon : 0.0) + "&serviceUri=" + serviceuri + "&message=" + message));
xmlHttp.open("GET", encodeURI(uri + "/?p=log" + "&pid=" + pidlocal + "&tmstmp=" + timestamp + "&modCom=" + modcom + "&IP_local=" + iplocal + "&IP_ext=" + ipext +
"&payloadSize=" + payloadsize + "&agent=" + agent + "&motivation=" + motivation + "&lang=" + lang + "&lat=" + (typeof lat != "undefined" ? lat : 0.0) + "&lon=" + (typeof lon != "undefined" ? lon : 0.0) + "&serviceUri=" + serviceuri + "&message=" + message), true); // false for synchronous request
xmlHttp.send(null);
}
},
retrieveCurrentUser: function (RED, node, authentication) {
var fs = require('fs');
var atob = require('atob');
var response = "";
if (node != null && authentication != null) {
node.s4cAuth = RED.nodes.getNode(authentication);
if (node.s4cAuth != null) {
response = node.s4cAuth.retrieveCurrentUser();
}
}
if (fs.existsSync('/data/refresh_token') && response == "") {
var refreshToken = fs.readFileSync('/data/refresh_token', 'utf-8');
var url = (RED.settings.keycloakBaseUri ? RED.settings.keycloakBaseUri : "https://www.snap4city.org/auth/realms/master/") + "/protocol/openid-connect/token/";
var params = "client_id=" + (RED.settings.keycloakClientid ? RED.settings.keycloakClientid : "nodered") + "&client_secret=" + (RED.settings.keycloakClientsecret ? RED.settings.keycloakClientsecret : "943106ae-c62c-4961-85a2-849f6955d404") + "&grant_type=refresh_token&scope=openid profile&refresh_token=" + refreshToken;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xmlHttp = new XMLHttpRequest();
console.log("Retrieve user from:" + encodeURI(url));
xmlHttp.open("POST", encodeURI(url), false);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(params);
if (xmlHttp.responseText != "") {
try {
response = JSON.parse(xmlHttp.responseText);
} catch (e) {}
}
if (response != "") {
fs.writeFileSync('/data/refresh_token', response.refresh_token);
try {
response = JSON.parse(atob(response.access_token.split('.')[1]));
} catch (e) {
console.log(e);
return "";
}
if (response.preferred_username != "" && response.preferred_username != undefined && response.preferred_username != "undefined") {
response = response.preferred_username;
} else {
response = response.username;
}
}
}
return response
},
retrieveAccessToken: function (RED, node, authentication, uid) {
return retrieveAccessToken(RED, node, authentication, uid, true);
},
retrieveAccessToken: function (RED, node, authentication, uid, fillStatus) {
var fs = require('fs');
var response = "";
if (node != null && authentication != null) {
node.s4cAuth = RED.nodes.getNode(authentication);
if (node.s4cAuth != null) {
var accessToken = node.s4cAuth.refreshTokenGetAccessToken(uid);
if (accessToken != "") {
if (fillStatus) node.status({
fill: "green",
shape: "dot",
text: "Authenticaton Ok"
});
response = accessToken;
} else {
if (fillStatus) node.status({
fill: "red",
shape: "dot",
text: "Authentication Problem"
});
}
}
}
if (fs.existsSync('/data/refresh_token') && response == "") {
var refreshToken = fs.readFileSync('/data/refresh_token', 'utf-8');
var url = (RED.settings.keycloakBaseUri ? RED.settings.keycloakBaseUri : "https://www.snap4city.org/auth/realms/master/") + "/protocol/openid-connect/token/";
var params = "client_id=" + (RED.settings.keycloakClientid ? RED.settings.keycloakClientid : "nodered") + "&grant_type=refresh_token&scope=openid profile&refresh_token=" + refreshToken;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xmlHttp = new XMLHttpRequest();
//console.log("Retrieve token from:" + encodeURI(url));
xmlHttp.open("POST", encodeURI(url), false);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(params);
if (xmlHttp.responseText != "") {
try {
response = JSON.parse(xmlHttp.responseText);
} catch (e) {}
}
if (response != "") {
fs.writeFileSync('/data/refresh_token', response.refresh_token);
response = response.access_token;
}
}
return response;
},
retrieveAppID: function (RED) {
var fs = require('fs');
var os = require('os');
var correctPath = os.homedir() + "/";
if (RED.settings.APPID != null) {
return RED.settings.APPID;
} else if (fs.existsSync(correctPath + ".snap4cityConfig/appid")) {
return fs.readFileSync(correctPath + ".snap4cityConfig/appid", 'utf-8')
} else if (fs.existsSync(__dirname + "/../node-red-contrib-snap4city-user/config/appid")) {
if (!fs.existsSync(correctPath + ".snap4cityConfig")) {
fs.mkdirSync(correctPath + ".snap4cityConfig");
}
fs.writeFileSync(correctPath + ".snap4cityConfig/appid", fs.readFileSync(__dirname + "/../node-red-contrib-snap4city-user/config/appid", 'utf-8'));
return fs.readFileSync(correctPath + ".snap4cityConfig/appid", 'utf-8')
} else if (fs.existsSync(__dirname + "/../node-red-contrib-snap4city-developer/config/appid")) {
if (!fs.existsSync(correctPath + ".snap4cityConfig")) {
fs.mkdirSync(correctPath + ".snap4cityConfig");
}
fs.writeFileSync(correctPath + ".snap4cityConfig/appid", fs.readFileSync(__dirname + "/../node-red-contrib-snap4city-developer/config/appid", 'utf-8'));
return fs.readFileSync(correctPath + ".snap4cityConfig/appid", 'utf-8')
} else {
var crypto = require('crypto');
var appid = "edge" + crypto.randomBytes(30).toString('hex');
if (!fs.existsSync(correctPath + ".snap4cityConfig")) {
fs.mkdirSync(correctPath + ".snap4cityConfig");
}
}
fs.writeFileSync(correctPath + ".snap4cityConfig/appid", appid);
return appid;
},
saveNodeContext: function (RED, node, context) {
var fs = require('fs');
var os = require('os');
var correctPath = os.homedir() + "/";
if (RED.settings.APPID != null) {
correctPath = "data/";
}
if (!fs.existsSync(correctPath + ".snap4cityConfig")) {
fs.mkdirSync(correctPath + ".snap4cityConfig");
}
if (!fs.existsSync(correctPath + ".snap4cityConfig/context")) {
fs.mkdirSync(correctPath + ".snap4cityConfig/context");
}
fs.writeFileSync(correctPath + ".snap4cityConfig/context/" + node.id, JSON.stringify(context));
},
retrieveNodeContext: function (RED, node) {
var fs = require('fs');
var os = require('os');
var correctPath = os.homedir() + "/";
if (RED.settings.APPID != null) {
correctPath = "data/";
}
if (!fs.existsSync(correctPath + ".snap4cityConfig")) {
fs.mkdirSync(correctPath + ".snap4cityConfig");
}
if (!fs.existsSync(correctPath + ".snap4cityConfig/context")) {
fs.mkdirSync(correctPath + ".snap4cityConfig/context");
}
if (fs.existsSync(correctPath + ".snap4cityConfig/context/" + node.id)) {
var response = "";
try {
response = JSON.parse(fs.readFileSync(correctPath + ".snap4cityConfig/context/" + node.id, 'utf-8'));
} catch (e) {
response = fs.readFileSync(correctPath + ".snap4cityConfig/context/" + node.id, 'utf-8');
}
return response;
}
return JSON.parse("{}");
},
deleteNodeContext: function (RED, node) {
var fs = require('fs');
var os = require('os');
var correctPath = os.homedir() + "/";
if (RED.settings.APPID != null) {
correctPath = "data/";
}
if (fs.existsSync(correctPath + ".snap4cityConfig") && fs.existsSync(correctPath + ".snap4cityConfig/context") && fs.existsSync(correctPath + ".snap4cityConfig/context/" + node.id)) {
fs.unlinkSync(correctPath + ".snap4cityConfig/context/" + node.id);
}
},
settingUrl: function(RED, node, parameterUrl, defaultUrl, basePath, isWebSocket){
var url = "";
if (node.s4cAuth != null && node.s4cAuth.domain){
if (isWebSocket){
url = node.s4cAuth.domain.replace("https", "wss").replace("http", "ws") + "/" + basePath + "/";
} else {
url = node.s4cAuth.domain + "/" + basePath + "/";
}
} else if (RED.settings[parameterUrl]){
url = RED.settings[parameterUrl] + "/";
} else {
url = defaultUrl + "/" + basePath + "/";
}
return url.replace(/\/\//g, "/").replace(/:\//g, "://");
}
}