2020-01-21 20:56:44 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
2021-12-01 12:11:29 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
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
|
|
|
)
|
|
|
|
|
2020-03-25 01:48:20 +00:00
|
|
|
func TestShouldSetDefaultTOTPValues(t *testing.T) {
|
2020-01-21 20:56:44 +00:00
|
|
|
validator := schema.NewStructValidator()
|
2021-12-01 12:11:29 +00:00
|
|
|
config := &schema.Configuration{
|
2022-03-03 11:20:43 +00:00
|
|
|
TOTP: schema.TOTPConfiguration{},
|
2021-12-01 12:11:29 +00:00
|
|
|
}
|
2020-01-21 20:56:44 +00:00
|
|
|
|
2021-12-01 12:11:29 +00:00
|
|
|
ValidateTOTP(config, validator)
|
2020-01-21 20:56:44 +00:00
|
|
|
|
|
|
|
require.Len(t, validator.Errors(), 0)
|
2021-12-01 12:11:29 +00:00
|
|
|
assert.Equal(t, "Authelia", config.TOTP.Issuer)
|
|
|
|
assert.Equal(t, schema.DefaultTOTPConfiguration.Algorithm, config.TOTP.Algorithm)
|
|
|
|
assert.Equal(t, schema.DefaultTOTPConfiguration.Skew, config.TOTP.Skew)
|
|
|
|
assert.Equal(t, schema.DefaultTOTPConfiguration.Period, config.TOTP.Period)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNormalizeTOTPAlgorithm(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
|
|
|
|
config := &schema.Configuration{
|
2022-03-03 11:20:43 +00:00
|
|
|
TOTP: schema.TOTPConfiguration{
|
2021-12-01 12:11:29 +00:00
|
|
|
Algorithm: "sha1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidateTOTP(config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
|
|
|
assert.Equal(t, "SHA1", config.TOTP.Algorithm)
|
2020-03-25 01:48:20 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 12:11:29 +00:00
|
|
|
func TestShouldRaiseErrorWhenInvalidTOTPAlgorithm(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
|
|
|
|
config := &schema.Configuration{
|
2022-03-03 11:20:43 +00:00
|
|
|
TOTP: schema.TOTPConfiguration{
|
2021-12-01 12:11:29 +00:00
|
|
|
Algorithm: "sha3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidateTOTP(config, validator)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-12-01 12:11:29 +00:00
|
|
|
require.Len(t, validator.Errors(), 1)
|
2022-02-28 03:15:01 +00:00
|
|
|
assert.EqualError(t, validator.Errors()[0], fmt.Sprintf(errFmtTOTPInvalidAlgorithm, strings.Join(schema.TOTPPossibleAlgorithms, "', '"), "SHA3"))
|
2021-12-01 12:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseErrorWhenInvalidTOTPValues(t *testing.T) {
|
2020-03-25 01:48:20 +00:00
|
|
|
validator := schema.NewStructValidator()
|
2021-12-01 12:11:29 +00:00
|
|
|
config := &schema.Configuration{
|
2022-03-03 11:20:43 +00:00
|
|
|
TOTP: schema.TOTPConfiguration{
|
2021-12-01 12:11:29 +00:00
|
|
|
Period: 5,
|
|
|
|
Digits: 20,
|
|
|
|
},
|
2020-03-25 01:48:20 +00:00
|
|
|
}
|
2021-12-01 12:11:29 +00:00
|
|
|
|
|
|
|
ValidateTOTP(config, validator)
|
|
|
|
|
|
|
|
require.Len(t, validator.Errors(), 2)
|
|
|
|
assert.EqualError(t, validator.Errors()[0], fmt.Sprintf(errFmtTOTPInvalidPeriod, 5))
|
|
|
|
assert.EqualError(t, validator.Errors()[1], fmt.Sprintf(errFmtTOTPInvalidDigits, 20))
|
2020-01-21 20:56:44 +00:00
|
|
|
}
|