Skip to content

Commit 8dba69e

Browse files
author
盛辉
committed
增加服务器配置缺失错误
1 parent 3506d99 commit 8dba69e

12 files changed

+56
-33
lines changed

alipay/action.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (a *Action) RespKey() string {
2727
// Encode 签名并生成请求Body
2828
func (a *Action) Encode(c *Client) (string, error) {
2929
if c.prvKey == nil {
30-
return "", errors.New("private key is nil (forgotten configure?)")
30+
return "", errors.New("missing private key (forgotten configure?)")
3131
}
3232

3333
v := make(V)

alipay/client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (c *Client) UploadWithReader(ctx context.Context, method string, fieldName,
214214

215215
func (c *Client) verifyResp(key string, body []byte) (gjson.Result, error) {
216216
if c.pubKey == nil {
217-
return internal.Fail(errors.New("public key is nil (forgotten configure?)"))
217+
return internal.Fail(errors.New("missing public key (forgotten configure?)"))
218218
}
219219

220220
ret := gjson.ParseBytes(body)
@@ -288,7 +288,7 @@ func (c *Client) Decrypt(encryptData string) ([]byte, error) {
288288
// DecodeEncryptData 解析加密数据,如:授权的用户信息和手机号
289289
func (c *Client) DecodeEncryptData(hash crypto.Hash, data, sign string) ([]byte, error) {
290290
if c.pubKey == nil {
291-
return nil, errors.New("public key is nil (forgotten configure?)")
291+
return nil, errors.New("missing public key (forgotten configure?)")
292292
}
293293

294294
signByte, err := base64.StdEncoding.DecodeString(sign)
@@ -304,7 +304,7 @@ func (c *Client) DecodeEncryptData(hash crypto.Hash, data, sign string) ([]byte,
304304
// VerifyNotify 验证回调通知表单数据
305305
func (c *Client) VerifyNotify(form url.Values) (V, error) {
306306
if c.pubKey == nil {
307-
return nil, errors.New("public key is nil (forgotten configure?)")
307+
return nil, errors.New("missing public key (forgotten configure?)")
308308
}
309309

310310
sign, err := base64.StdEncoding.DecodeString(form.Get("sign"))

alipay/client_v3.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (c *ClientV3) UploadWithReader(ctx context.Context, reqPath, fieldName, fil
265265
// Authorization 生成签名并返回 HTTP Authorization
266266
func (c *ClientV3) Authorization(method, path string, query url.Values, body []byte, header http.Header) (string, error) {
267267
if c.prvKey == nil {
268-
return "", errors.New("private key not found (forgotten configure?)")
268+
return "", errors.New("missing private key (forgotten configure?)")
269269
}
270270

271271
authStr := fmt.Sprintf("app_id=%s,nonce=%s,timestamp=%d", c.appid, internal.Nonce(32), time.Now().UnixMilli())
@@ -302,7 +302,7 @@ func (c *ClientV3) Authorization(method, path string, query url.Values, body []b
302302
// Verify 验证签名
303303
func (c *ClientV3) Verify(header http.Header, body []byte) error {
304304
if c.pubKey == nil {
305-
return errors.New("public key not found (forgotten configure?)")
305+
return errors.New("missing public key (forgotten configure?)")
306306
}
307307

308308
signByte, err := base64.StdEncoding.DecodeString(header.Get(HeaderSignature))

internal/client.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"github.com/go-resty/resty/v2"
1010
)
1111

12-
const MaxFormMemory = 32 << 20
13-
1412
const (
1513
HeaderAccept = "Accept"
1614
HeaderAuthorization = "Authorization"
@@ -20,9 +18,10 @@ const (
2018
const (
2119
ContentText = "text/plain; charset=utf-8"
2220
ContentJSON = "application/json"
21+
ContentXML = "application/xml"
2322
ContentForm = "application/x-www-form-urlencoded"
2423
ContentStream = "application/octet-stream"
25-
ContentFormMultipart = "multipart/form-data"
24+
ContentMultipartForm = "multipart/form-data"
2625
)
2726

2827
func NewClient() *resty.Client {

sandpay/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (c *Client) Do(ctx context.Context, reqURL string, form *Form) (*Form, erro
7575
// Verify 验证并解析杉德API结果或回调通知
7676
func (c *Client) Verify(form url.Values) (*Form, error) {
7777
if c.pubKey == nil {
78-
return nil, errors.New("public key is nil (forgotten configure?)")
78+
return nil, errors.New("missing public key (forgotten configure?)")
7979
}
8080

8181
sign, err := base64.StdEncoding.DecodeString(strings.Replace(form.Get("sign"), " ", "+", -1))

sandpay/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Form struct {
2525
// URLEncode 数据表单格式化为POST表单
2626
func (f *Form) URLEncode(mid string, key *xcrypto.PrivateKey) (string, error) {
2727
if key == nil {
28-
return "", errors.New("private key is nil (forgotten configure?)")
28+
return "", errors.New("missing private key (forgotten configure?)")
2929
}
3030

3131
f.Head["mid"] = mid

wechat/corp.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Corp struct {
2323
host string
2424
corpid string
2525
secret string
26-
srvCfg *ServerConfig
26+
srvCfg ServerConfig
2727
token atomic.Value
2828
client *resty.Client
2929
logger func(ctx context.Context, err error, data map[string]string)
@@ -352,6 +352,9 @@ func (c *Corp) UploadWithReader(ctx context.Context, reqPath, fieldName, fileNam
352352
//
353353
// [参考](https://developer.work.weixin.qq.com/document/path/90930)
354354
func (c *Corp) VerifyEventMsg(signature string, items ...string) error {
355+
if len(c.srvCfg.token) == 0 || len(c.srvCfg.aeskey) == 0 {
356+
return errors.New("missing server config (forgotten configure?)")
357+
}
355358
if v := SignWithSHA1(c.srvCfg.token, items...); v != signature {
356359
return fmt.Errorf("signature verified fail, expect=%s, actual=%s", signature, v)
357360
}
@@ -363,13 +366,19 @@ func (c *Corp) VerifyEventMsg(signature string, items ...string) error {
363366
// 使用包体内的 Encrypt 字段
364367
// [参考](https://developer.work.weixin.qq.com/document/path/96211)
365368
func (c *Corp) DecodeEventMsg(encrypt string) ([]byte, error) {
369+
if len(c.srvCfg.token) == 0 || len(c.srvCfg.aeskey) == 0 {
370+
return nil, errors.New("missing server config (forgotten configure?)")
371+
}
366372
return EventDecrypt(c.corpid, c.srvCfg.aeskey, encrypt)
367373
}
368374

369375
// EncodeEventReply 事件回复加密
370376
//
371377
// [参考](https://developer.work.weixin.qq.com/document/path/96211)
372378
func (c *Corp) EncodeEventReply(msg V) (V, error) {
379+
if len(c.srvCfg.token) == 0 || len(c.srvCfg.aeskey) == 0 {
380+
return nil, errors.New("missing server config (forgotten configure?)")
381+
}
373382
return EventReply(c.corpid, c.srvCfg.token, c.srvCfg.aeskey, msg)
374383
}
375384

@@ -406,7 +415,6 @@ func NewCorp(corpid, secret string, options ...CorpOption) *Corp {
406415
host: "https://qyapi.weixin.qq.com",
407416
corpid: corpid,
408417
secret: secret,
409-
srvCfg: new(ServerConfig),
410418
client: internal.NewClient(),
411419
}
412420
for _, f := range options {

wechat/miniprogram.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ type MiniProgram struct {
3737
host string
3838
appid string
3939
secret string
40-
srvCfg *ServerConfig
41-
sfMode *SafeMode
40+
srvCfg ServerConfig
41+
sfMode SafeMode
4242
token atomic.Value
4343
client *resty.Client
4444

@@ -179,7 +179,7 @@ func (mp *MiniProgram) doSafe(ctx context.Context, method, path string, query ur
179179

180180
func (mp *MiniProgram) encrypt(log *internal.ReqLog, path string, query url.Values, params X, timestamp int64) (X, error) {
181181
if len(mp.sfMode.aeskey) == 0 {
182-
return nil, errors.New("aes-gcm key not found (forgotten configure?)")
182+
return nil, errors.New("missing aes-gcm key (forgotten configure?)")
183183
}
184184

185185
if params == nil {
@@ -229,7 +229,7 @@ func (mp *MiniProgram) encrypt(log *internal.ReqLog, path string, query url.Valu
229229

230230
func (mp *MiniProgram) sign(path string, timestamp int64, body []byte) (string, error) {
231231
if mp.sfMode.prvKey == nil {
232-
return "", errors.New("private key not found (forgotten configure?)")
232+
return "", errors.New("missing private key (forgotten configure?)")
233233
}
234234

235235
var builder strings.Builder
@@ -251,7 +251,7 @@ func (mp *MiniProgram) sign(path string, timestamp int64, body []byte) (string,
251251

252252
func (mp *MiniProgram) verify(path string, header http.Header, body []byte) error {
253253
if mp.sfMode.pubKey == nil {
254-
return errors.New("public key not found (forgotten configure?)")
254+
return errors.New("missing public key (forgotten configure?)")
255255
}
256256

257257
if appid := header.Get(HeaderMPAppID); appid != mp.appid {
@@ -288,7 +288,7 @@ func (mp *MiniProgram) verify(path string, header http.Header, body []byte) erro
288288

289289
func (mp *MiniProgram) decrypt(path string, header http.Header, body []byte) ([]byte, error) {
290290
if len(mp.sfMode.aeskey) == 0 {
291-
return nil, errors.New("aes-gcm key not found (forgotten configure?)")
291+
return nil, errors.New("missing aes-gcm key (forgotten configure?)")
292292
}
293293

294294
key, err := base64.StdEncoding.DecodeString(mp.sfMode.aeskey)
@@ -705,6 +705,9 @@ func (mp *MiniProgram) DecodeEncryptData(sessionKey, iv, encryptData string) ([]
705705
//
706706
// [参考](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html)
707707
func (mp *MiniProgram) VerifyEventMsg(signature string, items ...string) error {
708+
if len(mp.srvCfg.token) == 0 || len(mp.srvCfg.aeskey) == 0 {
709+
return errors.New("missing server config (forgotten configure?)")
710+
}
708711
if v := SignWithSHA1(mp.srvCfg.token, items...); v != signature {
709712
return fmt.Errorf("signature verified fail, expect=%s, actual=%s", signature, v)
710713
}
@@ -717,6 +720,9 @@ func (mp *MiniProgram) VerifyEventMsg(signature string, items ...string) error {
717720
// 根据配置的数据格式,解析 XML/JSON
718721
// [参考](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html)
719722
func (mp *MiniProgram) DecodeEventMsg(encrypt string) ([]byte, error) {
723+
if len(mp.srvCfg.token) == 0 || len(mp.srvCfg.aeskey) == 0 {
724+
return nil, errors.New("missing server config (forgotten configure?)")
725+
}
720726
return EventDecrypt(mp.appid, mp.srvCfg.aeskey, encrypt)
721727
}
722728

@@ -725,6 +731,9 @@ func (mp *MiniProgram) DecodeEventMsg(encrypt string) ([]byte, error) {
725731
// 根据配置的数据格式,输出 XML/JSON
726732
// [参考](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html)
727733
func (mp *MiniProgram) EncodeEventReply(msg V) (V, error) {
734+
if len(mp.srvCfg.token) == 0 || len(mp.srvCfg.aeskey) == 0 {
735+
return nil, errors.New("missing server config (forgotten configure?)")
736+
}
728737
return EventReply(mp.appid, mp.srvCfg.token, mp.srvCfg.aeskey, msg)
729738
}
730739

@@ -784,7 +793,6 @@ func NewMiniProgram(appid, secret string, options ...MPOption) *MiniProgram {
784793
host: "https://api.weixin.qq.com",
785794
appid: appid,
786795
secret: secret,
787-
srvCfg: new(ServerConfig),
788796
client: internal.NewClient(),
789797
}
790798
for _, f := range options {

wechat/official_account.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@ import (
1818
"github.com/yiigo/sdk-go/internal"
1919
)
2020

21-
// ServerConfig 服务器配置
22-
type ServerConfig struct {
23-
token string
24-
aeskey string
25-
}
26-
2721
// OfficialAccount 微信公众号
2822
type OfficialAccount struct {
2923
host string
3024
appid string
3125
secret string
32-
srvCfg *ServerConfig
26+
srvCfg ServerConfig
3327
token atomic.Value
3428
client *resty.Client
3529
logger func(ctx context.Context, err error, data map[string]string)
@@ -469,6 +463,9 @@ func (oa *OfficialAccount) UploadWithReader(ctx context.Context, reqPath, fieldN
469463
// [安全模式] URL参数中的 msg_signature、timestamp、nonce 和 包体内的 Encrypt 字段
470464
// [参考](https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Message_encryption_and_decryption_instructions.html)
471465
func (oa *OfficialAccount) VerifyEventMsg(signature string, items ...string) error {
466+
if len(oa.srvCfg.token) == 0 || len(oa.srvCfg.aeskey) == 0 {
467+
return errors.New("missing server config (forgotten configure?)")
468+
}
472469
if v := SignWithSHA1(oa.srvCfg.token, items...); v != signature {
473470
return fmt.Errorf("signature verified fail, expect=%s, actual=%s", signature, v)
474471
}
@@ -481,6 +478,9 @@ func (oa *OfficialAccount) VerifyEventMsg(signature string, items ...string) err
481478
// 根据配置的数据格式,解析 XML/JSON
482479
// [参考](https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Message_encryption_and_decryption_instructions.html)
483480
func (oa *OfficialAccount) DecodeEventMsg(encrypt string) ([]byte, error) {
481+
if len(oa.srvCfg.token) == 0 || len(oa.srvCfg.aeskey) == 0 {
482+
return nil, errors.New("missing server config (forgotten configure?)")
483+
}
484484
return EventDecrypt(oa.appid, oa.srvCfg.aeskey, encrypt)
485485
}
486486

@@ -489,6 +489,9 @@ func (oa *OfficialAccount) DecodeEventMsg(encrypt string) ([]byte, error) {
489489
// 根据配置的数据格式,输出 XML/JSON
490490
// [参考](https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Message_encryption_and_decryption_instructions.html)
491491
func (oa *OfficialAccount) EncodeEventReply(msg V) (V, error) {
492+
if len(oa.srvCfg.token) == 0 || len(oa.srvCfg.aeskey) == 0 {
493+
return nil, errors.New("missing server config (forgotten configure?)")
494+
}
492495
return EventReply(oa.appid, oa.srvCfg.token, oa.srvCfg.aeskey, msg)
493496
}
494497

@@ -525,7 +528,6 @@ func NewOfficialAccount(appid, secret string, options ...OAOption) *OfficialAcco
525528
host: "https://api.weixin.qq.com",
526529
appid: appid,
527530
secret: secret,
528-
srvCfg: new(ServerConfig),
529531
client: internal.NewClient(),
530532
}
531533
for _, f := range options {

wechat/pay_v3.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (p *PayV3) url(path string, query url.Values) string {
6363
func (p *PayV3) publicKey(serialNO string) (*xcrypto.PublicKey, error) {
6464
v := p.pubKey.Load()
6565
if v == nil {
66-
return nil, errors.New("public key is empty (forgotten auto load?)")
66+
return nil, errors.New("missing public key (forgotten auto load?)")
6767
}
6868
keyMap, ok := v.(map[string]*xcrypto.PublicKey)
6969
if !ok {
@@ -356,7 +356,7 @@ func (p *PayV3) Download(ctx context.Context, downloadURL string, w io.Writer) e
356356
// Authorization 生成签名并返回 HTTP Authorization
357357
func (p *PayV3) Authorization(method, path string, query url.Values, body string) (string, error) {
358358
if p.prvKey == nil {
359-
return "", errors.New("private key not found (forgotten configure?)")
359+
return "", errors.New("missing private key (forgotten configure?)")
360360
}
361361

362362
nonce := internal.Nonce(32)

wechat/util.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type X map[string]any
1111

1212
type Form map[string]string
1313

14+
// ServerConfig 服务器配置
15+
type ServerConfig struct {
16+
token string
17+
aeskey string
18+
}
19+
1420
// APIResult API结果 (支付v3)
1521
type APIResult struct {
1622
Code int // HTTP状态码

ysepay/client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (c *Client) PostForm(ctx context.Context, api, serviceNO string, bizData V)
113113
// reqForm 生成请求表单
114114
func (c *Client) reqForm(reqID, serviceNO string, bizData V) (string, error) {
115115
if c.prvKey == nil {
116-
return "", errors.New("private key is nil (forgotten configure?)")
116+
return "", errors.New("missing private key (forgotten configure?)")
117117
}
118118

119119
v := V{}
@@ -145,7 +145,7 @@ func (c *Client) reqForm(reqID, serviceNO string, bizData V) (string, error) {
145145

146146
func (c *Client) verifyResp(body []byte) (gjson.Result, error) {
147147
if c.pubKey == nil {
148-
return internal.Fail(errors.New("public key is nil (forgotten configure?)"))
148+
return internal.Fail(errors.New("missing public key (forgotten configure?)"))
149149
}
150150

151151
ret := gjson.ParseBytes(body)
@@ -178,7 +178,7 @@ func (c *Client) verifyResp(body []byte) (gjson.Result, error) {
178178
// VerifyNotify 解析并验证异步回调通知,返回BizJSON数据
179179
func (c *Client) VerifyNotify(form url.Values) (gjson.Result, error) {
180180
if c.pubKey == nil {
181-
return internal.Fail(errors.New("public key is nil (forgotten configure?)"))
181+
return internal.Fail(errors.New("missing public key (forgotten configure?)"))
182182
}
183183

184184
sign, err := base64.StdEncoding.DecodeString(form.Get("sign"))

0 commit comments

Comments
 (0)