From 2effd3c36092b12409ce93bbb570a54331e42eb5 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 20 Feb 2024 20:07:26 +0800 Subject: [PATCH] Update authorization error messages --- service/guards.go | 8 ++++---- service/middleware.go | 10 +++++----- sui/api/guards.go | 12 +++++------ sui/api/request.go | 46 ++++++++++++++++++++++++++++++++++++++----- sui/core/request.go | 11 ++++++----- 5 files changed, 62 insertions(+), 25 deletions(-) diff --git a/service/guards.go b/service/guards.go index 1fe1ccc8..d2df51d2 100644 --- a/service/guards.go +++ b/service/guards.go @@ -46,13 +46,13 @@ func guardCookieTrace(c *gin.Context) { func guardCookieJWT(c *gin.Context) { tokenString, err := c.Cookie("__tk") if err != nil { - c.JSON(403, gin.H{"code": 403, "message": "No permission"}) + c.JSON(403, gin.H{"code": 403, "message": "Not Authorized"}) c.Abort() return } if tokenString == "" { - c.JSON(403, gin.H{"code": 403, "message": "No permission"}) + c.JSON(403, gin.H{"code": 403, "message": "Not Authorized"}) c.Abort() return } @@ -67,7 +67,7 @@ func guardBearerJWT(c *gin.Context) { tokenString := c.Request.Header.Get("Authorization") tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer ")) if tokenString == "" { - c.JSON(403, gin.H{"code": 403, "message": "No permission"}) + c.JSON(403, gin.H{"code": 403, "message": "Not Authorized"}) c.Abort() return } @@ -80,7 +80,7 @@ func guardBearerJWT(c *gin.Context) { func guardQueryJWT(c *gin.Context) { tokenString := c.Query("__tk") if tokenString == "" { - c.JSON(403, gin.H{"code": 403, "message": "No permission"}) + c.JSON(403, gin.H{"code": 403, "message": "Not Authorized"}) c.Abort() return } diff --git a/service/middleware.go b/service/middleware.go index 977122db..fcfef65b 100644 --- a/service/middleware.go +++ b/service/middleware.go @@ -1,7 +1,6 @@ package service import ( - "fmt" "path/filepath" "strings" @@ -76,21 +75,22 @@ func withStaticFileServer(c *gin.Context) { r, code, err := api.NewRequestContext(c) if err != nil { log.Error("Sui Reqeust Error: %s", err.Error()) - c.AbortWithError(code, err) + c.AbortWithStatusJSON(code, gin.H{"code": code, "message": err.Error()}) return } html, code, err := r.Render() if err != nil { if code == 301 || code == 302 { - fmt.Println(err.Error()) - c.Redirect(code, err.Error()) + url := err.Error() + // fmt.Println("Redirect to: ", url) + c.Redirect(code, url) c.Done() return } log.Error("Sui Render Error: %s", err.Error()) - c.AbortWithError(code, err) + c.AbortWithStatusJSON(code, gin.H{"code": code, "message": err.Error()}) return } diff --git a/sui/api/guards.go b/sui/api/guards.go index 95ec198e..53602838 100644 --- a/sui/api/guards.go +++ b/sui/api/guards.go @@ -54,15 +54,15 @@ func guardCookieJWT(r *Request) error { tokenString, err := c.Cookie("__tk") if err != nil { - c.JSON(403, gin.H{"code": 403, "message": "No permission"}) - c.Abort() - return fmt.Errorf("No permission") + // c.JSON(403, gin.H{"code": 403, "message": "No permission"}) + // c.Abort() + return fmt.Errorf("Not Authorized") } if tokenString == "" { - c.JSON(403, gin.H{"code": 403, "message": "No permission"}) - c.Abort() - return fmt.Errorf("No permission") + // c.JSON(403, gin.H{"code": 403, "message": "No permission"}) + // c.Abort() + return fmt.Errorf("Not Authorized") } claims := helper.JwtValidate(tokenString) diff --git a/sui/api/request.go b/sui/api/request.go index 389a7a50..6ba4d59b 100644 --- a/sui/api/request.go +++ b/sui/api/request.go @@ -87,6 +87,7 @@ func (r *Request) Render() (string, int, error) { } guard := "" + guardRedirect := "" configText := "" configSel := doc.Find("script[name=config]") if configSel != nil && configSel.Length() > 0 { @@ -98,7 +99,16 @@ func (r *Request) Render() (string, int, error) { if err != nil { return "", 500, fmt.Errorf("config error, please re-complie the page %s", err.Error()) } + + // Redirect the page (should refector before release) + // guard=cookie-jwt:redirect-url redirect to the url if not authorized + // guard=cookie-jwt return {code: 403, message: "Not Authorized"} guard = conf.Guard + if strings.Contains(conf.Guard, ":") { + parts := strings.Split(conf.Guard, ":") + guard = parts[0] + guardRedirect = parts[1] + } } dataText := "" @@ -123,11 +133,12 @@ func (r *Request) Render() (string, int, error) { // Save to The Cache // c = core.SetCache(r.File, html, dataText, globalDataText) c = &core.Cache{ - Data: dataText, - Global: globalDataText, - HTML: html, - Guard: guard, - Config: configText, + Data: dataText, + Global: globalDataText, + HTML: html, + Guard: guard, + GuardRedirect: guardRedirect, + Config: configText, } log.Trace("The page %s is cached", r.File) } @@ -138,6 +149,31 @@ func (r *Request) Render() (string, int, error) { if guard, has := Guards[c.Guard]; has { err := guard(r) if err != nil { + + // Redirect the page (should refector before release) + if c.GuardRedirect != "" { + redirect := c.GuardRedirect + data := core.Data{} + if c.Data != "" { + data, err = r.Request.ExecString(c.Data) + if err != nil { + return "", 500, fmt.Errorf("data error, please re-complie the page %s", err.Error()) + } + } + + if c.Global != "" { + global, err := r.Request.ExecString(c.Global) + if err != nil { + return "", 500, fmt.Errorf("global data error, please re-complie the page %s", err.Error()) + } + data["$global"] = global + } + + redirect, _ = data.Replace(redirect) + return "", 302, fmt.Errorf("%s", redirect) + } + + // Return the error ex := exception.Err(err, 403) return "", ex.Code, fmt.Errorf("%s", ex.Message) } diff --git a/sui/core/request.go b/sui/core/request.go index 7ffb8a51..c6a61757 100644 --- a/sui/core/request.go +++ b/sui/core/request.go @@ -12,11 +12,12 @@ import ( // Cache the cache type Cache struct { - Data string - Global string - Config string - Guard string - HTML string + Data string + Global string + Config string + Guard string + GuardRedirect string + HTML string } // Caches the caches