From 1ac37b048f39b795d33ba7d25b377af9e99c6622 Mon Sep 17 00:00:00 2001 From: yflau <5521134@qq.com> Date: Sun, 4 Aug 2024 12:54:05 +0800 Subject: [PATCH] return 401 for invalid token --- helper/jwt.go | 4 ++-- studio/middleware.go | 2 +- sui/api/guards.go | 16 ++++++++-------- widgets/form/process.go | 2 +- widgets/list/process.go | 2 +- widgets/login/process.go | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/helper/jwt.go b/helper/jwt.go index 3e771539..60531a83 100644 --- a/helper/jwt.go +++ b/helper/jwt.go @@ -41,7 +41,7 @@ func JwtValidate(tokenString string, secret ...[]byte) *JwtClaims { if err != nil { log.Error("JWT ParseWithClaims Error: %s", err) - exception.New("Invalid token", 403).Ctx(err.Error()).Throw() + exception.New("Invalid token", 401).Ctx(err.Error()).Throw() return nil } @@ -49,7 +49,7 @@ func JwtValidate(tokenString string, secret ...[]byte) *JwtClaims { return claims } - exception.New("Invalid token", 403).Ctx(token.Claims).Throw() + exception.New("Invalid token", 401).Ctx(token.Claims).Throw() return nil } diff --git a/studio/middleware.go b/studio/middleware.go index f0728efb..37363265 100644 --- a/studio/middleware.go +++ b/studio/middleware.go @@ -72,7 +72,7 @@ func hdAuth(c *gin.Context) { if strings.HasPrefix(tokenString, "Bearer") { tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer ")) if tokenString == "" { - c.JSON(403, gin.H{"code": 403, "message": "No permission"}) + c.JSON(401, gin.H{"code": 401, "message": "Not authenticated"}) c.Abort() return } diff --git a/sui/api/guards.go b/sui/api/guards.go index 53602838..7a6e01e2 100644 --- a/sui/api/guards.go +++ b/sui/api/guards.go @@ -28,15 +28,15 @@ var Guards = map[string]func(c *Request) error{ // JWT Bearer JWT func guardBearerJWT(r *Request) error { if r.context == nil { - return fmt.Errorf("No permission") + return fmt.Errorf("Not authenticated") } c := r.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(401, gin.H{"code": 401, "message": "Not authenticated"}) c.Abort() - return fmt.Errorf("No permission") + return fmt.Errorf("Not authenticated") } claims := helper.JwtValidate(tokenString) @@ -56,13 +56,13 @@ func guardCookieJWT(r *Request) error { if err != nil { // c.JSON(403, gin.H{"code": 403, "message": "No permission"}) // c.Abort() - return fmt.Errorf("Not Authorized") + return fmt.Errorf("Not authenticated") } if tokenString == "" { // c.JSON(403, gin.H{"code": 403, "message": "No permission"}) // c.Abort() - return fmt.Errorf("Not Authorized") + return fmt.Errorf("Not authenticated") } claims := helper.JwtValidate(tokenString) @@ -93,15 +93,15 @@ func guardCookieTrace(r *Request) error { // JWT Bearer JWT func guardQueryJWT(r *Request) error { if r.context == nil { - return fmt.Errorf("No permission") + return fmt.Errorf("Not authenticated") } c := r.context tokenString := c.Query("__tk") if tokenString == "" { - c.JSON(403, gin.H{"code": 403, "message": "No permission"}) + c.JSON(401, gin.H{"code": 401, "message": "Not authenticated"}) c.Abort() - return fmt.Errorf("No permission") + return fmt.Errorf("Not authenticated") } claims := helper.JwtValidate(tokenString) diff --git a/widgets/form/process.go b/widgets/form/process.go index 8c094b6c..36ebf9f6 100644 --- a/widgets/form/process.go +++ b/widgets/form/process.go @@ -94,7 +94,7 @@ func processDownload(process *gouProcess.Process) interface{} { // Auth tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer ")) if tokenString == "" { - exception.New("%s.%s No permission", 403, form.ID, field).Throw() + exception.New("%s.%s not authenticated", 401, form.ID, field).Throw() } claims := helper.JwtValidate(tokenString) diff --git a/widgets/list/process.go b/widgets/list/process.go index 930a68a6..00b89cb6 100644 --- a/widgets/list/process.go +++ b/widgets/list/process.go @@ -82,7 +82,7 @@ func processDownload(process *gouProcess.Process) interface{} { // Auth tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer ")) if tokenString == "" { - exception.New("%s.%s No permission", 403, list.ID, field).Throw() + exception.New("%s.%s not authenticated", 401, list.ID, field).Throw() } claims := helper.JwtValidate(tokenString) diff --git a/widgets/login/process.go b/widgets/login/process.go index 8a171c9f..e8959dab 100644 --- a/widgets/login/process.go +++ b/widgets/login/process.go @@ -44,7 +44,7 @@ func processLoginAdmin(process *process.Process) interface{} { if !helper.CaptchaValidate(id, value) { log.With(log.F{"id": id, "code": value}).Debug("ProcessLogin") - exception.New("验证码不正确", 403).Ctx(maps.Map{"id": id, "code": value}).Throw() + exception.New("验证码不正确", 401).Ctx(maps.Map{"id": id, "code": value}).Throw() return nil }