Browse Source

feat(encryption): add :db/encrypted? to metadata.edn

Storing the encryption status in the metadata.edn will allows the
app to prompt the user to enter the secret phrase when the graph
was already encrypted.
Tienson Qin 5 years ago
parent
commit
022abff722

+ 4 - 4
src/main/frontend/components/encryption.cljs

@@ -64,18 +64,18 @@
         {:type "button"
          :on-click (fn []
                      (e/generate-mnemonic-and-save repo-url)
-                     (close-fn))}
+                     (close-fn true))}
         (t :yes)]]
       [:span.mt-3.flex.w-full.rounded-md.shadow-sm.sm:mt-0.sm:w-auto
        [:button.inline-flex.justify-center.w-full.rounded-md.border.border-gray-300.px-4.py-2.bg-white.text-base.leading-6.font-medium.text-gray-700.shadow-sm.hover:text-gray-500.focus:outline-none.focus:border-blue-300.focus:shadow-outline-blue.transition.ease-in-out.duration-150.sm:text-sm.sm:leading-5
         {:type "button"
-         :on-click close-fn}
+         :on-click (fn [] (close-fn false))}
         (t :no)]]]]))
 
 (defn encryption-setup-dialog
   [repo-url close-fn]
   (fn [close-modal-fn]
-    (let [close-fn (fn []
-                     (close-fn)
+    (let [close-fn (fn [encrypted?]
+                     (close-fn encrypted?)
                      (close-modal-fn))]
       (encryption-setup-dialog-inner repo-url close-fn))))

+ 2 - 1
src/main/frontend/fs.cljs

@@ -65,7 +65,8 @@
 (defn write-file!
   [repo dir path content opts]
   (when content
-    (let [content (encrypt/encrypt content)]
+    (let [metadata? (string/ends-with? path config/metadata-file)
+          content (if metadata? content (encrypt/encrypt content))]
       (->
        (do
          (protocol/write-file! (get-fs dir) repo dir path content opts)

+ 23 - 7
src/main/frontend/handler/repo.cljs

@@ -161,13 +161,29 @@
         (when (empty? (db/get-page-blocks-no-cache repo today-page))
           (create-today-journal-if-not-exists repo))))))
 
-(defn create-default-files!
-  [repo-url]
+(defn create-metadata-file
+  [repo-url encrypted?]
   (spec/validate :repos/url repo-url)
-  (create-config-file-if-not-exists repo-url)
-  (create-today-journal-if-not-exists repo-url)
-  (create-contents-file repo-url)
-  (create-custom-theme repo-url))
+  (let [repo-dir (config/get-repo-dir repo-url)
+        path (str config/app-name "/" config/metadata-file)
+        file-path (str "/" path)
+        default-content (if encrypted? "{:db/encrypted? true}" "{}")]
+    (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/app-name))
+            file-exists? (fs/create-if-not-exists repo-url repo-dir file-path default-content)]
+      (when-not file-exists?
+        (file-handler/reset-file! repo-url path default-content)
+        (git-handler/git-add repo-url path)))))
+
+(defn create-default-files!
+  ([repo-url]
+   (create-default-files! repo-url false))
+  ([repo-url encrypted?]
+   (spec/validate :repos/url repo-url)
+   (create-metadata-file repo-url encrypted?)
+   (create-config-file-if-not-exists repo-url)
+   (create-today-journal-if-not-exists repo-url)
+   (create-contents-file repo-url)
+   (create-custom-theme repo-url)))
 
 (defn- reset-contents-and-blocks!
   [repo-url files blocks-pages delete-files delete-blocks]
@@ -201,7 +217,7 @@
       (state/set-modal!
        (encryption/encryption-setup-dialog
         repo-url
-        #(create-default-files! repo-url))))
+        #(create-default-files! repo-url %))))
     (when re-render?
       (ui-handler/re-render-root! re-render-opts))
     (state/set-importing-to-db! false)))