check existing user with bcrypt
parent
a369ed8565
commit
a2125bee24
|
@ -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 {
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue