2022-09-27 17:59:05 +08:00
|
|
|
package checker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2023-05-22 15:09:22 +08:00
|
|
|
"strings"
|
2022-09-27 17:59:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
levelD = iota
|
|
|
|
LevelC
|
|
|
|
LevelB
|
|
|
|
LevelA
|
|
|
|
LevelS
|
|
|
|
)
|
|
|
|
|
2023-05-22 15:09:22 +08:00
|
|
|
const (
|
|
|
|
PasswordCannotContainSpaces = "error.password.space_invalid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckPassword checks the password strength
|
|
|
|
func CheckPassword(password string) error {
|
|
|
|
if strings.Contains(password, " ") {
|
|
|
|
return fmt.Errorf(PasswordCannotContainSpaces)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
2023-05-22 15:09:22 +08:00
|
|
|
// TODO Currently there is no requirement for password strength
|
|
|
|
minLevel := 0
|
|
|
|
|
2022-10-21 12:00:50 +08:00
|
|
|
// The password strength level is initialized to D.
|
|
|
|
// The regular is used to verify the password strength.
|
|
|
|
// If the matching is successful, the password strength increases by 1
|
2022-11-01 15:25:44 +08:00
|
|
|
level := levelD
|
2022-09-27 17:59:05 +08:00
|
|
|
patternList := []string{`[0-9]+`, `[a-z]+`, `[A-Z]+`, `[~!@#$%^&*?_-]+`}
|
|
|
|
for _, pattern := range patternList {
|
2023-05-22 15:09:22 +08:00
|
|
|
match, _ := regexp.MatchString(pattern, password)
|
2022-09-27 17:59:05 +08:00
|
|
|
if match {
|
|
|
|
level++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 12:00:50 +08:00
|
|
|
// If the final password strength falls below the required minimum strength, return with an error
|
2022-09-27 17:59:05 +08:00
|
|
|
if level < minLevel {
|
2022-11-01 15:25:44 +08:00
|
|
|
return fmt.Errorf("the password does not satisfy the current policy requirements")
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|