auth_cipher_option.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. package option
  3. import (
  4. "context"
  5. "crypto/rsa"
  6. "crypto/x509"
  7. "git.nanodreamtech.com/sg/wechatpay-go/core"
  8. "git.nanodreamtech.com/sg/wechatpay-go/core/auth/signers"
  9. "git.nanodreamtech.com/sg/wechatpay-go/core/auth/validators"
  10. "git.nanodreamtech.com/sg/wechatpay-go/core/auth/verifiers"
  11. "git.nanodreamtech.com/sg/wechatpay-go/core/cipher/ciphers"
  12. "git.nanodreamtech.com/sg/wechatpay-go/core/cipher/decryptors"
  13. "git.nanodreamtech.com/sg/wechatpay-go/core/cipher/encryptors"
  14. "git.nanodreamtech.com/sg/wechatpay-go/core/downloader"
  15. )
  16. type withAuthCipherOption struct{ settings core.DialSettings }
  17. // Apply 设置 core.DialSettings 的 Signer、Validator 以及 Cipher
  18. func (w withAuthCipherOption) Apply(o *core.DialSettings) error {
  19. o.Signer = w.settings.Signer
  20. o.Validator = w.settings.Validator
  21. o.Cipher = w.settings.Cipher
  22. return nil
  23. }
  24. // WithWechatPayAuthCipher 一键初始化 Client,使其具备「签名/验签/敏感字段加解密」能力
  25. // Deprecated: 使用 WithWechatPayAutoAuthCipher 或 WithWechatPayPublicKeyAuthCipher 代替
  26. func WithWechatPayAuthCipher(
  27. mchID string, certificateSerialNo string, privateKey *rsa.PrivateKey, certificateList []*x509.Certificate,
  28. ) core.ClientOption {
  29. certGetter := core.NewCertificateMapWithList(certificateList)
  30. return withAuthCipherOption{
  31. settings: core.DialSettings{
  32. Signer: &signers.SHA256WithRSASigner{
  33. MchID: mchID,
  34. PrivateKey: privateKey,
  35. CertificateSerialNo: certificateSerialNo,
  36. },
  37. Validator: validators.NewWechatPayResponseValidator(verifiers.NewSHA256WithRSAVerifier(certGetter)),
  38. Cipher: ciphers.NewWechatPayCipher(
  39. encryptors.NewWechatPayEncryptor(certGetter),
  40. decryptors.NewWechatPayDecryptor(privateKey),
  41. ),
  42. },
  43. }
  44. }
  45. // WithWechatPayAutoAuthCipher 一键初始化 Client,使其具备「签名/验签/敏感字段加解密」能力。
  46. // 同时提供证书定时更新功能(因此需要提供 mchAPIv3Key 用于证书解密),不再需要本地提供平台证书
  47. func WithWechatPayAutoAuthCipher(
  48. mchID string, certificateSerialNo string, privateKey *rsa.PrivateKey, mchAPIv3Key string,
  49. ) core.ClientOption {
  50. mgr := downloader.MgrInstance()
  51. if !mgr.HasDownloader(context.Background(), mchID) {
  52. err := mgr.RegisterDownloaderWithPrivateKey(
  53. context.Background(), privateKey, certificateSerialNo, mchID, mchAPIv3Key,
  54. )
  55. if err != nil {
  56. return core.ErrorOption{Error: err}
  57. }
  58. }
  59. return WithWechatPayAutoAuthCipherUsingDownloaderMgr(mchID, certificateSerialNo, privateKey, mgr)
  60. }
  61. // WithWechatPayAutoAuthCipherUsingDownloaderMgr 一键初始化 Client,使其具备「签名/验签/敏感字段加解密」能力。
  62. // 需要使用者自行提供 CertificateDownloaderMgr 已实现平台证书的自动更新
  63. //
  64. // 【注意】本函数的能力与 WithWechatPayAutoAuthCipher 完全一致,除非有自行管理 CertificateDownloaderMgr 的需求,
  65. // 否则推荐直接使用 WithWechatPayAutoAuthCipher
  66. func WithWechatPayAutoAuthCipherUsingDownloaderMgr(
  67. mchID string, certificateSerialNo string, privateKey *rsa.PrivateKey, mgr *downloader.CertificateDownloaderMgr,
  68. ) core.ClientOption {
  69. certVisitor := mgr.GetCertificateVisitor(mchID)
  70. return withAuthCipherOption{
  71. settings: core.DialSettings{
  72. Signer: &signers.SHA256WithRSASigner{
  73. MchID: mchID,
  74. CertificateSerialNo: certificateSerialNo,
  75. PrivateKey: privateKey,
  76. },
  77. Validator: validators.NewWechatPayResponseValidator(verifiers.NewSHA256WithRSAVerifier(certVisitor)),
  78. Cipher: ciphers.NewWechatPayCipher(
  79. encryptors.NewWechatPayEncryptor(certVisitor),
  80. decryptors.NewWechatPayDecryptor(privateKey),
  81. ),
  82. },
  83. }
  84. }
  85. // WithWechatPayPublicKeyAuthCipher 一键初始化 Client,使其具备「签名/验签/敏感字段加解密」能力。
  86. // 使用微信支付提供的公钥验签
  87. func WithWechatPayPublicKeyAuthCipher(
  88. mchID, certificateSerialNo string, privateKey *rsa.PrivateKey, publicKeyID string, publicKey *rsa.PublicKey,
  89. ) core.ClientOption {
  90. return withAuthCipherOption{
  91. settings: core.DialSettings{
  92. Signer: &signers.SHA256WithRSASigner{
  93. MchID: mchID,
  94. CertificateSerialNo: certificateSerialNo,
  95. PrivateKey: privateKey,
  96. },
  97. Validator: validators.NewWechatPayResponseValidator(
  98. verifiers.NewSHA256WithRSAPubkeyVerifier(
  99. publicKeyID,
  100. *publicKey,
  101. )),
  102. Cipher: ciphers.NewWechatPayCipher(
  103. encryptors.NewWechatPayPubKeyEncryptor(publicKeyID, *publicKey),
  104. decryptors.NewWechatPayDecryptor(privateKey),
  105. ),
  106. },
  107. }
  108. }