sign.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package http
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "strings"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. const (
  11. _sobotAppKey = "bcef69bb71499209"
  12. _sobotAppSecret = "ace486f144f1467eefdce1fe5dfc7b14"
  13. _sobotAPI = "https://sso-api.bilibili.co/x/internal/workflow/sobot/user"
  14. )
  15. func sobotSign(handler func(*bm.Context)) func(*bm.Context) {
  16. return func(c *bm.Context) {
  17. req := c.Request
  18. query := req.Form
  19. if query.Get("ts") == "" {
  20. log.Error("ts is empty")
  21. c.JSON(nil, ecode.RequestErr)
  22. return
  23. }
  24. sign := query.Get("sign")
  25. query.Del("sign")
  26. sappkey := query.Get("appkey")
  27. if sappkey != _sobotAppKey {
  28. log.Error("appkey not matched")
  29. c.JSON(nil, ecode.RequestErr)
  30. return
  31. }
  32. query.Set("appsecret", _sobotAppSecret)
  33. tmp := query.Encode()
  34. if strings.IndexByte(tmp, '+') > -1 {
  35. tmp = strings.Replace(tmp, "+", "%20", -1)
  36. }
  37. mh := md5.Sum([]byte(_sobotAPI + "?" + strings.ToLower(tmp) + _sobotAppSecret))
  38. if hex.EncodeToString(mh[:]) != sign {
  39. mh1 := md5.Sum([]byte(_sobotAPI + "?" + tmp + _sobotAppSecret))
  40. if hex.EncodeToString(mh1[:]) != sign {
  41. log.Error("Get sign: %s, expect %x", sign, mh1)
  42. c.JSON(nil, ecode.SignCheckErr)
  43. return
  44. }
  45. }
  46. handler(c)
  47. }
  48. }