Merge pull request #582 from trheyi/main

Update authorization error messages
This commit is contained in:
Max 2024-02-20 20:20:35 +08:00 committed by GitHub
commit 0c25ab9279
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 62 additions and 25 deletions

View file

@ -46,13 +46,13 @@ func guardCookieTrace(c *gin.Context) {
func guardCookieJWT(c *gin.Context) { func guardCookieJWT(c *gin.Context) {
tokenString, err := c.Cookie("__tk") tokenString, err := c.Cookie("__tk")
if err != nil { 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() c.Abort()
return return
} }
if tokenString == "" { if tokenString == "" {
c.JSON(403, gin.H{"code": 403, "message": "No permission"}) c.JSON(403, gin.H{"code": 403, "message": "Not Authorized"})
c.Abort() c.Abort()
return return
} }
@ -67,7 +67,7 @@ func guardBearerJWT(c *gin.Context) {
tokenString := c.Request.Header.Get("Authorization") tokenString := c.Request.Header.Get("Authorization")
tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer ")) tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer "))
if tokenString == "" { if tokenString == "" {
c.JSON(403, gin.H{"code": 403, "message": "No permission"}) c.JSON(403, gin.H{"code": 403, "message": "Not Authorized"})
c.Abort() c.Abort()
return return
} }
@ -80,7 +80,7 @@ func guardBearerJWT(c *gin.Context) {
func guardQueryJWT(c *gin.Context) { func guardQueryJWT(c *gin.Context) {
tokenString := c.Query("__tk") tokenString := c.Query("__tk")
if tokenString == "" { if tokenString == "" {
c.JSON(403, gin.H{"code": 403, "message": "No permission"}) c.JSON(403, gin.H{"code": 403, "message": "Not Authorized"})
c.Abort() c.Abort()
return return
} }

View file

@ -1,7 +1,6 @@
package service package service
import ( import (
"fmt"
"path/filepath" "path/filepath"
"strings" "strings"
@ -76,21 +75,22 @@ func withStaticFileServer(c *gin.Context) {
r, code, err := api.NewRequestContext(c) r, code, err := api.NewRequestContext(c)
if err != nil { if err != nil {
log.Error("Sui Reqeust Error: %s", err.Error()) log.Error("Sui Reqeust Error: %s", err.Error())
c.AbortWithError(code, err) c.AbortWithStatusJSON(code, gin.H{"code": code, "message": err.Error()})
return return
} }
html, code, err := r.Render() html, code, err := r.Render()
if err != nil { if err != nil {
if code == 301 || code == 302 { if code == 301 || code == 302 {
fmt.Println(err.Error()) url := err.Error()
c.Redirect(code, err.Error()) // fmt.Println("Redirect to: ", url)
c.Redirect(code, url)
c.Done() c.Done()
return return
} }
log.Error("Sui Render Error: %s", err.Error()) log.Error("Sui Render Error: %s", err.Error())
c.AbortWithError(code, err) c.AbortWithStatusJSON(code, gin.H{"code": code, "message": err.Error()})
return return
} }

View file

@ -54,15 +54,15 @@ func guardCookieJWT(r *Request) error {
tokenString, err := c.Cookie("__tk") tokenString, err := c.Cookie("__tk")
if err != nil { if err != nil {
c.JSON(403, gin.H{"code": 403, "message": "No permission"}) // c.JSON(403, gin.H{"code": 403, "message": "No permission"})
c.Abort() // c.Abort()
return fmt.Errorf("No permission") return fmt.Errorf("Not Authorized")
} }
if tokenString == "" { if tokenString == "" {
c.JSON(403, gin.H{"code": 403, "message": "No permission"}) // c.JSON(403, gin.H{"code": 403, "message": "No permission"})
c.Abort() // c.Abort()
return fmt.Errorf("No permission") return fmt.Errorf("Not Authorized")
} }
claims := helper.JwtValidate(tokenString) claims := helper.JwtValidate(tokenString)

View file

@ -87,6 +87,7 @@ func (r *Request) Render() (string, int, error) {
} }
guard := "" guard := ""
guardRedirect := ""
configText := "" configText := ""
configSel := doc.Find("script[name=config]") configSel := doc.Find("script[name=config]")
if configSel != nil && configSel.Length() > 0 { if configSel != nil && configSel.Length() > 0 {
@ -98,7 +99,16 @@ func (r *Request) Render() (string, int, error) {
if err != nil { if err != nil {
return "", 500, fmt.Errorf("config error, please re-complie the page %s", err.Error()) 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 guard = conf.Guard
if strings.Contains(conf.Guard, ":") {
parts := strings.Split(conf.Guard, ":")
guard = parts[0]
guardRedirect = parts[1]
}
} }
dataText := "" dataText := ""
@ -123,11 +133,12 @@ func (r *Request) Render() (string, int, error) {
// Save to The Cache // Save to The Cache
// c = core.SetCache(r.File, html, dataText, globalDataText) // c = core.SetCache(r.File, html, dataText, globalDataText)
c = &core.Cache{ c = &core.Cache{
Data: dataText, Data: dataText,
Global: globalDataText, Global: globalDataText,
HTML: html, HTML: html,
Guard: guard, Guard: guard,
Config: configText, GuardRedirect: guardRedirect,
Config: configText,
} }
log.Trace("The page %s is cached", r.File) 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 { if guard, has := Guards[c.Guard]; has {
err := guard(r) err := guard(r)
if err != nil { 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) ex := exception.Err(err, 403)
return "", ex.Code, fmt.Errorf("%s", ex.Message) return "", ex.Code, fmt.Errorf("%s", ex.Message)
} }

View file

@ -12,11 +12,12 @@ import (
// Cache the cache // Cache the cache
type Cache struct { type Cache struct {
Data string Data string
Global string Global string
Config string Config string
Guard string Guard string
HTML string GuardRedirect string
HTML string
} }
// Caches the caches // Caches the caches