jwt.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "fmt"
  6. "git.getensh.com/common/gopkgs/cache"
  7. "git.getensh.com/common/gopkgs/jwtwrapper"
  8. "github.com/dgrijalva/jwt-go"
  9. "github.com/gin-gonic/gin"
  10. "github.com/tidwall/gjson"
  11. "google.golang.org/grpc/codes"
  12. "google.golang.org/grpc/status"
  13. "net/http"
  14. "property-household-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. const TokenUidKey = "household_token_uid"
  22. func cacheTokenUid(uid string) {
  23. if uid == "" {
  24. return
  25. }
  26. key := fmt.Sprintf("%s%s", TokenUidKey, uid)
  27. cache.Redis().SetEx(key, 600, "1")
  28. }
  29. func tokenUidExist(uid string) bool {
  30. key := fmt.Sprintf("%s%s", TokenUidKey, uid)
  31. r, _ := cache.Redis().Get(key)
  32. return r == "1"
  33. }
  34. // JWT is jwt middleware
  35. func Jwt() gin.HandlerFunc {
  36. return func(c *gin.Context) {
  37. s := status.New(1, "内部服务错误")
  38. token := c.GetHeader("token")
  39. if token == "" {
  40. if v, ok := status.FromError(errors.NoTokenError); ok {
  41. s = v
  42. }
  43. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  44. c.Abort()
  45. return
  46. }
  47. // 解析token
  48. claims, err := jwtwrapper.ParseToken(token)
  49. if err != nil {
  50. switch err.(*jwt.ValidationError).Errors {
  51. case jwt.ValidationErrorExpired:
  52. /*
  53. if claims != nil && tokenUidExist(claims.Id) {
  54. if v, ok := status.FromError(errors.TokenNeedRefresh); ok {
  55. s = v
  56. }
  57. } else {
  58. if v, ok := status.FromError(errors.TokenExpiredError); ok {
  59. s = v
  60. }
  61. }*/
  62. if v, ok := status.FromError(errors.TokenNeedRefresh); ok {
  63. s = v
  64. }
  65. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  66. c.Abort()
  67. return
  68. default:
  69. if v, ok := status.FromError(errors.TokenFailedError); ok {
  70. s = v
  71. }
  72. c.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  73. c.Abort()
  74. return
  75. }
  76. }
  77. //cacheTokenUid(claims.Id)
  78. // 将claims信息保存到上下文,为后续使用
  79. c.Set("claims", claims)
  80. /*
  81. supper := gjson.GetBytes(utils.StrToBytes(claims.Subject), "supper").Bool()
  82. if c.Request.Method == "PUT" || c.Request.Method == "POST" || c.Request.Method == "DELETE" {
  83. if supper == false && strings.Contains(c.Request.RequestURI, "/user") == false {
  84. c.JSON(http.StatusOK, Response{10008, "权限不足"})
  85. }
  86. }
  87. */
  88. // 进行下一次处理
  89. if gjson.GetBytes([]byte(claims.Subject), "tmp_token").Bool() {
  90. c.JSON(http.StatusOK, Response{10008, "无效凭据"})
  91. c.Abort()
  92. }
  93. c.Next()
  94. }
  95. }