2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-14 23:31:44 -04:00
|
|
|
const sql = require('./sql');
|
2017-11-02 20:48:02 -04:00
|
|
|
const options = require('./options');
|
2017-10-14 23:31:44 -04:00
|
|
|
const my_scrypt = require('./my_scrypt');
|
|
|
|
const utils = require('./utils');
|
|
|
|
const audit_category = require('./audit_category');
|
2017-11-09 23:25:23 -05:00
|
|
|
const password_encryption = require('./password_encryption');
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-11-06 19:48:02 -05:00
|
|
|
async function changePassword(currentPassword, newPassword, req) {
|
2017-11-09 23:25:23 -05:00
|
|
|
if (!await password_encryption.verifyPassword(currentPassword)) {
|
2017-10-14 23:31:44 -04:00
|
|
|
return {
|
2017-11-09 23:25:23 -05:00
|
|
|
success: false,
|
|
|
|
message: "Given current password doesn't match hash"
|
2017-10-14 23:31:44 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const newPasswordVerificationKey = utils.toBase64(await my_scrypt.getVerificationHash(newPassword));
|
2017-11-09 23:25:23 -05:00
|
|
|
const newPasswordDerivedKey = await my_scrypt.getPasswordDerivedKey(newPassword);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-11-18 12:53:17 -05:00
|
|
|
const decryptedDataKey = await password_encryption.getDataKey(currentPassword);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-10-29 18:50:28 -04:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-11-15 23:39:50 -05:00
|
|
|
await password_encryption.setDataKey(newPasswordDerivedKey, decryptedDataKey);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-11-02 20:48:02 -04:00
|
|
|
await options.setOption('password_verification_hash', newPasswordVerificationKey);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-11-05 10:41:54 -05:00
|
|
|
await sql.addAudit(audit_category.CHANGE_PASSWORD, utils.browserId(req));
|
2017-10-29 18:50:28 -04:00
|
|
|
});
|
2017-10-14 23:31:44 -04:00
|
|
|
|
|
|
|
return {
|
2017-11-09 23:25:23 -05:00
|
|
|
success: true,
|
|
|
|
new_encrypted_data_key: newEncryptedDataKey
|
2017-10-14 23:31:44 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
changePassword
|
|
|
|
};
|