Skip to content

Commit

Permalink
return 401 for invalid token
Browse files Browse the repository at this point in the history
  • Loading branch information
yflau committed Aug 4, 2024
1 parent 346a310 commit 1ac37b0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions helper/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ 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
}

if claims, ok := token.Claims.(*JwtClaims); ok && token.Valid {
return claims
}

exception.New("Invalid token", 403).Ctx(token.Claims).Throw()
exception.New("Invalid token", 401).Ctx(token.Claims).Throw()
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion studio/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions sui/api/guards.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion widgets/form/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion widgets/list/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion widgets/login/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 1ac37b0

Please sign in to comment.