2019-04-24 21:52:08 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
2020-03-25 01:48:20 +00:00
|
|
|
"fmt"
|
2021-12-01 12:11:29 +00:00
|
|
|
"strings"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2021-12-01 12:11:29 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
2023-02-13 20:39:46 +00:00
|
|
|
// ValidateTOTP validates and updates TOTP configuration.
|
2022-02-28 03:15:01 +00:00
|
|
|
func ValidateTOTP(config *schema.Configuration, validator *schema.StructValidator) {
|
2022-04-07 23:01:01 +00:00
|
|
|
if config.TOTP.Disable {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.TOTP.Issuer == "" {
|
|
|
|
config.TOTP.Issuer = schema.DefaultTOTPConfiguration.Issuer
|
2021-12-01 12:11:29 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 20:39:46 +00:00
|
|
|
if config.TOTP.DefaultAlgorithm == "" {
|
|
|
|
config.TOTP.DefaultAlgorithm = schema.DefaultTOTPConfiguration.DefaultAlgorithm
|
2021-12-01 12:11:29 +00:00
|
|
|
} else {
|
2023-02-13 20:39:46 +00:00
|
|
|
config.TOTP.DefaultAlgorithm = strings.ToUpper(config.TOTP.DefaultAlgorithm)
|
2021-12-01 12:11:29 +00:00
|
|
|
|
2023-02-13 20:39:46 +00:00
|
|
|
if !utils.IsStringInSlice(config.TOTP.DefaultAlgorithm, schema.TOTPPossibleAlgorithms) {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidAlgorithm, strings.Join(schema.TOTPPossibleAlgorithms, "', '"), config.TOTP.DefaultAlgorithm))
|
2021-12-01 12:11:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 20:39:46 +00:00
|
|
|
for i, algorithm := range config.TOTP.AllowedAlgorithms {
|
|
|
|
config.TOTP.AllowedAlgorithms[i] = strings.ToUpper(algorithm)
|
|
|
|
|
|
|
|
// TODO: Customize this error.
|
|
|
|
if !utils.IsStringInSlice(config.TOTP.AllowedAlgorithms[i], schema.TOTPPossibleAlgorithms) {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidAlgorithm, strings.Join(schema.TOTPPossibleAlgorithms, "', '"), config.TOTP.AllowedAlgorithms[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !utils.IsStringInSlice(config.TOTP.DefaultAlgorithm, config.TOTP.AllowedAlgorithms) {
|
|
|
|
config.TOTP.AllowedAlgorithms = append(config.TOTP.AllowedAlgorithms, config.TOTP.DefaultAlgorithm)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.TOTP.DefaultPeriod == 0 {
|
|
|
|
config.TOTP.DefaultPeriod = schema.DefaultTOTPConfiguration.DefaultPeriod
|
|
|
|
} else if config.TOTP.DefaultPeriod < 15 {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidPeriod, config.TOTP.DefaultPeriod))
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasDefaultPeriod bool
|
|
|
|
|
|
|
|
for _, period := range config.TOTP.AllowedPeriods {
|
|
|
|
// TODO: Customize this error.
|
|
|
|
if period < 15 {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidPeriod, period))
|
|
|
|
}
|
|
|
|
|
|
|
|
if period == config.TOTP.DefaultPeriod {
|
|
|
|
hasDefaultPeriod = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasDefaultPeriod {
|
|
|
|
config.TOTP.AllowedPeriods = append(config.TOTP.AllowedPeriods, config.TOTP.DefaultPeriod)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.TOTP.DefaultDigits == 0 {
|
|
|
|
config.TOTP.DefaultDigits = schema.DefaultTOTPConfiguration.DefaultDigits
|
|
|
|
} else if config.TOTP.DefaultDigits != 6 && config.TOTP.DefaultDigits != 8 {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidDigits, config.TOTP.DefaultDigits))
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasDefaultDigits bool
|
|
|
|
|
|
|
|
for _, digits := range config.TOTP.AllowedDigits {
|
|
|
|
// TODO: Customize this error.
|
|
|
|
if digits != 6 && digits != 8 {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidDigits, config.TOTP.DefaultDigits))
|
|
|
|
}
|
|
|
|
|
|
|
|
if digits == config.TOTP.DefaultDigits {
|
|
|
|
hasDefaultDigits = true
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2023-02-13 20:39:46 +00:00
|
|
|
if !hasDefaultDigits {
|
|
|
|
config.TOTP.AllowedDigits = append(config.TOTP.AllowedDigits, config.TOTP.DefaultDigits)
|
2020-03-25 01:48:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.TOTP.Skew == nil {
|
|
|
|
config.TOTP.Skew = schema.DefaultTOTPConfiguration.Skew
|
2020-03-25 01:48:20 +00:00
|
|
|
}
|
2022-04-07 23:01:01 +00:00
|
|
|
|
|
|
|
if config.TOTP.SecretSize == 0 {
|
|
|
|
config.TOTP.SecretSize = schema.DefaultTOTPConfiguration.SecretSize
|
|
|
|
} else if config.TOTP.SecretSize < schema.TOTPSecretSizeMinimum {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidSecretSize, schema.TOTPSecretSizeMinimum, config.TOTP.SecretSize))
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|