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
|
|
|
|
2018-04-01 12:03:21 -04:00
|
|
|
async function changePassword(currentPassword, newPassword) {
|
2018-04-01 21:27:46 -04:00
|
|
|
if (!await 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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
const newPasswordVerificationKey = utils.toBase64(await myScryptService.getVerificationHash(newPassword));
|
|
|
|
const decryptedDataKey = await passwordEncryptionService.getDataKey(currentPassword);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-04-07 13:03:16 -04:00
|
|
|
await sql.transactional(async () => {
|
2018-04-01 21:27:46 -04:00
|
|
|
await passwordEncryptionService.setDataKey(newPassword, decryptedDataKey);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-04-02 21:47:46 -04:00
|
|
|
await 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
|
|
|
|
};
|