Much shorter guest sharing URLs.
Ylian Saint-Hilaire committed
Sep 11, 2021 at 22:58 UTC
dc1c89ee932a860250da08aec67a00928b5a1c84
2 files changed
+32
-4
meshuser.js
+4
-3
@@ -4694,9 +4694,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4694
expireTime = command.end * 1000;
4695
}
4696
4697
- var cookie = { a: 5, p: command.p, uid: user._id, gn: command.guestname, nid: node._id, cf: command.consent, pid: publicid };
4698
- if ((startTime != null) && (expireTime != null)) { command.start = cookie.start = startTime; command.expire = cookie.expire = expireTime; }
4699
- if (command.viewOnly === true) { cookie.vo = 1; }
4697
+ //var cookie = { a: 5, p: command.p, uid: user._id, gn: command.guestname, nid: node._id, cf: command.consent, pid: publicid };
4698
+ var cookie = { a: 6, pid: publicid };
4699
+ //if ((startTime != null) && (expireTime != null)) { command.start = cookie.start = startTime; command.expire = cookie.expire = expireTime; }
4700
+ //if (command.viewOnly === true) { cookie.vo = 1; }
4701
const inviteCookie = parent.parent.encodeCookie(cookie, parent.parent.invitationLinkEncryptionKey);
4702
if (inviteCookie == null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'createDeviceShareLink', responseid: command.responseid, result: 'Unable to generate shareing cookie' })); } catch (ex) { } } return; }
4703
webserver.js
+28
-1
@@ -3477,8 +3477,35 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3477
3478
// Check the inbound guest sharing cookie
3479
var c = obj.parent.decodeCookie(req.query.c, obj.parent.invitationLinkEncryptionKey, 9999999999); // Decode cookies with unlimited time.
3480
- if ((c == null) || (c.a !== 5) || (typeof c.p !== 'number') || (c.p < 1) || (c.p > 7) || (typeof c.uid != 'string') || (typeof c.nid != 'string') || (typeof c.gn != 'string') || (typeof c.cf != 'number') || (typeof c.pid != 'string')) { res.sendStatus(404); return; }
3480
+ if (c == null) { res.sendStatus(404); return; }
3481
3482
+ if (c.a === 5) {
3483
+ // This is the older style sharing cookie with everything encoded within it.
3484
+ // This cookie style gives a very large URL, so it's not used anymore.
3485
+ if ((typeof c.p !== 'number') || (c.p < 1) || (c.p > 7) || (typeof c.uid != 'string') || (typeof c.nid != 'string') || (typeof c.gn != 'string') || (typeof c.cf != 'number') || (typeof c.pid != 'string')) { res.sendStatus(404); return; }
3486
+ handleSharingRequestEx(req, res, domain, c);
3487
+ return;
3488
+ }
3489
+ if (c.a === 6) {
3490
+ // This is the new style sharing cookie, just encodes the pointer to the sharing information in the database.
3491
+ // Gives a much more compact URL.
3492
+ if (typeof c.pid != 'string') { res.sendStatus(404); return; }
3493
+ obj.db.Get('deviceshare-' + c.pid, function (err, docs) {
3494
+ if ((err != null) || (docs == null) || (docs.length != 1)) { res.sendStatus(404); return; }
3495
+ const doc = docs[0];
3496
+ // Generate an old style cookie from the information in the database
3497
+ var cookie = { a: 5, p: doc.p, uid: doc.userid, gn: doc.guestName, nid: doc.nodeid, cf: doc.consent, pid: doc.publicid };
3498
+ if ((doc.startTime != null) && (doc.expireTime != null)) { cookie.start = doc.startTime; cookie.expire = doc.expireTime; }
3499
+ if (doc.viewOnly === true) { cookie.vo = 1; }
3500
+ handleSharingRequestEx(req, res, domain, cookie);
3501
+ });
3502
+ return;
3503
+ }
3504
+ res.sendStatus(404); return;
3505
+ }
3506
+
3507
+ // Serve the guest sharing page
3508
+ function handleSharingRequestEx(req, res, domain, c) {
3509
// Check the expired time, expire message.
3510
if ((c.expire != null) && (c.expire <= Date.now())) { render(req, res, getRenderPage((domain.sitestyle == 2) ? 'message2' : 'message', req, domain), getRenderArgs({ titleid: 2, msgid: 12, domainurl: encodeURIComponent(domain.url).replace(/'/g, '%27') }, req, domain)); return; }
3511