2022-04-02 22:32:57 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
2022-04-08 23:21:49 +00:00
|
|
|
"fmt"
|
2022-04-02 22:32:57 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidatePasswordPolicy validates and update Password Policy configuration.
|
2022-04-08 23:21:49 +00:00
|
|
|
func ValidatePasswordPolicy(config *schema.PasswordPolicyConfiguration, validator *schema.StructValidator) {
|
|
|
|
if !utils.IsBoolCountLessThanN(1, true, config.Standard.Enabled, config.ZXCVBN.Enabled) {
|
|
|
|
validator.Push(fmt.Errorf(errPasswordPolicyMultipleDefined))
|
2022-04-02 22:32:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 23:21:49 +00:00
|
|
|
if config.Standard.Enabled {
|
|
|
|
if config.Standard.MinLength == 0 {
|
|
|
|
config.Standard.MinLength = schema.DefaultPasswordPolicyConfiguration.Standard.MinLength
|
|
|
|
} else if config.Standard.MinLength < 0 {
|
2022-04-15 09:30:51 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtPasswordPolicyStandardMinLengthNotGreaterThanZero, config.Standard.MinLength))
|
2022-04-02 22:32:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 23:21:49 +00:00
|
|
|
if config.Standard.MaxLength == 0 {
|
|
|
|
config.Standard.MaxLength = schema.DefaultPasswordPolicyConfiguration.Standard.MaxLength
|
2022-04-02 22:32:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 09:30:51 +00:00
|
|
|
|
|
|
|
if config.ZXCVBN.Enabled {
|
|
|
|
switch {
|
|
|
|
case config.ZXCVBN.MinScore == 0:
|
|
|
|
config.ZXCVBN.MinScore = schema.DefaultPasswordPolicyConfiguration.ZXCVBN.MinScore
|
|
|
|
case config.ZXCVBN.MinScore < 0, config.ZXCVBN.MinScore > 4:
|
|
|
|
validator.Push(fmt.Errorf(errFmtPasswordPolicyZXCVBNMinScoreInvalid, config.ZXCVBN.MinScore))
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 22:32:57 +00:00
|
|
|
}
|