This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
forked from mozilla/socialapi-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
333 lines (282 loc) · 8.92 KB
/
app.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
var express = require("express"),
https = require("https"),
sys = require("sys"),
app = express();
var debugLogging = true;
function debugLog(str) {
if (debugLogging) {
sys.debug(str);
}
}
app.use(express.bodyParser());
app.use(express.cookieParser("thisistehsecret"));
app.use(express.session());
app.use(express.static(__dirname + "/static"));
var users = {};
var port = process.env.VMC_APP_PORT || process.env.PORT || 5000;
var audience;
if (process.env.AUDIENCE)
audience = process.env.AUDIENCE;
else if (process.env.VMC_APP_NAME)
audience = "https://" + process.env.VMC_APP_NAME + ".vcap.mozillalabs.com";
else
audience = "http://webrtc-social.herokuapp.com";
// We use EventSource for presence. The events are named "userjoined"
// and "userleft".
app.get("/events", function(req, res) {
var user = req.session.user;
if (!user) {
debugLog("/events connection rejected (unauthorized)");
res.send(401, "Unauthorized, events access denied");
return;
}
// Setup event channel.
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache"
});
// Ping every 1 second.
var pinger = setInterval(function() {
if (res)
res.write(":ping\n\n");
}, 20000);
var key = req.connection.remoteAddress + ":" + req.connection.remotePort;
debugLog("Adding " + user + ", connected from " + key);
// Auto logout on disconnect.
req.on("close", function() {
clearInterval(pinger);
delete users[key];
logout(req);
});
// First copy the list of existing users.
var keys = Object.keys(users);
// Add the current user to the list of online users.
users[key] = {id: user, response: res, sessionID: req.sessionID,
source: req.query["source"]};
for (var i = 0; i < keys.length; i++) {
var userName = users[keys[i]].id;
if (userName != req.session.user) {
channelWrite(res, "userjoined", userName);
channelWrite(users[keys[i]].response, "userjoined", user);
}
}
debugLog("There are now now " + Object.keys(users).length + " online users");
});
function findConnectionsForUser(aUser) {
return Object.keys(users)
.map(function(k) { return users[k]; })
.filter(function(u) { return u.id == aUser});
}
app.post("/call", function(req, res) {
if (!req.session.user) {
res.send(401, "Unauthorized, access denied");
return;
}
var connections = findConnectionsForUser(req.session.user);
if (!connections.length) {
res.send(400, "User not logged in for making calls");
return;
}
for (var i = 0; i < connections.length; ++i) {
if (connections[i].sessionID == req.sessionID &&
connections[i].source == "sidebar") {
channelWrite(connections[i].response, "call", JSON.stringify(req.body));
break;
}
}
res.send(200);
});
app.post("/login", function(req, res) {
if (req.session.user) {
debugLog("User session for " + req.session.user + " already created!");
res.send(200, req.session.user);
return;
}
if (!req.body.assertion) {
res.send(500, "Invalid login request");
return;
}
if (req.body.fake) {
finishLogin(req.body.assertion);
} else {
verifyAssertion(req.body.assertion, audience, function(val) {
if (val) {
finishLogin(val);
} else {
res.send(401, "Invalid Persona assertion");
}
});
}
function finishLogin(user) {
req.session.regenerate(function() {
debugLog("Creating user session for " + user);
req.session.user = user;
res.send(200, user);
});
}
});
// res has a value if the client sent a /logout request and expects a reply,
// and is undefined if the connection has been closed by the client.
function logout(req, res) {
if (!req.session.user) {
debugLog(JSON.stringify(req.session) + " " + req.session.user);
if (res) {
debugLog("Denying logout");
res.send(401, "No user currently logged in");
}
return;
}
var user = req.session.user;
// Only send the userleft events if this is the last channel for the user.
if (!findConnectionsForUser(user).length) {
var keys = Object.keys(users);
for (var i = 0; i < keys.length; ++i)
channelWrite(users[keys[i]].response, "userleft", user);
}
req.session.destroy(function() {
debugLog("Logging out " + user);
if (res)
res.send(200);
});
}
app.post("/logout", logout);
app.post("/offer", function(req, res) {
if (!checkRequest(req, res, "offer"))
return;
// Send the offer to all connections of the contact.
var to = req.body.to;
var connections = findConnectionsForUser(to);
if (!connections.length) {
res.send(400, to + " isn't connected (failed /offer)");
return;
}
req.body.from = req.session.user;
var data = JSON.stringify(req.body);
connections.forEach(function(c) { channelWrite(c.response, "offer", data); });
// Remember which connection of the user has initiated the call.
var userConnections = findConnectionsForUser(req.session.user);
for (var i = 0; i < userConnections.length; ++i) {
if (userConnections[i].sessionID == req.sessionID) {
userConnections[i].calling = to;
break;
}
}
res.send(200);
});
app.post("/answer", function(req, res) {
if (!checkRequest(req, res, "answer"))
return;
// Send the answer to the caller.
var connections = findConnectionsForUser(req.body.to);
for (var i = 0; i < connections.length; ++i) {
if (connections[i].calling != req.session.user)
continue;
req.body.from = req.session.user;
channelWrite(connections[i].response, "answer", JSON.stringify(req.body));
delete connections[i].calling;
break;
}
// Send a stopcall to all the other user connections that received the offer.
findConnectionsForUser(req.session.user).forEach(function(c) {
if (c.sessionID != req.sessionID)
channelWrite(c.response, "stopcall", JSON.stringify({from: req.body.to}));
});
res.send(200);
});
app.post("/stopcall", function(req, res) {
if (!checkRequest(req, res, "stopcall"))
return;
req.body.from = req.session.user;
// Check first if there are connections with pending calls. If there
// are, send the stopcall message only to these connections (this
// avoids cancelling an ongoing call if the same user tried to call
// from another connection point).
// Otherwise, send the stopcall to all connections, to cancel the active call.
// We shouldn't send a stopcall to connections that weren't part of
// the call, but the UI will just ignore them if there's no ongoing
// call from that person...
var connections = findConnectionsForUser(req.body.to);
var callingConnections =
connections.filter(function(c) { return "calling" in c; });
if (callingConnections.length)
connections = callingConnections;
connections.forEach(function(c) {
channelWrite(c.response, "stopcall", JSON.stringify(req.body));
delete c.calling;
});
// Send a stopcall to all the other user connections that received the offer.
findConnectionsForUser(req.session.user).forEach(function(c) {
if (c.sessionID != req.sessionID)
channelWrite(c.response, "stopcall", JSON.stringify({from: req.body.to}));
});
res.send(200);
});
app.listen(port, function() {
debugLog("Port is " + port + " with audience " + audience);
});
// Helper functions.
function checkRequest(req, res, type) {
if (!req.session.user) {
debugLog("Unathorized request for " + type);
res.send(401, "Unauthorized, " + type + " access denied");
return false;
}
if (!req.body.to || (!req.body.request && type != "stopcall")) {
res.send(400, "Invalid " + type + " request");
return false;
}
if (!findConnectionsForUser(req.body.to).length) {
res.send(400, "Invalid user for " + type);
return false;
}
return true;
}
function channelWrite(aChannel, aEventType, aData) {
if (debugLogging) {
var to = "";
for (var u in users) {
if (users[u].response === aChannel) {
to = users[u].id;
break;
}
}
debugLog("to: " + to + ", type = " + aEventType + ", data = " + aData);
}
aChannel.write("event: " + aEventType + "\ndata: " + aData + "\n\n");
}
function verifyAssertion(ast, aud, cb) {
var data = "audience=" + encodeURIComponent(aud);
data += "&assertion=" + encodeURIComponent(ast);
var options = {
host: "verifier.login.persona.org",
path: "/verify",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": data.length
}
};
var req = https.request(options, function(res) {
var ret = "";
res.on("data", function(chunk) {
ret += chunk;
});
res.on("end", function() {
try {
var val = JSON.parse(ret);
} catch(e) {
cb(false);
return;
}
if (val.status == "okay") {
cb(val.email);
} else {
debugLog(data);
debugLog(val);
cb(false);
}
});
});
req.write(data);
req.end();
}