Skip to content

Commit

Permalink
ref: moved migration side-effects outside the event
Browse files Browse the repository at this point in the history
  • Loading branch information
clauxx committed Nov 21, 2023
1 parent 5c271a0 commit 221e675
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
40 changes: 31 additions & 9 deletions src/status_im2/common/keychain/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,27 @@
(log/error
(str "Error while setting up keychain migration")))))

(re-frame/reg-fx
:keychain/get-migration-auth-hashed
(fn [[key-uid callback]]
(keychain/get-credentials
(str key-uid migration-server-suffix)
#(callback (boolean %)))))
(defn get-migration-auth-hashed!
[key-uid callback]
(keychain/get-credentials
(str key-uid migration-server-suffix)
#(callback (boolean %))))

(defn save-user-password!
[key-uid password]
(keychain/save-credentials key-uid key-uid (security/safe-unmask-data password) #()))

(defn get-user-password!
[key-uid callback]
(keychain/get-credentials key-uid
#(if %
(callback (security/mask-data (oops/oget % "password")))
(callback nil))))

(re-frame/reg-fx
:keychain/get-user-password
(fn [[key-uid callback]]
(keychain/get-credentials
key-uid
#(if % (callback (security/mask-data (oops/oget % "password"))) (callback nil)))))
(get-user-password! key-uid callback)))

(rf/defn get-user-password
[_ key-uid callback]
Expand All @@ -132,3 +136,21 @@
(.then #(save-migration-auth-hashed! key-uid))
(.then #(when on-success (on-success)))
(.catch #(when on-error (on-error %))))))

;; NOTE: migrating the plaintext password in the keychain
;; with the hashed one. Added due to the sync onboarding
;; flow, where the password arrives already hashed.
(re-frame/reg-fx
:keychain/password-hash-migration
(fn [{:keys [key-uid callback]}]
(-> (get-migration-auth-hashed! key-uid identity)
(.then (fn [pw-already-hashed?]
(if pw-already-hashed?
(callback)
(-> (get-user-password! key-uid identity)
(.then #(security/hash-masked-password %))
(.then #(save-user-password! key-uid %))
(.then #(save-migration-auth-hashed! key-uid))
(.then #(callback))
(.catch #(log/error "Failed to migrate the keychain password for " key-uid
"\nError: " %)))))))))
42 changes: 9 additions & 33 deletions src/status_im2/contexts/profile/login/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -160,42 +160,18 @@
{:keychain/get-auth-method [key-uid
#(rf/dispatch [:profile.login/get-auth-method-success % key-uid])]})

;; NOTE: replacing the plaintext password in the keychain with the hashed one
(rf/defn migrate-biometrics-keychain-password
{:events [:profile.login/migrate-biometrics-keychain-password]}
[_ key-uid callback]
{:keychain/get-user-password
[key-uid
(fn [password]
(-> password
security/hash-masked-password
(->> (keychain/save-user-password! key-uid))
(.then #(keychain/save-migration-auth-hashed! key-uid))
(.then #(callback))
(.catch #(log/error "Failed to migrate the keychain for " key-uid))))]})

(rf/defn check-biometrics-keychain-migration
{:events [:profile.login/check-biometrics-keychain-migration]}
[_ key-uid callback]
{:keychain/get-migration-auth-hashed
[key-uid
(fn [hashed?]
(if hashed?
(callback)
(rf/dispatch [:profile.login/migrate-biometrics-keychain-password key-uid callback])))]})

(rf/defn get-auth-method-success
{:events [:profile.login/get-auth-method-success]}
[{:keys [db] :as cofx} auth-method key-uid]
(rf/merge cofx
{:db (assoc db :auth-method auth-method)}
(when (= auth-method keychain/auth-method-biometric)
(check-biometrics-keychain-migration
key-uid
(fn []
(rf/dispatch [:biometric/authenticate
{:on-success #(rf/dispatch [:profile.login/biometric-success])
:on-fail #(rf/dispatch [:profile.login/biometric-auth-fail])}]))))))
(merge {:db (assoc db :auth-method auth-method)}
(when (= auth-method keychain/auth-method-biometric)
{:keychain/password-hash-migration
{:key-uid key-uid
:callback (fn []
(rf/dispatch [:biometric/authenticate
{:on-success #(rf/dispatch [:profile.login/biometric-success])
:on-fail #(rf/dispatch
[:profile.login/biometric-auth-fail])}]))}})))

(rf/defn biometric-auth-success
{:events [:profile.login/biometric-success]}
Expand Down

0 comments on commit 221e675

Please sign in to comment.