diff --git a/config.go b/config.go index d8f3ec6..1cc689b 100644 --- a/config.go +++ b/config.go @@ -88,13 +88,6 @@ func WithAlgorithm(algo Algorithm) Option { } } -// WithCheckHIBP enables the HIBP check for newly generated passwords -func WithCheckHIBP() Option { - return func(config *Config) { - config.CheckHIBP = true - } -} - // WithExcludeChars sets a list of characters to be excluded in the generated // passwords func WithExcludeChars(chars string) Option { @@ -117,6 +110,50 @@ func WithMinLength(length int64) Option { } } +// WithMinLowercase sets the minimum amount of lowercase characters that +// the generated password should contain +// +// CAVEAT: using to high values with this option, can lead to extraordinary +// calculation times, resulting in apg-go to never finish +func WithMinLowercase(amount int64) Option { + return func(config *Config) { + config.MinLowerCase = amount + } +} + +// WithMinNumeric sets the minimum amount of numeric characters that +// the generated password should contain +// +// CAVEAT: using to high values with this option, can lead to extraordinary +// calculation times, resulting in apg-go to never finish +func WithMinNumeric(amount int64) Option { + return func(config *Config) { + config.MinNumeric = amount + } +} + +// WithMinSpecial sets the minimum amount of special characters that +// the generated password should contain +// +// CAVEAT: using to high values with this option, can lead to extraordinary +// calculation times, resulting in apg-go to never finish +func WithMinSpecial(amount int64) Option { + return func(config *Config) { + config.MinSpecial = amount + } +} + +// WithMinUppercase sets the minimum amount of uppercase characters that +// the generated password should contain +// +// CAVEAT: using to high values with this option, can lead to extraordinary +// calculation times, resulting in apg-go to never finish +func WithMinUppercase(amount int64) Option { + return func(config *Config) { + config.MinUpperCase = amount + } +} + // WithMaxLength overrides the maximum password length func WithMaxLength(length int64) Option { return func(config *Config) { @@ -124,9 +161,9 @@ func WithMaxLength(length int64) Option { } } -// WithNumberPass overrides the amount of generated passwords setting -func WithNumberPass(amount int64) Option { +// WithModeMask overrides the default mode mask for the random algorithm +func WithModeMask(mask ModeMask) Option { return func(config *Config) { - config.NumberPass = amount + config.Mode = mask } } diff --git a/config_test.go b/config_test.go index c3f6a70..19a92ab 100644 --- a/config_test.go +++ b/config_test.go @@ -31,6 +31,10 @@ func TestNewConfig(t *testing.T) { t.Errorf("NewConfig() failed, expected number of passwords: %d, got: %d", DefaultNumberPass, c.NumberPass) } + if c.Mode != DefaultMode { + t.Errorf("NewConfig() failed, expected mode mask: %d, got: %d", + DefaultMode, c.Mode) + } } func TestWithAlgorithm(t *testing.T) { @@ -39,7 +43,7 @@ func TestWithAlgorithm(t *testing.T) { algo Algorithm want int }{ - {"Pronouncble passwords", AlgoPronounceable, 0}, + {"Pronounceable passwords", AlgoPronounceable, 0}, {"Random passwords", AlgoRandom, 1}, {"Coinflip", AlgoCoinFlip, 2}, {"Unsupported", AlgoUnsupported, 3}, @@ -63,18 +67,6 @@ func TestWithAlgorithm(t *testing.T) { } } -func TestWithCheckHIBP(t *testing.T) { - c := NewConfig(WithCheckHIBP()) - if c == nil { - t.Errorf("NewConfig(WithCheckHIBP()) failed, expected config pointer but got nil") - return - } - if c.CheckHIBP != true { - t.Errorf("NewConfig(WithCheckHIBP()) failed, expected min length: %t, got: %t", - true, c.CheckHIBP) - } -} - func TestWithExcludeChars(t *testing.T) { e := "abcdefg" c := NewConfig(WithExcludeChars(e)) @@ -83,7 +75,7 @@ func TestWithExcludeChars(t *testing.T) { return } if c.ExcludeChars != e { - t.Errorf("NewConfig(WithExcludeChars()) failed, expected min length: %s, got: %s", + t.Errorf("NewConfig(WithExcludeChars()) failed, expected chars: %s, got: %s", e, c.ExcludeChars) } } @@ -96,7 +88,7 @@ func TestWithFixedLength(t *testing.T) { return } if c.FixedLength != e { - t.Errorf("NewConfig(WithFixedLength()) failed, expected min length: %d, got: %d", + t.Errorf("NewConfig(WithFixedLength()) failed, expected fixed length: %d, got: %d", e, c.FixedLength) } } @@ -127,15 +119,67 @@ func TestWithMinLength(t *testing.T) { } } -func TestWithNumberPass(t *testing.T) { - var e int64 = 123 - c := NewConfig(WithNumberPass(e)) +func TestWithMinLowercase(t *testing.T) { + var e int64 = 2 + c := NewConfig(WithMinLowercase(e)) if c == nil { - t.Errorf("NewConfig(WithNumberPass()) failed, expected config pointer but got nil") + t.Errorf("NewConfig(WithMinLowercase()) failed, expected config pointer but got nil") return } - if c.NumberPass != e { - t.Errorf("NewConfig(WithNumberPass()) failed, expected number of passwords: %d, got: %d", - e, c.NumberPass) + if c.MinLowerCase != e { + t.Errorf("NewConfig(WithMinLowercase()) failed, expected min amount: %d, got: %d", + e, c.MinLowerCase) + } +} + +func TestWithMinNumeric(t *testing.T) { + var e int64 = 3 + c := NewConfig(WithMinNumeric(e)) + if c == nil { + t.Errorf("NewConfig(WithMinNumeric()) failed, expected config pointer but got nil") + return + } + if c.MinNumeric != e { + t.Errorf("NewConfig(WithMinNumeric()) failed, expected min amount: %d, got: %d", + e, c.MinNumeric) + } +} + +func TestWithMinSpecial(t *testing.T) { + var e int64 = 4 + c := NewConfig(WithMinSpecial(e)) + if c == nil { + t.Errorf("NewConfig(WithMinSpecial()) failed, expected config pointer but got nil") + return + } + if c.MinSpecial != e { + t.Errorf("NewConfig(WithMinSpecial()) failed, expected min amount: %d, got: %d", + e, c.MinSpecial) + } +} + +func TestWithMinUppercase(t *testing.T) { + var e int64 = 5 + c := NewConfig(WithMinUppercase(e)) + if c == nil { + t.Errorf("NewConfig(WithMinUppercase()) failed, expected config pointer but got nil") + return + } + if c.MinUpperCase != e { + t.Errorf("NewConfig(WithMinUppercase()) failed, expected min amount: %d, got: %d", + e, c.MinUpperCase) + } +} + +func TestWithModeMask(t *testing.T) { + e := DefaultMode + c := NewConfig(WithModeMask(e)) + if c == nil { + t.Errorf("NewConfig(WithModeMask()) failed, expected config pointer but got nil") + return + } + if c.Mode != e { + t.Errorf("NewConfig(WithModeMask()) failed, expected mask: %d, got: %d", + e, c.Mode) } } diff --git a/random_test.go b/random_test.go index e537123..3528783 100644 --- a/random_test.go +++ b/random_test.go @@ -416,8 +416,8 @@ func TestCheckMinimumRequirements(t *testing.T) { } func TestGenerateRandom(t *testing.T) { - config := NewConfig(WithAlgorithm(AlgoRandom), WithNumberPass(1), - WithMinLength(1), WithMaxLength(1)) + config := NewConfig(WithAlgorithm(AlgoRandom), WithMinLength(1), + WithMaxLength(1)) config.MinNumeric = 1 generator := New(config) pw, err := generator.generateRandom()