token.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package token
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "mtp2_if/config"
  9. "mtp2_if/global/e"
  10. "mtp2_if/rediscli"
  11. "net/http"
  12. "runtime"
  13. "strings"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // TouristToken 游客Token
  17. var TouristToken string = "c886a057f3d820d4dbc41473686c7c2d"
  18. // CheckToken Token校验
  19. func CheckToken(loginid string, token string, group string) (string, error) {
  20. key := ""
  21. if len(group) == 0 {
  22. key = fmt.Sprintf("monitor:online_loginid::%s", loginid)
  23. } else {
  24. key = fmt.Sprintf("monitor:online_loginid:%s:%s", loginid, group)
  25. }
  26. field := "Token"
  27. realToken, err := rediscli.GetRedisClient().HGet(key, field).Result()
  28. if err != nil {
  29. return "", err
  30. }
  31. if realToken != token {
  32. return "", errors.New("token is invalid")
  33. }
  34. // 获取UserID
  35. userID, err := rediscli.GetRedisClient().HGet(key, "UserID").Result()
  36. return userID, err
  37. }
  38. // Auth Token校验中间件
  39. func Auth() gin.HandlerFunc {
  40. return func(c *gin.Context) {
  41. // if config.SerCfg.GetDebugMode() {
  42. // c.Next()
  43. // return
  44. // }
  45. // windows下方便开发调试, 不做token校验
  46. if config.SerCfg.GetDebugMode() &&
  47. runtime.GOOS == "windows" {
  48. c.Next()
  49. return
  50. }
  51. var code int
  52. var data interface{}
  53. userID := ""
  54. code = e.SUCCESS
  55. token := c.GetHeader("Authorization")
  56. if token == "" {
  57. // Token缺失
  58. code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
  59. } else {
  60. // 获取loginid
  61. s := strings.Split(token, "_")
  62. loginid := s[0]
  63. // 支持分组功能
  64. group := ""
  65. if len(s) == 3 {
  66. group = s[2]
  67. }
  68. var err error
  69. userID, err = CheckToken(loginid, token, group)
  70. if err != nil {
  71. // Token错误
  72. code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
  73. }
  74. }
  75. // Token检验失败
  76. if code != e.SUCCESS {
  77. c.JSON(http.StatusUnauthorized, gin.H{
  78. "code": code,
  79. "msg": e.GetMsg(code),
  80. "data": data,
  81. })
  82. c.Abort()
  83. return
  84. }
  85. // FIXME: - 针对POST接口,应判断传入TOKEN对应的用户是否正确(比如判断UserID或AccountID是否对得上等),后期处理
  86. if c.Request.Method == "POST" {
  87. c.Set("requserid", userID)
  88. }
  89. if config.SerCfg.GetApiKeyMode() {
  90. timestamp := c.GetHeader("Timestamp")
  91. verification := c.GetHeader("Verification")
  92. if timestamp == "" || token == "" || verification == "" {
  93. c.JSON(http.StatusUnauthorized, gin.H{
  94. "code": e.ERROR,
  95. "msg": "缺少检验参数",
  96. "data": struct{}{},
  97. })
  98. c.Abort()
  99. return
  100. }
  101. s := fmt.Sprintf("%s%s", token, timestamp)
  102. hashed := hmac.New(sha256.New, []byte(config.SerCfg.WebCfg.ApiKey))
  103. hashed.Write([]byte(s))
  104. h := hex.EncodeToString(hashed.Sum(nil))
  105. if h == "" {
  106. c.JSON(http.StatusUnauthorized, gin.H{
  107. "code": e.ERROR,
  108. "msg": "接口检验失败",
  109. "data": struct{}{},
  110. })
  111. c.Abort()
  112. return
  113. }
  114. if h != verification {
  115. c.JSON(http.StatusUnauthorized, gin.H{
  116. "code": e.ERROR,
  117. "msg": "非法调用接口",
  118. "data": struct{}{},
  119. })
  120. c.Abort()
  121. return
  122. }
  123. }
  124. // Token检验成功
  125. c.Next()
  126. }
  127. }
  128. // AuthByHsby 游客鉴权
  129. func AuthByHsby() gin.HandlerFunc {
  130. return func(c *gin.Context) {
  131. // 包含accountID、accountIDs、userID和userIDs等参数需要走正常鉴权
  132. accountID := c.Query("accountID")
  133. accountIDs := c.Query("accountIDs")
  134. userID := c.Query("userID")
  135. userIDs := c.Query("userIDs")
  136. loginID := c.Query("loginID")
  137. if len(accountID) != 0 || len(accountIDs) != 0 || len(userID) != 0 || len(userIDs) != 0 || len(loginID) != 0 {
  138. realToken(c)
  139. return
  140. }
  141. var code int
  142. var data interface{}
  143. code = e.SUCCESS
  144. token := c.GetHeader("Authorization")
  145. if token == "" {
  146. // Token缺失
  147. code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
  148. } else {
  149. // Token带下划线的走正常鉴权
  150. if strings.Contains(token, "_") {
  151. realToken(c)
  152. return
  153. }
  154. if token != TouristToken {
  155. // Token错误
  156. code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
  157. }
  158. }
  159. // Token检验失败
  160. if code != e.SUCCESS {
  161. c.JSON(http.StatusUnauthorized, gin.H{
  162. "code": code,
  163. "msg": e.GetMsg(code),
  164. "data": data,
  165. })
  166. c.Abort()
  167. return
  168. }
  169. // Token检验成功
  170. c.Next()
  171. }
  172. }
  173. func realToken(c *gin.Context) {
  174. // if config.SerCfg.GetDebugMode() {
  175. // c.Next()
  176. // return
  177. // }
  178. var code int
  179. var data interface{}
  180. code = e.SUCCESS
  181. token := c.GetHeader("Authorization")
  182. if token == "" {
  183. // Token缺失
  184. code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
  185. } else {
  186. // 获取loginid
  187. s := strings.Split(token, "_")
  188. loginid := s[0]
  189. // 支持分组功能
  190. group := ""
  191. if len(s) == 3 {
  192. group = s[2]
  193. }
  194. if _, err := CheckToken(loginid, token, group); err != nil {
  195. // Token错误
  196. code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
  197. }
  198. }
  199. // Token检验失败
  200. if code != e.SUCCESS {
  201. c.JSON(http.StatusUnauthorized, gin.H{
  202. "code": code,
  203. "msg": e.GetMsg(code),
  204. "data": data,
  205. })
  206. c.Abort()
  207. return
  208. }
  209. // FIXME: - 针对POST接口,应判断传入TOKEN对应的用户是否正确(比如判断UserID或AccountID是否对得上等),后期处理
  210. // Token检验成功
  211. c.Next()
  212. return
  213. }