From a10425e31802be1244178ef8ed3c9c5a57a67670 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Mon, 27 Mar 2023 12:49:35 +0100 Subject: [PATCH] Save biometrics --- .../multiaccounts/biometric/core.cljs | 22 ++++++++ src/status_im/multiaccounts/login/core.cljs | 2 +- src/status_im/utils/keychain/core.cljs | 50 +++++++++++-------- .../contexts/onboarding/events.cljs | 25 ++++++++-- 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/src/status_im/multiaccounts/biometric/core.cljs b/src/status_im/multiaccounts/biometric/core.cljs index 0037dc05f2..7741df8c2b 100644 --- a/src/status_im/multiaccounts/biometric/core.cljs +++ b/src/status_im/multiaccounts/biometric/core.cljs @@ -125,6 +125,28 @@ (fn [[cb options]] (authenticate-fx #(cb %) options))) +(re-frame/reg-fx + :biometric/enable-and-save-password + (fn [{:keys [key-uid + masked-password + on-success + on-error]}] + (-> (keychain/save-user-password! + key-uid + masked-password) + (.then + (fn [_] + (keychain/save-auth-method! + key-uid + keychain/auth-method-biometric))) + (.then + (fn [_] + (when on-success + (on-success)))) + (.catch (fn [error] + (when on-error + (on-error error))))))) + (rf/defn update-biometric [{db :db :as cofx} biometric-auth?] (let [key-uid (or (get-in db [:multiaccount :key-uid]) diff --git a/src/status_im/multiaccounts/login/core.cljs b/src/status_im/multiaccounts/login/core.cljs index 90de058dc6..d71ff11a04 100644 --- a/src/status_im/multiaccounts/login/core.cljs +++ b/src/status_im/multiaccounts/login/core.cljs @@ -480,7 +480,7 @@ (re-frame/dispatch [:syncing/pairing-completed]) (get db :onboarding-2/new-account?) - (re-frame/dispatch [:navigate-to :enable-notifications]) + (re-frame/dispatch [:onboarding-2/finalize-setup]) (get db :tos/accepted?) (re-frame/dispatch [:init-root :shell-stack]) diff --git a/src/status_im/utils/keychain/core.cljs b/src/status_im/utils/keychain/core.cljs index 75a5b99fe7..f405d94edc 100644 --- a/src/status_im/utils/keychain/core.cljs +++ b/src/status_im/utils/keychain/core.cljs @@ -155,19 +155,36 @@ (callback nil)))) (callback nil)))))) +(defn save-user-password! + [key-uid password] + (save-credentials + key-uid + key-uid + (security/safe-unmask-data password) + #(when-not % + (log/error + (str "Error while saving password." + " " + "The app will continue to work normally, " + "but you will have to login again next time you launch it."))))) + +(defn save-auth-method! + [key-uid method] + (save-credentials + (str key-uid "-auth") + key-uid + method + #(when-not % + (log/error + (str "Error while saving auth method." + " " + "The app will continue to work normally, " + "but you will have to login again next time you launch it."))))) + (re-frame/reg-fx :keychain/save-user-password - (fn [[key-uid password]] - (save-credentials - key-uid - key-uid - (security/safe-unmask-data password) - #(when-not % - (log/error - (str "Error while saving password." - " " - "The app will continue to work normally, " - "but you will have to login again next time you launch it.")))))) + (fn [[key-uid masked-password]] + (save-user-password! key-uid masked-password))) (re-frame/reg-fx :keychain/save-auth-method @@ -178,16 +195,7 @@ "method" method) (when-not (empty? key-uid) ; key-uid may be nil after restore from local pairing - (save-credentials - (str key-uid "-auth") - key-uid - method - #(when-not % - (log/error - (str "Error while saving auth method." - " " - "The app will continue to work normally, " - "but you will have to login again next time you launch it."))))))) + (save-auth-method! key-uid method)))) (re-frame/reg-fx :keychain/save-keycard-keys diff --git a/src/status_im2/contexts/onboarding/events.cljs b/src/status_im2/contexts/onboarding/events.cljs index f382402171..333607c648 100644 --- a/src/status_im2/contexts/onboarding/events.cljs +++ b/src/status_im2/contexts/onboarding/events.cljs @@ -1,6 +1,7 @@ (ns status-im2.contexts.onboarding.events (:require [utils.re-frame :as rf] + [taoensso.timbre :as log] [re-frame.core :as re-frame] [status-im.utils.types :as types] [status-im2.config :as config] @@ -101,9 +102,7 @@ :previewPrivacy config/blank-preview?}] {effect request :dispatch [:navigate-to :generating-keys] - :db (-> db - (dissoc :onboarding-2/profile) - (assoc :onboarding-2/new-account? true))})) + :db (assoc db :onboarding-2/new-account? true)})) (rf/defn on-delete-profile-success {:events [:onboarding-2/on-delete-profile-success]} @@ -151,3 +150,23 @@ ;; Restart the flow {:db (dissoc db :onboarding-2/profile) :dispatch [:navigate-to :create-profile]}) + +(rf/defn onboarding-new-account-finalize-setup + {:events [:onboarding-2/finalize-setup]} + [{:keys [db]}] + (let [masked-password (get-in db [:onboarding-2/profile :password]) + key-uid (get-in db [:multiaccount :key-uid]) + biometric-enabled? (= + constants/auth-method-biometric + (get-in db [:onboarding-2/profile :auth-method]))] + + (cond-> {:db (dissoc db :onboarding-2/profile) + :dispatch [:navigate-to :enable-notifications]} + biometric-enabled? + (assoc :biometric/enable-and-save-password + {:key-uid key-uid + :masked-password masked-password + :on-success #(log/debug "successfully saved biometric") + :on-error #(log/error "failed to save biometrics" + {:key-uid key-uid + :error %})}))))