mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2024-12-25 12:59:06 +01:00
token based login: fix re-logins
This commit is contained in:
parent
dc0bf80742
commit
d4b1b32ca0
@ -2,6 +2,7 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
|
"database/sql"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -106,15 +107,23 @@ func (ja *JWTAuthenticator) Login(user *User, rw http.ResponseWriter, r *http.Re
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if rawrole, ok := claims["roles"].(string); ok {
|
||||||
|
roles = append(roles, rawrole)
|
||||||
|
}
|
||||||
|
|
||||||
if user == nil {
|
if user == nil {
|
||||||
user = &User{
|
user, err = ja.auth.GetUser(sub)
|
||||||
Username: sub,
|
if err != nil && err != sql.ErrNoRows {
|
||||||
Roles: roles,
|
|
||||||
AuthSource: AuthViaToken,
|
|
||||||
}
|
|
||||||
if err := ja.auth.AddUser(user); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
|
} else if user == nil {
|
||||||
|
user = &User{
|
||||||
|
Username: sub,
|
||||||
|
Roles: roles,
|
||||||
|
AuthSource: AuthViaToken,
|
||||||
|
}
|
||||||
|
if err := ja.auth.AddUser(user); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,13 +38,8 @@ 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)
|
cols := []string{"username", "roles"}
|
||||||
if err != nil {
|
vals := []interface{}{user.Username, string(rolesJson)}
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
cols := []string{"username", "password", "roles"}
|
|
||||||
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)
|
||||||
@ -53,6 +48,14 @@ func (auth *Authentication) AddUser(user *User) error {
|
|||||||
cols = append(cols, "email")
|
cols = append(cols, "email")
|
||||||
vals = append(vals, user.Email)
|
vals = append(vals, user.Email)
|
||||||
}
|
}
|
||||||
|
if user.Password != "" {
|
||||||
|
password, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cols = append(cols, "password")
|
||||||
|
vals = append(vals, string(password))
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := sq.Insert("user").Columns(cols...).Values(vals...).RunWith(auth.db).Exec(); err != nil {
|
if _, err := sq.Insert("user").Columns(cols...).Values(vals...).RunWith(auth.db).Exec(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user