2020-04-05 12:37:21 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2020-04-05 12:37:21 +00:00
|
|
|
)
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// ValidateRegulation validates and update regulator configuration.
|
2022-02-28 03:15:01 +00:00
|
|
|
func ValidateRegulation(config *schema.Configuration, validator *schema.StructValidator) {
|
|
|
|
if config.Regulation == nil {
|
|
|
|
config.Regulation = &schema.DefaultRegulationConfiguration
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Regulation.FindTime == "" {
|
|
|
|
config.Regulation.FindTime = schema.DefaultRegulationConfiguration.FindTime // 2 min.
|
2020-04-05 12:37:21 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.Regulation.BanTime == "" {
|
|
|
|
config.Regulation.BanTime = schema.DefaultRegulationConfiguration.BanTime // 5 min.
|
2020-04-05 12:37:21 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
findTime, err := utils.ParseDurationString(config.Regulation.FindTime)
|
2020-04-05 12:37:21 +00:00
|
|
|
if err != nil {
|
2022-02-28 03:15:01 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtRegulationParseDuration, "find_time", err))
|
2020-04-05 12:37:21 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
banTime, err := utils.ParseDurationString(config.Regulation.BanTime)
|
2020-04-05 12:37:21 +00:00
|
|
|
if err != nil {
|
2022-02-28 03:15:01 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtRegulationParseDuration, "ban_time", err))
|
2020-04-05 12:37:21 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
if findTime > banTime {
|
2022-02-28 03:15:01 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtRegulationFindTimeGreaterThanBanTime))
|
2020-04-05 12:37:21 +00:00
|
|
|
}
|
|
|
|
}
|