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.
|
2021-12-01 19:11:29 +07:00
|
|
|
func ValidateTOTP(configuration *schema.Configuration, validator *schema.StructValidator) {
|
|
|
|
if configuration.TOTP == nil {
|
|
|
|
configuration.TOTP = &schema.DefaultTOTPConfiguration
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.TOTP.Issuer == "" {
|
|
|
|
configuration.TOTP.Issuer = schema.DefaultTOTPConfiguration.Issuer
|
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.TOTP.Algorithm == "" {
|
|
|
|
configuration.TOTP.Algorithm = schema.DefaultTOTPConfiguration.Algorithm
|
|
|
|
} else {
|
|
|
|
configuration.TOTP.Algorithm = strings.ToUpper(configuration.TOTP.Algorithm)
|
|
|
|
|
|
|
|
if !utils.IsStringInSlice(configuration.TOTP.Algorithm, schema.TOTPPossibleAlgorithms) {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidAlgorithm, configuration.TOTP.Algorithm, strings.Join(schema.TOTPPossibleAlgorithms, ", ")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.TOTP.Period == 0 {
|
|
|
|
configuration.TOTP.Period = schema.DefaultTOTPConfiguration.Period
|
|
|
|
} else if configuration.TOTP.Period < 15 {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidPeriod, configuration.TOTP.Period))
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
if configuration.TOTP.Digits == 0 {
|
|
|
|
configuration.TOTP.Digits = schema.DefaultTOTPConfiguration.Digits
|
|
|
|
} else if configuration.TOTP.Digits != 6 && configuration.TOTP.Digits != 8 {
|
|
|
|
validator.Push(fmt.Errorf(errFmtTOTPInvalidDigits, configuration.TOTP.Digits))
|
2020-03-25 08:48:20 +07:00
|
|
|
}
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
if configuration.TOTP.Skew == nil {
|
|
|
|
configuration.TOTP.Skew = schema.DefaultTOTPConfiguration.Skew
|
2020-03-25 08:48:20 +07:00
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|