auth.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package middleware
  2. import (
  3. "context"
  4. "designs/common"
  5. "designs/config"
  6. "designs/global"
  7. "fmt"
  8. "net/http"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. )
  12. /* 包体大小 */
  13. func LimitRequestBodySize(maxSize int64) gin.HandlerFunc {
  14. return func(c *gin.Context) {
  15. c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxSize)
  16. if err := c.Request.ParseForm(); err != nil {
  17. c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": "Request body too large"})
  18. c.Abort()
  19. return
  20. }
  21. c.Next()
  22. }
  23. }
  24. /* token中间件 */
  25. func TokenAuthMiddleware() gin.HandlerFunc {
  26. return func(c *gin.Context) {
  27. //contentLength := c.Request.ContentLength
  28. //if contentLength > config.GetInt64("app.max_content") {
  29. // // 输出请求体的大小
  30. // // fmt.Printf("Request body size: %d bytes\n", contentLength)
  31. // common.RetJson(1003, "ruquest too max", "", c)
  32. // c.Abort()
  33. // return
  34. //}
  35. token := c.GetHeader("Authorization")
  36. //校验token
  37. if token == "" {
  38. common.RetJson(-2, "Unauthorized", "", c)
  39. c.Abort()
  40. return
  41. }
  42. ok, openid, gid, pf := isValidToken(token)
  43. if !ok {
  44. common.RetJson(-1, "authorized invalid!", "", c)
  45. c.Abort()
  46. return
  47. }
  48. //校验数据合法性(用户信息是否有效)
  49. userKey := gid + ":" + pf + ":" + config.Get("app.user_table_key") + openid
  50. userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
  51. if err != nil {
  52. common.RetJson(-1, "authorized invalid,redis cant find!", "", c)
  53. c.Abort()
  54. return
  55. }
  56. if len(userData) == 0 {
  57. common.RetJson(-1, "用户信息不在数据库中", "", c)
  58. c.Abort()
  59. return
  60. }
  61. ////校验请求次数
  62. //apiPath := c.FullPath()
  63. //key := fmt.Sprintf("%s:%s:%s:%s:%s", gid, config.Get("app.api_limit_key"), pf, openid, apiPath)
  64. //count, err := global.App.Redis.Incr(context.Background(), key).Result()
  65. //if err != nil {
  66. // common.RetJson(1001, "server error!", "", c)
  67. // c.Abort()
  68. // return
  69. //}
  70. //if count == 1 {
  71. // global.App.Redis.Expire(context.Background(), key, time.Minute).Result()
  72. //}
  73. //if count > config.GetInt64("app.api_limit_count") {
  74. // common.RetJson(1002, "too many requests!", "", c)
  75. // c.Abort()
  76. // return
  77. //}
  78. //设置上下文数据
  79. c.Set("openid", openid)
  80. c.Set("gid", gid)
  81. c.Set("pf", pf)
  82. //如果校验通过
  83. }
  84. }
  85. /* token是否有效 */
  86. func isValidToken(token string) (bool, string, string, string) {
  87. openid, gid, pf, tokenType := common.ParseJwtWithClaims(token)
  88. //fmt.Printf("openid:%v,gid:%v,pf:%v,tokenType:%v", openid, gid, pf, tokenType)
  89. if openid == "" {
  90. return false, openid, gid, pf
  91. } else {
  92. //登录tonken 类型
  93. if tokenType == 0 {
  94. return true, openid, gid, pf
  95. }
  96. return false, openid, gid, pf
  97. }
  98. }
  99. /* 刷新token中间件 */
  100. func RefreshTokenAuthMiddleware() gin.HandlerFunc {
  101. return func(c *gin.Context) {
  102. token := c.GetHeader("Authorization")
  103. //校验token
  104. if token == "" {
  105. common.RetJson(-2, "Unauthorized", "", c)
  106. c.Abort()
  107. return
  108. }
  109. ok, openid, gid, pf := isValidRefreshToken(token)
  110. if !ok {
  111. common.RetJson(-1, "authorized invalid!", "", c)
  112. c.Abort()
  113. return
  114. }
  115. //校验请求次数
  116. apiPath := c.FullPath()
  117. key := fmt.Sprintf("%s:%s:%s:%s:%s", gid, config.Get("app.api_limit_key"), pf, openid, apiPath)
  118. count, err := global.App.Redis.Incr(context.Background(), key).Result()
  119. if err != nil {
  120. common.RetJson(1001, "server error!", "", c)
  121. c.Abort()
  122. return
  123. }
  124. if count == 1 {
  125. global.App.Redis.Expire(context.Background(), key, time.Minute).Result()
  126. }
  127. if count > config.GetInt64("app.api_limit_count") {
  128. common.RetJson(1002, "too many requests!", "", c)
  129. c.Abort()
  130. return
  131. }
  132. //设置上下文数据
  133. c.Set("openid", openid)
  134. c.Set("gid", gid)
  135. c.Set("pf", pf)
  136. //如果校验通过
  137. c.Next()
  138. }
  139. }
  140. /* token是否有效 */
  141. func isValidRefreshToken(token string) (bool, string, string, string) {
  142. openid, gid, pf, tokenType := common.ParseJwtWithClaims(token)
  143. //fmt.Printf("openid:%v,gid:%v", openid, gid)
  144. if openid == "" {
  145. return false, openid, gid, pf
  146. } else {
  147. //登录刷新tonken 类型
  148. if tokenType == 1 {
  149. return true, openid, gid, pf
  150. }
  151. return false, openid, gid, pf
  152. }
  153. }