starting asyncifying

This commit is contained in:
Andris Reinman 2019-07-11 10:52:43 +03:00
parent 231f52d72a
commit 7ab83dd6a0
3 changed files with 671 additions and 710 deletions

View file

@ -24,24 +24,26 @@ module.exports = redis => {
});
return {
ttlcounter(key, count, max, windowSize, callback) {
async asyncTTLCounter(key, count, max, windowSize) {
if (!max || isNaN(max)) {
return callback(null, {
return {
success: true,
value: 0,
ttl: 0
});
};
}
redis.ttlcounter(key, count, max, windowSize || 86400, (err, res) => {
if (err) {
return callback(err);
}
return callback(null, {
success: !!((res && res[0]) || 0),
value: (res && res[1]) || 0,
ttl: (res && res[2]) || 0
});
});
let res = await redis.ttlcounter(key, count, max, windowSize || 86400);
return {
success: !!((res && res[0]) || 0),
value: (res && res[1]) || 0,
ttl: (res && res[2]) || 0
};
},
ttlcounter(key, count, max, windowSize, callback) {
return this.asyncTTLCounter(key, count, max, windowSize)
.then(res => callback(null, res))
.catch(callback);
},
cachedcounter(key, count, ttl, callback) {

View file

@ -7,28 +7,31 @@ const cryptMD5 = require('./md5/cryptmd5').cryptMD5;
const consts = require('./consts');
// just pass hashing through to bcrypt
module.exports.hash = (password, callback) => {
module.exports.asyncHash = async password => {
password = (password || '').toString();
switch (consts.DEFAULT_HASH_ALGO) {
case 'pbkdf2':
return pbkdf2
.hash(password, {
iterations: consts.PDKDF2_ITERATIONS,
saltSize: consts.PDKDF2_SALT_SIZE,
digest: consts.PDKDF2_DIGEST
})
.then(hash => callback(null, hash))
.catch(callback);
return await pbkdf2.hash(password, {
iterations: consts.PDKDF2_ITERATIONS,
saltSize: consts.PDKDF2_SALT_SIZE,
digest: consts.PDKDF2_DIGEST
});
case 'bcrypt':
default:
return bcrypt.hash(password, consts.BCRYPT_ROUNDS, callback);
return await bcrypt.hash(password, consts.BCRYPT_ROUNDS);
}
};
module.exports.hash = (password, callback) => {
module.exports
.asyncHash(password)
.then(hash => callback(null, hash))
.catch(callback);
};
// compare against known hashing algos
module.exports.compare = (password, hash, callback) => {
module.exports.asyncCompare = async (password, hash) => {
password = (password || '').toString();
hash = (hash || '').toString();
@ -39,31 +42,33 @@ module.exports.compare = (password, hash, callback) => {
case 'pbkdf2-sha512':
case 'pbkdf2-sha256':
case 'pbkdf2-sha1':
return pbkdf2
.verify(hash, password)
.then(result => callback(null, result))
.catch(callback);
return await pbkdf2.verify(hash, password);
case '2a':
case '2b':
case '2y':
return bcrypt.compare(password, hash, callback);
return await bcrypt.compare(password, hash);
case '1': {
let result;
try {
let salt = hash.split('$')[2] || '';
result = cryptMD5(password, salt) === hash;
} catch (err) {
return callback(err);
}
return callback(null, result);
let salt = hash.split('$')[2] || '';
result = cryptMD5(password, salt) === hash;
return result;
}
default:
return callback(new Error('Invalid algo: ' + JSON.stringify(algo)));
throw new Error('Invalid algo: ' + JSON.stringify(algo));
}
};
module.exports.compare = (password, hash, callback) => {
module.exports
.asyncCompare(password, hash)
.then(result => callback(null, result))
.catch(callback);
};
module.exports.shouldRehash = hash => {
hash = (hash || '').toString();
let algo = [].concat(hash.match(/^\$([^$]+)\$/) || [])[1];

File diff suppressed because it is too large Load diff