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]
(.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 []
(log/warn "realm: deleting all realms")
(try
(do
(delete-realm (.-defaultPath rn-dependencies/realm))
(delete-realm new-account-filename))
(catch :default ex
(log/warn "failed to delete realm" ex))))
(..
(fs/read-dir (default-realm-dir (.-defaultPath rn-dependencies/realm)))
(then #(->> (js->clj % :keywordize-keys true)
(map :path)
(filter is-realm-file?)
(run! delete-realm)))))
(defn- close [realm]
(when realm

View File

@ -682,9 +682,9 @@
;; invalid-key
:invalid-key-title "Invalid key detected"
: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-confirm "Reset database"
:invalid-key-title "We detected a problem with the encryption key"
: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 "Apply"
;; browser
:browser "Browser"

View File

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

View File

@ -223,22 +223,30 @@
(fn [_ _]
{: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)
:content (i18n/label :invalid-key-content)
: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 []
(realm/delete-realms)
(.. (keychain/reset)
(then
#(re-frame/dispatch [:initialize-keychain]))))})
(.. (realm/delete-realms)
(then reset-keychain)
(catch reset-keychain)))})
(handlers/register-handler-fx
:initialize-app
(fn [_ [_ encryption-key error]]
(if (= error :invalid-key)
{:show-confirmation handle-invalid-key-parameters}
(if (= :invalid-key error)
{:show-confirmation (handle-invalid-key-parameters encryption-key)}
{::init-device-UUID nil
::testfairy-alert nil
:dispatch-n [[:initialize-db encryption-key]

View File

@ -10,3 +10,6 @@
(-> (.readFile rn-dependencies/fs path encoding)
(.then on-read)
(.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]}]
(if (= :weak-key error)
(log/warn "weak key used, database might not be encrypted properly")
(log/error "invalid key detected"))
(re-frame/dispatch (into [] (concat event [key error]))))
(log/warn "invalid key detected"))
(re-frame/dispatch (into [] (concat event [(or key "")
(or error :invalid-key)]))))
(re-frame/reg-fx
:get-encryption-key