[slow sign in] Fetch generic password only once

Previously it was fetched each time when any realm db was opened,
but it should happen only once. Saves up to 300ms on sign in.
This commit is contained in:
Roman Volosovskyi 2018-12-19 08:00:44 +02:00
parent e3cc50c7d7
commit 768da1a5f0
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
2 changed files with 22 additions and 10 deletions

View File

@ -129,14 +129,23 @@
(comp validate (comp validate
string->js-array)) string->js-array))
(defonce generic-password (atom nil))
(defn get-encryption-key [] (defn get-encryption-key []
(log/debug "initializing realm encryption key...") (log/debug "PERF" "initializing realm encryption key..." (.now js/Date))
(if @generic-password
(js/Promise.
(fn [on-success _]
(on-success (security/unmask @generic-password))))
(.. (.getGenericPassword rn/keychain) (.. (.getGenericPassword rn/keychain)
(then (then
(fn [res] (fn [res]
(if res (if res
(handle-found res) (let [handled-res (handle-found res)]
(handle-not-found)))))) (reset! generic-password
(security/->MaskedData handled-res))
handled-res)
(handle-not-found)))))))
(defn safe-get-encryption-key (defn safe-get-encryption-key
"Return encryption key or empty string in case invalid/empty" "Return encryption key or empty string in case invalid/empty"

View File

@ -40,7 +40,8 @@
(deftest key-is-weak (deftest key-is-weak
(async (async
done done
(with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password (key->json weak-key)}))}] (with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password (key->json weak-key)}))}
keychain/generic-password (atom nil)]
(testing "it returns a valid key" (testing "it returns a valid key"
(.. (keychain/get-encryption-key) (.. (keychain/get-encryption-key)
(then (fn [_] (then (fn [_]
@ -54,7 +55,8 @@
(deftest safe-key-is-not-valid (deftest safe-key-is-not-valid
(async (async
done done
(with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password (key->json weak-key)}))}] (with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password (key->json weak-key)}))}
keychain/generic-password (atom nil)]
(testing "it returns a valid key" (testing "it returns a valid key"
(.. (keychain/safe-get-encryption-key) (.. (keychain/safe-get-encryption-key)
(then (fn [k] (then (fn [k]
@ -67,7 +69,8 @@
(deftest safe-key-is-nil (deftest safe-key-is-nil
(async (async
done done
(with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password nil}))}] (with-redefs [rn/keychain #js {:getGenericPassword (constantly (.resolve js/Promise #js {:password nil}))}
keychain/generic-password (atom nil)]
(testing "it returns a valid key" (testing "it returns a valid key"
(.. (keychain/safe-get-encryption-key) (.. (keychain/safe-get-encryption-key)
(then (fn [k] (then (fn [k]