2020-01-21 20:56:44 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
2021-12-01 12:11:29 +00:00
|
|
|
"fmt"
|
2020-01-21 20:56:44 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2020-01-21 20:56:44 +00:00
|
|
|
)
|
|
|
|
|
2022-04-07 23:01:01 +00:00
|
|
|
func TestValidateTOTP(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
have schema.TOTPConfiguration
|
|
|
|
expected schema.TOTPConfiguration
|
|
|
|
errs []string
|
|
|
|
warns []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "ShouldSetDefaultTOTPValues",
|
|
|
|
expected: schema.DefaultTOTPConfiguration,
|
2021-12-01 12:11:29 +00:00
|
|
|
},
|
2022-04-07 23:01:01 +00:00
|
|
|
{
|
|
|
|
desc: "ShouldNotSetDefaultTOTPValuesWhenDisabled",
|
|
|
|
have: schema.TOTPConfiguration{Disable: true},
|
|
|
|
expected: schema.TOTPConfiguration{Disable: true},
|
2021-12-01 12:11:29 +00:00
|
|
|
},
|
2022-04-07 23:01:01 +00:00
|
|
|
{
|
|
|
|
desc: "ShouldNormalizeTOTPAlgorithm",
|
|
|
|
have: schema.TOTPConfiguration{
|
2023-02-13 20:39:46 +00:00
|
|
|
DefaultAlgorithm: digestSHA1,
|
|
|
|
DefaultDigits: 6,
|
|
|
|
DefaultPeriod: 30,
|
|
|
|
SecretSize: 32,
|
|
|
|
Skew: schema.DefaultTOTPConfiguration.Skew,
|
|
|
|
Issuer: "abc",
|
2022-04-07 23:01:01 +00:00
|
|
|
},
|
|
|
|
expected: schema.TOTPConfiguration{
|
2023-02-13 20:39:46 +00:00
|
|
|
DefaultAlgorithm: "SHA1",
|
|
|
|
DefaultDigits: 6,
|
|
|
|
DefaultPeriod: 30,
|
|
|
|
SecretSize: 32,
|
|
|
|
Skew: schema.DefaultTOTPConfiguration.Skew,
|
|
|
|
Issuer: "abc",
|
2022-04-07 23:01:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldRaiseErrorWhenInvalidTOTPAlgorithm",
|
|
|
|
have: schema.TOTPConfiguration{
|
2023-02-13 20:39:46 +00:00
|
|
|
DefaultAlgorithm: "sha3",
|
|
|
|
DefaultDigits: 6,
|
|
|
|
DefaultPeriod: 30,
|
|
|
|
SecretSize: 32,
|
|
|
|
Skew: schema.DefaultTOTPConfiguration.Skew,
|
|
|
|
Issuer: "abc",
|
2022-04-07 23:01:01 +00:00
|
|
|
},
|
2023-04-13 10:58:18 +00:00
|
|
|
errs: []string{
|
|
|
|
"totp: option 'algorithm' must be one of 'SHA1', 'SHA256', or 'SHA512' but it's configured as 'SHA3'",
|
|
|
|
},
|
2022-04-07 23:01:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldRaiseErrorWhenInvalidTOTPValue",
|
|
|
|
have: schema.TOTPConfiguration{
|
2023-02-13 20:39:46 +00:00
|
|
|
DefaultAlgorithm: "sha3",
|
|
|
|
DefaultPeriod: 5,
|
|
|
|
DefaultDigits: 20,
|
|
|
|
SecretSize: 10,
|
|
|
|
Skew: schema.DefaultTOTPConfiguration.Skew,
|
|
|
|
Issuer: "abc",
|
2022-04-07 23:01:01 +00:00
|
|
|
},
|
|
|
|
errs: []string{
|
2023-04-13 10:58:18 +00:00
|
|
|
"totp: option 'algorithm' must be one of 'SHA1', 'SHA256', or 'SHA512' but it's configured as 'SHA3'",
|
|
|
|
"totp: option 'period' option must be 15 or more but it's configured as '5'",
|
|
|
|
"totp: option 'digits' must be 6 or 8 but it's configured as '20'",
|
|
|
|
"totp: option 'secret_size' must be 20 or higher but it's configured as '10'",
|
2022-04-07 23:01:01 +00:00
|
|
|
},
|
2021-12-01 12:11:29 +00:00
|
|
|
},
|
2020-03-25 01:48:20 +00:00
|
|
|
}
|
2021-12-01 12:11:29 +00:00
|
|
|
|
2022-04-07 23:01:01 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := &schema.Configuration{TOTP: tc.have}
|
|
|
|
|
|
|
|
ValidateTOTP(config, validator)
|
|
|
|
|
|
|
|
errs := validator.Errors()
|
|
|
|
warns := validator.Warnings()
|
|
|
|
|
|
|
|
if len(tc.errs) == 0 {
|
|
|
|
assert.Len(t, errs, 0)
|
|
|
|
assert.Len(t, warns, 0)
|
|
|
|
assert.Equal(t, tc.expected.Disable, config.TOTP.Disable)
|
|
|
|
assert.Equal(t, tc.expected.Issuer, config.TOTP.Issuer)
|
2023-02-13 20:39:46 +00:00
|
|
|
assert.Equal(t, tc.expected.DefaultAlgorithm, config.TOTP.DefaultAlgorithm)
|
2022-04-07 23:01:01 +00:00
|
|
|
assert.Equal(t, tc.expected.Skew, config.TOTP.Skew)
|
2023-02-13 20:39:46 +00:00
|
|
|
assert.Equal(t, tc.expected.DefaultPeriod, config.TOTP.DefaultPeriod)
|
2022-04-07 23:01:01 +00:00
|
|
|
assert.Equal(t, tc.expected.SecretSize, config.TOTP.SecretSize)
|
|
|
|
} else {
|
|
|
|
expectedErrs := len(tc.errs)
|
|
|
|
|
|
|
|
require.Len(t, errs, expectedErrs)
|
|
|
|
|
|
|
|
for i := 0; i < expectedErrs; i++ {
|
|
|
|
t.Run(fmt.Sprintf("Err%d", i+1), func(t *testing.T) {
|
|
|
|
assert.EqualError(t, errs[i], tc.errs[i])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedWarns := len(tc.warns)
|
|
|
|
require.Len(t, warns, expectedWarns)
|
|
|
|
|
|
|
|
for i := 0; i < expectedWarns; i++ {
|
|
|
|
t.Run(fmt.Sprintf("Err%d", i+1), func(t *testing.T) {
|
|
|
|
assert.EqualError(t, warns[i], tc.warns[i])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-01-21 20:56:44 +00:00
|
|
|
}
|