auth.go 4.4 KB

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