jwt.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2020 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package middleware
  4. import (
  5. "git.getensh.com/common/gopkgs/jwtwrapper"
  6. "github.com/dgrijalva/jwt-go"
  7. "github.com/gin-gonic/gin"
  8. "github.com/tidwall/gjson"
  9. "google.golang.org/grpc/codes"
  10. "google.golang.org/grpc/status"
  11. "net/http"
  12. "property-system-gateway/errors"
  13. "property-system-gateway/utils"
  14. "strconv"
  15. "strings"
  16. )
  17. // 响应数据
  18. type Response struct {
  19. Code codes.Code `json:"code" default:"1"`
  20. Message string `json:"message" default:"success"`
  21. }
  22. // JWT is jwt middleware
  23. func Jwt() gin.HandlerFunc {
  24. return func(c *gin.Context) {
  25. //fmt.Printf("xxx:%v,%v\n", c.Request.RequestURI, c.Request.Method)
  26. s := status.New(1, "内部服务错误")
  27. token := c.GetHeader("token")
  28. if token == "" {
  29. if v, ok := status.FromError(errors.NoTokenError); ok {
  30. s = v
  31. }
  32. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  33. c.Abort()
  34. return
  35. }
  36. // 解析token
  37. claims, err := jwtwrapper.ParseToken(token)
  38. if err != nil {
  39. switch err.(*jwt.ValidationError).Errors {
  40. case jwt.ValidationErrorExpired:
  41. if v, ok := status.FromError(errors.TokenExpiredError); ok {
  42. s = v
  43. }
  44. default:
  45. if v, ok := status.FromError(errors.TokenFailedError); ok {
  46. s = v
  47. }
  48. }
  49. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  50. c.Abort()
  51. return
  52. }
  53. // 将claims信息保存到上下文,为后续使用
  54. c.Set("claims", claims)
  55. gardenId := gjson.GetBytes([]byte(claims.Subject), "garden_id").Int()
  56. uid, _ := strconv.ParseInt(claims.Id, 10, 64)
  57. cid := gjson.GetBytes([]byte(claims.Subject), "cid").Int()
  58. /*
  59. supper := gjson.GetBytes(utils.StrToBytes(claims.Subject), "supper").Bool()
  60. if c.Request.Method == "PUT" || c.Request.Method == "POST" || c.Request.Method == "DELETE" {
  61. if supper == false && strings.Contains(c.Request.RequestURI, "/user") == false {
  62. c.JSON(http.StatusOK, Response{10008, "权限不足"})
  63. }
  64. }
  65. */
  66. isCompanyEnter := gjson.GetBytes([]byte(claims.Subject), "is_company_enter").Bool()
  67. if isCompanyEnter && !strings.Contains(c.Request.RequestURI, "user/company_login") {
  68. c.JSON(http.StatusOK, Response{10010, "token错误"})
  69. c.Abort()
  70. return
  71. }
  72. if isCompanyEnter && strings.Contains(c.Request.RequestURI, "user/company_login") {
  73. c.Next()
  74. return
  75. }
  76. if uid == 0 && !strings.Contains(c.Request.RequestURI, "user/choose_user") {
  77. c.JSON(http.StatusOK, Response{10008, "请先选择账户"})
  78. c.Abort()
  79. return
  80. }
  81. gPermissionTime := gjson.GetBytes([]byte(claims.Subject), "g_permission_time").String()
  82. uPermissionTime := gjson.GetBytes([]byte(claims.Subject), "u_permission_time").String()
  83. byCompany := gjson.GetBytes([]byte(claims.Subject), "by_company").Bool()
  84. // 权限变更触发重新登录
  85. if gardenId > 0 && gPermissionTime != gloablePermissionTime(gardenId) {
  86. if strings.Contains(c.Request.RequestURI, "permission_reget") {
  87. c.Next()
  88. return
  89. }
  90. c.JSON(http.StatusOK, Response{19999, "权限已变更重新获取权限"})
  91. c.Abort()
  92. return
  93. }
  94. if !byCompany && uPermissionTime != userPermissionTime(uid) {
  95. if strings.Contains(c.Request.RequestURI, "permission_reget") {
  96. c.Next()
  97. return
  98. }
  99. c.JSON(http.StatusOK, Response{19999, "权限已变更重新获取权限"})
  100. c.Abort()
  101. return
  102. }
  103. // 单点登录检查
  104. if false {
  105. singleSignTime := gjson.GetBytes([]byte(claims.Subject), "single_sign_time").String()
  106. if singleSignTime != utils.GetSingleSignTime(utils.GetSingleFlag(byCompany, cid, gardenId, uid)) &&
  107. !strings.Contains(c.Request.RequestURI, "user/choose_user") &&
  108. !strings.Contains(c.Request.RequestURI, "user/company_login") {
  109. c.JSON(http.StatusOK, Response{10008, "账号已在其他地方登录"})
  110. c.Abort()
  111. return
  112. }
  113. }
  114. // 权限检查 TODO 目前暂时放开,后续需检查权限
  115. super := gjson.GetBytes([]byte(claims.Subject), "is_super_group").Bool()
  116. if super {
  117. //c.Next()
  118. //return
  119. }
  120. if byCompany {
  121. //c.Next()
  122. //return
  123. }
  124. if c.Request.Method == "GET" {
  125. c.Next()
  126. return
  127. }
  128. if strings.Contains(c.Request.RequestURI, "openim") {
  129. c.Next()
  130. return
  131. }
  132. routers := gjson.GetBytes([]byte(claims.Subject), "routers").Map()
  133. err = checkPermission(routers, c.Request.RequestURI, uid, gPermissionTime, uPermissionTime)
  134. if err != nil {
  135. if v, ok := status.FromError(err); ok {
  136. s = v
  137. }
  138. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  139. c.Abort()
  140. return
  141. }
  142. c.Next()
  143. }
  144. }