auth.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 := 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. //如果校验通过
  69. }
  70. }
  71. /* token是否有效 */
  72. func isValidToken(token string) (bool, string) {
  73. userName, tokenType := common.ParseJwtWithClaims(token)
  74. //fmt.Printf("openid:%v,gid:%v,pf:%v,tokenType:%v", openid, gid, pf, tokenType)
  75. if userName == "" {
  76. return false, userName
  77. } else {
  78. //登录tonken 类型
  79. if tokenType == 0 {
  80. return true, userName
  81. }
  82. return false, userName
  83. }
  84. }
  85. /* 刷新token中间件 */
  86. func RefreshTokenAuthMiddleware() gin.HandlerFunc {
  87. return func(c *gin.Context) {
  88. token := c.GetHeader("Authorization")
  89. //校验token
  90. if token == "" {
  91. response.Fail(c, -2, "Unauthorized")
  92. c.Abort()
  93. return
  94. }
  95. ok, userName := isValidRefreshToken(token)
  96. if !ok {
  97. response.Fail(c, -1, "authorized invalid!")
  98. c.Abort()
  99. return
  100. }
  101. //校验请求次数
  102. apiPath := c.FullPath()
  103. key := fmt.Sprintf("%s:%s", config.Get("app.api_limit_key"), apiPath)
  104. count, err := global.App.Redis.Incr(context.Background(), key).Result()
  105. if err != nil {
  106. response.Fail(c, 1001, "server error!")
  107. c.Abort()
  108. return
  109. }
  110. if count == 1 {
  111. global.App.Redis.Expire(context.Background(), key, time.Minute).Result()
  112. }
  113. if count > config.GetInt64("app.api_limit_count") {
  114. response.Fail(c, 1002, "too many requests!")
  115. c.Abort()
  116. return
  117. }
  118. //设置上下文数据
  119. c.Set("userName", userName)
  120. //如果校验通过
  121. c.Next()
  122. }
  123. }
  124. /* token是否有效 */
  125. func isValidRefreshToken(token string) (bool, string) {
  126. userName, tokenType := common.ParseJwtWithClaims(token)
  127. //fmt.Printf("openid:%v,gid:%v", openid, gid)
  128. if userName == "" {
  129. return false, userName
  130. } else {
  131. //登录刷新tonken 类型
  132. if tokenType == 1 {
  133. return true, userName
  134. }
  135. return false, userName
  136. }
  137. }