check existing user with bcrypt

pull/6010/head
David Norton 2016-03-14 18:40:25 -04:00
parent a369ed8565
commit a2125bee24
2 changed files with 10 additions and 2 deletions

View File

@ -429,6 +429,14 @@ func (c *Client) CreateUser(name, password string, admin bool) (*UserInfo, error
data := c.cacheData.Clone()
// See if the user already exists.
if u := data.User(name); u != nil {
if err := bcrypt.CompareHashAndPassword([]byte(u.Hash), []byte(password)); err != nil || u.Admin != admin {
return nil, ErrUserExists
}
return u, nil
}
// Hash the password before serializing it.
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
if err != nil {

View File

@ -459,8 +459,8 @@ func (data *Data) CreateUser(name, hash string, admin bool) error {
// Ensure the user doesn't already exist.
if name == "" {
return ErrUsernameRequired
} else if data.User(name) != nil {
return nil
} else if u := data.User(name); u != nil {
return ErrUserExists
}
// Append new user.