Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: vault password can now be changed

thecodrr47b7eee4aab4
diff --git a/packages/core/__tests__/vault.test.js b/packages/core/__tests__/vault.test.js
index c4185143b..5f2d93384 100644
--- a/packages/core/__tests__/vault.test.js
+++ b/packages/core/__tests__/vault.test.js
@@ -111,3 +111,16 @@ test("save an edited locked note", () =>
expect(contentData.cipher).not.toBeDefined();
expect(contentData.key).not.toBeDefined();
}));
+
+test("change vault password", () =>
+ noteTest().then(async ({ db, id }) => {
+ await db.vault.create("password");
+ await db.vault.add(id);
+
+ await expect(db.vault.open(id, "password")).resolves.toBeDefined();
+
+ await db.vault.changePassword("password", "newPassword");
+
+ await expect(db.vault.open(id, "password")).rejects.toThrow();
+ await expect(db.vault.open(id, "newPassword")).resolves.toBeDefined();
+ }));
diff --git a/packages/core/api/vault.js b/packages/core/api/vault.js
index d24e2f15d..780665d2c 100644
--- a/packages/core/api/vault.js
+++ b/packages/core/api/vault.js
@@ -25,7 +25,7 @@ export default class Vault {
/**
* Creates a new vault (replacing if any older exists)
* @param {string} password The password
- * @returns {Boolean}
+ * @returns {Promise<Boolean>}
*/
async create(password) {
const vaultKey = await this._context.read("vaultKey");
@@ -44,7 +44,7 @@ export default class Vault {
/**
* Unlocks the vault with the given password
* @param {string} password The password
- * @throws ERR_NO_VAULT | ERR_WRONG_PASSWORD
+ * @throws ERR_NO_VAULT | ERR_WRONG_PASSWORD
* @returns {Promise<Boolean>}
*/
async unlock(password) {
@@ -64,6 +64,20 @@ export default class Vault {
return true;
}
+ async changePassword(oldPassword, newPassword) {
+ if (await this.unlock(oldPassword)) {
+ const lockedNotes = this._db.notes.all.filter((v) => v.locked);
+ for (var note of lockedNotes) {
+ await this._unlockNote(note, true);
+ }
+ await this._context.remove("vaultKey");
+ await this.create(newPassword);
+ for (var note of lockedNotes) {
+ await this._lockNote(note);
+ }
+ }
+ }
+
_startEraser() {
setTimeout(() => {
this._password = "";
@@ -93,6 +107,8 @@ export default class Vault {
}
}
+ async clear(password) {}
+
/**
* Temporarily unlock (open) a note
* @param {string} noteId The note id