-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsso.js
784 lines (700 loc) · 22.3 KB
/
sso.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
const express = require('express');
const cookieParser = require('cookie-parser')
const exphbs = require('express-handlebars');
const cors = require('cors')
const session = require('express-session');
const serveStatic = require('serve-static')
const jwtDecode = require( "jwt-decode");
const fetch = require('node-fetch');
const jwt = require('jsonwebtoken');
const {
v4: uuidv4
} = require('uuid');
const DB = require('./db');
const { start } = require('pm2');
const {
SSO_IFRAME_PROTOCOL,
SESSION_SECRET,
NODE_ENV,
BEARER
} = process.env;
let t = jwtDecode(BEARER);
console.log("BEARER = " + JSON.stringify(t))
console.log("Time = ", Date.now());
console.log("Days till expired: ", (t.exp - Date.now() / 1000) / (60 * 60 * 24))
const app = express();
var corsOptions = {
origin: 'https://xr.realitymedia.digital',
credentials: true
}
app.use(cors(corsOptions))
app.options('*', cors(corsOptions))
// for the iframe that wants the index file and script
app.use(serveStatic("public", {fallthrough: true}));
const PROTOCOL = SSO_IFRAME_PROTOCOL || "https:";
// setup route middleware
app.set('trust proxy', 1) // trust first proxy
app.use(session({
secret: SESSION_SECRET || "SuperSecretValue",
resave: false,
saveUninitialized: true,
cookie: {
secure: NODE_ENV ? true : false
}
}))
// parse application/json
app.use(express.json());
// parse cookies for token
app.use(cookieParser())
// enable handlebars templating engine
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
let accountId = function(token) {
try {
return jwtDecode(token).sub
} catch (e) {
return -1
}
}
let accountInfo = async function(email) {
let json = await fetch('https://xr.realitymedia.digital/api/v1/accounts/search', {
method: 'post',
body: JSON.stringify({ email: email }),
headers: { 'Content-Type': 'application/json', "Authorization" : "bearer " + BEARER },
})
.then(res => res.json());
//console.log("account id for " + email + ": " + json)
if (json.data) {
return json.data[0]
} else {
return null
}
}
let validateId = async function(email, token) {
let id = accountId(token)
let info = await accountInfo(email)
if (id > 0 && info && id == info.id) {
return id
}
return 0
}
var userWork = [];
let startUserWork = async function (id) {
console.log("starting user work for " + id)
if (!userWork[id]) {
console.log("no need to wait, can do work for " + id)
userWork[id] = true;
return true;
}
console.log("waiting for user work to finish for " + id)
while (userWork[id]) {
await sleep(100);
}
console.log("done waiting, can do work for " + id)
userWork[id] = true;
return true;
}
let endUserWork = function (id) {
console.log("ending user work for " + id)
userWork[id] = false;
}
let roomProtos = [
{
name: "Onboarding and Rotunda",
scene_id: "HJKfYJk",
description: "Entrance room and Central Hub",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
},
{
name: "History",
scene_id: "zDncjsX",
description: "The History of Reality Media",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
},
{
name: "3D Graphics and Sensing",
scene_id: "BMwKB9V",
description: "Computer Graphics and Sensing for Reality Media",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
},
{
name: "Presence and Aura",
scene_id: "DY4gSzC",
description: "Presence and Aura and Reality Media",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
},
{
name: "The Pit",
scene_id: "Jnop2M4",
description: "Testing Presence effects in the UNC Pit",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
},
{
name: "The Acropolis",
scene_id: "A6MXLQn",
description: "Leveraging Aura in the Acropolis room with the Parthenon",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
},
{
name: "Privacy and the Future",
scene_id: "GLvFfFb",
description: "Privacy, Public Spaces, and the Future of Reality Media",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
},
{
name: "Genres of Reality Media",
scene_id: "kNSUBeB",
description: "Genres of Reality Media",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
},
{
name: "What are Reality Media",
scene_id: "Go3FTHC",
description: "What are Reality Media and What are AR and VR",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
},
{
name: "Public Space",
scene_id: "6etpG95",
description: "Public Space",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
},
{
name: "Private Space",
scene_id: "U5RCYmp",
description: "Public Space",
room_size: 30,
user_data: {
script_url: "https://resources.realitymedia.digital/core-components/index.min.js"
}
}
]
let createRoom = async function (i) {
console.log("creating room " + i)
if (i < 0 || i >= roomProtos.length) {
console.warn("tried to create room " + i + " when max is " + (roomProtos.length - 1))
}
let body = {
"hub": roomProtos[i]
}
//console.log("creating room on server:")
//console.log(body)
// console.log(BEARER)
try {
let result = await fetch('https://xr.realitymedia.digital/api/v1/hubs', {
method: 'post',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json', "Authorization" : "bearer " + BEARER }
})
.then(res => {
try {
return res.json()
} catch (e) {
console.error("could not decode res as JSON: " + res.text())
console.error(e, JSON.stringify(body));
}
})
//console.log("return from hubs server: " + result)
return {scene: roomProtos[i].scene_id, room: result.hub_id}
} catch (e) {
console.error("failure to create room: " + roomProtos[i].scene_id)
//console.error(e);
return null;
}
}
// // GET /sso/
// app.get('/dumpData', async (req, res) => {
// const env = NODE_ENV || "development";
// if (env === "development") {
// // return all models in dev
// const data = await Promise.all(Object.keys(DB.models).map(model => DB.models[model].findAll())).catch(e => {
// console.log(e);
// });
// return res.json({
// data,
// loggedIn: req.session
// });
// }
// return res.status(200).json({
// message: "sso get login token",
// loggedIn: req.session.loggedIn
// })
// });
app.get('/resetUserRooms', async (req, res) => {
// if (!req.session.loggedIn) {
// return res.sendStatus(401)
// }
let {
email,
token
} = req.query;
if (email) {
email = decodeURIComponent(email)
}
if (token) {
token = decodeURIComponent(token)
}
let tokenCookie = req.cookies.__ael_hubs_token;
var cookieData = {}
if (tokenCookie) {
try {
cookieData = jwt.verify(tokenCookie, SESSION_SECRET);
} catch(err) {
console.error(err, req.body);
}
}
if (!(email && email.length) && cookieData.email && cookieData.email.length) {
email = cookieData.email
}
if (!(token && token.length) && cookieData.token && cookieData.token.length) {
token = cookieData.token
}
if (!(email && email.length) && !(token && token.length)) {
return res.status(400).json({
message: "Invalid input",
email,
// token
})
}
let id = await validateId(email, token)
if (!id) {
return res.status(400).json({
message: "email and credentials don't match or account doesn't exist in hubs",
email,
// token
})
}
console.log("reseting for id ", id)
await startUserWork(id);
try {
// delete all records associated with this ID
// first any Rooms
await DB.models.Room.destroy({
where: {
ownerId: id
}
});
endUserWork(id);
return res.status(200).json({
user: {id:id, email: email}
});
} catch (e) {
console.error(e, req.body);
endUserWork(id);
return res.status(500).json(e);
}
})
app.get('/user', async (req, res) => {
// if (!req.session.loggedIn) {
// return res.sendStatus(401)
// }
let {
email,
token
} = req.query;
if (email) {
email = decodeURIComponent(email)
}
if (token) {
token = decodeURIComponent(token)
}
let tokenCookie = req.cookies.__ael_hubs_token;
var cookieData = {}
if (tokenCookie) {
try {
cookieData = jwt.verify(tokenCookie, SESSION_SECRET);
} catch(err) {
console.error(err, req.body);
}
}
if (!(email && email.length) && cookieData.email && cookieData.email.length) {
email = cookieData.email
}
if (!(token && token.length) && cookieData.token && cookieData.token.length) {
token = cookieData.token
}
if (!(email && email.length) && !(token && token.length)) {
return res.status(400).json({
message: "Invalid input",
email,
// token
})
}
let id = await validateId(email, token)
if (!id) {
return res.status(400).json({
message: "email and credentials don't match or account doesn't exist in hubs",
email,
// token
})
}
if (!tokenCookie || cookieData.email != email || cookieData.token != token) {
createCookie(req, res, email, token)
}
console.log("get user info for id ", id)
startUserWork(id);
try {
const users = await DB.query("User", { id });
if (!users.length) {
//return res.sendStatus(204)
// create the user and return it
let cuRet = await createUser(req, res, id, email, token)
endUserWork(id);
return cuRet;
}
if (users.length > 1) {
console.error("Shouldn't happen: multiple record with same id!")
// for (i = 1; i < users.length; i++) {
// API.models.User.destroy({where: { id: id, email: users[i].email, token: users[i].token}})
// }
}
let user = {id: id}
// need to start sending these back because they might be in the cookie
user.email = email;
//user.token = token;
const rooms = await DB.query("Room", { ownerId: id } );
let roomIds = await createOrUpdateRooms(req, id, rooms)
endUserWork(id);
return res.status(200).json({
user: user,
rooms: roomIds
});
} catch (e) {
console.error(e, req.body);
endUserWork(id);
return res.status(500).json(e);
}
});
let createCookie = function(req, res, email, token) {
res.cookie(
'__ael_hubs_token',
jwt.sign(
{ email: email, token: token },
SESSION_SECRET
),
{
// NOTICE the . behind the domain. This is necessary to ensure
// that the cookies are shared between subdomains
domain: '.' + req.headers.host,
httpOnly: true,
secure: true,
maxAge: 1000 * 60 * 60 * 24 * 30 // one month-ish
}
);
}
app.get('/userRooms', async (req, res) => {
// if (!req.session.loggedIn) {
// return res.sendStatus(401)
// }
console.log("user rooms request")
let {
email,
token,
hubId
} = req.query;
if (email) {
email = decodeURIComponent(email)
}
if (token) {
token = decodeURIComponent(token)
}
let tokenCookie = req.cookies.__ael_hubs_token;
var cookieData = {}
if (tokenCookie) {
try {
cookieData = jwt.verify(tokenCookie, SESSION_SECRET);
} catch(err) {
console.error(err, req.body);
}
}
if (!(email && email.length) && cookieData.email && cookieData.email.length) {
email = cookieData.email
}
if (!(token && token.length) && cookieData.token && cookieData.token.length) {
token = cookieData.token
}
if (!(email && email.length) && !(token && token.length)) {
return res.status(400).json({
message: "Invalid input",
email,
// token
})
}
let id = await validateId(email, token)
if (!id) {
return res.status(400).json({
message: "email and credentials don't match or account doesn't exist in hubs",
email,
// token
})
}
if (!tokenCookie || cookieData.email != email || cookieData.token != token) {
createCookie(req, res, email, token)
}
startUserWork(id);
try {
// first see if this is my room
const room = await DB.query("Room", { id, roomUri: hubId });
endUserWork(id);
if (room.length) {
// my room so just signal that. Send roomId and [] for the room list
return res.status(200).json({
localRooms: [],
roomId: room[0].roomId
});
}
} catch (e) {
console.error(e, req.body);
endUserWork(id);
return res.status(500).json(e);
}
console.log("get user rooms info for hubId ", hubId)
let localRooms = []
let roomId = -1;
if (hubId) {
const room = await DB.query("Room", { roomUri: hubId } );
if (room.length) {
roomId = room[0].roomId
let ownerId = room[0].ownerId
startUserWork(ownerId);
try {
const rooms = await DB.query("Room", { ownerId: ownerId } );
localRooms = await createOrUpdateRooms(req, ownerId, rooms)
endUserWork(ownerId);
} catch (e) {
console.error(e, req.body);
endUserWork(ownerId);
}
}
}
console.log("roomID = " + roomId + ", localRooms = ", localRooms)
return res.status(200).json({
localRooms: localRooms,
roomId: roomId
});
});
app.get('/signout', async (req, res) => {
const d = new Date();
d.setTime(d.getTime() - (24*60*60*1000)); // 1 day in the past
res.cookie(
'__ael_hubs_token',
"gobbledygook",
{
// NOTICE the . behind the domain. This is necessary to ensure
// that the cookies are shared between subdomains
domain: '.' + req.headers.host,
httpOnly: true,
secure: true,
expires: d
}
);
return res.status(200).json({user: null});
});
app.post('/user', async (req, res) => {
if (!req.session.loggedIn) {
return res.sendStatus(401);
}
const {
token,
email
} = req.body;
if (!(email && email.length) && !(token && token.length)) {
return res.status(400).json({
message: "Invalid input",
email,
// token
})
}
let id = accountId(token)
let info = await accountInfo(email)
if (!id || !info || id != info.id) {
return res.status(400).json({
message: "email and credentials don't match or account doesn't exist in hubs",
email,
// token
})
}
createCookie(req, res, email, token)
try {
const exists = await DB.count("User", {
id
});
if (exists) {
console.log("User already exists", email);
return res.status(200).json({
error: "User already exists for token and " + email
});
}
} catch (e) {
console.error(e, req.body);
return res.status(500).json(e);
}
startUserWork(id);
let cuRet = await createUser(req, res, id, email, token)
endUserWork(id);
return cuRet;
});
let createUser = async function(req, res, id, email, token) {
try {
const newUser = await DB.models.User.create({
id,
createdAt: Date.now(),
});
let roomIds = await createOrUpdateRooms(req, id, [])
let user = {id: id}
// need to start sending these back because they might be in the cookie
user.email = email;
//user.token = token;
return res.status(201).json({
user: user,
rooms: roomIds
});
} catch (e) {
console.error(e, req.body);
return res.status(500).json(e);
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let createOrUpdateRooms = async function(req, id, rooms) {
// if we have the right number of rooms, assume it's ok
// if (rooms.length == 2) {
// // should really check if the rooms point at the right URI's ...
// // ... loop through array checking the URi against the scene list above,
// // based on the roomId
// return rooms xLUtdAD', 'yCigoPG', 'ZL9zbdt', 'Lt8zjo6', 'ANoZHrM', 'PyLJTQk', 'Nj9THor
// }
console.log("createOrUpdate " + id + " starting with " + rooms.length + " rooms")
try {
let ret = []
// for (let i = 0; i < fakeRooms.length; i++) {
for (let i = 0; i < roomProtos.length; i++) {
let r = null;
for (let j = 0; j < rooms.length; j++) {
if (rooms[j].roomId == i) {
if (!r) {
r = rooms[j];
} else {
console.log("delete duplicate room " + i + " " + r.id + " " + r.roomId)
try {
await DB.models.Room.destroy({
where: {
id: rooms[j].id
}
});
} catch (e) {
console.error(e, req.body);
}
}
}
}
// if (rooms.length <= i || rooms[i].sceneUri != fakeScenes[i]) {
if (!r || r.sceneUri != roomProtos[i].scene_id) {
// room exists with wrong URI, so delete
if (r) {
try {
await DB.models.Room.destroy({
where: {
ownerId: id,
id: r.id
}
});
} catch (e) {
console.error(e, req.body);
}
}
var room = null;
while (!room) {
room = await createRoom(i)
if (room) {
// create room with right URI
console.log("creating room " + room.room + " with scene " + room.scene + " for user " + id)
try {
r = await DB.models.Room.create({
ownerId: id,
roomId: i,
roomUri: room.room,
sceneUri: room.scene
})
} catch (e) {
console.error(e, req.body);
r = {roomId : i, roomUri: "ERROR"}
}
// console.log("creating room " + fakeRooms[i] + " with scene " + fakeScenes[i] + " for user " + id)
// r = await DB.models.Room.create({
// ownerId: id,
// roomId: i,
// roomUri: fakeRooms[i],
// sceneUri: fakeScenes[i]
// })
} else {
console.log("FAILED to create room with scene " + roomProtos[i].scene_id + " for user " + id)
}
await sleep(1100);
}
}
ret[i] = r
// // create rooms for the user
// const r1 = await DB.models.Room.create({
// ownerId: id,
// roomId: 0,
// roomUri: fakeRooms[0]
// })
// const r2 = await DB.models.Room.create({
// ownerId: id,
// roomId: 1,
// roomUri: fakeRooms[1]
// })
}
const roomIds = []
ret.forEach(r => roomIds[r.roomId] = r.roomUri );
return roomIds //[r1.roomUri, r2.roomUri]
} catch (e) {
console.error(e, req.body);
return [];
}
}
app.get("/bundle.js", (req, res) => {
if (req.session && !req.session.loggedIn) {
req.session.loggedIn = uuidv4()
}
res.header('Content-Type', 'application/javascript');
res.render('bundle.hbs', {
PROTOCOL,
LOCAL_STORAGE_KEY: "__ael_hubs_sso",
TOKEN: req.session.loggedIn,
BASE_URL: req.headers.host
});
})
module.exports = app;