token.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package token
  2. import (
  3. "errors"
  4. "fmt"
  5. "mtp2_if/global/e"
  6. "mtp2_if/rediscli"
  7. "net/http"
  8. "strings"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // TouristToken 游客Token
  12. var TouristToken string = "c886a057f3d820d4dbc41473686c7c2d"
  13. // CheckToken Token校验
  14. func CheckToken(loginid string, token string) error {
  15. key := fmt.Sprintf("monitor:online_loginid::%s", loginid)
  16. field := "Token"
  17. realToken, err := rediscli.GetRedisClient().HGet(key, field).Result()
  18. if err != nil {
  19. return err
  20. }
  21. if realToken != token {
  22. return errors.New("token is invalid")
  23. }
  24. return nil
  25. }
  26. // Auth Token校验中间件
  27. func Auth() gin.HandlerFunc {
  28. return func(c *gin.Context) {
  29. // if config.SerCfg.GetDebugMode() {
  30. // c.Next()
  31. // return
  32. // }
  33. var code int
  34. var data interface{}
  35. code = e.SUCCESS
  36. token := c.GetHeader("Authorization")
  37. if token == "" {
  38. // Token缺失
  39. code = e.ERROR_AUTH_CHECK_TOKEN_MISSING
  40. } else {
  41. // 获取loginid
  42. s := strings.Split(token, "_")
  43. loginid := s[0]
  44. if err := CheckToken(loginid, token); err != nil {
  45. // Token错误
  46. code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
  47. }
  48. }
  49. // Token检验失败
  50. if code != e.SUCCESS {
  51. c.JSON(http.StatusUnauthorized, gin.H{
  52. "code": code,
  53. "msg": e.GetMsg(code),
  54. "data": data,
  55. })
  56. c.Abort()
  57. return
  58. }
  59. // FIXME: - 针对POST接口,应判断传入TOKEN对应的用户是否正确(比如判断UserID或AccountID是否对得上等),后期处理
  60. // Token检验成功
  61. c.Next()
  62. }
  63. }
  64. // AuthByHsby 游客鉴权
  65. func AuthByHsby() gin.HandlerFunc {
  66. return func(c *gin.Context) {
  67. // 包含accountID、accountIDs、userID和userIDs等参数需要走正常鉴权
  68. accountID := c.Query("accountID")
  69. accountIDs := c.Query("accountIDs")
  70. userID := c.Query("userID")
  71. userIDs := c.Query("userIDs")
  72. if len(accountID) != 0 || len(accountIDs) != 0 || len(userID) != 0 || len(userIDs) != 0 {
  73. realToken(c)
  74. return
  75. }
  76. var code int
  77. var data interface{}
  78. code = e.SUCCESS
  79. token := c.GetHeader("Authorization")
  80. if token == "" {
  81. // Token缺失
  82. code = e.ERROR_AUTH_CHECK_TOKEN_MISSING
  83. } else {
  84. // Token带下划线的走正常鉴权
  85. if strings.Contains(token, "_") {
  86. realToken(c)
  87. return
  88. }
  89. if token != TouristToken {
  90. // Token错误
  91. code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
  92. }
  93. }
  94. // Token检验失败
  95. if code != e.SUCCESS {
  96. c.JSON(http.StatusUnauthorized, gin.H{
  97. "code": code,
  98. "msg": e.GetMsg(code),
  99. "data": data,
  100. })
  101. c.Abort()
  102. return
  103. }
  104. // Token检验成功
  105. c.Next()
  106. }
  107. }
  108. func realToken(c *gin.Context) {
  109. // if config.SerCfg.GetDebugMode() {
  110. // c.Next()
  111. // return
  112. // }
  113. var code int
  114. var data interface{}
  115. code = e.SUCCESS
  116. token := c.GetHeader("Authorization")
  117. if token == "" {
  118. // Token缺失
  119. code = e.ERROR_AUTH_CHECK_TOKEN_MISSING
  120. } else {
  121. // 获取loginid
  122. s := strings.Split(token, "_")
  123. loginid := s[0]
  124. if err := CheckToken(loginid, token); err != nil {
  125. // Token错误
  126. code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
  127. }
  128. }
  129. // Token检验失败
  130. if code != e.SUCCESS {
  131. c.JSON(http.StatusUnauthorized, gin.H{
  132. "code": code,
  133. "msg": e.GetMsg(code),
  134. "data": data,
  135. })
  136. c.Abort()
  137. return
  138. }
  139. // FIXME: - 针对POST接口,应判断传入TOKEN对应的用户是否正确(比如判断UserID或AccountID是否对得上等),后期处理
  140. // Token检验成功
  141. c.Next()
  142. return
  143. }