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/password.js

54 lines
1.2 KiB
JavaScript

10 years ago
'use strict';
9 years ago
var async = require('async');
10 years ago
var nconf = require('nconf');
var db = require('../database');
var Password = require('../password');
module.exports = function (User) {
User.hashPassword = function (password, callback) {
10 years ago
if (!password) {
return callback(null, password);
}
Password.hash(nconf.get('bcrypt_rounds') || 12, password, callback);
};
User.isPasswordCorrect = function (uid, password, callback) {
9 years ago
password = password || '';
8 years ago
var hashedPassword;
9 years ago
async.waterfall([
function (next) {
db.getObjectField('user:' + uid, 'password', next);
},
8 years ago
function (_hashedPassword, next) {
hashedPassword = _hashedPassword;
9 years ago
if (!hashedPassword) {
9 years ago
return callback(null, true);
9 years ago
}
9 years ago
7 years ago
User.isPasswordValid(password, 0, next);
8 years ago
},
function (next) {
Password.compare(password, hashedPassword, next);
},
], function (err, ok) {
if (err) {
return callback(err);
}
// Delay return for incorrect current password
setTimeout(function () {
callback(null, ok);
}, ok ? 0 : 2500);
});
10 years ago
};
User.hasPassword = function (uid, callback) {
db.getObjectField('user:' + uid, 'password', function (err, hashedPassword) {
callback(err, !!hashedPassword);
});
};
8 years ago
};