jwt.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "strings"
  13. "time"
  14. "xingjia-management-gateway/errors"
  15. )
  16. // 响应数据
  17. type Response struct {
  18. Code codes.Code `json:"code" default:"1"`
  19. Message string `json:"message" default:"success"`
  20. }
  21. // JWT is jwt middleware
  22. func Jwt() gin.HandlerFunc {
  23. return func(c *gin.Context) {
  24. s := status.New(1, "内部服务错误")
  25. token := c.GetHeader("token")
  26. if token == "" {
  27. if v, ok := status.FromError(errors.NoTokenError); ok {
  28. s = v
  29. }
  30. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  31. c.Abort()
  32. return
  33. }
  34. // 解析token
  35. claims, err := jwtwrapper.ParseToken(token)
  36. if err != nil {
  37. switch err.(*jwt.ValidationError).Errors {
  38. case jwt.ValidationErrorExpired:
  39. if v, ok := status.FromError(errors.TokenExpiredError); ok {
  40. s = v
  41. }
  42. default:
  43. if v, ok := status.FromError(errors.TokenFailedError); ok {
  44. s = v
  45. }
  46. }
  47. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  48. c.Abort()
  49. return
  50. }
  51. // 将claims信息保存到上下文,为后续使用
  52. c.Set("claims", claims)
  53. /*
  54. supper := gjson.GetBytes(utils.StrToBytes(claims.Subject), "supper").Bool()
  55. if c.Request.Method == "PUT" || c.Request.Method == "POST" || c.Request.Method == "DELETE" {
  56. if supper == false && strings.Contains(c.Request.RequestURI, "/user") == false {
  57. c.JSON(http.StatusOK, Response{10008, "权限不足"})
  58. }
  59. }
  60. */
  61. userType := gjson.GetBytes([]byte(claims.Subject), "user_type").Int()
  62. userEnd := gjson.GetBytes([]byte(claims.Subject), "effective_end").Int()
  63. userStart := gjson.GetBytes([]byte(claims.Subject), "effective_start").Int()
  64. if userType != 1 && userType != 2 && userType != 3 {
  65. if v, ok := status.FromError(errors.UserWrong); ok {
  66. s = v
  67. }
  68. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  69. c.Abort()
  70. return
  71. }
  72. if userType == 3 && (userEnd < time.Now().Unix() || userStart > time.Now().Unix()) {
  73. if v, ok := status.FromError(errors.UserNotEffective); ok {
  74. s = v
  75. }
  76. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  77. c.Abort()
  78. return
  79. }
  80. if userType != 1 {
  81. // 非超级管理员不能账号管理和日志查看
  82. if !strings.Contains(c.Request.RequestURI, "/jt/") {
  83. if v, ok := status.FromError(errors.PermissionError); ok {
  84. s = v
  85. }
  86. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  87. c.Abort()
  88. return
  89. }
  90. }
  91. // 进行下一次处理
  92. if gjson.GetBytes([]byte(claims.Subject), "tmp_token").Bool() {
  93. c.JSON(http.StatusOK, Response{10008, "无效凭据"})
  94. c.Abort()
  95. }
  96. c.Next()
  97. }
  98. }