improve auth interface, fix issue in meta

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

View File

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

View File

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

View File

@@ -179,14 +179,14 @@ func RequestGetAPI(path string, context RequestContext) (*http.Response, int, er
return response, response.StatusCode, nil 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") _, errAuth := c.Cookie("auth")
username, errUsername := c.Cookie("username") username, errUsername := c.Cookie("username")
token, errToken := c.Cookie("PVEAuthCookie") token, errToken := c.Cookie("PVEAuthCookie")
csrf, errCSRF := c.Cookie("CSRFPreventionToken") csrf, errCSRF := c.Cookie("CSRFPreventionToken")
if errUsername != nil || errAuth != nil || errToken != nil || errCSRF != nil { 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 { } else {
return username, token, csrf, nil return Auth{username, token, csrf}, nil
} }
} }

View File

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

View File

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

View File

@@ -31,11 +31,11 @@ type Instance struct {
DeleteBtnIcon common.Icon 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{ ctx := common.RequestContext{
Cookies: map[string]string{ Cookies: map[string]string{
"PVEAuthCookie": token, "PVEAuthCookie": auth.Token,
"CSRFPreventionToken": csrf, "CSRFPreventionToken": auth.CSRF,
}, },
Body: map[string]any{}, Body: map[string]any{},
} }
@@ -91,9 +91,9 @@ func GetClusterResources(token string, csrf string) (map[uint]Instance, map[stri
} }
func HandleGETIndex(c *gin.Context) { 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 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 { if err != nil {
common.HandleNonFatalError(c, err) common.HandleNonFatalError(c, err)
} }
@@ -108,9 +108,9 @@ func HandleGETIndex(c *gin.Context) {
} }
func HandleGETInstancesFragment(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 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 { if err != nil {
common.HandleNonFatalError(c, err) common.HandleNonFatalError(c, err)
} }

View File

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