2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-14 23:31:44 -04:00
|
|
|
const sql = require('./sql');
|
2018-04-01 21:27:46 -04:00
|
|
|
const optionService = require('./options');
|
|
|
|
const myScryptService = require('./my_scrypt');
|
2017-10-14 23:31:44 -04:00
|
|
|
const utils = require('./utils');
|
2018-04-01 21:27:46 -04:00
|
|
|
const passwordEncryptionService = require('./password_encryption');
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function changePassword(currentPassword, newPassword) {
|
|
|
|
if (!passwordEncryptionService.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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
sql.transactional(() => {
|
2020-09-25 20:55:45 +02:00
|
|
|
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);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
optionService.setOption('passwordVerificationHash', newPasswordVerificationKey);
|
2017-10-29 18:50:28 -04:00
|
|
|
});
|
2017-10-14 23:31:44 -04:00
|
|
|
|
|
|
|
return {
|
2017-11-22 20:36:07 -05:00
|
|
|
success: true
|
2017-10-14 23:31:44 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
changePassword
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|