-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
362 lines (311 loc) · 9.63 KB
/
server.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* CONFIGURATION */
var OpenVidu = require("openvidu-node-client").OpenVidu;
var OpenViduRole = require("openvidu-node-client").OpenViduRole;
// Check launch arguments: must receive openvidu-server URL and the secret
if (process.argv.length != 4) {
console.log("Usage: node " + __filename + " OPENVIDU_URL OPENVIDU_SECRET");
process.exit(-1);
}
// For demo purposes we ignore self-signed certificate
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
// Node imports
var express = require("express");
var fs = require("fs");
var session = require("express-session");
var https = require("https");
var bodyParser = require("body-parser"); // Pull information from HTML POST (express4)
var app = express(); // Create our app with express
const bcrypt = require("bcrypt");
const passport = require("passport");
const flash = require("express-flash");
const methodOverride = require("method-override");
const initializePassport = require("./passport-config");
initializePassport(
passport,
(email) => users_new.find((user) => user.email === email),
(id) => users_new.find((user) => user.id === id)
);
const users_new = [];
// Server configuration
// for parsing application/json
app.use(bodyParser.json());
app.set("view-engine", "ejs");
app.use(express.urlencoded({ extended: false }));
app.use(flash());
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(methodOverride("_method"));
app.use(
session({
saveUninitialized: true,
resave: false,
secret: "MY_SECRET",
})
);
app.use("/public", express.static("public")); // Set the static files location
app.use(
bodyParser.urlencoded({
extended: "true",
})
); // Parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // Parse application/json
app.use(
bodyParser.json({
type: "application/vnd.api+json",
})
); // Parse application/vnd.api+json as json
// Listen (start app with node server.js)
var options = {
key: fs.readFileSync("openvidukey.pem"),
cert: fs.readFileSync("openviducert.pem"),
};
// Mock database
var users = [
{
user: "publisher1",
pass: "pass",
role: OpenViduRole.PUBLISHER,
},
{
user: "publisher2",
pass: "pass",
role: OpenViduRole.PUBLISHER,
},
{
user: "subscriber",
pass: "pass",
role: OpenViduRole.SUBSCRIBER,
},
];
// Environment variable: URL where our OpenVidu server is listening
var OPENVIDU_URL = process.argv[2];
// Environment variable: secret shared with our OpenVidu server
var OPENVIDU_SECRET = process.argv[3];
// Entrypoint to OpenVidu Node Client SDK
var OV = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET);
// Collection to pair session names with OpenVidu Session objects
var mapSessions = {};
// Collection to pair session names with tokens
var mapSessionNamesTokens = {};
/* CONFIGURATION */
/* REST API */
// Login
app.get("/", checkAuthenticated, (req, res) => {
res.sendFile("views/index.html", { root: __dirname });
});
app.get("/login", checkNotAuthenticated, (req, res) => {
res.render("intro.ejs");
});
app.get("/index", (req, res) => {
res.render("index.ejs", { name: "KATY" });
});
app.get("lol", (req, res) => {
res.render("lo.html");
});
app.post(
"/login",
checkNotAuthenticated,
passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/login",
failureFlash: true,
})
);
app.get("/register", checkNotAuthenticated, (req, res) => {
res.render("register.ejs");
});
app.post("/register", checkNotAuthenticated, async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
users_new.push({
id: Date.now().toString(),
name: req.body.name,
email: req.body.email,
password: hashedPassword,
});
res.redirect("/login");
console.log("Success create account");
} catch {
console.log("Failed to create account");
}
});
app.delete("/logout", (req, res) => {
req.logOut();
res.redirect("/login");
});
app.post("/api-login/login", function (req, res) {
// Retrieve params from POST body
var user = req.body.user;
var pass = req.body.pass;
console.log("Logging in | {user, pass}={" + user + ", " + pass + "}");
if (login(user, pass)) {
// Correct user-pass
// Validate session and return OK
// Value stored in req.session allows us to identify the user in future requests
console.log("'" + user + "' has logged in");
req.session.loggedUser = user;
res.status(200).send();
} else {
// Wrong user-pass
// Invalidate session and return error
console.log("'" + user + "' invalid credentials");
req.session.destroy();
res.status(401).send("User/Pass incorrect");
}
});
// Logout
app.post("/api-login/logout", function (req, res) {
console.log("'" + req.session.loggedUser + "' has logged out");
req.session.destroy();
res.status(200).send();
});
// Get token (add new user to session)
app.post("/api-sessions/get-token", function (req, res) {
if (!isLogged(req.session)) {
req.session.destroy();
res.status(401).send("User not logged");
} else {
// The video-call to connect
var sessionName = req.body.sessionName;
// Role associated to this user
var role = users.find((u) => u.user === req.session.loggedUser).role;
// Optional data to be passed to other users when this user connects to the video-call
// In this case, a JSON with the value we stored in the req.session object on login
var serverData = JSON.stringify({ serverData: req.session.loggedUser });
console.log("Getting a token | {sessionName}={" + sessionName + "}");
// Build connectionProperties object with the serverData and the role
var connectionProperties = {
data: serverData,
role: role,
};
if (mapSessions[sessionName]) {
// Session already exists
console.log("Existing session " + sessionName);
// Get the existing Session from the collection
var mySession = mapSessions[sessionName];
// Generate a new token asynchronously with the recently created connectionProperties
mySession
.createConnection(connectionProperties)
.then((connection) => {
// Store the new token in the collection of tokens
mapSessionNamesTokens[sessionName].push(connection.token);
// Return the token to the client
res.status(200).send({
0: connection.token,
});
})
.catch((error) => {
console.error(error);
});
} else {
// New session
console.log("New session " + sessionName);
// Create a new OpenVidu Session asynchronously
OV.createSession()
.then((session) => {
// Store the new Session in the collection of Sessions
mapSessions[sessionName] = session;
// Store a new empty array in the collection of tokens
mapSessionNamesTokens[sessionName] = [];
// Generate a new connection asynchronously with the recently created connectionProperties
session
.createConnection(connectionProperties)
.then((connection) => {
// Store the new token in the collection of tokens
mapSessionNamesTokens[sessionName].push(connection.token);
// Return the Token to the client
res.status(200).send({
0: connection.token,
});
})
.catch((error) => {
console.error(error);
});
})
.catch((error) => {
console.error(error);
});
}
}
});
// Remove user from session
app.post("/api-sessions/remove-user", function (req, res) {
if (!isLogged(req.session)) {
req.session.destroy();
res.status(401).send("User not logged");
} else {
// Retrieve params from POST body
var sessionName = req.body.sessionName;
var token = req.body.token;
console.log(
"Removing user | {sessionName, token}={" +
sessionName +
", " +
token +
"}"
);
// If the session exists
if (mapSessions[sessionName] && mapSessionNamesTokens[sessionName]) {
var tokens = mapSessionNamesTokens[sessionName];
var index = tokens.indexOf(token);
// If the token exists
if (index !== -1) {
// Token removed
tokens.splice(index, 1);
console.log(sessionName + ": " + tokens.toString());
} else {
var msg = "Problems in the app server: the TOKEN wasn't valid";
console.log(msg);
res.status(500).send(msg);
}
if (tokens.length == 0) {
// Last user left: session must be removed
console.log(sessionName + " empty!");
delete mapSessions[sessionName];
}
res.status(200).send();
} else {
var msg = "Problems in the app server: the SESSION does not exist";
console.log(msg);
res.status(500).send(msg);
}
}
});
/* REST API */
/* AUXILIARY METHODS */
function login(user, pass) {
return users.find((u) => u.user === user && u.pass === pass);
}
function isLogged(session) {
return session.loggedUser != null;
}
function getBasicAuth() {
return (
"Basic " + new Buffer("OPENVIDUAPP:" + OPENVIDU_SECRET).toString("base64")
);
}
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect("/login");
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect("/");
}
next();
}
/* AUXILIARY METHODS */
https.createServer(options, app).listen(5000);
console.log("App listening on port 5000");