Update authorization error messages

This commit is contained in:
Max 2024-02-20 20:07:26 +08:00
parent 8095996c95
commit 2effd3c360
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) {
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
}

View file

@ -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
}

View file

@ -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)

View file

@ -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)
}

View file

@ -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