-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
247 lines (209 loc) · 7.62 KB
/
routes.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
const express = require("express");
const router = express.Router();
const path = require("path");
const srcPath = path.join(__dirname, "public");
const config = require("./adapters/adapter-config");
const ixon = require("./adapters/ixon-adapter");
const ewon = require("./adapters/ewon-adapter");
const mysql = require("mysql")
async function getAdapterSession(username, password, adapter, expiresIn, twoFactorAuthentication){
try {
if (adapter.getAdapterName() === "ixon" && twoFactorAuthentication.length > 0){
return await adapter.getSession(username, password, expiresIn, twoFactorAuthentication=twoFactorAuthentication);
}
return await adapter.getSession(username, password, expiresIn);
}
catch (e) {
throw e;
}
}
function setSessionCookies(res, sessionKey, adapterName, expiresIn, publicId=""){
res.cookie(`${adapterName}-session`, sessionKey, {maxAge: expiresIn * 10000}); // expiresin to milliseconds
if (publicId.length > 0){
res.cookie(`${adapterName}-publicId`, publicId, {maxAge: expiresIn * 10000}); // expiresin to milliseconds
}
return res;
}
/****************
* Sign in routes
****************/
router.get("/", async (req, res) => {
try {
const iSession = req.cookies["ixon-session"];
const validSession = await ixon.isValidSession(iSession);
if (validSession) {
res.redirect("/devices");
}
else {
res.status(200).sendFile(path.join(srcPath, "/signin.html"));
}
}
catch(e) {
console.error(e);
res.status(502).sendFile(path.join(srcPath, "/signin.html"));
}
});
router.post("/signin/ixon", async (req, res) => {
const username = req.body.username;
const password = req.body.password;
const twoFactorAuthentication = req.body.twoFactorAuthentication;
const expiresIn = 3600; // in seconds
let ixonPermissions;
try {
await ixon.getLinkList();
const ixonSessionData = await getAdapterSession(username, password, ixon, expiresIn, twoFactorAuthentication);
const ixonPublicId = ixonSessionData["publicId"];
const ixonSession = ixonSessionData["secretId"];
res = setSessionCookies(res, ixonSession, ixon.getAdapterName(), expiresIn, ixonPublicId);
ixonPermissions = await ixon.getPermissions(ixonSession);
}
catch (e){
console.log(e);
res.status(401).redirect("/?signin=failed");
return;
}
// also get ewon devices if the user has access to all devices
try {
if (ixonPermissions.includes("COMPANY_WIDE_ROLE") || (ixonPermissions.includes("COMPANY_WIDE_ROLE") && ixonPermissions.includes("MANAGE_AGENT"))) {
const ewonSession = await getAdapterSession(config.ewon.username, config.ewon.password, ewon, 60);
res = setSessionCookies(res, ewonSession, ewon.getAdapterName(), expiresIn);
}
res.redirect("/devices");
}
catch (e){
console.log(e);
const params = "?ewonsignin=failed,invalidCompany";
res.status(502).redirect("/devices" + params);
}
});
router.get("/signout", async (req, res) => {
const isession = req.cookies["ixon-session"];
const iPublicId = req.cookies["ixon-publicId"];
const esession = req.cookies["ewon-session"];
try {
const ixonPromise = ixon.deleteSession(isession, iPublicId);
const ewonPromise = ewon.deleteSession(esession);
Promise.all([ixonPromise, ewonPromise])
.then(() => {
res.status(200).redirect("/");
})
.catch((error) => {
res.status(502).redirect("/");
});
}
catch (e) {
res.status(502).redirect("/");
console.log(e);
}
});
/****************
* Devices routes
****************/
router.get("/devices", async (req, res) => {
try {
const iSession = req.cookies["ixon-session"];
const validSession = await ixon.isValidSession(iSession);
if (validSession){
res.status(200).sendFile(path.join(srcPath, "/device-list.html"));
} else {
res.redirect("/");
}
}
catch(e) {
console.error(e);
res.status(401).redirect("/?signin=failed");
}
});
router.get("/devices/ixon", async (req, res) => {
try {
const ixonSession = req.cookies["ixon-session"];
const deviceData = await ixon.getDevices(ixonSession);
// deviceData only contains data for the vnc servers but not for
// the http servers, appendHttpServers() retrieves these links.
await ixon.appendHttpServers(ixonSession, deviceData);
const formattedDevices = ixon.formatDevices(deviceData);
res.status(200).json({"status": "success", "data": formattedDevices});
}
catch (e){
console.error(e);
res.status(502).send("Failed to fetch ixon devices.");
}
});
router.get("/devices/ixon/user", async (req, res) => {
try {
const ixonSession = req.cookies["ixon-session"];
const userData = await ixon.getUserData(ixonSession);
res.status(200).json({"status": "success", "data": userData});
}
catch (e){
res.status(502).send(e);
}
});
router.get("/devices/ixon/logsErrors", async (req, res) => {
try {
// add ixon verification. i.e. access to sql iff access via ixon.
const ixonSession = req.cookies["ixon-session"];
const validSession = await ixon.isValidSession(ixonSession);
if (!validSession){
res.status(401).send("Session unauthorized");
return;
}
const credentials = config.mysql;
const pool = mysql.createPool(credentials);
const query = "SELECT * FROM recenterrors";
pool.query(query, (error, results) => {
if (error) throw error;
res.status(200).send({"status": "success", "data": results});
});
}
catch (e) {
console.log(e);
res.status(502).send({"status": "failed", "data": []});
}
});
router.get("/devices/ixon/recentErrors", async (req, res) => {
try {
// add ixon verification. i.e. access to sql iff access via ixon.
const ixonSession = req.cookies["ixon-session"];
const validSession = await ixon.isValidSession(ixonSession);
if (!validSession){
res.status(401).send("Session unauthorized");
return;
}
const credentials = config.mysql;
const pool = mysql.createPool(credentials);
const query = "SELECT * FROM recenterrors";
pool.query(query, (error, results) => {
if (error) throw error;
res.status(200).send({"status": "success", "data": results});
});
}
catch (e) {
console.log(e);
res.status(502).send({"status": "failed", "data": []});
}
});
router.get("/devices/ewon", async (req, res) => {
try {
const ewonSession = req.cookies["ewon-session"];
const deviceData = await ewon.getDevices(ewonSession);
res.status(200).json({"status": "success", "data": deviceData});
}
catch (e){
console.log(e);
res.status(401).send(e);
}
});
router.get("/devices/ewon/alive", async (req, res) => {
try {
const ewonSession = req.cookies["ewon-session"];
const validSession = await ewon.isValidSession(ewonSession);
if (!validSession) throw new Error("Keep alive request to Ewon failed.");
res.status(200).json({"status": "success"});
}
catch (e){
console.error(e);
res.status(401).json({"status": "failed"});
}
});
module.exports = router;