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