authelia/internal/configuration/validator/password_policy.go
Manuel Nuñez 8659ba394d
feat(authentication): password policy (#2723)
Implement a password policy with visual feedback in the web portal.

Co-authored-by: Manuel Nuñez <@mind-ar>
Co-authored-by: James Elliott <james-d-elliott@users.noreply.github.com>
2022-04-03 08:32:57 +10:00

34 lines
1.3 KiB
Go

package validator
import (
"errors"
"github.com/authelia/authelia/v4/internal/configuration/schema"
"github.com/authelia/authelia/v4/internal/utils"
)
// ValidatePasswordPolicy validates and update Password Policy configuration.
func ValidatePasswordPolicy(configuration *schema.PasswordPolicyConfiguration, validator *schema.StructValidator) {
if !utils.IsBoolCountLessThanN(1, true, configuration.Standard.Enabled, configuration.Zxcvbn.Enabled) {
validator.Push(errors.New("password_policy:only one password policy can be enabled at a time"))
}
if configuration.Standard.Enabled {
if configuration.Standard.MinLength == 0 {
configuration.Standard.MinLength = schema.DefaultPasswordPolicyConfiguration.Standard.MinLength
} else if configuration.Standard.MinLength < 0 {
validator.Push(errors.New("password_policy: min_length must be > 0"))
}
if configuration.Standard.MaxLength == 0 {
configuration.Standard.MaxLength = schema.DefaultPasswordPolicyConfiguration.Standard.MaxLength
}
} else if configuration.Zxcvbn.Enabled {
if configuration.Zxcvbn.MinScore == 0 {
configuration.Zxcvbn.MinScore = schema.DefaultPasswordPolicyConfiguration.Zxcvbn.MinScore
} else if configuration.Zxcvbn.MinScore < 0 || configuration.Zxcvbn.MinScore > 4 {
validator.Push(errors.New("min_score must be between 0 and 4"))
}
}
}