nodebb-plugin-session-sharing 7.2.0 → 7.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/eslint.config.mjs +10 -0
- package/library.js +37 -32
- package/package-lock.json +6428 -0
- package/package.json +7 -7
- package/static/lib/admin.js +0 -2
- package/static/lib/main.js +0 -2
- package/test/index.js +0 -1
- package/.eslintrc +0 -137
package/library.js
CHANGED
|
@@ -14,6 +14,7 @@ const groups = require.main.require('./src/groups');
|
|
|
14
14
|
const SocketPlugins = require.main.require('./src/socket.io/plugins');
|
|
15
15
|
const db = require.main.require('./src/database');
|
|
16
16
|
const plugins = require.main.require('./src/plugins');
|
|
17
|
+
const routeHelpers = require.main.require('./src/routes/helpers');
|
|
17
18
|
|
|
18
19
|
const controllers = require('./lib/controllers');
|
|
19
20
|
const nbbAuthController = require.main.require('./src/controllers/authentication');
|
|
@@ -61,11 +62,9 @@ payloadKeys.forEach(function (key) {
|
|
|
61
62
|
plugin.defaults = Object.freeze({ ...plugin.settings });
|
|
62
63
|
|
|
63
64
|
plugin.init = async (params) => {
|
|
64
|
-
|
|
65
|
-
var hostMiddleware = params.middleware;
|
|
65
|
+
const { router } = params;
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
router.get('/api/admin/plugins/session-sharing', controllers.renderAdminPage);
|
|
67
|
+
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/session-sharing', controllers.renderAdminPage);
|
|
69
68
|
|
|
70
69
|
router.get('/api/session-sharing/lookup', controllers.retrieveUser);
|
|
71
70
|
router.post('/api/session-sharing/user', controllers.process);
|
|
@@ -101,7 +100,9 @@ SocketPlugins.sessionSharing.showUserIds = async (socket, data) => {
|
|
|
101
100
|
throw new Error('no-uids-supplied');
|
|
102
101
|
}
|
|
103
102
|
|
|
104
|
-
return Promise.all(
|
|
103
|
+
return Promise.all(
|
|
104
|
+
uids.map(async uid => db.getSortedSetRangeByScore(plugin.settings.name + ':uid', 0, -1, uid, uid))
|
|
105
|
+
);
|
|
105
106
|
};
|
|
106
107
|
|
|
107
108
|
SocketPlugins.sessionSharing.findUserByRemoteId = async (socket, data) => {
|
|
@@ -115,7 +116,7 @@ SocketPlugins.sessionSharing.findUserByRemoteId = async (socket, data) => {
|
|
|
115
116
|
/* End Websocket Listeners */
|
|
116
117
|
|
|
117
118
|
/*
|
|
118
|
-
*
|
|
119
|
+
* Given a remoteId, show user data
|
|
119
120
|
*/
|
|
120
121
|
plugin.getUser = async (remoteId) => {
|
|
121
122
|
const uid = await db.sortedSetScore(plugin.settings.name + ':uid', remoteId);
|
|
@@ -151,7 +152,7 @@ plugin.normalizePayload = async (payload) => {
|
|
|
151
152
|
|
|
152
153
|
payloadKeys.forEach(function (key) {
|
|
153
154
|
const propName = plugin.settings['payload:' + key];
|
|
154
|
-
if (payload
|
|
155
|
+
if (payload.hasOwnProperty(propName)) {
|
|
155
156
|
userData[key] = payload[propName];
|
|
156
157
|
}
|
|
157
158
|
});
|
|
@@ -160,8 +161,10 @@ plugin.normalizePayload = async (payload) => {
|
|
|
160
161
|
winston.warn('[session-sharing] No user id was given in payload');
|
|
161
162
|
throw new Error('payload-invalid');
|
|
162
163
|
}
|
|
163
|
-
|
|
164
|
-
|
|
164
|
+
const setFullname = userData.hasOwnProperty('fullname') || userData.hasOwnProperty('firstName') || userData.hasOwnProperty('lastName');
|
|
165
|
+
if (setFullname) {
|
|
166
|
+
userData.fullname = (userData.fullname || [userData.firstName, userData.lastName].join(' ')).trim();
|
|
167
|
+
}
|
|
165
168
|
|
|
166
169
|
if (!userData.username) {
|
|
167
170
|
userData.username = userData.fullname;
|
|
@@ -313,12 +316,12 @@ plugin.updateUserGroups = async (uid, userData) => {
|
|
|
313
316
|
// Retrieve user groups
|
|
314
317
|
let [userGroups] = await groups.getUserGroupsFromSet('groups:createtime', [uid]);
|
|
315
318
|
// Normalize user group data to just group names
|
|
316
|
-
userGroups = userGroups.map(
|
|
319
|
+
userGroups = userGroups.map(groupObj => groupObj.name);
|
|
317
320
|
|
|
318
321
|
// Build join and leave arrays
|
|
319
|
-
let join = userData.groups.filter(
|
|
322
|
+
let join = userData.groups.filter(name => !userGroups.includes(name));
|
|
320
323
|
if (plugin.settings.syncGroupList === 'on') {
|
|
321
|
-
join = join.filter(
|
|
324
|
+
join = join.filter(group => plugin.settings.syncGroups.includes(group));
|
|
322
325
|
}
|
|
323
326
|
|
|
324
327
|
let leave = userGroups.filter((name) => {
|
|
@@ -330,7 +333,7 @@ plugin.updateUserGroups = async (uid, userData) => {
|
|
|
330
333
|
return !userData.groups.includes(name);
|
|
331
334
|
});
|
|
332
335
|
if (plugin.settings.syncGroupList === 'on') {
|
|
333
|
-
leave = leave.filter(
|
|
336
|
+
leave = leave.filter(group => plugin.settings.syncGroups.includes(group));
|
|
334
337
|
}
|
|
335
338
|
|
|
336
339
|
await executeJoinLeave(uid, join, leave);
|
|
@@ -343,14 +346,14 @@ async function executeJoinLeave(uid, join, leave) {
|
|
|
343
346
|
return;
|
|
344
347
|
}
|
|
345
348
|
|
|
346
|
-
await Promise.all(join.map(
|
|
349
|
+
await Promise.all(join.map(name => groups.join(name, uid)));
|
|
347
350
|
})(),
|
|
348
351
|
(async () => {
|
|
349
352
|
if (plugin.settings.syncGroupLeave !== 'on') {
|
|
350
353
|
return;
|
|
351
354
|
}
|
|
352
355
|
|
|
353
|
-
await Promise.all(leave.map(
|
|
356
|
+
await Promise.all(leave.map(name => groups.leave(name, uid)));
|
|
354
357
|
})(),
|
|
355
358
|
]);
|
|
356
359
|
}
|
|
@@ -395,13 +398,13 @@ plugin.addMiddleware = async function ({ req, res }) {
|
|
|
395
398
|
const hasLoginLock = req.session.hasOwnProperty('loginLock');
|
|
396
399
|
|
|
397
400
|
if (
|
|
398
|
-
!plugin.ready ||
|
|
399
|
-
(plugin.settings.behaviour === 'trust' && hasSession) ||
|
|
401
|
+
!plugin.ready || // plugin not ready
|
|
402
|
+
(plugin.settings.behaviour === 'trust' && hasSession) || // user logged in + "trust" behaviour
|
|
400
403
|
((plugin.settings.behaviour === 'revalidate' || plugin.settings.behaviour === 'update') && hasLoginLock) ||
|
|
401
|
-
req.originalUrl.startsWith(nconf.get('relative_path') + '/api')
|
|
404
|
+
req.originalUrl.startsWith(nconf.get('relative_path') + '/api') // api routes
|
|
402
405
|
) {
|
|
403
406
|
// Let requests through under "update" or "revalidate" behaviour only if they're logging in for the first time
|
|
404
|
-
delete req.session.loginLock;
|
|
407
|
+
delete req.session.loginLock; // remove login lock for "update" or "revalidate" logins
|
|
405
408
|
|
|
406
409
|
return;
|
|
407
410
|
}
|
|
@@ -429,7 +432,9 @@ plugin.addMiddleware = async function ({ req, res }) {
|
|
|
429
432
|
return handleGuest.call(null, req, res);
|
|
430
433
|
}
|
|
431
434
|
|
|
432
|
-
if (Object.keys(req.cookies).length &&
|
|
435
|
+
if (Object.keys(req.cookies).length &&
|
|
436
|
+
req.cookies.hasOwnProperty(plugin.settings.cookieName) &&
|
|
437
|
+
req.cookies[plugin.settings.cookieName].length) {
|
|
433
438
|
try {
|
|
434
439
|
const uid = await plugin.process(req.cookies[plugin.settings.cookieName]);
|
|
435
440
|
if (uid === req.uid) {
|
|
@@ -448,16 +453,16 @@ plugin.addMiddleware = async function ({ req, res }) {
|
|
|
448
453
|
let handleAsGuest = false;
|
|
449
454
|
|
|
450
455
|
switch (error.message) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
456
|
+
case 'payload-invalid':
|
|
457
|
+
winston.warn('[session-sharing] The passed-in payload was invalid and could not be processed');
|
|
458
|
+
break;
|
|
459
|
+
case 'no-match':
|
|
460
|
+
winston.info('[session-sharing] Payload valid, but local account not found. Assuming guest.');
|
|
461
|
+
handleAsGuest = true;
|
|
462
|
+
break;
|
|
463
|
+
default:
|
|
464
|
+
winston.warn('[session-sharing] Error encountered while parsing token: ' + error.message);
|
|
465
|
+
break;
|
|
461
466
|
}
|
|
462
467
|
|
|
463
468
|
const data = await plugins.hooks.fire('filter:sessionSharing.error', {
|
|
@@ -600,10 +605,10 @@ plugin.appendTemplate = async (data) => {
|
|
|
600
605
|
|
|
601
606
|
plugin.saveReverseToken = async ({ req, userData: data }) => {
|
|
602
607
|
if (!plugin.ready || !data || plugin.settings.reverseToken !== 'on') {
|
|
603
|
-
return;
|
|
608
|
+
return; // no reverse token if secret not set
|
|
604
609
|
}
|
|
605
610
|
|
|
606
|
-
const res = req
|
|
611
|
+
const { res } = req;
|
|
607
612
|
const userData = await user.getUserFields(data.uid, ['uid', 'username', 'picture', 'reputation', 'postcount', 'banned']);
|
|
608
613
|
userData.groups = (await groups.getUserGroups([data.uid])).pop();
|
|
609
614
|
const token = jwt.sign(userData, plugin.settings.secret);
|