2019-04-25 04:52:08 +07:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
2020-03-25 08:48:20 +07:00
|
|
|
"fmt"
|
2021-12-01 19:11:29 +07:00
|
|
|
"strings"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2021-12-01 19:11:29 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateTOTP validates and update TOTP configuration.
|
2022-02-28 10:15:01 +07:00
|
|
|
func ValidateTOTP(config *schema.Configuration, validator *schema.StructValidator) {
|
|
|
|
if config.TOTP.Issuer == "" {
|
|
|
|
config.TOTP.Issuer = schema.DefaultTOTPConfiguration.Issuer
|
2021-12-01 19:11:29 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.TOTP.Algorithm == "" {
|
|
|
|
config.TOTP.Algorithm = schema.DefaultTOTPConfiguration.Algorithm
|
2021-12-01 19:11:29 +07:00
|
|
|
} else {
|
2022-02-28 10:15:01 +07:00
|
|
|
config.TOTP.Algorithm = strings.ToUpper(config.TOTP.Algorithm)
|
2021-12-01 19:11:29 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if !utils.IsStringInSlice(config.TOTP.Algorithm, schema.TOTPPossibleAlgorithms) {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidAlgorithm, strings.Join(schema.TOTPPossibleAlgorithms, "', '"), config.TOTP.Algorithm))
|
2021-12-01 19:11:29 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07: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-25 04:52:08 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2022-02-28 10:15:01 +07: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 08:48:20 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.TOTP.Skew == nil {
|
|
|
|
config.TOTP.Skew = schema.DefaultTOTPConfiguration.Skew
|
2020-03-25 08:48:20 +07:00
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|