Fix auth.AddUser method

This commit is contained in:
Lou Knauer 2022-07-26 11:00:41 +02:00
parent dc4c7f0033
commit dc0bf80742
2 changed files with 10 additions and 2 deletions

View File

@ -157,7 +157,8 @@ func (auth *Authentication) Login(onsuccess http.Handler, onfailure func(rw http
user := (*User)(nil) user := (*User)(nil)
if username != "" { if username != "" {
if user, _ = auth.GetUser(username); err != nil { if user, _ = auth.GetUser(username); err != nil {
log.Warnf("login of unkown user %#v", username) // log.Warnf("login of unkown user %#v", username)
_ = err
} }
} }

View File

@ -11,6 +11,7 @@ import (
"github.com/ClusterCockpit/cc-backend/pkg/log" "github.com/ClusterCockpit/cc-backend/pkg/log"
sq "github.com/Masterminds/squirrel" sq "github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"golang.org/x/crypto/bcrypt"
) )
func (auth *Authentication) GetUser(username string) (*User, error) { func (auth *Authentication) GetUser(username string) (*User, error) {
@ -36,8 +37,14 @@ func (auth *Authentication) GetUser(username string) (*User, error) {
func (auth *Authentication) AddUser(user *User) error { func (auth *Authentication) AddUser(user *User) error {
rolesJson, _ := json.Marshal(user.Roles) rolesJson, _ := json.Marshal(user.Roles)
password, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
return err
}
cols := []string{"username", "password", "roles"} cols := []string{"username", "password", "roles"}
vals := []interface{}{user.Username, user.Password, string(rolesJson)} vals := []interface{}{user.Username, string(password), string(rolesJson)}
if user.Name != "" { if user.Name != "" {
cols = append(cols, "name") cols = append(cols, "name")
vals = append(vals, user.Name) vals = append(vals, user.Name)