You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/user/auth.js

58 lines
1.3 KiB
JavaScript

11 years ago
'use strict';
11 years ago
var async = require('async'),
db = require('../database'),
10 years ago
meta = require('../meta'),
events = require('../events');
11 years ago
module.exports = function(User) {
User.auth = {};
User.auth.logAttempt = function(uid, callback) {
db.exists('lockout:' + uid, function(err, exists) {
11 years ago
if (err) {
return callback(err);
}
if (exists) {
return callback(new Error('[[error:account-locked]]'));
11 years ago
}
11 years ago
db.increment('loginAttempts:' + uid, function(err, attempts) {
if (err) {
return callback(err);
}
if ((meta.config.loginAttempts || 5) < attempts) {
// Lock out the account
db.set('lockout:' + uid, '', function(err) {
if (err) {
return callback(err);
}
10 years ago
var duration = 1000 * 60 * (meta.config.lockoutDuration || 60);
11 years ago
db.delete('loginAttempts:' + uid);
10 years ago
db.pexpire('lockout:' + uid, duration);
events.logAccountLock(uid, duration);
11 years ago
callback(new Error('account-locked'));
});
} else {
db.pexpire('loginAttempts:' + uid, 1000 * 60 * 60);
callback();
}
});
});
11 years ago
};
User.auth.clearLoginAttempts = function(uid) {
db.delete('loginAttempts:' + uid);
};
11 years ago
User.auth.resetLockout = function(uid, callback) {
async.parallel([
async.apply(db.delete, 'loginAttempts:' + uid),
11 years ago
async.apply(db.delete, 'lockout:' + uid)
], callback);
};
11 years ago
};