token.go 5.5 KB

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