nodebb-plugin-session-sharing 8.0.0 → 8.0.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.
Files changed (2) hide show
  1. package/library.js +30 -10
  2. package/package.json +1 -1
package/library.js CHANGED
@@ -93,19 +93,27 @@ plugin.appendConfig = async (config) => {
93
93
  SocketPlugins.sessionSharing = {};
94
94
 
95
95
  SocketPlugins.sessionSharing.showUserIds = async (socket, data) => {
96
- // Retrieve the hash and find matches
97
- const { uids } = data;
98
-
99
- if (!uids.length) {
100
- throw new Error('no-uids-supplied');
96
+ if (!socket.uid || !data || !Array.isArray(data.uids)) {
97
+ throw new Error('[[error:invalid-data]]');
98
+ }
99
+ const isAdmin = await user.isAdministrator(socket.uid);
100
+ if (!isAdmin) {
101
+ throw new Error('[[error:no-privileges]]');
101
102
  }
102
103
 
103
104
  return Promise.all(
104
- uids.map(async uid => db.getSortedSetRangeByScore(plugin.settings.name + ':uid', 0, -1, uid, uid))
105
+ data.uids.map(uid => db.getSortedSetRangeByScore(`${plugin.settings.name}:uid`, 0, -1, uid, uid))
105
106
  );
106
107
  };
107
108
 
108
109
  SocketPlugins.sessionSharing.findUserByRemoteId = async (socket, data) => {
110
+ if (!socket.uid || !data || !Array.isArray(data.uids)) {
111
+ throw new Error('[[error:invalid-data]]');
112
+ }
113
+ const isAdmin = await user.isAdministrator(socket.uid);
114
+ if (!isAdmin) {
115
+ throw new Error('[[error:no-privileges]]');
116
+ }
109
117
  if (!data.remoteId) {
110
118
  throw new Error('no-remote-id-supplied');
111
119
  }
@@ -359,11 +367,19 @@ async function executeJoinLeave(uid, join, leave) {
359
367
  }
360
368
 
361
369
  plugin.createUser = async (userData) => {
362
- winston.verbose('[session-sharing] No user found, creating a new user for this login');
370
+ const count = await db.incrObjectField('locks', `session-sharing-create:${userData.id}`);
371
+ if (count > 1) {
372
+ throw new Error('duplicate-creation');
373
+ }
374
+ try {
375
+ winston.verbose('[session-sharing] No user found, creating a new user for this login');
363
376
 
364
- const uid = await user.create(_.pick(userData, profileFields));
365
- await db.sortedSetAdd(plugin.settings.name + ':uid', uid, userData.id);
366
- return uid;
377
+ const uid = await user.create(_.pick(userData, profileFields));
378
+ await db.sortedSetAdd(plugin.settings.name + ':uid', uid, userData.id);
379
+ return uid;
380
+ } finally {
381
+ await db.deleteObjectFields('locks', [`session-sharing-create:${userData.id}`]);
382
+ }
367
383
  };
368
384
 
369
385
  plugin.addMiddleware = async function ({ req, res }) {
@@ -439,6 +455,7 @@ plugin.addMiddleware = async function ({ req, res }) {
439
455
  const uid = await plugin.process(req.cookies[plugin.settings.cookieName]);
440
456
  if (uid === req.uid) {
441
457
  winston.verbose(`[session-sharing] Re-validated login for uid ${uid}, path ${req.originalUrl}`);
458
+ await nbbAuthController.onSuccessfulLogin(req, uid);
442
459
  return;
443
460
  }
444
461
 
@@ -460,6 +477,9 @@ plugin.addMiddleware = async function ({ req, res }) {
460
477
  winston.info('[session-sharing] Payload valid, but local account not found. Assuming guest.');
461
478
  handleAsGuest = true;
462
479
  break;
480
+ case 'duplicate-creation':
481
+ winston.warn('[session-sharing] Duplicate account creation attempt detected');
482
+ break;
463
483
  default:
464
484
  winston.warn('[session-sharing] Error encountered while parsing token: ' + error.message);
465
485
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-session-sharing",
3
- "version": "8.0.0",
3
+ "version": "8.0.2",
4
4
  "description": "Allows login sessions from your app to persist in NodeBB",
5
5
  "main": "library.js",
6
6
  "repository": {