Skip to content

Commit df51a10

Browse files
authored
feature/wx_cert_verify (#430)
* update wechat support cert verify sign
1 parent 9573af5 commit df51a10

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

wechat/v3/cert.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"crypto/sha256"
99
"encoding/base64"
1010
"encoding/json"
11+
"errors"
1112
"fmt"
1213
"net/http"
1314
"runtime"
@@ -131,16 +132,17 @@ func GetPlatformSM2Certs(ctx context.Context, mchid, apiV3Key, serialNo, private
131132
// 注意1:如已开启自动验签功能 client.AutoVerifySign(),无需再调用此方法设置
132133
// 注意2:请预先通过 wechat.GetPlatformCerts() 获取 微信平台公钥证书 和 证书序列号
133134
// 部分接口请求参数中敏感信息加密,使用此 微信支付平台公钥 和 证书序列号
134-
func (c *ClientV3) SetPlatformCert(wxPublicKeyContent []byte, wxSerialNo string) (client *ClientV3) {
135+
func (c *ClientV3) SetPlatformCert(wxPublicKeyContent []byte, wxSerialNo string) (err error) {
135136
pubKey, err := xpem.DecodePublicKey(wxPublicKeyContent)
136137
if err != nil {
137-
c.logger.Errorf("SetPlatformCert(%s),err:%+v", wxPublicKeyContent, err)
138+
return err
138139
}
139-
if pubKey != nil {
140-
c.wxPublicKey = pubKey
140+
if pubKey == nil {
141+
return errors.New("xpem.DecodePublicKey() failed, pubKey is nil")
141142
}
143+
c.wxPublicKey = pubKey
142144
c.WxSerialNo = wxSerialNo
143-
return c
145+
return nil
144146
}
145147

146148
// 获取最新的 微信平台证书

wechat/v3/client.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package wechat
33
import (
44
"context"
55
"crypto/rsa"
6+
"errors"
67
"sync"
78

89
"github.com/go-pay/crypto/xpem"
@@ -85,11 +86,31 @@ func (c *ClientV3) AutoVerifySign(autoRefresh ...bool) (err error) {
8586
c.WxSerialNo = wxSerialNo
8687
c.wxPublicKey = c.SnCertMap[wxSerialNo]
8788
if len(autoRefresh) == 1 && !autoRefresh[0] {
88-
return
89+
return nil
8990
}
9091
c.autoSign = true
9192
go c.autoCheckCertProc()
92-
return
93+
return nil
94+
}
95+
96+
// wxPublicKeyContent:微信公钥证书文件内容[]byte
97+
// wxPublicKeyID:微信公钥证书ID
98+
func (c *ClientV3) AutoVerifySignByCert(wxPublicKeyContent []byte, wxPublicKeyID string) (err error) {
99+
pubKey, err := xpem.DecodePublicKey(wxPublicKeyContent)
100+
if err != nil {
101+
return err
102+
}
103+
if pubKey == nil {
104+
return errors.New("xpem.DecodePublicKey() failed, pubKey is nil")
105+
}
106+
if len(c.SnCertMap) <= 0 {
107+
c.SnCertMap = make(map[string]*rsa.PublicKey)
108+
}
109+
c.SnCertMap[wxPublicKeyID] = pubKey
110+
c.wxPublicKey = pubKey
111+
c.WxSerialNo = wxPublicKeyID
112+
c.autoSign = true
113+
return nil
93114
}
94115

95116
// SetBodySize 设置http response body size(MB)

0 commit comments

Comments
 (0)