refactor: password requirements variables;
refactor: password validation function moved to custom validators; refactor: adjusted model's validation fields
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"easywish/config"
|
||||
"regexp"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
@@ -46,6 +47,36 @@ func GetCustomHandlers() []CustomValidatorHandler {
|
||||
return regexp.MustCompile(`^[.]{1,75}$`).MatchString(username)
|
||||
}},
|
||||
|
||||
{
|
||||
FieldName: "password",
|
||||
Function: func(fl validator.FieldLevel) bool {
|
||||
password := fl.Field().String()
|
||||
cfg := config.GetConfig()
|
||||
|
||||
if cfg.PasswordMaxLength < len(password) || len(password) < cfg.PasswordMinLength {
|
||||
return false
|
||||
}
|
||||
|
||||
if cfg.PasswordCheckNumbers && !regexp.MustCompile(`\d+`).MatchString(password) {
|
||||
return false
|
||||
}
|
||||
|
||||
if cfg.PasswordCheckCases && !regexp.MustCompile(`^(?=.*[a-z])(?=.*[A-Z]).*$`).MatchString(password) ||
|
||||
cfg.PasswordCheckCharacters && !regexp.MustCompile(`[a-zA-Z]`).MatchString(password) {
|
||||
return false
|
||||
}
|
||||
|
||||
if cfg.PasswordCheckSymbols && !regexp.MustCompile(`[.,/;'[\]\-=_+{}:"<>?\/|!@#$%^&*()~]`).MatchString(password) {
|
||||
return false
|
||||
}
|
||||
|
||||
if cfg.PasswordCheckLeaked {
|
||||
// TODO: implement rockme check
|
||||
}
|
||||
|
||||
return true
|
||||
}},
|
||||
|
||||
}
|
||||
|
||||
return handlers
|
||||
|
||||
Reference in New Issue
Block a user