auth.go 4.2 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. ////校验数据合法性(用户信息是否有效)
  50. //userKey := gid + ":" + pf + ":" + config.Get("app.user_table_key") + openid
  51. //userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
  52. //if err != nil {
  53. // common.RetJson(-1, "authorized invalid,redis cant find!", "", c)
  54. // c.Abort()
  55. // return
  56. //}
  57. //if len(userData) == 0 {
  58. // common.RetJson(-1, "用户信息不在数据库中", "", c)
  59. // c.Abort()
  60. // return
  61. //}
  62. ////校验请求次数
  63. //apiPath := c.FullPath()
  64. //key := fmt.Sprintf("%s:%s:%s:%s:%s", gid, config.Get("app.api_limit_key"), pf, openid, apiPath)
  65. //count, err := global.App.Redis.Incr(context.Background(), key).Result()
  66. //if err != nil {
  67. // common.RetJson(1001, "server error!", "", c)
  68. // c.Abort()
  69. // return
  70. //}
  71. //if count == 1 {
  72. // global.App.Redis.Expire(context.Background(), key, time.Minute).Result()
  73. //}
  74. //if count > config.GetInt64("app.api_limit_count") {
  75. // common.RetJson(1002, "too many requests!", "", c)
  76. // c.Abort()
  77. // return
  78. //}
  79. ////设置上下文数据
  80. //c.Set("openid", openid)
  81. //c.Set("gid", gid)
  82. //c.Set("pf", pf)
  83. ////如果校验通过
  84. }
  85. }
  86. /* token是否有效 */
  87. func isValidToken(token string) (bool, string, string, string) {
  88. openid, gid, pf, tokenType := common.ParseJwtWithClaims(token)
  89. //fmt.Printf("openid:%v,gid:%v,pf:%v,tokenType:%v", openid, gid, pf, tokenType)
  90. if openid == "" {
  91. return false, openid, gid, pf
  92. } else {
  93. //登录tonken 类型
  94. if tokenType == 0 {
  95. return true, openid, gid, pf
  96. }
  97. return false, openid, gid, pf
  98. }
  99. }
  100. /* 刷新token中间件 */
  101. func RefreshTokenAuthMiddleware() gin.HandlerFunc {
  102. return func(c *gin.Context) {
  103. token := c.GetHeader("Authorization")
  104. //校验token
  105. if token == "" {
  106. common.RetJson(-2, "Unauthorized", "", c)
  107. c.Abort()
  108. return
  109. }
  110. ok, openid, gid, pf := isValidRefreshToken(token)
  111. if !ok {
  112. common.RetJson(-1, "authorized invalid!", "", c)
  113. c.Abort()
  114. return
  115. }
  116. //校验请求次数
  117. apiPath := c.FullPath()
  118. key := fmt.Sprintf("%s:%s:%s:%s:%s", gid, config.Get("app.api_limit_key"), pf, openid, apiPath)
  119. count, err := global.App.Redis.Incr(context.Background(), key).Result()
  120. if err != nil {
  121. common.RetJson(1001, "server error!", "", c)
  122. c.Abort()
  123. return
  124. }
  125. if count == 1 {
  126. global.App.Redis.Expire(context.Background(), key, time.Minute).Result()
  127. }
  128. if count > config.GetInt64("app.api_limit_count") {
  129. common.RetJson(1002, "too many requests!", "", c)
  130. c.Abort()
  131. return
  132. }
  133. //设置上下文数据
  134. c.Set("openid", openid)
  135. c.Set("gid", gid)
  136. c.Set("pf", pf)
  137. //如果校验通过
  138. c.Next()
  139. }
  140. }
  141. /* token是否有效 */
  142. func isValidRefreshToken(token string) (bool, string, string, string) {
  143. openid, gid, pf, tokenType := common.ParseJwtWithClaims(token)
  144. //fmt.Printf("openid:%v,gid:%v", openid, gid)
  145. if openid == "" {
  146. return false, openid, gid, pf
  147. } else {
  148. //登录刷新tonken 类型
  149. if tokenType == 1 {
  150. return true, openid, gid, pf
  151. }
  152. return false, openid, gid, pf
  153. }
  154. }