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
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateTOTP validates and update 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
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.TOTP.Algorithm == "" {
|
|
|
|
config.TOTP.Algorithm = schema.DefaultTOTPConfiguration.Algorithm
|
2021-12-01 12:11:29 +00:00
|
|
|
} else {
|
2022-02-28 03:15:01 +00:00
|
|
|
config.TOTP.Algorithm = strings.ToUpper(config.TOTP.Algorithm)
|
2021-12-01 12:11:29 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if !utils.IsStringInSlice(config.TOTP.Algorithm, schema.TOTPPossibleAlgorithms) {
|
2023-04-13 10:58:18 +00:00
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidAlgorithm, strJoinOr(schema.TOTPPossibleAlgorithms), config.TOTP.Algorithm))
|
2021-12-01 12:11:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.TOTP.Period == 0 {
|
|
|
|
config.TOTP.Period = schema.DefaultTOTPConfiguration.Period
|
|
|
|
} else if config.TOTP.Period < 15 {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidPeriod, config.TOTP.Period))
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-02-28 03:15:01 +00:00
|
|
|
if config.TOTP.Digits == 0 {
|
|
|
|
config.TOTP.Digits = schema.DefaultTOTPConfiguration.Digits
|
|
|
|
} else if config.TOTP.Digits != 6 && config.TOTP.Digits != 8 {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidDigits, config.TOTP.Digits))
|
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
|
|
|
}
|