diff --git a/app/common/meta.go b/app/common/meta.go index a71e05c..f8d7092 100644 --- a/app/common/meta.go +++ b/app/common/meta.go @@ -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", diff --git a/app/common/types.go b/app/common/types.go index 7b4a9ea..1a45fdc 100644 --- a/app/common/types.go +++ b/app/common/types.go @@ -33,3 +33,9 @@ type RequestContext struct { Cookies map[string]string Body map[string]any } + +type Auth struct { + Username string + Token string + CSRF string +} diff --git a/app/common/utils.go b/app/common/utils.go index f5eaf76..2a337c7 100644 --- a/app/common/utils.go +++ b/app/common/utils.go @@ -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 } } diff --git a/app/routes/account.go b/app/routes/account.go index e7e7d7a..05bf7cc 100644 --- a/app/routes/account.go +++ b/app/routes/account.go @@ -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{} diff --git a/app/routes/config.go b/app/routes/config.go index 899f152..361e9dc 100644 --- a/app/routes/config.go +++ b/app/routes/config.go @@ -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, diff --git a/app/routes/index.go b/app/routes/index.go index c58e3cf..cae435f 100644 --- a/app/routes/index.go +++ b/app/routes/index.go @@ -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) } diff --git a/app/routes/settings.go b/app/routes/settings.go index 4fadc85..5659bf0 100644 --- a/app/routes/settings.go +++ b/app/routes/settings.go @@ -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,