jwt.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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-applete-gateway/errors"
  13. "property-applete-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. /*
  56. supper := gjson.GetBytes(utils.StrToBytes(claims.Subject), "supper").Bool()
  57. if c.Request.Method == "PUT" || c.Request.Method == "POST" || c.Request.Method == "DELETE" {
  58. if supper == false && strings.Contains(c.Request.RequestURI, "/user") == false {
  59. c.JSON(http.StatusOK, Response{10008, "权限不足"})
  60. }
  61. }
  62. */
  63. isCompanyEnter := gjson.GetBytes([]byte(claims.Subject), "is_company_enter").Bool()
  64. if isCompanyEnter && !strings.Contains(c.Request.RequestURI, "user/company_login") {
  65. c.JSON(http.StatusOK, Response{10010, "token错误"})
  66. c.Abort()
  67. return
  68. }
  69. if isCompanyEnter && strings.Contains(c.Request.RequestURI, "user/company_login"){
  70. c.Next()
  71. return
  72. }
  73. uid, _ := strconv.ParseInt(claims.Id, 10, 64)
  74. if uid == 0 && !strings.Contains(c.Request.RequestURI, "user/choose_user") {
  75. c.JSON(http.StatusOK, Response{10008, "请先选择账户"})
  76. c.Abort()
  77. return
  78. }
  79. // 单点登录检查
  80. singleSignTime := gjson.GetBytes([]byte(claims.Subject), "single_sign_time").String()
  81. if singleSignTime != utils.GetSingleSignTime(uid) &&
  82. !strings.Contains(c.Request.RequestURI, "user/choose_user") &&
  83. !strings.Contains(c.Request.RequestURI, "user/company_login"){
  84. c.JSON(http.StatusOK, Response{10008, "账号已在其他地方登录"})
  85. c.Abort()
  86. return
  87. }
  88. // 权限检查
  89. super := gjson.GetBytes([]byte(claims.Subject), "is_super_group").Bool()
  90. if super {
  91. c.Next()
  92. return
  93. }
  94. byCompany := gjson.GetBytes([]byte(claims.Subject), "by_company").Bool()
  95. if byCompany {
  96. c.Next()
  97. return
  98. }
  99. if c.Request.Method == "GET" {
  100. c.Next()
  101. return
  102. }
  103. routers := gjson.GetBytes([]byte(claims.Subject), "routers").Map()
  104. gPermissionTime := gjson.GetBytes([]byte(claims.Subject), "g_permission_time").String()
  105. uPermissionTime := gjson.GetBytes([]byte(claims.Subject), "u_permission_time").String()
  106. err = checkPermission(routers, c.Request.RequestURI, uid, gPermissionTime, uPermissionTime)
  107. if err != nil {
  108. if v, ok := status.FromError(err); ok {
  109. s=v
  110. }
  111. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  112. c.Abort()
  113. return
  114. }
  115. c.Next()
  116. }
  117. }