@@ -37,8 +37,8 @@ type MiniProgram struct {
37
37
host string
38
38
appid string
39
39
secret string
40
- srvCfg * ServerConfig
41
- sfMode * SafeMode
40
+ srvCfg ServerConfig
41
+ sfMode SafeMode
42
42
token atomic.Value
43
43
client * resty.Client
44
44
@@ -179,7 +179,7 @@ func (mp *MiniProgram) doSafe(ctx context.Context, method, path string, query ur
179
179
180
180
func (mp * MiniProgram ) encrypt (log * internal.ReqLog , path string , query url.Values , params X , timestamp int64 ) (X , error ) {
181
181
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?)" )
183
183
}
184
184
185
185
if params == nil {
@@ -229,7 +229,7 @@ func (mp *MiniProgram) encrypt(log *internal.ReqLog, path string, query url.Valu
229
229
230
230
func (mp * MiniProgram ) sign (path string , timestamp int64 , body []byte ) (string , error ) {
231
231
if mp .sfMode .prvKey == nil {
232
- return "" , errors .New ("private key not found (forgotten configure?)" )
232
+ return "" , errors .New ("missing private key (forgotten configure?)" )
233
233
}
234
234
235
235
var builder strings.Builder
@@ -251,7 +251,7 @@ func (mp *MiniProgram) sign(path string, timestamp int64, body []byte) (string,
251
251
252
252
func (mp * MiniProgram ) verify (path string , header http.Header , body []byte ) error {
253
253
if mp .sfMode .pubKey == nil {
254
- return errors .New ("public key not found (forgotten configure?)" )
254
+ return errors .New ("missing public key (forgotten configure?)" )
255
255
}
256
256
257
257
if appid := header .Get (HeaderMPAppID ); appid != mp .appid {
@@ -288,7 +288,7 @@ func (mp *MiniProgram) verify(path string, header http.Header, body []byte) erro
288
288
289
289
func (mp * MiniProgram ) decrypt (path string , header http.Header , body []byte ) ([]byte , error ) {
290
290
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?)" )
292
292
}
293
293
294
294
key , err := base64 .StdEncoding .DecodeString (mp .sfMode .aeskey )
@@ -705,6 +705,9 @@ func (mp *MiniProgram) DecodeEncryptData(sessionKey, iv, encryptData string) ([]
705
705
//
706
706
// [参考](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html)
707
707
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
+ }
708
711
if v := SignWithSHA1 (mp .srvCfg .token , items ... ); v != signature {
709
712
return fmt .Errorf ("signature verified fail, expect=%s, actual=%s" , signature , v )
710
713
}
@@ -717,6 +720,9 @@ func (mp *MiniProgram) VerifyEventMsg(signature string, items ...string) error {
717
720
// 根据配置的数据格式,解析 XML/JSON
718
721
// [参考](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html)
719
722
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
+ }
720
726
return EventDecrypt (mp .appid , mp .srvCfg .aeskey , encrypt )
721
727
}
722
728
@@ -725,6 +731,9 @@ func (mp *MiniProgram) DecodeEventMsg(encrypt string) ([]byte, error) {
725
731
// 根据配置的数据格式,输出 XML/JSON
726
732
// [参考](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html)
727
733
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
+ }
728
737
return EventReply (mp .appid , mp .srvCfg .token , mp .srvCfg .aeskey , msg )
729
738
}
730
739
@@ -784,7 +793,6 @@ func NewMiniProgram(appid, secret string, options ...MPOption) *MiniProgram {
784
793
host : "https://api.weixin.qq.com" ,
785
794
appid : appid ,
786
795
secret : secret ,
787
- srvCfg : new (ServerConfig ),
788
796
client : internal .NewClient (),
789
797
}
790
798
for _ , f := range options {
0 commit comments