Skip to content

Commit

Permalink
fix: password-strength to be consistent for create/change password
Browse files Browse the repository at this point in the history
  • Loading branch information
seanstrom authored and VolodLytvynenko committed Nov 12, 2024
1 parent 4b34682 commit de0b073
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
28 changes: 28 additions & 0 deletions src/status_im/common/validation/password.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns status-im.common.validation.password
(:require
[status-im.constants :as constants]
[utils.string :as utils.string]))

(defn validate-short-enough?
[password]
(utils.string/at-least-n-chars? password
constants/new-password-min-length))

(defn validate-long-enough?
[password]
(and (validate-short-enough? password)
(utils.string/at-most-n-chars? password
constants/new-password-max-length)))

(defn validate
[password]
(let [validations (juxt
utils.string/has-lower-case?
utils.string/has-upper-case?
utils.string/has-numbers?
utils.string/has-symbols?
validate-short-enough?
validate-long-enough?)]
(->> password
validations
(zipmap constants/password-tips))))
9 changes: 8 additions & 1 deletion src/status_im/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@
(def ^:const default-number-of-messages 20)
(def ^:const default-number-of-pin-messages 3)

(def ^:const password-tips [:lower-case? :upper-case? :numbers? :symbols?])
(def ^:const password-tips
[:lower-case?
:upper-case?
:numbers?
:symbols?
:long-enough?
:short-enough?])

(def ^:const strength-status
{1 :very-weak
2 :weak
Expand Down
18 changes: 3 additions & 15 deletions src/status_im/contexts/onboarding/create_password/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
[react-native.safe-area :as safe-area]
[status-im.common.floating-button-page.view :as floating-button]
[status-im.common.password-with-hint.view :as password-with-hint]
[status-im.common.validation.password :as password]
[status-im.constants :as constants]
[status-im.contexts.onboarding.create-password.style :as style]
[utils.i18n :as i18n]
[utils.re-frame :as rf]
[utils.security.core :as security]
[utils.string :as utils.string]))
[utils.security.core :as security]))

(defn header
[]
Expand Down Expand Up @@ -82,18 +82,6 @@
[quo/tips {:completed? symbols?}
(i18n/label :t/password-creation-tips-4)]]])

(defn validate-password
[password]
(let [validations (juxt utils.string/has-lower-case?
utils.string/has-upper-case?
utils.string/has-numbers?
utils.string/has-symbols?
#(utils.string/at-least-n-chars? % constants/new-password-min-length)
#(utils.string/at-most-n-chars? % constants/new-password-max-length))]
(->> password
validations
(zipmap (conj constants/password-tips :long-enough? :short-enough?)))))

(defn calc-password-strength
[validations]
(->> (vals validations)
Expand All @@ -105,7 +93,7 @@
(rn/use-memo
(fn []
(let [{:keys [long-enough? short-enough?]
:as validations} (validate-password password)]
:as validations} (password/validate password)]
{:password-long-enough? long-enough?
:password-short-enough? short-enough?
:password-validations validations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
[quo.core :as quo]
[react-native.core :as rn]
[status-im.common.password-with-hint.view :as password-with-hint]
[status-im.common.validation.password :as password]
[status-im.constants :as constant]
[status-im.contexts.profile.settings.screens.password.change-password.events]
[status-im.contexts.profile.settings.screens.password.change-password.header :as header]
[status-im.contexts.profile.settings.screens.password.change-password.style :as style]
[utils.i18n :as i18n]
[utils.re-frame :as rf]
[utils.security.core :as security]
[utils.string :as utils.string]))
[utils.security.core :as security]))

(defn- calc-password-strength
[validations]
Expand All @@ -36,12 +36,7 @@
[quo/tips {:completed? symbols?}
(i18n/label :t/password-creation-tips-4)]]]))

(defn- password-validations
[password]
{:lower-case? (utils.string/has-lower-case? password)
:upper-case? (utils.string/has-upper-case? password)
:numbers? (utils.string/has-numbers? password)
:symbols? (utils.string/has-symbols? password)})
(def not-blank? (complement string/blank?))

(defn view
[]
Expand All @@ -52,15 +47,8 @@
[focused? set-focused] (rn/use-state false)
[show-validation? set-show-validation] (rn/use-state false)

;; validations
not-blank? (complement string/blank?)
validations (password-validations password)
long-enough? (utils.string/at-least-n-chars?
password
constant/new-password-min-length)
short-enough? (utils.string/at-most-n-chars?
password
constant/new-password-max-length)
{:keys [long-enough? short-enough?]
:as validations} (password/validate password)
empty-password? (string/blank? password)
same-passwords? (and (not empty-password?)
(= password repeat-password))
Expand Down

0 comments on commit de0b073

Please sign in to comment.