2017-10-22 09:10:33 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-15 11:31:44 +08:00
|
|
|
const sql = require('./sql');
|
2018-04-02 09:27:46 +08:00
|
|
|
const optionService = require('./options');
|
|
|
|
const myScryptService = require('./my_scrypt');
|
2017-10-15 11:31:44 +08:00
|
|
|
const utils = require('./utils');
|
2018-04-02 09:27:46 +08:00
|
|
|
const passwordEncryptionService = require('./password_encryption');
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-04-02 00:03:21 +08:00
|
|
|
async function changePassword(currentPassword, newPassword) {
|
2018-04-02 09:27:46 +08:00
|
|
|
if (!await passwordEncryptionService.verifyPassword(currentPassword)) {
|
2017-10-15 11:31:44 +08:00
|
|
|
return {
|
2017-11-10 12:25:23 +08:00
|
|
|
success: false,
|
|
|
|
message: "Given current password doesn't match hash"
|
2017-10-15 11:31:44 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
const newPasswordVerificationKey = utils.toBase64(await myScryptService.getVerificationHash(newPassword));
|
|
|
|
const decryptedDataKey = await passwordEncryptionService.getDataKey(currentPassword);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2018-04-02 09:27:46 +08:00
|
|
|
await passwordEncryptionService.setDataKey(newPassword, decryptedDataKey);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await optionService.setOption('password_verification_hash', newPasswordVerificationKey);
|
2017-10-30 06:50:28 +08:00
|
|
|
});
|
2017-10-15 11:31:44 +08:00
|
|
|
|
|
|
|
return {
|
2017-11-23 09:36:07 +08:00
|
|
|
success: true
|
2017-10-15 11:31:44 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
changePassword
|
|
|
|
};
|