-
Notifications
You must be signed in to change notification settings - Fork 4
/
password_policies.go
39 lines (33 loc) · 1.15 KB
/
password_policies.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package passhash
import (
"errors"
"fmt"
"unicode/utf8"
)
// PasswordPolicy is an interface used to determine if a password is acceptable. e.g. meets the given policy
type PasswordPolicy interface {
PasswordAcceptable(string) error
}
// AtLeastNRunes is a PasswordPolicy that ensures that the password is at least N runes in length
type AtLeastNRunes struct {
N int
}
// PasswordAcceptable accepts passwords that are at least N runes in length
func (pp AtLeastNRunes) PasswordAcceptable(password string) error {
if utf8.RuneCountInString(password) < pp.N {
return fmt.Errorf("Password must be at least %d characters in length", pp.N)
}
return nil
}
// NotCommonPasswordNaive is a PasswordPolicy that ensures that the password is not a common password.
// The method of checking is naive in that only exact password matches are rejected
type NotCommonPasswordNaive struct {
CommonPasswords map[string]bool
}
// PasswordAcceptable accepts passwords that are not common passwords
func (pp NotCommonPasswordNaive) PasswordAcceptable(password string) error {
if pp.CommonPasswords[password] {
return errors.New("Password is a common password")
}
return nil
}