Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

66 lines
1.5KB

  1. package main
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "io/ioutil"
  7. "log"
  8. )
  9. //WechatAPIConfig all secret related API Config
  10. type WechatAPIConfig struct {
  11. //Token is the wechat API shared secrete
  12. Token string `json:"Token"`
  13. //EncodingAESKey is the Key for encrypt messages
  14. EncodingAESKey string `'json:"EncodingAESKey"`
  15. //Appid is wechat public account appid
  16. Appid string `json:"Appid"`
  17. //AppSecret is how we identify ourselves.
  18. AppSecret string `json:"AppSecret"`
  19. //AuthTokenSaveTo
  20. AuthTokenSaveTo string `json:"AuthTokenSaveTo"`
  21. }
  22. //APIConfig contains secrets that cannot store in source file
  23. var APIConfig WechatAPIConfig
  24. func readConfig() error {
  25. log.Printf("read config from %s\r\n", "server_config.json")
  26. body, err := ioutil.ReadFile("server_config.json")
  27. if err != nil {
  28. log.Fatal("Cannot read config from server_config.json")
  29. return err
  30. }
  31. err = json.Unmarshal(body, &APIConfig)
  32. if APIConfig.AppSecret == "" {
  33. return errors.New("AppSecret not available")
  34. }
  35. if APIConfig.Appid == "" {
  36. return errors.New("AppId not available")
  37. }
  38. if APIConfig.AuthTokenSaveTo == "" {
  39. return errors.New("where to save accesstoken is unknown")
  40. }
  41. if APIConfig.EncodingAESKey == "" {
  42. return errors.New("unknown AesEncryptionKey")
  43. }
  44. if APIConfig.Token == "" {
  45. return errors.New("hidden shared secret token is empty")
  46. }
  47. return err
  48. }
  49. //convert AesEncryptKey into Binary 32bytes
  50. func getAesEncryptKey() [32]byte {
  51. key, _ := base64.StdEncoding.DecodeString(APIConfig.EncodingAESKey + "=")
  52. var k [32]byte
  53. copy(k[:], key)
  54. return k
  55. }