middleware.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package handlers
  2. import (
  3. "github.com/spf13/viper"
  4. "net/http"
  5. "passport/app/models"
  6. "passport/pkg/ierr"
  7. "passport/pkg/jwt"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. )
  11. var Cors = func(c *gin.Context) {
  12. method := c.Request.Method
  13. c.Header("Access-Control-Allow-Origin", "*")
  14. c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
  15. c.Header("Access-Control-Allow-Headers", "*")
  16. if method == "OPTIONS" {
  17. c.AbortWithStatus(http.StatusNoContent)
  18. }
  19. c.Next()
  20. }
  21. func AuthToken() gin.HandlerFunc {
  22. //这里不管是什么身份只要登录了就可以
  23. return func(c *gin.Context) {
  24. token := c.GetHeader("shopManageToken")
  25. if token == "" {
  26. token = c.Query("shopManageToken")
  27. }
  28. if token == "" {
  29. ResponseErr(c, ierr.NewIError(ierr.NotAllowed, "need login"))
  30. c.Abort()
  31. return
  32. }
  33. res, err := jwt.ParseToken(token, viper.GetString("token_secret"))
  34. if err != nil {
  35. ResponseErr(c, ierr.NewIError(ierr.NotAllowed, err.Error()))
  36. c.Abort()
  37. return
  38. }
  39. if res.Expire < time.Now().Unix() {
  40. ResponseErr(c, ierr.NewIError(ierr.TokenExpire, "token expire"))
  41. c.Abort()
  42. return
  43. }
  44. c.Set("uid", res.UserId)
  45. c.Next()
  46. }
  47. }
  48. func AdminToken() gin.HandlerFunc {
  49. return func(c *gin.Context) {
  50. token := c.GetHeader("shopManageToken")
  51. if token == "" {
  52. token = c.Query("shopManageToken")
  53. }
  54. if token == "" {
  55. ResponseErr(c, ierr.NewIError(ierr.NotAllowed, "need login"))
  56. c.Abort()
  57. return
  58. }
  59. res, err := jwt.ParseToken(token, viper.GetString("token_secret"))
  60. if err != nil {
  61. ResponseErr(c, ierr.NewIError(ierr.NotAllowed, err.Error()))
  62. c.Abort()
  63. return
  64. }
  65. if res.Expire < time.Now().Unix() {
  66. ResponseErr(c, ierr.NewIError(ierr.TokenExpire, "token expire"))
  67. c.Abort()
  68. return
  69. }
  70. if res.Role != models.RoleAdmin {
  71. ResponseErr(c, ierr.NewIError(ierr.NotAllowed, "have no admin access"))
  72. c.Abort()
  73. return
  74. }
  75. c.Set("uid", res.UserId)
  76. c.Next()
  77. }
  78. }
  79. func MerchantToken() gin.HandlerFunc {
  80. return func(c *gin.Context) {
  81. token := c.GetHeader("shopManageToken")
  82. if token == "" {
  83. token = c.Query("shopManageToken")
  84. }
  85. if token == "" {
  86. ResponseErr(c, ierr.NewIError(ierr.NotAllowed, "need login"))
  87. c.Abort()
  88. return
  89. }
  90. res, err := jwt.ParseToken(token, viper.GetString("token_secret"))
  91. if err != nil {
  92. ResponseErr(c, ierr.NewIError(ierr.NotAllowed, err.Error()))
  93. c.Abort()
  94. return
  95. }
  96. if res.Expire < time.Now().Unix() {
  97. ResponseErr(c, ierr.NewIError(ierr.TokenExpire, "token expire"))
  98. c.Abort()
  99. return
  100. }
  101. if res.Role != models.RoleMerchant {
  102. ResponseErr(c, ierr.NewIError(ierr.NotAllowed, "have no merchant access"))
  103. c.Abort()
  104. return
  105. }
  106. c.Set("uid", res.UserId)
  107. c.Next()
  108. }
  109. }