[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:
parent
e3cc50c7d7
commit
768da1a5f0
|
@ -129,14 +129,23 @@
|
|||
(comp validate
|
||||
string->js-array))
|
||||
|
||||
(defonce generic-password (atom nil))
|
||||
|
||||
(defn get-encryption-key []
|
||||
(log/debug "initializing realm encryption key...")
|
||||
(.. (.getGenericPassword rn/keychain)
|
||||
(then
|
||||
(fn [res]
|
||||
(if res
|
||||
(handle-found res)
|
||||
(handle-not-found))))))
|
||||
(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)
|
||||
(then
|
||||
(fn [res]
|
||||
(if res
|
||||
(let [handled-res (handle-found res)]
|
||||
(reset! generic-password
|
||||
(security/->MaskedData handled-res))
|
||||
handled-res)
|
||||
(handle-not-found)))))))
|
||||
|
||||
(defn safe-get-encryption-key
|
||||
"Return encryption key or empty string in case invalid/empty"
|
||||
|
|
|
@ -40,7 +40,8 @@
|
|||
(deftest key-is-weak
|
||||
(async
|
||||
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"
|
||||
(.. (keychain/get-encryption-key)
|
||||
(then (fn [_]
|
||||
|
@ -54,7 +55,8 @@
|
|||
(deftest safe-key-is-not-valid
|
||||
(async
|
||||
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"
|
||||
(.. (keychain/safe-get-encryption-key)
|
||||
(then (fn [k]
|
||||
|
@ -67,7 +69,8 @@
|
|||
(deftest safe-key-is-nil
|
||||
(async
|
||||
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"
|
||||
(.. (keychain/safe-get-encryption-key)
|
||||
(then (fn [k]
|
||||
|
|
Loading…
Reference in New Issue