auth.go 3.6 KB

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