encryption.cljs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. (ns frontend.components.encryption
  2. (:require [clojure.string :as string]
  3. [frontend.context.i18n :refer [t]]
  4. [frontend.encrypt :as encrypt]
  5. [frontend.handler.metadata :as metadata-handler]
  6. [frontend.handler.notification :as notification]
  7. [frontend.fs.sync :as sync]
  8. [frontend.state :as state]
  9. [frontend.ui :as ui]
  10. [frontend.util :as util]
  11. [frontend.config :as config]
  12. [promesa.core :as p]
  13. [cljs.core.async :as async]
  14. [rum.core :as rum]))
  15. (rum/defcs encryption-dialog-inner <
  16. (rum/local false ::reveal-secret-phrase?)
  17. [state repo-url close-fn]
  18. (let [reveal-secret-phrase? (get state ::reveal-secret-phrase?)
  19. public-key (encrypt/get-public-key repo-url)
  20. private-key (encrypt/get-secret-key repo-url)]
  21. [:div
  22. [:div.sm:flex.sm:items-start
  23. [:div.mt-3.text-center.sm:mt-0.sm:text-left
  24. [:h3#modal-headline.text-lg.leading-6.font-medium
  25. "This graph is encrypted with " [:a {:href "https://age-encryption.org/" :target "_blank" :rel "noopener"} "age-encryption.org/v1"]]]]
  26. [:div.mt-1
  27. [:div.max-w-2xl.rounded-md.shadow-sm.sm:max-w-xl
  28. [:div.cursor-pointer.block.w-full.rounded-sm.p-2
  29. {:on-click (fn []
  30. (when (not @reveal-secret-phrase?)
  31. (reset! reveal-secret-phrase? true)))}
  32. [:div.font-medium "Public Key:"]
  33. [:div.font-mono.select-all.break-all public-key]
  34. (if @reveal-secret-phrase?
  35. [:div
  36. [:div.mt-1.font-medium "Private Key:"]
  37. [:div.font-mono.select-all.break-all private-key]]
  38. [:div.underline "click to view the private key"])]]]
  39. [:div.mt-5.sm:mt-4.sm:flex.sm:flex-row-reverse
  40. [:span.mt-3.flex.w-full.rounded-md.shadow-sm.sm:mt-0.sm:w-auto
  41. [: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
  42. {:type "button"
  43. :on-click close-fn}
  44. (t :close)]]]]))
  45. (defn encryption-dialog
  46. [repo-url]
  47. (fn [close-fn]
  48. (encryption-dialog-inner repo-url close-fn)))
  49. (rum/defcs ^:large-vars/cleanup-todo input-password-inner < rum/reactive
  50. (rum/local "" ::password)
  51. (rum/local "" ::pw-confirm)
  52. (rum/local false ::pw-confirm-focused?)
  53. {:will-mount (fn [state]
  54. ;; try to close tour tips
  55. (some->> (state/sub :file-sync/jstour-inst)
  56. (.complete))
  57. state)}
  58. [state repo-url close-fn {:keys [type GraphName GraphUUID init-graph-keys after-input-password]}]
  59. (let [*password (get state ::password)
  60. *pw-confirm (get state ::pw-confirm)
  61. *pw-confirm-focused? (get state ::pw-confirm-focused?)
  62. *input-ref-0 (rum/create-ref)
  63. *input-ref-1 (rum/create-ref)
  64. remote-pw? (= type :input-pwd-remote)
  65. loading? (state/sub [:ui/loading? :set-graph-password])
  66. pw-strength (when (and init-graph-keys
  67. (not (string/blank? @*password)))
  68. (util/check-password-strength @*password))
  69. can-submit? #(if init-graph-keys
  70. (and (>= (count @*password) 6)
  71. (>= (:id pw-strength) 1))
  72. true)
  73. set-remote-graph-pwd-result (state/sub [:file-sync/set-remote-graph-password-result])
  74. submit-handler
  75. (fn []
  76. (let [value @*password]
  77. (cond
  78. (string/blank? value)
  79. nil
  80. (and init-graph-keys (not= @*password @*pw-confirm))
  81. (notification/show! "The passwords are not matched." :error)
  82. :else
  83. (case type
  84. :local
  85. (p/let [keys (encrypt/generate-key-pair-and-save! repo-url)
  86. db-encrypted-secret (encrypt/encrypt-with-passphrase value keys)]
  87. (metadata-handler/set-db-encrypted-secret! db-encrypted-secret)
  88. (close-fn true))
  89. (:create-pwd-remote :input-pwd-remote)
  90. (do
  91. (state/set-state! [:ui/loading? :set-graph-password] true)
  92. (state/set-state! [:file-sync/set-remote-graph-password-result] {})
  93. (async/go
  94. (let [persist-r (async/<! (sync/encrypt+persist-pwd! @*password GraphUUID))]
  95. (if (instance? js/Error persist-r)
  96. (js/console.error persist-r)
  97. (when (fn? after-input-password)
  98. (async/<! (after-input-password))
  99. ;; TODO: it's better if based on sync state
  100. (when init-graph-keys
  101. (js/setTimeout #(state/pub-event! [:file-sync/maybe-onboarding-show :sync-learn]) 10000)))))))))))
  102. cancel-handler
  103. (fn []
  104. (state/set-state! [:file-sync/set-remote-graph-password-result] {})
  105. (close-fn))
  106. enter-handler
  107. (fn [^js e]
  108. (when-let [^js input (and e (= 13 (.-which e)) (.-target e))]
  109. (when-not (string/blank? (.-value input))
  110. (let [input-0? (= (util/safe-lower-case (.-placeholder input)) "password")]
  111. (if init-graph-keys
  112. ;; setup mode
  113. (if input-0?
  114. (.select (rum/deref *input-ref-1))
  115. (submit-handler))
  116. ;; unlock mode
  117. (submit-handler))))))]
  118. [:div.encryption-password.max-w-2xl.-mb-2
  119. [:div.cp__file-sync-related-normal-modal
  120. [:div.flex.justify-center.pb-4 [:span.icon-wrap (ui/icon "lock-access")]]
  121. [:div.mt-3.text-center.sm:mt-0.sm:text-left
  122. [:h1#modal-headline.text-2xl.font-bold.text-center
  123. (if init-graph-keys
  124. (if remote-pw?
  125. "Secure this remote graph!"
  126. "Encrypt this graph")
  127. (if remote-pw?
  128. "Unlock this remote graph!"
  129. "Decrypt this graph"))]]
  130. ;; decrypt remote graph with one password
  131. (when (and remote-pw? (not init-graph-keys))
  132. [:<>
  133. [:div.folder-tip.flex.flex-col.items-center
  134. [:h3
  135. [:span.flex.space-x-2.leading-none.pb-1
  136. (ui/icon "cloud-lock")
  137. [:span GraphName]
  138. [:span.scale-75 (ui/icon "arrow-right")]
  139. [:span (ui/icon "folder")]]]
  140. [:h4.px-2.-mb-1.5 (config/get-string-repo-dir repo-url)]]
  141. [:div.input-hints.text-sm.py-2.px-3.rounded.mb-2.mt-2.flex.items-center
  142. (if-let [display-str (:fail set-remote-graph-pwd-result)]
  143. [:<>
  144. [:span.scale-125.pr-1.text-red-600 (ui/icon "alert-circle" {:class "text-md mr-1"})]
  145. [:span.text-red-600 display-str]]
  146. [:<>
  147. [:span.scale-125.pr-1 (ui/icon "bulb" {:class "text-md mr-1"})]
  148. [:span "Please enter the password for this graph to continue syncing."]])]])
  149. ;; secure this remote graph
  150. (when (and remote-pw? init-graph-keys)
  151. (let [pattern-ok? #(>= (count @*password) 6)]
  152. [:<>
  153. [:h2.text-center.opacity-70.text-sm.py-2
  154. "Each graph you want to synchronize via Logseq needs its own password for end-to-end encryption."]
  155. [:div.input-hints.text-sm.py-2.px-3.rounded.mb-3.mt-4.flex.items-center
  156. (if (or (not (string/blank? @*password))
  157. (not (string/blank? @*pw-confirm)))
  158. (if (or (not (pattern-ok?))
  159. (not= @*password @*pw-confirm))
  160. [:span.scale-125.pr-1.text-red-600 (ui/icon "alert-circle" {:class "text-md mr-1"})]
  161. [:span.scale-125.pr-1.text-green-600 (ui/icon "circle-check" {:class "text-md mr-1"})])
  162. [:span.scale-125.pr-1 (ui/icon "bulb" {:class "text-md mr-1"})])
  163. (if (not (string/blank? @*password))
  164. (if-not (pattern-ok?)
  165. [:span "Password can't be less than 6 characters"]
  166. (if (not (string/blank? @*pw-confirm))
  167. (if (not= @*pw-confirm @*password)
  168. [:span "Password fields are not matching!"]
  169. [:span "Password fields are matching!"])
  170. [:span "Enter your chosen password again!"]))
  171. [:span "Choose a strong and hard to guess password!"])
  172. ]
  173. ;; password strength checker
  174. (when-not (string/blank? @*password)
  175. [:<>
  176. [:div.input-hints.text-sm.py-2.px-3.rounded.mb-2.-mt-1.5.flex.items-center.space-x-3
  177. (let [included-set (set (:contains pw-strength))]
  178. (for [i ["lowercase" "uppercase" "number" "symbol"]
  179. :let [included? (contains? included-set i)]]
  180. [:span.strength-item
  181. {:key i
  182. :class (when included? "included")}
  183. (ui/icon (if included? "check" "x") {:class "mr-1"})
  184. [:span.capitalize i]
  185. ]))]
  186. [:div.input-pw-strength
  187. [:div.indicator.flex
  188. (for [i (range 4)
  189. :let [title (get ["Too weak" "Weak" "Medium" "Strong"] i)]]
  190. [:i {:key i
  191. :title title
  192. :class (when (>= (int (:id pw-strength)) i) "active")} i])]]])]))
  193. [:input.form-input.block.w-full.sm:text-sm.sm:leading-5.my-2
  194. {:type "password"
  195. :ref *input-ref-0
  196. :placeholder "Password"
  197. :auto-focus true
  198. :disabled loading?
  199. :on-key-up enter-handler
  200. :on-change (fn [^js e]
  201. (reset! *password (util/evalue e))
  202. (when (:fail set-remote-graph-pwd-result)
  203. (state/set-state! [:file-sync/set-remote-graph-password-result] {})))}]
  204. (when init-graph-keys
  205. [:input.form-input.block.w-full.sm:text-sm.sm:leading-5.my-2
  206. {:type "password"
  207. :ref *input-ref-1
  208. :placeholder "Re-enter the password"
  209. :on-focus #(reset! *pw-confirm-focused? true)
  210. :on-blur #(reset! *pw-confirm-focused? false)
  211. :disabled loading?
  212. :on-key-up enter-handler
  213. :on-change (fn [^js e]
  214. (reset! *pw-confirm (util/evalue e)))}])
  215. (when init-graph-keys
  216. [:div.init-remote-pw-tips.space-x-4.pt-2.hidden.sm:flex
  217. [:div.flex-1.flex.items-center
  218. [:span.px-3.scale-125 (ui/icon "key")]
  219. [:p.dark:text-gray-100
  220. [:span "Please make sure you "]
  221. "remember the password you have set, "
  222. [:span "and we recommend you "]
  223. "keep a secure backup "
  224. [:span "of the password."]]]
  225. [:div.flex-1.flex.items-center
  226. [:span.px-3.scale-125 (ui/icon "lock")]
  227. [:p.dark:text-gray-100
  228. "If you lose your password, all of your data in the cloud can’t be decrypted. "
  229. [:span "You will still be able to access the local version of your graph."]]]])]
  230. [:div.mt-5.sm:mt-4.flex.justify-center.sm:justify-end.space-x-3
  231. (ui/button (t :cancel) :background "gray" :disabled loading? :class "opacity-60" :on-click cancel-handler)
  232. (ui/button [:span.inline-flex.items-center.leading-none
  233. [:span (t :submit)]
  234. (when loading?
  235. [:span.ml-1 (ui/loading "" {:class "w-4 h-4"})])]
  236. :disabled (or (not (can-submit?)) loading?)
  237. :on-click submit-handler)]]))
  238. (defn input-password
  239. ([repo-url close-fn] (input-password repo-url close-fn {:type :local}))
  240. ([repo-url close-fn opts]
  241. (fn [_close-fn]
  242. (let [close-fn' (if (fn? close-fn)
  243. #(do (close-fn %)
  244. (_close-fn))
  245. _close-fn)]
  246. (input-password-inner repo-url close-fn' opts)))))
  247. (rum/defcs encryption-setup-dialog-inner
  248. [state repo-url close-fn]
  249. [:div
  250. [:div.sm:flex.sm:items-start
  251. [:div.mt-3.text-center.sm:mt-0.sm:text-left
  252. [:h3#modal-headline.text-lg.leading-6.font-medium
  253. "Do you want to create an encrypted graph?"]]]
  254. [:div.mt-5.sm:mt-4.sm:flex.sm:flex-row-reverse
  255. [:span.flex.w-full.rounded-md.shadow-sm.sm:ml-3.sm:w-auto
  256. [:button.inline-flex.justify-center.w-full.rounded-md.border.border-transparent.px-4.py-2.bg-indigo-600.text-base.leading-6.font-medium.text-white.shadow-sm.hover:bg-indigo-500.focus:outline-none.focus:border-indigo-700.focus:shadow-outline-indigo.transition.ease-in-out.duration-150.sm:text-sm.sm:leading-5
  257. {:type "button"
  258. :on-click (fn []
  259. (state/set-modal!
  260. (input-password repo-url close-fn)
  261. {:center? true :close-btn? false}))}
  262. (t :yes)]]
  263. [:span.mt-3.flex.w-full.rounded-md.shadow-sm.sm:mt-0.sm:w-auto
  264. [: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
  265. {:type "button"
  266. :on-click (fn [] (close-fn false))}
  267. (t :no)]]]])
  268. (defn encryption-setup-dialog
  269. [repo-url close-fn]
  270. (fn [close-modal-fn]
  271. (let [close-fn (fn [encrypted?]
  272. (close-fn encrypted?)
  273. (close-modal-fn))]
  274. (encryption-setup-dialog-inner repo-url close-fn))))
  275. (rum/defcs encryption-input-secret-inner <
  276. (rum/local "" ::secret)
  277. (rum/local false ::loading)
  278. [state _repo-url db-encrypted-secret close-fn]
  279. (let [secret (::secret state)
  280. loading (::loading state)
  281. on-click-fn (fn []
  282. (reset! loading true)
  283. (let [value @secret]
  284. (when-not (string/blank? value) ; TODO: length or other checks
  285. (let [repo (state/get-current-repo)]
  286. (p/do!
  287. (-> (encrypt/decrypt-with-passphrase value db-encrypted-secret)
  288. (p/then (fn [keys]
  289. (encrypt/save-key-pair! repo keys)
  290. (close-fn true)
  291. (state/set-state! :encryption/graph-parsing? false)))
  292. (p/catch #(notification/show! "The password is not matched." :warning true))
  293. (p/finally #(reset! loading false))))))))]
  294. [:div
  295. [:div.sm:flex.sm:items-start
  296. [:div.mt-3.text-center.sm:mt-0.sm:text-left
  297. [:h3#modal-headline.text-lg.leading-6.font-medium
  298. "Enter your password"]]]
  299. [:input.form-input.block.w-full.sm:text-sm.sm:leading-5.my-2
  300. {:type "password"
  301. :auto-focus true
  302. :on-change (fn [e]
  303. (reset! secret (util/evalue e)))
  304. :on-key-down (fn [e]
  305. (when (= (.-key e) "Enter")
  306. (on-click-fn)))}]
  307. [:div.mt-5.sm:mt-4.sm:flex.sm:flex-row-reverse
  308. [:span.flex.w-full.rounded-md.shadow-sm.sm:ml-3.sm:w-auto
  309. [:button.inline-flex.justify-center.w-full.rounded-md.border.border-transparent.px-4.py-2.bg-indigo-600.text-base.leading-6.font-medium.text-white.shadow-sm.hover:bg-indigo-500.focus:outline-none.focus:border-indigo-700.focus:shadow-outline-indigo.transition.ease-in-out.duration-150.sm:text-sm.sm:leading-5
  310. {:type "button"
  311. :on-click on-click-fn}
  312. (if @loading (ui/loading "Decrypting") "Decrypt")]]]]))
  313. (defn encryption-input-secret-dialog
  314. [repo-url db-encrypted-secret close-fn]
  315. (fn [close-modal-fn]
  316. (let [close-fn (fn [encrypted?]
  317. (close-fn encrypted?)
  318. (close-modal-fn))]
  319. (encryption-input-secret-inner repo-url db-encrypted-secret close-fn))))