token.go 3.7 KB

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