2022-04-03 00:48:26 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewPasswordPolicyProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2023-01-26 02:23:47 +00:00
|
|
|
have schema.PasswordPolicy
|
2022-04-03 00:48:26 +00:00
|
|
|
expected PasswordPolicyProvider
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "ShouldReturnUnconfiguredProvider",
|
2023-01-26 02:23:47 +00:00
|
|
|
have: schema.PasswordPolicy{},
|
2022-04-15 09:30:51 +00:00
|
|
|
expected: &StandardPasswordPolicyProvider{},
|
2022-04-03 00:48:26 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-15 09:30:51 +00:00
|
|
|
desc: "ShouldReturnProviderWhenZxcvbn",
|
2023-01-26 02:23:47 +00:00
|
|
|
have: schema.PasswordPolicy{ZXCVBN: schema.PasswordPolicyZXCVBN{Enabled: true, MinScore: 10}},
|
2022-04-15 09:30:51 +00:00
|
|
|
expected: &ZXCVBNPasswordPolicyProvider{minScore: 10},
|
2022-04-03 00:48:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldReturnConfiguredProviderWithMin",
|
2023-01-26 02:23:47 +00:00
|
|
|
have: schema.PasswordPolicy{Standard: schema.PasswordPolicyStandard{Enabled: true, MinLength: 8}},
|
2022-04-15 09:30:51 +00:00
|
|
|
expected: &StandardPasswordPolicyProvider{min: 8},
|
2022-04-03 00:48:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldReturnConfiguredProviderWitHMinMax",
|
2023-01-26 02:23:47 +00:00
|
|
|
have: schema.PasswordPolicy{Standard: schema.PasswordPolicyStandard{Enabled: true, MinLength: 8, MaxLength: 100}},
|
2022-04-15 09:30:51 +00:00
|
|
|
expected: &StandardPasswordPolicyProvider{min: 8, max: 100},
|
2022-04-03 00:48:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldReturnConfiguredProviderWithMinLowercase",
|
2023-01-26 02:23:47 +00:00
|
|
|
have: schema.PasswordPolicy{Standard: schema.PasswordPolicyStandard{Enabled: true, MinLength: 8, RequireLowercase: true}},
|
2022-04-15 09:30:51 +00:00
|
|
|
expected: &StandardPasswordPolicyProvider{min: 8, patterns: []regexp.Regexp{*regexp.MustCompile(`[a-z]+`)}},
|
2022-04-03 00:48:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldReturnConfiguredProviderWithMinLowercaseUppercase",
|
2023-01-26 02:23:47 +00:00
|
|
|
have: schema.PasswordPolicy{Standard: schema.PasswordPolicyStandard{Enabled: true, MinLength: 8, RequireLowercase: true, RequireUppercase: true}},
|
2022-04-15 09:30:51 +00:00
|
|
|
expected: &StandardPasswordPolicyProvider{min: 8, patterns: []regexp.Regexp{*regexp.MustCompile(`[a-z]+`), *regexp.MustCompile(`[A-Z]+`)}},
|
2022-04-03 00:48:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldReturnConfiguredProviderWithMinLowercaseUppercaseNumber",
|
2023-01-26 02:23:47 +00:00
|
|
|
have: schema.PasswordPolicy{Standard: schema.PasswordPolicyStandard{Enabled: true, MinLength: 8, RequireLowercase: true, RequireUppercase: true, RequireNumber: true}},
|
2022-04-15 09:30:51 +00:00
|
|
|
expected: &StandardPasswordPolicyProvider{min: 8, patterns: []regexp.Regexp{*regexp.MustCompile(`[a-z]+`), *regexp.MustCompile(`[A-Z]+`), *regexp.MustCompile(`[0-9]+`)}},
|
2022-04-03 00:48:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldReturnConfiguredProviderWithMinLowercaseUppercaseSpecial",
|
2023-01-26 02:23:47 +00:00
|
|
|
have: schema.PasswordPolicy{Standard: schema.PasswordPolicyStandard{Enabled: true, MinLength: 8, RequireLowercase: true, RequireUppercase: true, RequireSpecial: true}},
|
2022-04-15 09:30:51 +00:00
|
|
|
expected: &StandardPasswordPolicyProvider{min: 8, patterns: []regexp.Regexp{*regexp.MustCompile(`[a-z]+`), *regexp.MustCompile(`[A-Z]+`), *regexp.MustCompile(`[^a-zA-Z0-9]+`)}},
|
2022-04-03 00:48:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
actual := NewPasswordPolicyProvider(tc.have)
|
|
|
|
assert.Equal(t, tc.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPasswordPolicyProvider_Validate(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2023-01-26 02:23:47 +00:00
|
|
|
config schema.PasswordPolicy
|
2022-04-03 00:48:26 +00:00
|
|
|
have []string
|
|
|
|
expected []error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "ShouldValidateAllPasswords",
|
2023-01-26 02:23:47 +00:00
|
|
|
config: schema.PasswordPolicy{},
|
2022-04-03 00:48:26 +00:00
|
|
|
have: []string{"a", "1", "a really str0ng pass12nm3kjl12word@@#4"},
|
|
|
|
expected: []error{nil, nil, nil},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldValidatePasswordMinLength",
|
2023-01-26 02:23:47 +00:00
|
|
|
config: schema.PasswordPolicy{Standard: schema.PasswordPolicyStandard{Enabled: true, MinLength: 8}},
|
2022-04-03 00:48:26 +00:00
|
|
|
have: []string{"a", "b123", "1111111", "aaaaaaaa", "1o23nm1kio2n3k12jn"},
|
|
|
|
expected: []error{errPasswordPolicyNoMet, errPasswordPolicyNoMet, errPasswordPolicyNoMet, nil, nil},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldValidatePasswordMaxLength",
|
2023-01-26 02:23:47 +00:00
|
|
|
config: schema.PasswordPolicy{Standard: schema.PasswordPolicyStandard{Enabled: true, MaxLength: 30}},
|
2022-04-03 00:48:26 +00:00
|
|
|
have: []string{
|
|
|
|
"a1234567894654wkjnkjasnskjandkjansdkjnas",
|
|
|
|
"012345678901234567890123456789a",
|
|
|
|
"0123456789012345678901234567890123456789",
|
|
|
|
"012345678901234567890123456789",
|
|
|
|
"1o23nm1kio2n3k12jn",
|
|
|
|
},
|
|
|
|
expected: []error{errPasswordPolicyNoMet, errPasswordPolicyNoMet, errPasswordPolicyNoMet, nil, nil},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldValidatePasswordAdvancedLowerUpperMin8",
|
2023-01-26 02:23:47 +00:00
|
|
|
config: schema.PasswordPolicy{Standard: schema.PasswordPolicyStandard{Enabled: true, MinLength: 8, RequireLowercase: true, RequireUppercase: true}},
|
2022-04-03 00:48:26 +00:00
|
|
|
have: []string{"a", "b123", "1111111", "aaaaaaaa", "1o23nm1kio2n3k12jn", "ANJKJQ@#NEK!@#NJK!@#", "qjik2nkjAkjlmn123"},
|
|
|
|
expected: []error{errPasswordPolicyNoMet, errPasswordPolicyNoMet, errPasswordPolicyNoMet, errPasswordPolicyNoMet, errPasswordPolicyNoMet, errPasswordPolicyNoMet, nil},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ShouldValidatePasswordAdvancedAllMax100Min8",
|
2023-01-26 02:23:47 +00:00
|
|
|
config: schema.PasswordPolicy{Standard: schema.PasswordPolicyStandard{Enabled: true, MinLength: 8, MaxLength: 100, RequireLowercase: true, RequireUppercase: true, RequireNumber: true, RequireSpecial: true}},
|
2022-04-03 00:48:26 +00:00
|
|
|
have: []string{
|
|
|
|
"a",
|
|
|
|
"b123",
|
|
|
|
"1111111",
|
|
|
|
"aaaaaaaa",
|
|
|
|
"1o23nm1kio2n3k12jn",
|
|
|
|
"ANJKJQ@#NEK!@#NJK!@#",
|
|
|
|
"qjik2nkjAkjlmn123",
|
|
|
|
"qjik2n@jAkjlmn123",
|
|
|
|
"qjik2n@jAkjlmn123qjik2n@jAkjlmn123qjik2n@jAkjlmn123qjik2n@jAkjlmn123qjik2n@jAkjlmn123qjik2n@jAkjlmn123",
|
|
|
|
},
|
|
|
|
expected: []error{
|
|
|
|
errPasswordPolicyNoMet,
|
|
|
|
errPasswordPolicyNoMet,
|
|
|
|
errPasswordPolicyNoMet,
|
|
|
|
errPasswordPolicyNoMet,
|
|
|
|
errPasswordPolicyNoMet,
|
|
|
|
errPasswordPolicyNoMet,
|
|
|
|
errPasswordPolicyNoMet,
|
|
|
|
nil,
|
|
|
|
errPasswordPolicyNoMet,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
require.Equal(t, len(tc.have), len(tc.expected))
|
|
|
|
for i := 0; i < len(tc.have); i++ {
|
|
|
|
provider := NewPasswordPolicyProvider(tc.config)
|
|
|
|
t.Run(tc.have[i], func(t *testing.T) {
|
|
|
|
assert.Equal(t, tc.expected[i], provider.Check(tc.have[i]))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|