@perfood/couch-auth
Version:
Easy and secure authentication for CouchDB/Cloudant. Based on SuperLogin, updated and rewritten in Typescript.
49 lines (48 loc) • 1.76 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hashCouchPassword = hashCouchPassword;
exports.verifyCouchPassword = verifyCouchPassword;
const couch_pwd_1 = __importDefault(require("@sl-nx/couch-pwd"));
// Hasher for hashing _users passwords
const pwdCouch = new couch_pwd_1.default(600000, 32, 16, 'hex', 'sha256');
// Function for hashing _users passwords
function hashCouchPassword(password) {
return new Promise(function (resolve, reject) {
pwdCouch.hash(password, function (err, salt, hash) {
if (err) {
return reject(err);
}
return resolve({
salt: salt,
derived_key: hash,
password_scheme: 'pbkdf2',
pbkdf2_prf: pwdCouch.digest,
iterations: pwdCouch.iterations
});
});
});
}
function verifyCouchPassword(hashObj, pw) {
return new Promise((resolve, reject) => {
const iterations = hashObj.iterations || 10;
const digest = hashObj.pbkdf2_prf || 'sha1';
const length = digest === 'sha1' ? 20 : 32;
const pwdCouch = new couch_pwd_1.default(iterations, length, 16, 'hex', digest);
const salt = hashObj.salt;
const derived_key = hashObj.derived_key;
pwdCouch.hash(pw, salt, (err, hash) => {
if (err) {
return reject(err);
}
else if (hash !== derived_key) {
return reject(false);
}
else {
return resolve(true);
}
});
});
}