mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 02:55:58 +01:00 
			
		
		
		
	refactor: password async/await
This commit is contained in:
		| @@ -1,42 +1,13 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
| var path = require('path'); | const path = require('path'); | ||||||
| var bcrypt = require('bcryptjs'); | const bcrypt = require('bcryptjs'); | ||||||
| var async = require('async'); | const util = require('util'); | ||||||
|  |  | ||||||
| var fork = require('./meta/debugFork'); | const 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); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| function forkChild(message, callback) { | function forkChild(message, callback) { | ||||||
| 	var child = fork(path.join(__dirname, 'password')); | 	const child = fork(path.join(__dirname, 'password')); | ||||||
|  |  | ||||||
| 	child.on('message', function (msg) { | 	child.on('message', function (msg) { | ||||||
| 		callback(msg.err ? new Error(msg.err) : null, msg.result); | 		callback(msg.err ? new Error(msg.err) : null, msg.result); | ||||||
| @@ -45,30 +16,54 @@ function forkChild(message, callback) { | |||||||
| 	child.send(message); | 	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 | // child process | ||||||
| process.on('message', function (msg) { | process.on('message', function (msg) { | ||||||
| 	if (msg.type === 'hash') { | 	if (msg.type === 'hash') { | ||||||
| 		hashPassword(msg.password, msg.rounds); | 		tryMethod(hashPassword, msg); | ||||||
| 	} else if (msg.type === 'compare') { | 	} else if (msg.type === 'compare') { | ||||||
| 		bcrypt.compare(String(msg.password || ''), String(msg.hash || ''), done); | 		tryMethod(compare, msg); | ||||||
| 	} | 	} | ||||||
| }); | }); | ||||||
|  |  | ||||||
| function hashPassword(password, rounds) { | async function tryMethod(method, msg) { | ||||||
| 	async.waterfall([ | 	try { | ||||||
| 		function (next) { | 		const result = await method(msg); | ||||||
| 			bcrypt.genSalt(parseInt(rounds, 10), next); | 		process.send({ result: result }); | ||||||
| 		}, | 	} catch (err) { | ||||||
| 		function (salt, next) { | 		process.send({ err: err.message }); | ||||||
| 			bcrypt.hash(password, salt, next); | 	} finally { | ||||||
| 		}, | 		process.disconnect(); | ||||||
| 	], done); | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| function done(err, result) { | async function hashPassword(msg) { | ||||||
| 	process.send(err ? { err: err.message } : { result: result }); | 	const salt = await bcrypt.genSalt(parseInt(msg.rounds, 10)); | ||||||
| 	process.disconnect(); | 	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); | require('./promisify')(exports); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user