Skip to content

Commit

Permalink
fix: addressed @ilmotta's review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
clauxx committed Nov 22, 2023
1 parent ae6074b commit 29f3f60
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
18 changes: 10 additions & 8 deletions src/react_native/keychain.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@

(defn save-credentials
"Stores the credentials for the address to the Keychain"
[server username password callback]
(-> (.setInternetCredentials ^js react-native-keychain
(string/lower-case server)
username
password
keychain-secure-hardware
keychain-restricted-availability)
(.then callback)))
([server username password]
(save-credentials server username password identity))
([server username password callback]
(-> (.setInternetCredentials ^js react-native-keychain
(string/lower-case server)
username
password
keychain-secure-hardware
keychain-restricted-availability)
(.then callback))))

(defn get-credentials
"Gets the credentials for a specified server from the Keychain"
Expand Down
48 changes: 27 additions & 21 deletions src/status_im2/common/keychain/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@
(keychain/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.")))))
method)
(.catch (fn [err]
(log/error "Failed to save auth method in the keychain"
{:error err
:key-uid key-uid
:auth-method method}))))

(re-frame/reg-fx
:keychain/save-auth-method
Expand All @@ -79,7 +78,8 @@
(if can-save?
(keychain/get-credentials
(str key-uid "-auth")
#(callback (if % (oops/oget % "password") auth-method-none)))
(fn [value]
(callback (if value (oops/oget value "password") auth-method-none))))
(callback nil))))))

(defn save-user-password!
Expand All @@ -89,10 +89,11 @@
(defn get-user-password!
[key-uid callback]
(keychain/get-credentials key-uid
#(callback (when %
(-> %
(oops/oget "password")
(security/mask-data))))))
(fn [value]
(callback (when value
(-> value
(oops/oget "password")
(security/mask-data)))))))

(re-frame/reg-fx
:keychain/get-user-password
Expand All @@ -114,10 +115,11 @@
key-uid
;; NOTE: using the key-id as the password, but we don't really care about the
;; value, we only care that it's there
key-uid
#(when-not %
(log/error
(str "Error while setting up keychain migration")))))
key-uid)
(.catch (fn [error]
(log/error "Failed to get the keychain password migration flag"
{:error error
:key-uid key-uid}))))

(defn get-password-migration!
[key-uid callback]
Expand Down Expand Up @@ -145,15 +147,19 @@
;; flow, where the password arrives already hashed.
(re-frame/reg-fx
:keychain/password-hash-migration
(fn [{:keys [key-uid callback]}]
(fn [{:keys [key-uid callback]
:or {callback identity}}]
(-> (get-password-migration! key-uid identity)
(.then (fn [migrated?]
(if migrated?
(callback)
(-> (get-user-password! key-uid identity)
(.then #(security/hash-masked-password %))
(.then security/hash-masked-password)
(.then #(save-user-password! key-uid %))
(.then #(save-password-migration! key-uid))
(.then #(callback))
(.catch #(log/error "Failed to migrate the keychain password for " key-uid
"\nError: " %)))))))))
(.then callback)))))
(.catch (fn [err]
(log/error "Failed to migrate the keychain password"
{:error err
:key-uid key-uid
:event :keychain/password-hash-migration}))))))

0 comments on commit 29f3f60

Please sign in to comment.