From 9e5547cdf8110a80cf8df9cdba3a6e139d80db16 Mon Sep 17 00:00:00 2001 From: gazebo Date: Tue, 26 Feb 2019 15:27:06 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E6=96=B0=E5=A2=9E=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E6=B6=88=E6=81=AF=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=9A=84API=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platformapi/weixinapi/msg.go | 41 ++++++++++++++++++++++++++++++ platformapi/weixinapi/weixinapi.go | 3 +++ 2 files changed, 44 insertions(+) create mode 100644 platformapi/weixinapi/msg.go diff --git a/platformapi/weixinapi/msg.go b/platformapi/weixinapi/msg.go new file mode 100644 index 00000000..da63c665 --- /dev/null +++ b/platformapi/weixinapi/msg.go @@ -0,0 +1,41 @@ +package weixinapi + +import ( + "crypto/sha1" + "fmt" + "sort" + "strings" + + "git.rosy.net.cn/baseapi" +) + +func (a *API) SetMsgTokenAndKey(msgToken, msgKey string) { + a.locker.Lock() + defer a.locker.Unlock() + + a.msgToken = msgToken + a.msgKey = msgKey +} + +func (a *API) GetMsgTokenAndKey() (msgToken, msgKey string) { + a.locker.RLock() + defer a.locker.RUnlock() + + return a.msgToken, a.msgKey +} + +func (a *API) ValidateWXCallbackURL(signature, timestamp, nonce string) (isValid bool) { + msgToken, _ := a.GetMsgTokenAndKey() + if msgToken == "" { + panic("you must call SetMsgTokenAndKey first") + } + strList := []string{ + msgToken, + timestamp, + nonce, + } + sort.Sort(sort.StringSlice(strList)) + sha1Str := fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(strList, "")))) + baseapi.SugarLogger.Debugf("ValidateWXCallbackURL sha1Str:%s, signature:%s", sha1Str, signature) + return sha1Str == signature +} diff --git a/platformapi/weixinapi/weixinapi.go b/platformapi/weixinapi/weixinapi.go index 998abffe..0f0c1bf7 100644 --- a/platformapi/weixinapi/weixinapi.go +++ b/platformapi/weixinapi/weixinapi.go @@ -39,6 +39,9 @@ type API struct { client *http.Client config *platformapi.APIConfig locker sync.RWMutex + + msgToken string + msgKey string } type SNSTokenInfo struct {