mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 15:11:31 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			41 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
"use strict";
 | 
						|
 | 
						|
const sql = require('./sql');
 | 
						|
const options = require('./options');
 | 
						|
const my_scrypt = require('./my_scrypt');
 | 
						|
const utils = require('./utils');
 | 
						|
const audit_category = require('./audit_category');
 | 
						|
const password_encryption = require('./password_encryption');
 | 
						|
 | 
						|
async function changePassword(currentPassword, newPassword, req) {
 | 
						|
    if (!await password_encryption.verifyPassword(currentPassword)) {
 | 
						|
        return {
 | 
						|
            success: false,
 | 
						|
            message: "Given current password doesn't match hash"
 | 
						|
        };
 | 
						|
    }
 | 
						|
 | 
						|
    const newPasswordVerificationKey = utils.toBase64(await my_scrypt.getVerificationHash(newPassword));
 | 
						|
    const newPasswordDerivedKey = await my_scrypt.getPasswordDerivedKey(newPassword);
 | 
						|
 | 
						|
    const decryptedDataKey = await password_encryption.getDecryptedDataKey(currentPassword);
 | 
						|
 | 
						|
    const newEncryptedDataKey = password_encryption.encryptDataKey(newPasswordDerivedKey, decryptedDataKey);
 | 
						|
 | 
						|
    await sql.doInTransaction(async () => {
 | 
						|
        await options.setOption('encrypted_data_key', newEncryptedDataKey);
 | 
						|
 | 
						|
        await options.setOption('password_verification_hash', newPasswordVerificationKey);
 | 
						|
 | 
						|
        await sql.addAudit(audit_category.CHANGE_PASSWORD, utils.browserId(req));
 | 
						|
    });
 | 
						|
 | 
						|
    return {
 | 
						|
        success: true,
 | 
						|
        new_encrypted_data_key: newEncryptedDataKey
 | 
						|
    };
 | 
						|
}
 | 
						|
 | 
						|
module.exports = {
 | 
						|
    changePassword
 | 
						|
}; |