You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.9KB

  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. //PublicAccountID
  22. PublicAccountID string `json:"PublicAccountID"`
  23. }
  24. //APIConfig contains secrets that cannot store in source file
  25. var APIConfig WechatAPIConfig
  26. type EspoCRMAPIConfig struct {
  27. //where the EspoCRM is installed
  28. BaseURL string `json:"BaseURL"`
  29. //access UserName
  30. UserName string `json:"UserName"`
  31. //access Password
  32. UserPass string `json:"password"`
  33. }
  34. //CRMConfig contains secrets that cannot store in source file
  35. //
  36. var CRMConfig EspoCRMAPIConfig
  37. func readConfig() error {
  38. log.Printf("read config from %s\r\n", "server_config.json")
  39. body, err := ioutil.ReadFile("server_config.json")
  40. if err != nil {
  41. log.Fatal("Cannot read config from server_config.json")
  42. return err
  43. }
  44. err = json.Unmarshal(body, &APIConfig)
  45. if APIConfig.AppSecret == "" {
  46. return errors.New("AppSecret not available")
  47. }
  48. if APIConfig.Appid == "" {
  49. return errors.New("AppId not available")
  50. }
  51. if APIConfig.AuthTokenSaveTo == "" {
  52. return errors.New("where to save accesstoken is unknown")
  53. }
  54. if APIConfig.EncodingAESKey == "" {
  55. return errors.New("unknown AesEncryptionKey")
  56. }
  57. if APIConfig.Token == "" {
  58. return errors.New("hidden shared secret token is empty")
  59. }
  60. return err
  61. }
  62. //convert AesEncryptKey into Binary 32bytes
  63. func getAesEncryptKey() [32]byte {
  64. key, _ := base64.StdEncoding.DecodeString(APIConfig.EncodingAESKey + "=")
  65. var k [32]byte
  66. copy(k[:], key)
  67. return k
  68. }