2020-07-29 21:11:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
2020-11-22 16:36:43 +00:00
|
|
|
// Validator allows for validation of passwords.
|
2020-07-29 21:11:28 +00:00
|
|
|
type Validator struct {
|
|
|
|
minLength, upper, lower, number, special int
|
|
|
|
criteria ValidatorConf
|
|
|
|
}
|
|
|
|
|
|
|
|
type ValidatorConf map[string]int
|
|
|
|
|
|
|
|
func (vd *Validator) init(criteria ValidatorConf) {
|
|
|
|
vd.criteria = criteria
|
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// This isn't used, its for swagger
|
|
|
|
type PasswordValidation struct {
|
2020-10-20 22:00:30 +00:00
|
|
|
Characters bool `json:"length,omitempty"` // Number of characters
|
|
|
|
Lowercase bool `json:"lowercase,omitempty"` // Number of lowercase characters
|
|
|
|
Uppercase bool `json:"uppercase,omitempty"` // Number of uppercase characters
|
|
|
|
Numbers bool `json:"number,omitempty"` // Number of numbers
|
|
|
|
Specials bool `json:"special,omitempty"` // Number of special characters
|
2020-09-24 16:51:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 21:11:28 +00:00
|
|
|
func (vd *Validator) validate(password string) map[string]bool {
|
2020-07-31 11:48:37 +00:00
|
|
|
count := map[string]int{}
|
2020-09-24 16:51:13 +00:00
|
|
|
for key := range vd.criteria {
|
2020-07-29 21:11:28 +00:00
|
|
|
count[key] = 0
|
|
|
|
}
|
|
|
|
for _, c := range password {
|
2020-10-20 22:00:30 +00:00
|
|
|
count["length"] += 1
|
2020-07-29 21:11:28 +00:00
|
|
|
if unicode.IsUpper(c) {
|
2020-10-20 22:00:30 +00:00
|
|
|
count["uppercase"] += 1
|
2020-07-29 21:11:28 +00:00
|
|
|
} else if unicode.IsLower(c) {
|
2020-10-20 22:00:30 +00:00
|
|
|
count["lowercase"] += 1
|
2020-07-29 21:11:28 +00:00
|
|
|
} else if unicode.IsNumber(c) {
|
2020-10-22 16:50:40 +00:00
|
|
|
count["number"] += 1
|
2021-01-09 01:00:27 +00:00
|
|
|
} else if unicode.ToUpper(c) == unicode.ToLower(c) {
|
|
|
|
count["special"] += 1
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 11:48:37 +00:00
|
|
|
results := map[string]bool{}
|
2020-07-29 21:11:28 +00:00
|
|
|
for criterion, num := range count {
|
|
|
|
if num < vd.criteria[criterion] {
|
|
|
|
results[criterion] = false
|
2020-07-31 11:48:37 +00:00
|
|
|
} else {
|
|
|
|
results[criterion] = true
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2020-10-20 22:00:30 +00:00
|
|
|
func (vd *Validator) getCriteria() ValidatorConf {
|
|
|
|
criteria := ValidatorConf{}
|
|
|
|
for key, num := range vd.criteria {
|
|
|
|
if num != 0 {
|
|
|
|
criteria[key] = num
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-20 22:00:30 +00:00
|
|
|
return criteria
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|