Notes/src/services/change_password.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
const sql = require('./sql');
const optionService = require('./options');
const myScryptService = require('./my_scrypt');
const utils = require('./utils');
const passwordEncryptionService = require('./password_encryption');
2020-06-20 12:31:38 +02:00
function changePassword(currentPassword, newPassword) {
if (!passwordEncryptionService.verifyPassword(currentPassword)) {
return {
success: false,
message: "Given current password doesn't match hash"
};
}
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
const decryptedDataKey = passwordEncryptionService.getDataKey(currentPassword);
optionService.setOption('passwordVerificationSalt', utils.randomSecureToken(32));
optionService.setOption('passwordDerivedKeySalt', utils.randomSecureToken(32));
const newPasswordVerificationKey = utils.toBase64(myScryptService.getVerificationHash(newPassword));
2020-06-20 12:31:38 +02:00
passwordEncryptionService.setDataKey(newPassword, decryptedDataKey);
2020-06-20 12:31:38 +02:00
optionService.setOption('passwordVerificationHash', newPasswordVerificationKey);
});
return {
2017-11-22 20:36:07 -05:00
success: true
};
}
module.exports = {
changePassword
2020-06-20 12:31:38 +02:00
};