diff --git a/src/status_im2/common/keychain/events.cljs b/src/status_im2/common/keychain/events.cljs index e417783cd393..9128b53555d2 100644 --- a/src/status_im2/common/keychain/events.cljs +++ b/src/status_im2/common/keychain/events.cljs @@ -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] @@ -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: " %))))))))) diff --git a/src/status_im2/contexts/profile/login/events.cljs b/src/status_im2/contexts/profile/login/events.cljs index df3d89b34048..5cd360fdf20a 100644 --- a/src/status_im2/contexts/profile/login/events.cljs +++ b/src/status_im2/contexts/profile/login/events.cljs @@ -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]}