forked from julianlam/nodebb-plugin-session-sharing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.js
More file actions
303 lines (263 loc) · 8.86 KB
/
Copy pathlibrary.js
File metadata and controls
303 lines (263 loc) · 8.86 KB
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
"use strict";
var meta = module.parent.require('./meta'),
user = module.parent.require('./user');
var _ = module.parent.require('underscore'),
winston = module.parent.require('winston'),
async = module.parent.require('async'),
db = module.parent.require('./database'),
nconf = module.parent.require('nconf'),
superagent = require('superagent');
var jwt = require('jsonwebtoken');
var controllers = require('./lib/controllers'),
plugin = {
ready: false,
settings: {
name: 'appId',
cookieName: 'token',
cookieDomain: undefined,
secret: '',
behaviour: 'trust',
'payload:id': 'id',
'payload:email': 'email',
'payload:username': undefined,
'payload:firstName': undefined,
'payload:lastName': undefined,
'payload:picture': 'picture',
'payload:parent': undefined,
exchangeTokenEndpoint: '',
logoutEndpoint: ''
}
};
plugin.init = function(params, callback) {
var router = params.router,
hostMiddleware = params.middleware,
hostControllers = params.controllers;
router.get('/admin/plugins/session-sharing', hostMiddleware.admin.buildHeader, controllers.renderAdminPage);
router.get('/api/admin/plugins/session-sharing', controllers.renderAdminPage);
if (process.env.NODE_ENV === 'development') {
router.get('/debug/session', plugin.generate);
}
plugin.reloadSettings(callback);
};
plugin.process = function(token, callback) {
async.waterfall([
async.apply(plugin.getToken, token),
async.apply(plugin.verify),
async.apply(plugin.findUser)
], callback);
};
plugin.getToken = function (payload, callback) {
superagent
.post(plugin.settings.exchangeTokenEndpoint)
.set('Accept', 'application/json')
.send({
refreshToken: payload
})
.end(function(err, res) {
if(err) {
callback(err);
return;
}
jwt.verify(res.body.token, plugin.settings.secret, function (err, res) {
if(err) {
return callback(err);
}
callback(null, res);
})
});
}
plugin.verify = function(payload, callback) {
var parent = plugin.settings['payload:parent'],
email = parent ? payload[parent][plugin.settings['payload:email']] : payload[plugin.settings['payload:email']];
if (!email ) {
return callback(new Error('payload-invalid'));
}
callback(null, payload);
}
plugin.findUser = function(payload, callback) {
// If payload id resolves to a user, return the uid, otherwise register a new user
winston.verbose('[session-sharing] Payload verified');
var parent = plugin.settings['payload:parent'],
id = parent ? payload[parent][plugin.settings['payload:id']] : payload[plugin.settings['payload:id']],
email = parent ? payload[parent][plugin.settings['payload:email']] : payload[plugin.settings['payload:email']],
username = parent ? payload[parent][plugin.settings['payload:username']] : payload[plugin.settings['payload:username']],
firstName = parent ? payload[parent][plugin.settings['payload:firstName']] : payload[plugin.settings['payload:firstName']],
lastName = parent ? payload[parent][plugin.settings['payload:lastName']] : payload[plugin.settings['payload:lastName']],
picture = parent ? payload[parent][plugin.settings['payload:picture']] : payload[plugin.settings['payload:picture']];
picture = picture.small;
if (!username && firstName && lastName) {
username = [firstName, lastName].join(' ').trim();
} else if (!username && firstName && !lastName) {
username = firstName;
} else if (!username && !firstName && lastName) {
username = lastName;
}
async.parallel({
uid: async.apply(db.getObjectField, plugin.settings.name + ':uid', id),
mergeUid: async.apply(db.sortedSetScore, 'email:uid', email)
}, function(err, checks) {
var setUserName = function (uid, cb) {
user.getUserData(uid, function (err, data) {
if(err) {
return cb(err);
}
if(!data) {
return cb('payload-invalid');
}
if(picture) {
user.setUserField(uid, 'uploadedpicture', picture);
user.setUserField(uid, 'picture', picture);
}
if(data.username !== username) {
user.updateProfile(uid, {'username': username}, function (err, res) {
if(err) {
return cb(err);
}
cb(null, res);
});
} else {
cb();
}
});
}
if (err) { return callback(err); }
if (checks.uid && !isNaN(parseInt(checks.uid, 10))) {
setUserName(checks.mergeUid, function () {
callback(null, {uid: checks.uid, user: {id: id}});
})
return;
}
else if (email && email.length && checks.mergeUid && !isNaN(parseInt(checks.mergeUid, 10))) {
winston.info('[session-sharing] Found user via their email, associating this id (' + id + ') with their NodeBB account');
db.setObjectField(plugin.settings.name + ':uid', id, checks.mergeUid);
setUserName(checks.mergeUid, function () {
callback(null, {uid: checks.mergeUid, user: {id: id}});
})
return;
}
// If no match, create a new user
winston.info('[session-sharing] No user found, creating a new user for this login');
username = username.trim();
user.create({
username: username,
email: email,
picture: picture,
fullname: [firstName, lastName].join(' ').trim()
}, function(err, uid) {
if (err) { return callback(err); }
db.setObjectField(plugin.settings.name + ':uid', id, uid);
callback(null, {uid: uid, user: {id: id}});
});
});
};
plugin.addMiddleware = function(data, callback) {
function handleGuest (req, res, next) {
if (plugin.settings.guestRedirect) {
// If a guest redirect is specified, follow it
res.redirect(plugin.settings.guestRedirect.replace('%1', encodeURIComponent(nconf.get('url') + req.path)));
} else {
return next();
}
};
data.app.use(function(req, res, next) {
// Only respond to page loads by guests, not api or asset calls
var blacklistedRoute = new RegExp('^' + nconf.get('relative_path') + '/(api|vendor|uploads|language|templates|debug)'),
blacklistedExt = /\.(css|js|tpl|json)$/,
hasSession = req.hasOwnProperty('user') && req.user.hasOwnProperty('uid') && parseInt(req.user.uid, 10) > 0;
if (
!plugin.ready // plugin not ready
|| (plugin.settings.behaviour === 'trust' && hasSession) // user logged in
|| (req.path.match(blacklistedRoute) || req.path.match(blacklistedExt)) // path matches a blacklist
) {
return next();
} else {
if(req.path === '/logout') {
req.session.logout = true;
return next();
}
if(req.session.logout) {
req.session.logout = false;
if(plugin.settings.logoutEndpoint) {
return res.redirect(plugin.settings.logoutEndpoint);
}
return next();
}
if (Object.keys(req.cookies).length && req.cookies.hasOwnProperty(plugin.settings.cookieName) && req.cookies[plugin.settings.cookieName].length) {
return plugin.process(req.cookies[plugin.settings.cookieName], function(err, payload) {
if (err) {
switch(err.message) {
case 'payload-invalid':
winston.warn('[session-sharing] The passed-in payload was invalid and could not be processed');
break;
default:
winston.warn('[session-sharing] Error encountered while parsing token: ' + err.message);
break;
}
return handleGuest(req, res, next);
}
winston.info('[session-sharing] Processing login for uid ' + payload.uid);
req.login({
uid: payload.uid
}, function() {
req.uid = payload.uid;
next();
});
});
} else if (hasSession) {
// Has login session but no cookie, logout
req.logout();
handleGuest.apply(null, arguments);
} else {
handleGuest.apply(null, arguments);
}
}
});
callback();
};
plugin.cleanup = function(req, res) {
if (plugin.settings.cookieDomain) {
winston.verbose('[session-sharing] Clearing cookie');
res.cookie(plugin.settings.cookieName, {
domain: plugin.settings.cookieDomain,
expires: new Date(),
path: '/'
});
}
};
plugin.generate = function(req, res) {
var payload = {};
payload[plugin.settings['payload:id']] = 1;
payload[plugin.settings['payload:username']] = 'testUser';
payload[plugin.settings['payload:email']] = 'testUser@example.org';
var token = jwt.sign(payload, plugin.settings.secret)
res.cookie('token', token, {
maxAge: 1000*60*60*24*21,
httpOnly: true,
domain: plugin.settings.cookieDomain
});
res.sendStatus(200);
};
plugin.addAdminNavigation = function(header, callback) {
header.plugins.push({
route: '/plugins/session-sharing',
icon: 'fa-user-secret',
name: 'Session Sharing'
});
callback(null, header);
};
plugin.reloadSettings = function(callback) {
meta.settings.get('session-sharing', function(err, settings) {
if (err) {
return callback(err);
}
if (!settings.hasOwnProperty('secret') || !settings.secret.length) {
winston.error('[session-sharing] JWT Secret not found, session sharing disabled.');
return callback();
}
winston.info('[session-sharing] Settings OK');
plugin.settings = _.defaults(_.pick(settings, Boolean), plugin.settings);
plugin.ready = true;
callback();
});
};
module.exports = plugin;