audit.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package http
  2. import (
  3. "crypto/md5"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "sort"
  8. "go-common/app/interface/main/tv/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. )
  13. // audit season with content
  14. func audit(c *bm.Context) {
  15. if err := auditT(c); err != nil { // if some error, return it
  16. c.JSON(nil, err)
  17. return
  18. }
  19. c.JSON(nil, nil)
  20. }
  21. func auditT(c *bm.Context) (err error) {
  22. var (
  23. audit model.Audit
  24. req = c.Request
  25. )
  26. defer req.Body.Close()
  27. body, _ := ioutil.ReadAll(req.Body)
  28. if err = json.Unmarshal(body, &audit); err != nil {
  29. log.Error("audit json(%s) error:(%v)", string(body), err)
  30. err = ecode.RequestErr
  31. return
  32. }
  33. if !validateJSONData(audit) {
  34. log.Error("audit msg (%s), missing field", string(body))
  35. err = ecode.RequestErr
  36. return
  37. }
  38. if !checkSign(c, string(body)) {
  39. log.Error("audit msg (%s), sign error", string(body))
  40. err = ecode.RequestErr
  41. return
  42. }
  43. return auditSvc.HandleAudits(c, audit.IDList)
  44. }
  45. // validateJSONData check json format whether valid
  46. func validateJSONData(a model.Audit) bool {
  47. if a.OpType == "" {
  48. return false
  49. }
  50. for _, v := range a.IDList {
  51. if v.Type == "" || v.VID == "" || v.Action == "" {
  52. return false
  53. }
  54. }
  55. return a.Count > 0
  56. }
  57. // checkSign check sign whether valid
  58. func checkSign(c *bm.Context, body string) bool {
  59. var (
  60. req = c.Request.Form
  61. query = make(map[string]string)
  62. ts = req.Get("ts")
  63. key = req.Get("key")
  64. sign = req.Get("sign")
  65. )
  66. if key != signCfg.Key {
  67. log.Error("The appkey not exists")
  68. return false
  69. }
  70. if ts == "" {
  71. log.Error("The timestamp not exists")
  72. return false
  73. }
  74. query["ts"] = ts
  75. query["body"] = body
  76. query["appkey"] = key
  77. if sign == "" {
  78. log.Error("The sign not exists")
  79. return false
  80. }
  81. getSign := signature(query)
  82. if sign != getSign {
  83. log.Error("The expected signature is :(%s)", getSign)
  84. return false
  85. }
  86. return sign == getSign
  87. }
  88. func signature(query map[string]string) string {
  89. secret := signCfg.Secret
  90. var keys []string
  91. for k := range query {
  92. keys = append(keys, k)
  93. }
  94. sort.Strings(keys)
  95. var str string
  96. for _, v := range keys {
  97. str += string(v) + "=" + query[v] + "&"
  98. }
  99. str = str[:len(str)-1] + secret
  100. hash := md5.New()
  101. hash.Write([]byte(str))
  102. sign := fmt.Sprintf("%x", hash.Sum(nil))
  103. return sign
  104. }