1
0
Fork 0

Tweak hashed password implementation

master
Philip O'Toole 7 years ago
parent 1abbe0fa4b
commit 6fae0d4cee

@ -18,7 +18,6 @@ type BasicAuther interface {
type Credential struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Hashed *bool `json:"hashed,omitempty"`
Perms []string `json:"perms,omitempty"`
}
@ -26,7 +25,6 @@ type Credential struct {
type CredentialsStore struct {
store map[string]string
perms map[string]map[string]bool
isbcrypted map[string]bool
}
// NewCredentialsStore returns a new instance of a CredentialStore.
@ -34,7 +32,6 @@ func NewCredentialsStore() *CredentialsStore {
return &CredentialsStore{
store: make(map[string]string),
perms: make(map[string]map[string]bool),
isbcrypted: make(map[string]bool),
}
}
@ -58,9 +55,6 @@ func (c *CredentialsStore) Load(r io.Reader) error {
for _, p := range cred.Perms {
c.perms[cred.Username][p] = true
}
if cred.Hashed != nil && *cred.Hashed {
c.isbcrypted[cred.Username] = true
}
}
// Read closing bracket.
@ -78,12 +72,8 @@ func (c *CredentialsStore) Check(username, password string) bool {
if !ok {
return false
}
if _, ok = c.isbcrypted[username]; ok {
err := bcrypt.CompareHashAndPassword([]byte(pw), []byte(password))
return err == nil
} else {
return password == pw
}
return password == pw ||
bcrypt.CompareHashAndPassword([]byte(pw), []byte(password)) == nil
}
// CheckRequest returns true if b contains a valid username and password.

@ -167,10 +167,11 @@ func Test_AuthLoadHashedSingleRequest(t *testing.T) {
[
{
"username": "username1",
"password": "$2a$10$fKRHxrEuyDTP6tXIiDycr.nyC8Q7UMIfc31YMyXHDLgRDyhLK3VFS",
"hashed": true
"password": "$2a$10$fKRHxrEuyDTP6tXIiDycr.nyC8Q7UMIfc31YMyXHDLgRDyhLK3VFS"
},
{"username": "username2", "password": "password2", "hashed":false}
{ "username": "username2",
"password": "password2"
}
]
`

Loading…
Cancel
Save