Skip to content

Commit

Permalink
feat: Added new PasswordStrengthScore method
Browse files Browse the repository at this point in the history
By integrating `zxcvbn` module, it has been added a new method to get the specific password strength score information.
  • Loading branch information
noeliaSD committed Mar 22, 2022
1 parent 12ccff4 commit a12521e
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions mobile/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
signercore "github.com/ethereum/go-ethereum/signer/core"

"github.com/status-im/zxcvbn-go"
"github.com/status-im/zxcvbn-go/scoring"

"github.com/status-im/status-go/api"
"github.com/status-im/status-go/api/multiformat"
Expand Down Expand Up @@ -755,23 +756,53 @@ func ImageServerTLSCert() string {
return cert
}

type GetPasswordStrengthRequest struct {
Password string `json:"password"`
UserInputs []string `json:"userInputs"`
}

type PasswordScoreResponse struct {
Score int `json:"score"`
}

// GetPasswordStrength uses zxcvbn module and generates a JSON containing information about the quality of the given password
// (Entropy, CrackTime, CrackTimeDisplay, Score, MatchSequence and CalcTime).
// userInputs argument can be whatever list of strings like user's personal info or site-specific vocabulary that zxcvbn will
// make use to determine the result.
// For more details on usage see https://github.com/status-im/zxcvbn-go
func GetPasswordStrength(paramsJSON string) string {
var params struct {
Password string `json:"password"`
UserInputs []string `json:"userInputs"`
var requestParams GetPasswordStrengthRequest

err := json.Unmarshal([]byte(paramsJSON), &requestParams)
if err != nil {
return makeJSONResponse(err)
}

err := json.Unmarshal([]byte(paramsJSON), &params)
data, err := json.Marshal(zxcvbn.PasswordStrength(requestParams.Password, requestParams.UserInputs))
if err != nil {
return makeJSONResponse(fmt.Errorf("Error marshalling to json: %v", err))
}
return string(data)
}

// GetPasswordStrengthScore uses zxcvbn module and gets the score information about the given password.
// userInputs argument can be whatever list of strings like user's personal info or site-specific vocabulary that zxcvbn will
// make use to determine the result.
// For more details on usage see https://github.com/status-im/zxcvbn-go
func GetPasswordStrengthScore(paramsJSON string) string {
var requestParams GetPasswordStrengthRequest
var quality scoring.MinEntropyMatch

err := json.Unmarshal([]byte(paramsJSON), &requestParams)
if err != nil {
return makeJSONResponse(err)
}

data, err := json.Marshal(zxcvbn.PasswordStrength(params.Password, params.UserInputs))
quality = zxcvbn.PasswordStrength(requestParams.Password, requestParams.UserInputs)

data, err := json.Marshal(PasswordScoreResponse{
Score: quality.Score,
})
if err != nil {
return makeJSONResponse(fmt.Errorf("Error marshalling to json: %v", err))
}
Expand Down

0 comments on commit a12521e

Please sign in to comment.