Skip to content

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

fix: re-enable account password changing

thecodrrf838826e4295
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index c5ca459ca..7c3c026c6 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -84,7 +84,7 @@ class Database {
this.session = new Session(this.storage);
await this._validate();
- this.user = new UserManager(this.storage);
+ this.user = new UserManager(this.storage, this);
this.syncer = new Sync(this);
this.vault = new Vault(this);
this.conflicts = new Conflicts(this);
diff --git a/packages/core/api/outbox.js b/packages/core/api/outbox.js
index 65e6e3fd8..dc3d20087 100644
--- a/packages/core/api/outbox.js
+++ b/packages/core/api/outbox.js
@@ -16,14 +16,8 @@ class Outbox {
switch (id) {
case "reset_password":
case "change_password":
- const key = await this._db.user.getEncryptionKey();
- const { email } = await this._db.user.getUser();
- await this._db.storage.deriveCryptoKey(`_uk_@${email}`, {
- password: data.newPassword,
- salt: key.salt,
- });
- await this._db.sync(false, true);
- await this.delete(id);
+ if (await this._db.user._updatePassword(id, data))
+ await this.delete(id);
break;
}
}
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index 246f39722..d774b0353 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -109,17 +109,19 @@ export default class Sync {
let { lastSynced } = await this._performChecks();
if (force) lastSynced = 0;
- // We request and merge remote attachments beforehand to handle
- // all possible conflicts that will occur if a user attaches
- // the same file/image on both his devices. Since both files
- // will have the same hash but different encryption key, it
- // will cause problems on the local device.
- await this._mergeAttachments(lastSynced);
-
- // All pending attachments are uploaded before anything else.
- // This is done to ensure that when any note arrives on user's
- // device, its attachments can be downloaded.
- await this._uploadAttachments();
+ if (full) {
+ // We request and merge remote attachments beforehand to handle
+ // all possible conflicts that will occur if a user attaches
+ // the same file/image on both his devices. Since both files
+ // will have the same hash but different encryption key, it
+ // will cause problems on the local device.
+ await this._mergeAttachments(lastSynced);
+
+ // All pending attachments are uploaded before anything else.
+ // This is done to ensure that when any note arrives on user's
+ // device, its attachments can be downloaded.
+ await this._uploadAttachments();
+ }
// We collect, encrypt, and ready local changes before asking
// the server for remote changes. This is done to ensure we
diff --git a/packages/core/api/user-manager.js b/packages/core/api/user-manager.js
index c03cc95c5..c3217d3d6 100644
--- a/packages/core/api/user-manager.js
+++ b/packages/core/api/user-manager.js
@@ -23,9 +23,11 @@ class UserManager {
/**
*
* @param {import("../database/storage").default} storage
+ * @param {import("../api/index").default} db
*/
- constructor(storage) {
+ constructor(storage, db) {
this._storage = storage;
+ this._db = db;
this.tokenManager = new TokenManager(storage);
}
@@ -98,6 +100,19 @@ class UserManager {
return this._storage.read("user");
}
+ async updateUser(user) {
+ if (!user) return;
+
+ let token = await this.tokenManager.getAccessToken();
+ await http.patch.json(
+ `${constants.API_HOST}${ENDPOINTS.user}`,
+ user,
+ token
+ );
+
+ await this.setUser(user);
+ }
+
async deleteUser(password) {
let token = await this.tokenManager.getAccessToken();
if (!token) return;
@@ -167,21 +182,12 @@ class UserManager {
if (!user.attachmentsKey) {
const key = await this._storage.generateRandomKey();
- const encryptedKey = await this._storage.encrypt(
+ user.attachmentsKey = await this._storage.encrypt(
userEncryptionKey,
JSON.stringify(key)
);
- user.attachmentsKey = encryptedKey;
-
- let token = await this.tokenManager.getAccessToken();
- await http.patch.json(
- `${constants.API_HOST}${ENDPOINTS.user}`,
- user,
- token
- );
-
- await this.setUser(user);
+ await this.updateUser(user);
return key;
}
@@ -229,45 +235,62 @@ class UserManager {
async _updatePassword(type, data) {
let token = await this.tokenManager.getAccessToken();
- if (!token) return;
+ if (!token) throw new Error("You are not logged in.");
- // we hash the passwords beforehand
- const { email, salt } = await this.getUser();
- var hashedData = {};
- if (data.old_password)
- hashedData.old_password = await this._storage.hash(
- data.old_password,
- email
- );
- if (data.new_password)
- hashedData.new_password = await this._storage.hash(
- data.new_password,
- email
+ let user = await this.getUser();
+ if (!user) throw new Error("You are not logged in.");
+
+ const { new_password, old_password } = data;
+ if (old_password && !(await this.verifyPassword(old_password)))
+ throw new Error("Incorrect old password.");
+
+ const attachmentsKey = await this.getAttachmentsKey();
+
+ await this._db.outbox.add(type, data, async () => {
+ // we hash the passwords beforehand
+ const { email, salt } = await this.getUser();
+ var hashedData = {
+ old_password: undefined,
+ new_password: undefined,
+ };
+
+ if (old_password)
+ hashedData.old_password = await this._storage.hash(old_password, email);
+ if (new_password)
+ hashedData.new_password = await this._storage.hash(new_password, email);
+
+ await this._db.sync(true, true);
+
+ await this._storage.deriveCryptoKey(`_uk_@${email}`, {
+ password: data.new_password,
+ salt,
+ });
+
+ if (attachmentsKey) {
+ const userEncryptionKey = await this.getEncryptionKey();
+ if (!userEncryptionKey) return;
+ user.attachmentsKey = await this._storage.encrypt(
+ userEncryptionKey,
+ JSON.stringify(attachmentsKey)
+ );
+ await this.updateUser(user);
+ }
+
+ await this._db.sync(false, true);
+
+ await http.patch(
+ `${constants.AUTH_HOST}${ENDPOINTS.patchUser}`,
+ {
+ type,
+ ...hashedData,
+ },
+ token
);
- await http.patch(
- `${constants.AUTH_HOST}${ENDPOINTS.patchUser}`,
- {
- type,
- ...hashedData,
- },
- token
- );
- // TODO
- // await this._db.outbox.add(
- // type,
- // { newPassword: data.new_password },
- // async () => {
- // await this._db.sync(true);
-
- // await this._storage.deriveCryptoKey(`_uk_@${email}`, {
- // password: data.new_password,
- // salt,
- // });
-
- // await this._db.sync(false, true);
- // }
- // );
+ if (type !== "change_password")
+ await this.logout(true, "Password Changed");
+ });
+
return true;
}
}