trilium/src/services/change_password.js

33 lines
1 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08: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');
2018-04-02 00:03:21 +08:00
async function changePassword(currentPassword, newPassword) {
if (!await passwordEncryptionService.verifyPassword(currentPassword)) {
return {
success: false,
message: "Given current password doesn't match hash"
};
}
const newPasswordVerificationKey = utils.toBase64(await myScryptService.getVerificationHash(newPassword));
const decryptedDataKey = await passwordEncryptionService.getDataKey(currentPassword);
await sql.doInTransaction(async () => {
await passwordEncryptionService.setDataKey(newPassword, decryptedDataKey);
await optionService.setOption('password_verification_hash', newPasswordVerificationKey);
});
return {
2017-11-23 09:36:07 +08:00
success: true
};
}
module.exports = {
changePassword
};