From c19a333d90e4668ef12f76c8fa62f7d6c0c9f258 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 11 Nov 2025 03:17:32 +0800 Subject: [PATCH] enhance(e2ee): store aes encrypted password in OPFS so no need to input password repeatly --- src/main/frontend/common/file/opfs.cljs | 43 +++++++++++++++++++ src/main/frontend/components/e2ee.cljs | 4 +- src/main/frontend/components/settings.cljs | 19 ++++---- src/main/frontend/handler/events/rtc.cljs | 29 +++++++++---- src/main/frontend/state.cljs | 4 +- src/main/frontend/worker/rtc/crypt.cljs | 50 +++++++++++++++++++--- 6 files changed, 124 insertions(+), 25 deletions(-) create mode 100644 src/main/frontend/common/file/opfs.cljs diff --git a/src/main/frontend/common/file/opfs.cljs b/src/main/frontend/common/file/opfs.cljs new file mode 100644 index 0000000000..e5aa840b0b --- /dev/null +++ b/src/main/frontend/common/file/opfs.cljs @@ -0,0 +1,43 @@ +(ns frontend.common.file.opfs + "OPFS fs api" + (:require [promesa.core :as p])) + +(defn (p/let [root (.. js/navigator -storage (getDirectory))] + (.removeEntry root filename)) + (p/catch (fn [err] + (if (and ignore-not-found? + (= (.-name err) "NotFoundError")) + nil + (throw err)))))) diff --git a/src/main/frontend/components/e2ee.cljs b/src/main/frontend/components/e2ee.cljs index f1d169204c..f9b106e946 100644 --- a/src/main/frontend/components/e2ee.cljs +++ b/src/main/frontend/components/e2ee.cljs @@ -1,6 +1,7 @@ (ns frontend.components.e2ee (:require [clojure.string :as string] [frontend.common.crypt :as crypt] + [frontend.state :as state] [frontend.util :as util] [logseq.shui.hooks :as hooks] [logseq.shui.ui :as shui] @@ -34,12 +35,13 @@ "Submit")]]])) (rum/defc e2ee-password-to-decrypt-private-key - [encrypted-private-key private-key-promise] + [encrypted-private-key private-key-promise refresh-token] (let [[password set-password!] (hooks/use-state "") [decrypt-fail? set-decrypt-fail!] (hooks/use-state false) on-submit (fn [] (-> (p/let [private-key (crypt/ (p/do! - (state/ (p/do! - (set-reset-password-status! "Updating E2EE password ...") - (state/ + (p/let [{:keys [password]} (state/ (storage/get "refresh-token") str) :auth/access-token nil :auth/id-token nil @@ -2149,7 +2149,7 @@ Similar to re-frame subscriptions" (sub :auth/id-token)) (defn get-auth-refresh-token [] - (:auth/refresh-token @state)) + (str (:auth/refresh-token @state))) (defn set-file-sync-manager [graph-uuid v] (when (and graph-uuid v) diff --git a/src/main/frontend/worker/rtc/crypt.cljs b/src/main/frontend/worker/rtc/crypt.cljs index 4c5f625a34..1b291b1d10 100644 --- a/src/main/frontend/worker/rtc/crypt.cljs +++ b/src/main/frontend/worker/rtc/crypt.cljs @@ -5,6 +5,7 @@ Server stores the encrypted AES key, public key, and encrypted private key." (:require ["/frontend/idbkv" :as idb-keyval] [frontend.common.crypt :as crypt] + [frontend.common.file.opfs :as opfs] [frontend.common.missionary :as c.m] [frontend.common.thread-api :refer [def-thread-api]] [frontend.worker.rtc.ws-util :as ws-util] @@ -15,6 +16,34 @@ (:import [missionary Cancelled])) (defonce ^:private store (delay (idb-keyval/newStore "localforage" "keyvaluepairs" 2))) +(defonce ^:private e2ee-password-file "e2ee-password") + +(defn-