improve auth interface, fix issue in meta

This commit is contained in:
Arthur Lu 2025-04-02 21:18:29 +00:00
parent bd0387976f
commit 13339cc56d
7 changed files with 26 additions and 20 deletions

@ -151,7 +151,7 @@ var Icons = map[string]map[string]Icon{
Clickable: false,
},
},
"uknown": {
"unknown": {
"status": {
Src: "images/status/inactive.svg",
Alt: "Node is offline",

@ -33,3 +33,9 @@ type RequestContext struct {
Cookies map[string]string
Body map[string]any
}
type Auth struct {
Username string
Token string
CSRF string
}

@ -179,14 +179,14 @@ func RequestGetAPI(path string, context RequestContext) (*http.Response, int, er
return response, response.StatusCode, nil
}
func GetAuth(c *gin.Context) (string, string, string, error) {
func GetAuth(c *gin.Context) (Auth, error) {
_, errAuth := c.Cookie("auth")
username, errUsername := c.Cookie("username")
token, errToken := c.Cookie("PVEAuthCookie")
csrf, errCSRF := c.Cookie("CSRFPreventionToken")
if errUsername != nil || errAuth != nil || errToken != nil || errCSRF != nil {
return "", "", "", fmt.Errorf("error occured getting user cookies: (auth: %s, token: %s, csrf: %s)", errAuth, errToken, errCSRF)
return Auth{}, fmt.Errorf("error occured getting user cookies: (auth: %s, token: %s, csrf: %s)", errAuth, errToken, errCSRF)
} else {
return username, token, csrf, nil
return Auth{username, token, csrf}, nil
}
}

@ -83,9 +83,9 @@ type ResourceChart struct {
}
func HandleGETAccount(c *gin.Context) {
username, token, csrf, err := common.GetAuth(c)
auth, err := common.GetAuth(c)
if err == nil {
account, err := GetUserAccount(username, token, csrf)
account, err := GetUserAccount(auth)
if err != nil {
common.HandleNonFatalError(c, err)
return
@ -153,16 +153,16 @@ func HandleGETAccount(c *gin.Context) {
}
}
func GetUserAccount(username string, token string, csrf string) (Account, error) {
func GetUserAccount(auth common.Auth) (Account, error) {
account := Account{
Resources: map[string]any{},
}
ctx := common.RequestContext{
Cookies: map[string]string{
"username": username,
"PVEAuthCookie": token,
"CSRFPreventionToken": csrf,
"username": auth.Username,
"PVEAuthCookie": auth.Token,
"CSRFPreventionToken": auth.CSRF,
},
Body: map[string]any{},
}
@ -179,7 +179,7 @@ func GetUserAccount(username string, token string, csrf string) (Account, error)
if err != nil {
return account, err
} else {
account.Username = username
account.Username = auth.Username
}
ctx.Body = map[string]any{}

@ -8,7 +8,7 @@ import (
)
func HandleGETConfig(c *gin.Context) {
_, _, _, err := common.GetAuth(c)
_, err := common.GetAuth(c)
if err == nil {
c.HTML(http.StatusOK, "html/config.html", gin.H{
"global": common.Global,

@ -31,11 +31,11 @@ type Instance struct {
DeleteBtnIcon common.Icon
}
func GetClusterResources(token string, csrf string) (map[uint]Instance, map[string]Node, error) {
func GetClusterResources(auth common.Auth) (map[uint]Instance, map[string]Node, error) {
ctx := common.RequestContext{
Cookies: map[string]string{
"PVEAuthCookie": token,
"CSRFPreventionToken": csrf,
"PVEAuthCookie": auth.Token,
"CSRFPreventionToken": auth.CSRF,
},
Body: map[string]any{},
}
@ -91,9 +91,9 @@ func GetClusterResources(token string, csrf string) (map[uint]Instance, map[stri
}
func HandleGETIndex(c *gin.Context) {
_, token, csrf, err := common.GetAuth(c)
auth, err := common.GetAuth(c)
if err == nil { // user should be authed, try to return index with population
instances, _, err := GetClusterResources(token, csrf)
instances, _, err := GetClusterResources(auth)
if err != nil {
common.HandleNonFatalError(c, err)
}
@ -108,9 +108,9 @@ func HandleGETIndex(c *gin.Context) {
}
func HandleGETInstancesFragment(c *gin.Context) {
_, token, csrf, err := common.GetAuth(c)
Auth, err := common.GetAuth(c)
if err == nil { // user should be authed, try to return index with population
instances, _, err := GetClusterResources(token, csrf)
instances, _, err := GetClusterResources(Auth)
if err != nil {
common.HandleNonFatalError(c, err)
}

@ -8,7 +8,7 @@ import (
)
func HandleGETSettings(c *gin.Context) {
_, _, _, err := common.GetAuth(c)
_, err := common.GetAuth(c)
if err == nil {
c.HTML(http.StatusOK, "html/settings.html", gin.H{
"global": common.Global,