|
|
|
@ -1,15 +1,14 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
|
var winston = require('winston');
|
|
|
|
|
var validator = require('validator');
|
|
|
|
|
const winston = require('winston');
|
|
|
|
|
const validator = require('validator');
|
|
|
|
|
const util = require('util');
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
var db = require('../database');
|
|
|
|
|
var meta = require('../meta');
|
|
|
|
|
var events = require('../events');
|
|
|
|
|
var batch = require('../batch');
|
|
|
|
|
var utils = require('../utils');
|
|
|
|
|
const db = require('../database');
|
|
|
|
|
const meta = require('../meta');
|
|
|
|
|
const events = require('../events');
|
|
|
|
|
const batch = require('../batch');
|
|
|
|
|
const utils = require('../utils');
|
|
|
|
|
|
|
|
|
|
module.exports = function (User) {
|
|
|
|
|
User.auth = {};
|
|
|
|
@ -28,7 +27,7 @@ module.exports = function (User) {
|
|
|
|
|
}
|
|
|
|
|
// Lock out the account
|
|
|
|
|
await db.set('lockout:' + uid, '');
|
|
|
|
|
var duration = 1000 * 60 * meta.config.lockoutDuration;
|
|
|
|
|
const duration = 1000 * 60 * meta.config.lockoutDuration;
|
|
|
|
|
|
|
|
|
|
await db.delete('loginAttempts:' + uid);
|
|
|
|
|
await db.pexpire('lockout:' + uid, duration);
|
|
|
|
@ -44,7 +43,7 @@ module.exports = function (User) {
|
|
|
|
|
if (!(parseInt(uid, 10) > 0)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var _token = await db.getObjectField('user:' + uid, 'rss_token');
|
|
|
|
|
const _token = await db.getObjectField('user:' + uid, 'rss_token');
|
|
|
|
|
const token = _token || utils.generateUUID();
|
|
|
|
|
if (!_token) {
|
|
|
|
|
await User.setUserField(uid, 'rss_token', token);
|
|
|
|
@ -63,9 +62,12 @@ module.exports = function (User) {
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getSessionFromStore = util.promisify((sid, callback) => db.sessionStore.get(sid, (err, sessObj) => callback(err, sessObj || null)));
|
|
|
|
|
const sessionStoreDestroy = util.promisify((sid, callback) => db.sessionStore.destroy(sid, err => callback(err)));
|
|
|
|
|
|
|
|
|
|
User.auth.getSessions = async function (uid, curSessionId) {
|
|
|
|
|
const sids = await db.getSortedSetRevRange('uid:' + uid + ':sessions', 0, 19);
|
|
|
|
|
let sessions = await async.map(sids, db.sessionStore.get.bind(db.sessionStore));
|
|
|
|
|
let sessions = await Promise.all(sids.map(sid => getSessionFromStore(sid)));
|
|
|
|
|
sessions.forEach(function (sessionObj, idx) {
|
|
|
|
|
if (sessionObj && sessionObj.meta) {
|
|
|
|
|
sessionObj.meta.current = curSessionId === sids[idx];
|
|
|
|
@ -73,11 +75,9 @@ module.exports = function (User) {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Revoke any sessions that have expired, return filtered list
|
|
|
|
|
var expiredSids = [];
|
|
|
|
|
var expired;
|
|
|
|
|
|
|
|
|
|
const expiredSids = [];
|
|
|
|
|
sessions = sessions.filter(function (sessionObj, idx) {
|
|
|
|
|
expired = !sessionObj || !sessionObj.hasOwnProperty('passport') ||
|
|
|
|
|
const expired = !sessionObj || !sessionObj.hasOwnProperty('passport') ||
|
|
|
|
|
!sessionObj.passport.hasOwnProperty('user') ||
|
|
|
|
|
parseInt(sessionObj.passport.user, 10) !== parseInt(uid, 10);
|
|
|
|
|
|
|
|
|
@ -106,21 +106,15 @@ module.exports = function (User) {
|
|
|
|
|
await db.sortedSetAdd('uid:' + uid + ':sessions', Date.now(), sessionId);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getSessionFromStore = util.promisify(function (sessionId, callback) {
|
|
|
|
|
db.sessionStore.get(sessionId, function (err, sessionObj) {
|
|
|
|
|
callback(err, sessionObj || null);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
User.auth.revokeSession = async function (sessionId, uid) {
|
|
|
|
|
winston.verbose('[user.auth] Revoking session ' + sessionId + ' for user ' + uid);
|
|
|
|
|
const sessionObj = await getSessionFromStore(sessionId);
|
|
|
|
|
if (sessionObj && sessionObj.meta && sessionObj.meta.uuid) {
|
|
|
|
|
await db.deleteObjectField('uid:' + uid + ':sessionUUID:sessionId', sessionObj.meta.uuid);
|
|
|
|
|
}
|
|
|
|
|
await async.parallel([
|
|
|
|
|
async.apply(db.sortedSetRemove, 'uid:' + uid + ':sessions', sessionId),
|
|
|
|
|
async.apply(db.sessionStore.destroy.bind(db.sessionStore), sessionId),
|
|
|
|
|
await Promise.all([
|
|
|
|
|
db.sortedSetRemove('uid:' + uid + ':sessions', sessionId),
|
|
|
|
|
sessionStoreDestroy(sessionId),
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -139,14 +133,13 @@ module.exports = function (User) {
|
|
|
|
|
const sessionKeys = uids.map(uid => 'uid:' + uid + ':sessions');
|
|
|
|
|
const sessionUUIDKeys = uids.map(uid => 'uid:' + uid + ':sessionUUID:sessionId');
|
|
|
|
|
const sids = _.flatten(await db.getSortedSetRange(sessionKeys, 0, -1));
|
|
|
|
|
await async.parallel([
|
|
|
|
|
async.apply(db.deleteAll, sessionKeys.concat(sessionUUIDKeys)),
|
|
|
|
|
function (next) {
|
|
|
|
|
async.each(sids, function (sid, next) {
|
|
|
|
|
db.sessionStore.destroy(sid, next);
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const promises = [
|
|
|
|
|
db.deleteAll(sessionKeys.concat(sessionUUIDKeys)),
|
|
|
|
|
...sids.map(sid => sessionStoreDestroy(sid)),
|
|
|
|
|
];
|
|
|
|
|
console.log('epic', promises);
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
}, { batch: 1000 });
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|