Notes/services/change_password.js

33 lines
992 B
JavaScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
const sql = require('./sql');
2017-11-02 20:48:02 -04:00
const options = require('./options');
const my_scrypt = require('./my_scrypt');
const utils = require('./utils');
const password_encryption = require('./password_encryption');
2017-11-06 19:48:02 -05:00
async function changePassword(currentPassword, newPassword, req) {
if (!await password_encryption.verifyPassword(currentPassword)) {
return {
success: false,
message: "Given current password doesn't match hash"
};
}
const newPasswordVerificationKey = utils.toBase64(await my_scrypt.getVerificationHash(newPassword));
const decryptedDataKey = await password_encryption.getDataKey(currentPassword);
await sql.doInTransaction(async db => {
await password_encryption.setDataKey(db, newPassword, decryptedDataKey);
await options.setOption(db, 'password_verification_hash', newPasswordVerificationKey);
});
return {
2017-11-22 20:36:07 -05:00
success: true
};
}
module.exports = {
changePassword
};