Skip to content

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

fix: do not request access token eagerly

thecodrr9e9386f9709d
diff --git a/packages/core/api/user-manager.js b/packages/core/api/user-manager.js
index 90ab7cb7c..6a5197a5b 100644
--- a/packages/core/api/user-manager.js
+++ b/packages/core/api/user-manager.js
@@ -160,42 +160,46 @@ class UserManager {
}
async getAttachmentsKey() {
- let user = await this.getUser();
- if (!user) return;
+ try {
+ let user = await this.getUser();
+ if (!user) return;
- let token = await this.tokenManager.getAccessToken();
- if (!token) return;
+ if (!user.attachmentsKey) {
+ let token = await this.tokenManager.getAccessToken();
+ user = await http.get(`${constants.API_HOST}${ENDPOINTS.user}`, token);
+ }
- if (!user.attachmentsKey) {
- user = await http.get(`${constants.API_HOST}${ENDPOINTS.user}`, token);
- }
+ const userEncryptionKey = await this.getEncryptionKey();
+ if (!userEncryptionKey) return;
- const userEncryptionKey = await this.getEncryptionKey();
- if (!userEncryptionKey) return;
+ if (!user.attachmentsKey) {
+ const key = await this._storage.generateRandomKey();
+ const encryptedKey = await this._storage.encrypt(
+ userEncryptionKey,
+ JSON.stringify(key)
+ );
- if (!user.attachmentsKey) {
- const key = await this._storage.generateRandomKey();
- const encryptedKey = await this._storage.encrypt(
- userEncryptionKey,
- JSON.stringify(key)
- );
+ user.attachmentsKey = encryptedKey;
- user.attachmentsKey = encryptedKey;
- await http.patch.json(
- `${constants.API_HOST}${ENDPOINTS.user}`,
- user,
- token
- );
+ let token = await this.tokenManager.getAccessToken();
+ await http.patch.json(
+ `${constants.API_HOST}${ENDPOINTS.user}`,
+ user,
+ token
+ );
- await this.setUser(user);
- return key;
- }
+ await this.setUser(user);
+ return key;
+ }
- const plainData = await this._storage.decrypt(
- userEncryptionKey,
- user.attachmentsKey
- );
- return JSON.parse(plainData);
+ const plainData = await this._storage.decrypt(
+ userEncryptionKey,
+ user.attachmentsKey
+ );
+ return JSON.parse(plainData);
+ } catch (e) {
+ throw new Error("Could not get attachments encryption key.");
+ }
}
async sendVerificationEmail() {
diff --git a/packages/core/utils/http.js b/packages/core/utils/http.js
index 89b3d9680..1bf7868a6 100644
--- a/packages/core/utils/http.js
+++ b/packages/core/utils/http.js
@@ -7,19 +7,19 @@ function deleteRequest(url, token) {
}
function patch(url, data, token) {
- return bodyRequest(url, data, false, token, "PATCH");
+ return bodyRequest(url, data, token, "PATCH");
}
patch.json = function (url, data, token) {
- return bodyRequest(url, data, true, token, "PATCH");
+ return bodyRequest(url, data, token, "PATCH", "application/json");
};
function post(url, data, token) {
- return bodyRequest(url, data, false, token, "POST");
+ return bodyRequest(url, data, token, "POST");
}
post.json = function (url, data, token) {
- return bodyRequest(url, data, true, token, "POST");
+ return bodyRequest(url, data, token, "POST", "application/json");
};
export default {
@@ -29,9 +29,9 @@ export default {
patch,
};
-function transformer(data, json) {
+function transformer(data, type) {
if (!data) return;
- if (json) return JSON.stringify(data);
+ if (type === "application/json") return JSON.stringify(data);
else {
return Object.entries(data)
.map(
@@ -70,16 +70,20 @@ async function request(url, token, method) {
);
}
-async function bodyRequest(url, data, json = true, token, method) {
+async function bodyRequest(
+ url,
+ data,
+ token,
+ method,
+ contentType = "application/x-www-form-urlencoded"
+) {
return handleResponse(
await fetch(url, {
method,
- body: transformer(data, json),
+ body: transformer(data, contentType),
headers: {
...getAuthorizationHeader(token),
- "Content-Type": json
- ? "application/json"
- : "application/x-www-form-urlencoded",
+ "Content-Type": contentType,
},
})
);