diff --git a/src/password.js b/src/password.js index c2e7e88bfb..fdca33f53f 100644 --- a/src/password.js +++ b/src/password.js @@ -1,42 +1,13 @@ 'use strict'; -var path = require('path'); -var bcrypt = require('bcryptjs'); -var async = require('async'); +const path = require('path'); +const bcrypt = require('bcryptjs'); +const util = require('util'); -var fork = require('./meta/debugFork'); - -exports.hash = function (rounds, password, callback) { - forkChild({ type: 'hash', rounds: rounds, password: password }, callback); -}; - -exports.compare = function (password, hash, callback) { - async.waterfall([ - getFakeHash, - function (fakeHash, next) { - forkChild({ type: 'compare', password: password, hash: hash || fakeHash }, next); - }, - ], callback); -}; - -var fakeHashCache; -function getFakeHash(callback) { - if (fakeHashCache) { - return callback(null, fakeHashCache); - } - async.waterfall([ - function (next) { - exports.hash(12, Math.random().toString(), next); - }, - function (hash, next) { - fakeHashCache = hash; - next(null, fakeHashCache); - }, - ], callback); -} +const fork = require('./meta/debugFork'); function forkChild(message, callback) { - var child = fork(path.join(__dirname, 'password')); + const child = fork(path.join(__dirname, 'password')); child.on('message', function (msg) { callback(msg.err ? new Error(msg.err) : null, msg.result); @@ -45,30 +16,54 @@ function forkChild(message, callback) { child.send(message); } +const forkChildAsync = util.promisify(forkChild); + +exports.hash = async function (rounds, password) { + return await forkChildAsync({ type: 'hash', rounds: rounds, password: password }); +}; + +exports.compare = async function (password, hash) { + const fakeHash = await getFakeHash(); + return await forkChildAsync({ type: 'compare', password: password, hash: hash || fakeHash }); +}; + +let fakeHashCache; +async function getFakeHash() { + if (fakeHashCache) { + return fakeHashCache; + } + fakeHashCache = await exports.hash(12, Math.random().toString()); + return fakeHashCache; +} + // child process process.on('message', function (msg) { if (msg.type === 'hash') { - hashPassword(msg.password, msg.rounds); + tryMethod(hashPassword, msg); } else if (msg.type === 'compare') { - bcrypt.compare(String(msg.password || ''), String(msg.hash || ''), done); + tryMethod(compare, msg); } }); -function hashPassword(password, rounds) { - async.waterfall([ - function (next) { - bcrypt.genSalt(parseInt(rounds, 10), next); - }, - function (salt, next) { - bcrypt.hash(password, salt, next); - }, - ], done); +async function tryMethod(method, msg) { + try { + const result = await method(msg); + process.send({ result: result }); + } catch (err) { + process.send({ err: err.message }); + } finally { + process.disconnect(); + } } -function done(err, result) { - process.send(err ? { err: err.message } : { result: result }); - process.disconnect(); +async function hashPassword(msg) { + const salt = await bcrypt.genSalt(parseInt(msg.rounds, 10)); + const hash = await bcrypt.hash(msg.password, salt); + return hash; } +async function compare(msg) { + return await bcrypt.compare(String(msg.password || ''), String(msg.hash || '')); +} require('./promisify')(exports);