Allow user to access account on invalid key

Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
Andrea Maria Piana 2018-06-21 20:08:10 +02:00 committed by Julien Eluard
parent fafd50775b
commit 5935d29d7d
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
7 changed files with 41 additions and 22 deletions

View File

@ -41,14 +41,22 @@
[file-name] [file-name]
(.deleteFile rn-dependencies/realm (clj->js {:path file-name}))) (.deleteFile rn-dependencies/realm (clj->js {:path file-name})))
(defn- default-realm-dir [path]
(string/replace path #"default\.realm$" ""))
(defn- is-realm-file? [n]
(or (re-matches #".*/default\.realm$" n)
(re-matches #".*/new-account$" n)
(re-matches #".*/[0-9a-f]{40}$" n)))
(defn- delete-realms [] (defn- delete-realms []
(log/warn "realm: deleting all realms") (log/warn "realm: deleting all realms")
(try (..
(do (fs/read-dir (default-realm-dir (.-defaultPath rn-dependencies/realm)))
(delete-realm (.-defaultPath rn-dependencies/realm)) (then #(->> (js->clj % :keywordize-keys true)
(delete-realm new-account-filename)) (map :path)
(catch :default ex (filter is-realm-file?)
(log/warn "failed to delete realm" ex)))) (run! delete-realm)))))
(defn- close [realm] (defn- close [realm]
(when realm (when realm

View File

@ -682,9 +682,9 @@
;; invalid-key ;; invalid-key
:invalid-key-title "Invalid key detected" :invalid-key-title "We detected a problem with the encryption key"
:invalid-key-content "The key used to encrypt your data is invalid. Clicking on 'Reset database' will delete any existing realm and create a new one with a stronger key. If you would like to backup you data please click on cancel and the app will quit. Ensure the data directory of the app is clean and make sure no data is backed up by your cloud provider." :invalid-key-content "To protect yourself, you need to create new account and erase your old data by tapping “Apply”. If you have an existing account and would like to save your seed phrase then choose “Cancel”, back it up, and restart the app. We strongly recommend creating new account because the old one is stored unencrypted."
:invalid-key-confirm "Reset database" :invalid-key-confirm "Apply"
;; browser ;; browser
:browser "Browser" :browser "Browser"

View File

@ -198,4 +198,4 @@
(handlers-macro/merge-fx (handlers-macro/merge-fx
cofx cofx
(wallet-set-up-passed db) (wallet-set-up-passed db)
(accounts.utils/account-update {:wallet-set-up-passed? true})))) (accounts.utils/account-update {:wallet-set-up-passed? true}))))

View File

@ -44,8 +44,7 @@
(catch (fn [{:keys [error key]}] (catch (fn [{:keys [error key]}]
;; no need of further error handling as already taken care ;; no need of further error handling as already taken care
;; when starting the app ;; when starting the app
(when (= error :weak-key) (change-account address (or key "")))))))
(change-account address key)))))))
;;;; Handlers ;;;; Handlers

View File

@ -223,22 +223,30 @@
(fn [_ _] (fn [_ _]
{:get-encryption-key [:initialize-app]})) {:get-encryption-key [:initialize-app]}))
(def handle-invalid-key-parameters (defn- reset-keychain []
(.. (keychain/reset)
(then
#(re-frame/dispatch [:initialize-keychain]))))
(defn handle-invalid-key-parameters [encryption-key]
{:title (i18n/label :invalid-key-title) {:title (i18n/label :invalid-key-title)
:content (i18n/label :invalid-key-content) :content (i18n/label :invalid-key-content)
:confirm-button-text (i18n/label :invalid-key-confirm) :confirm-button-text (i18n/label :invalid-key-confirm)
:on-cancel #(.exitApp react/back-handler) ;; On cancel we initialize the app with the invalid key, to allow the user
;; to recover the seed phrase
:on-cancel #(do
(log/warn "initializing app with invalid key")
(re-frame/dispatch [:initialize-app encryption-key]))
:on-accept (fn [] :on-accept (fn []
(realm/delete-realms) (.. (realm/delete-realms)
(.. (keychain/reset) (then reset-keychain)
(then (catch reset-keychain)))})
#(re-frame/dispatch [:initialize-keychain]))))})
(handlers/register-handler-fx (handlers/register-handler-fx
:initialize-app :initialize-app
(fn [_ [_ encryption-key error]] (fn [_ [_ encryption-key error]]
(if (= error :invalid-key) (if (= :invalid-key error)
{:show-confirmation handle-invalid-key-parameters} {:show-confirmation (handle-invalid-key-parameters encryption-key)}
{::init-device-UUID nil {::init-device-UUID nil
::testfairy-alert nil ::testfairy-alert nil
:dispatch-n [[:initialize-db encryption-key] :dispatch-n [[:initialize-db encryption-key]

View File

@ -10,3 +10,6 @@
(-> (.readFile rn-dependencies/fs path encoding) (-> (.readFile rn-dependencies/fs path encoding)
(.then on-read) (.then on-read)
(.catch on-error))) (.catch on-error)))
(defn read-dir [path]
(.readDir rn-dependencies/fs path))

View File

@ -6,8 +6,9 @@
(defn handle-key-error [event {:keys [error key]}] (defn handle-key-error [event {:keys [error key]}]
(if (= :weak-key error) (if (= :weak-key error)
(log/warn "weak key used, database might not be encrypted properly") (log/warn "weak key used, database might not be encrypted properly")
(log/error "invalid key detected")) (log/warn "invalid key detected"))
(re-frame/dispatch (into [] (concat event [key error])))) (re-frame/dispatch (into [] (concat event [(or key "")
(or error :invalid-key)]))))
(re-frame/reg-fx (re-frame/reg-fx
:get-encryption-key :get-encryption-key