本文整理汇总了Golang中github.com/chanxuehong/wechat/util.MsgSign函数的典型用法代码示例。如果您正苦于以下问题:Golang MsgSign函数的具体用法?Golang MsgSign怎么用?Golang MsgSign使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MsgSign函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: WriteResponse
// 回复消息给微信服务器.
// 要求 msg 是有效的消息数据结构(经过 encoding/xml marshal 后符合消息的格式);
// 如果有必要可以修改 Request 里面的某些值, 比如 TimeStamp.
func WriteResponse(w http.ResponseWriter, r *Request, msg interface{}) (err error) {
if w == nil {
return errors.New("nil http.ResponseWriter")
}
if r == nil {
return errors.New("nil Request")
}
if msg == nil {
return errors.New("nil message")
}
MsgRawXML, err := xml.Marshal(msg)
if err != nil {
return
}
EncryptedMsg := util.AESEncryptMsg(r.Random, MsgRawXML, r.CorpId, r.AESKey)
base64EncryptedMsg := base64.StdEncoding.EncodeToString(EncryptedMsg)
responseHttpBody := ResponseHttpBody{
EncryptedMsg: base64EncryptedMsg,
TimeStamp: r.TimeStamp,
Nonce: r.Nonce,
}
TimestampStr := strconv.FormatInt(responseHttpBody.TimeStamp, 10)
responseHttpBody.MsgSignature = util.MsgSign(r.AgentToken, TimestampStr,
responseHttpBody.Nonce, responseHttpBody.EncryptedMsg)
return xml.NewEncoder(w).Encode(&responseHttpBody)
}
示例2: ServeHTTP
// ServeHTTP 处理 http 消息请求
// NOTE: 调用者保证所有参数有效
func ServeHTTP(w http.ResponseWriter, r *http.Request, queryValues url.Values, srv AgentServer, irh InvalidRequestHandler) {
LogInfoln("[WECHAT_DEBUG] request uri:", r.RequestURI)
LogInfoln("[WECHAT_DEBUG] request remote-addr:", r.RemoteAddr)
LogInfoln("[WECHAT_DEBUG] request user-agent:", r.UserAgent())
switch r.Method {
case "POST": // 消息处理
msgSignature1 := queryValues.Get("msg_signature")
if msgSignature1 == "" {
irh.ServeInvalidRequest(w, r, errors.New("msg_signature is empty"))
return
}
if len(msgSignature1) != 40 { // sha1
err := fmt.Errorf("the length of msg_signature mismatch, have: %d, want: 40", len(msgSignature1))
irh.ServeInvalidRequest(w, r, err)
return
}
timestampStr := queryValues.Get("timestamp")
if timestampStr == "" {
irh.ServeInvalidRequest(w, r, errors.New("timestamp is empty"))
return
}
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
err = errors.New("can not parse timestamp to int64: " + timestampStr)
irh.ServeInvalidRequest(w, r, err)
return
}
nonce := queryValues.Get("nonce")
if nonce == "" {
irh.ServeInvalidRequest(w, r, errors.New("nonce is empty"))
return
}
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
irh.ServeInvalidRequest(w, r, err)
return
}
LogInfoln("[WECHAT_DEBUG] request msg http body:\r\n", string(reqBody))
// 解析 RequestHttpBody
var requestHttpBody RequestHttpBody
if err := xml.Unmarshal(reqBody, &requestHttpBody); err != nil {
irh.ServeInvalidRequest(w, r, err)
return
}
corpId := srv.CorpId()
haveCorpId := requestHttpBody.CorpId
if len(haveCorpId) != len(corpId) {
err = fmt.Errorf("the RequestHttpBody's ToUserName mismatch, have: %s, want: %s", haveCorpId, corpId)
irh.ServeInvalidRequest(w, r, err)
return
}
if subtle.ConstantTimeCompare([]byte(haveCorpId), []byte(corpId)) != 1 {
err = fmt.Errorf("the RequestHttpBody's ToUserName mismatch, have: %s, want: %s", haveCorpId, corpId)
irh.ServeInvalidRequest(w, r, err)
return
}
agentId := srv.AgentId()
haveAgentId := requestHttpBody.AgentId
if haveAgentId != agentId && haveAgentId != 0 {
err = fmt.Errorf("the RequestHttpBody's AgentId mismatch, have: %d, want: %d", haveAgentId, agentId)
irh.ServeInvalidRequest(w, r, err)
return
}
// 此时
// 要么 haveAgentId == wantAgentId,
// 要么 haveAgentId == 0
agentToken := srv.Token()
// 验证签名
msgSignature2 := util.MsgSign(agentToken, timestampStr, nonce, requestHttpBody.EncryptedMsg)
if subtle.ConstantTimeCompare([]byte(msgSignature1), []byte(msgSignature2)) != 1 {
err = fmt.Errorf("check msg_signature failed, input: %s, local: %s", msgSignature1, msgSignature2)
irh.ServeInvalidRequest(w, r, err)
return
}
// 解密
encryptedMsgBytes, err := base64.StdEncoding.DecodeString(requestHttpBody.EncryptedMsg)
if err != nil {
irh.ServeInvalidRequest(w, r, err)
return
}
aesKey := srv.CurrentAESKey()
random, rawMsgXML, err := util.AESDecryptMsg(encryptedMsgBytes, corpId, aesKey)
if err != nil {
//.........这里部分代码省略.........
示例3: ServeHTTP
// ServeHTTP 处理 http 消息请求
// NOTE: 调用者保证所有参数有效
func ServeHTTP(w http.ResponseWriter, r *http.Request, urlValues url.Values,
agentServer AgentServer, invalidRequestHandler InvalidRequestHandler) {
switch r.Method {
case "POST": // 消息处理
msgSignature1, timestampStr, nonce, err := parsePostURLQuery(urlValues)
if err != nil {
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
// 首先判断签名长度是否合法
if len(msgSignature1) != 40 {
err = fmt.Errorf("the length of msg_signature mismatch, have: %d, want: 40", len(msgSignature1))
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
err = errors.New("can not parse timestamp to int64: " + timestampStr)
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
// 解析 RequestHttpBody
var requestHttpBody RequestHttpBody
if err := xml.NewDecoder(r.Body).Decode(&requestHttpBody); err != nil {
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
haveCorpId := requestHttpBody.CorpId
wantCorpId := agentServer.CorpId()
if len(haveCorpId) != len(wantCorpId) {
err = fmt.Errorf("the RequestHttpBody's ToUserName mismatch, have: %s, want: %s", haveCorpId, wantCorpId)
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
if subtle.ConstantTimeCompare([]byte(haveCorpId), []byte(wantCorpId)) != 1 {
err = fmt.Errorf("the RequestHttpBody's ToUserName mismatch, have: %s, want: %s", haveCorpId, wantCorpId)
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
haveAgentId := requestHttpBody.AgentId
wantAgentId := agentServer.AgentId()
if haveAgentId != wantAgentId && haveAgentId != 0 {
err = fmt.Errorf("the RequestHttpBody's AgentId mismatch, have: %d, want: %d", haveAgentId, wantAgentId)
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
// 此时
// 要么 haveAgentId == wantAgentId,
// 要么 haveAgentId == 0
agentToken := agentServer.Token()
// 验证签名
msgSignature2 := util.MsgSign(agentToken, timestampStr, nonce, requestHttpBody.EncryptedMsg)
if subtle.ConstantTimeCompare([]byte(msgSignature1), []byte(msgSignature2)) != 1 {
err = fmt.Errorf("check signature failed, input: %s, local: %s", msgSignature1, msgSignature2)
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
// 解密
EncryptedMsgBytes, err := base64.StdEncoding.DecodeString(requestHttpBody.EncryptedMsg)
if err != nil {
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
AESKey := agentServer.CurrentAESKey()
Random, RawMsgXML, err := util.AESDecryptMsg(EncryptedMsgBytes, wantCorpId, AESKey)
if err != nil {
// 尝试用上一次的 AESKey 来解密
LastAESKey := agentServer.LastAESKey()
if bytes.Equal(AESKey[:], LastAESKey[:]) || bytes.Equal(zeroAESKey[:], LastAESKey[:]) {
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
AESKey = LastAESKey // NOTE
Random, RawMsgXML, err = util.AESDecryptMsg(EncryptedMsgBytes, wantCorpId, AESKey)
if err != nil {
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
}
// 解密成功, 解析 MixedMessage
var MixedMsg MixedMessage
if err = xml.Unmarshal(RawMsgXML, &MixedMsg); err != nil {
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
//.........这里部分代码省略.........
示例4: ServeHTTP
// ServeHTTP 处理 http 消息请求
// NOTE: 调用者保证所有参数有效
func ServeHTTP(w http.ResponseWriter, r *http.Request, queryValues url.Values, srv Server, errHandler mp.ErrorHandler) {
switch r.Method {
case "POST": // 消息处理
switch encryptType := queryValues.Get("encrypt_type"); encryptType {
case "aes":
msgSignature1 := queryValues.Get("msg_signature")
if msgSignature1 == "" {
errHandler.ServeError(w, r, errors.New("msg_signature is empty"))
return
}
if len(msgSignature1) != 40 { // sha1
err := fmt.Errorf("the length of msg_signature mismatch, have: %d, want: 40", len(msgSignature1))
errHandler.ServeError(w, r, err)
return
}
timestampStr := queryValues.Get("timestamp")
if timestampStr == "" {
errHandler.ServeError(w, r, errors.New("timestamp is empty"))
return
}
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
err = errors.New("can not parse timestamp to int64: " + timestampStr)
errHandler.ServeError(w, r, err)
return
}
nonce := queryValues.Get("nonce")
if nonce == "" {
errHandler.ServeError(w, r, errors.New("nonce is empty"))
return
}
var requestHttpBody RequestHttpBody
if err := xml.NewDecoder(r.Body).Decode(&requestHttpBody); err != nil {
errHandler.ServeError(w, r, err)
return
}
appId := srv.AppId()
// 安全考虑验证下 AppId
haveAppId := requestHttpBody.AppId
if len(haveAppId) != len(appId) {
err = fmt.Errorf("the RequestHttpBody's AppId mismatch, have: %s, want: %s", haveAppId, appId)
errHandler.ServeError(w, r, err)
return
}
if subtle.ConstantTimeCompare([]byte(haveAppId), []byte(appId)) != 1 {
err = fmt.Errorf("the RequestHttpBody's AppId mismatch, have: %s, want: %s", haveAppId, appId)
errHandler.ServeError(w, r, err)
return
}
token := srv.Token()
// 验证签名
msgSignature2 := util.MsgSign(token, timestampStr, nonce, requestHttpBody.EncryptedMsg)
if subtle.ConstantTimeCompare([]byte(msgSignature1), []byte(msgSignature2)) != 1 {
err = fmt.Errorf("check msg_signature failed, input: %s, local: %s", msgSignature1, msgSignature2)
errHandler.ServeError(w, r, err)
return
}
// 解密
encryptedMsgBytes, err := base64.StdEncoding.DecodeString(requestHttpBody.EncryptedMsg)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
aesKey := srv.CurrentAESKey()
random, rawMsgXML, err := util.AESDecryptMsg(encryptedMsgBytes, appId, aesKey)
if err != nil {
// 尝试用上一次的 AESKey 来解密
lastAESKey, isLastAESKeyValid := srv.LastAESKey()
if !isLastAESKeyValid {
errHandler.ServeError(w, r, err)
return
}
aesKey = lastAESKey // NOTE
random, rawMsgXML, err = util.AESDecryptMsg(encryptedMsgBytes, appId, aesKey)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
}
// 解密成功, 解析 MixedMessage
var mixedMsg MixedMessage
if err := xml.Unmarshal(rawMsgXML, &mixedMsg); err != nil {
errHandler.ServeError(w, r, err)
return
//.........这里部分代码省略.........
示例5: ServeHTTP
// ServeHTTP 处理 http 消息请求
// NOTE: 调用者保证所有参数有效
func ServeHTTP(w http.ResponseWriter, r *http.Request, queryValues url.Values, srv AgentServer, errHandler ErrorHandler) {
switch r.Method {
case "POST": // 消息处理
msgSignature1 := queryValues.Get("msg_signature")
if msgSignature1 == "" {
errHandler.ServeError(w, r, errors.New("msg_signature is empty"))
return
}
timestampStr := queryValues.Get("timestamp")
if timestampStr == "" {
errHandler.ServeError(w, r, errors.New("timestamp is empty"))
return
}
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
err = errors.New("can not parse timestamp to int64: " + timestampStr)
errHandler.ServeError(w, r, err)
return
}
nonce := queryValues.Get("nonce")
if nonce == "" {
errHandler.ServeError(w, r, errors.New("nonce is empty"))
return
}
// 解析 RequestHttpBody
var requestHttpBody RequestHttpBody
if err := xml.NewDecoder(r.Body).Decode(&requestHttpBody); err != nil {
errHandler.ServeError(w, r, err)
return
}
haveCorpId := requestHttpBody.CorpId
wantCorpId := srv.CorpId()
if wantCorpId != "" && !security.SecureCompareString(haveCorpId, wantCorpId) {
err = fmt.Errorf("the RequestHttpBody's ToUserName mismatch, have: %s, want: %s", haveCorpId, wantCorpId)
errHandler.ServeError(w, r, err)
return
}
haveAgentId := requestHttpBody.AgentId
wantAgentId := srv.AgentId()
if wantCorpId != "" && wantAgentId != -1 {
if haveAgentId != wantAgentId && haveAgentId != 0 {
err = fmt.Errorf("the RequestHttpBody's AgentId mismatch, have: %d, want: %d", haveAgentId, wantAgentId)
errHandler.ServeError(w, r, err)
return
}
// 此时
// 要么 haveAgentId == wantAgentId,
// 要么 haveAgentId == 0
}
agentToken := srv.Token()
// 验证签名
msgSignature2 := util.MsgSign(agentToken, timestampStr, nonce, requestHttpBody.EncryptedMsg)
if !security.SecureCompareString(msgSignature1, msgSignature2) {
err := fmt.Errorf("check msg_signature failed, input: %s, local: %s", msgSignature1, msgSignature2)
errHandler.ServeError(w, r, err)
return
}
// 解密
encryptedMsgBytes, err := base64.StdEncoding.DecodeString(requestHttpBody.EncryptedMsg)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
aesKey := srv.CurrentAESKey()
random, rawMsgXML, aesAppId, err := util.AESDecryptMsg(encryptedMsgBytes, aesKey)
if err != nil {
// 尝试用上一次的 AESKey 来解密
lastAESKey, isLastAESKeyValid := srv.LastAESKey()
if !isLastAESKeyValid {
errHandler.ServeError(w, r, err)
return
}
aesKey = lastAESKey // NOTE
random, rawMsgXML, aesAppId, err = util.AESDecryptMsg(encryptedMsgBytes, aesKey)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
}
if haveCorpId != string(aesAppId) {
err = fmt.Errorf("the RequestHttpBody's ToUserName(==%s) mismatch the CorpId with aes encrypt(==%s)", haveCorpId, aesAppId)
errHandler.ServeError(w, r, err)
return
}
// 解密成功, 解析 MixedMessage
//.........这里部分代码省略.........
示例6: ServeHTTP
// ServeHTTP 处理 http 消息请求
// NOTE: 调用者保证所有参数有效
func ServeHTTP(w http.ResponseWriter, r *http.Request, queryValues url.Values, srv Server, errHandler mp.ErrorHandler) {
mp.LogInfoln("[WECHAT_DEBUG] request uri:", r.RequestURI)
mp.LogInfoln("[WECHAT_DEBUG] request remote-addr:", r.RemoteAddr)
mp.LogInfoln("[WECHAT_DEBUG] request user-agent:", r.UserAgent())
switch r.Method {
case "POST": // 消息处理
switch encryptType := queryValues.Get("encrypt_type"); encryptType {
case "aes":
msgSignature1 := queryValues.Get("msg_signature")
if msgSignature1 == "" {
errHandler.ServeError(w, r, errors.New("msg_signature is empty"))
return
}
timestampStr := queryValues.Get("timestamp")
if timestampStr == "" {
errHandler.ServeError(w, r, errors.New("timestamp is empty"))
return
}
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
err = errors.New("can not parse timestamp to int64: " + timestampStr)
errHandler.ServeError(w, r, err)
return
}
nonce := queryValues.Get("nonce")
if nonce == "" {
errHandler.ServeError(w, r, errors.New("nonce is empty"))
return
}
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
mp.LogInfoln("[WECHAT_DEBUG] request msg http body:\r\n", string(reqBody))
var requestHttpBody RequestHttpBody
if err := xml.Unmarshal(reqBody, &requestHttpBody); err != nil {
errHandler.ServeError(w, r, err)
return
}
haveAppId := requestHttpBody.AppId
wantAppId := srv.AppId()
if wantAppId != "" && !security.SecureCompareString(haveAppId, wantAppId) {
err = fmt.Errorf("the RequestHttpBody's AppId mismatch, have: %s, want: %s", haveAppId, wantAppId)
errHandler.ServeError(w, r, err)
return
}
token := srv.Token()
// 验证签名
msgSignature2 := util.MsgSign(token, timestampStr, nonce, requestHttpBody.EncryptedMsg)
if !security.SecureCompareString(msgSignature1, msgSignature2) {
err = fmt.Errorf("check msg_signature failed, input: %s, local: %s", msgSignature1, msgSignature2)
errHandler.ServeError(w, r, err)
return
}
// 解密
encryptedMsgBytes, err := base64.StdEncoding.DecodeString(requestHttpBody.EncryptedMsg)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
aesKey := srv.CurrentAESKey()
random, rawMsgXML, aesAppId, err := util.AESDecryptMsg(encryptedMsgBytes, aesKey)
if err != nil {
// 尝试用上一次的 AESKey 来解密
lastAESKey, isLastAESKeyValid := srv.LastAESKey()
if !isLastAESKeyValid {
errHandler.ServeError(w, r, err)
return
}
aesKey = lastAESKey // NOTE
random, rawMsgXML, aesAppId, err = util.AESDecryptMsg(encryptedMsgBytes, aesKey)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
}
if haveAppId != string(aesAppId) {
err = fmt.Errorf("the RequestHttpBody's ToUserName(==%s) mismatch the AppId with aes encrypt(==%s)", haveAppId, aesAppId)
errHandler.ServeError(w, r, err)
return
}
mp.LogInfoln("[WECHAT_DEBUG] request msg raw xml:\r\n", string(rawMsgXML))
//.........这里部分代码省略.........
示例7: ServeHTTP
// ServeHTTP 处理 http 消息请求
// NOTE: 调用者保证所有参数有效
func ServeHTTP(w http.ResponseWriter, r *http.Request, queryValues url.Values, srv Server, errHandler corp.ErrorHandler) {
corp.LogInfoln("[WECHAT_DEBUG] request uri:", r.RequestURI)
corp.LogInfoln("[WECHAT_DEBUG] request remote-addr:", r.RemoteAddr)
corp.LogInfoln("[WECHAT_DEBUG] request user-agent:", r.UserAgent())
switch r.Method {
case "POST": // 消息处理
msgSignature1 := queryValues.Get("msg_signature")
if msgSignature1 == "" {
errHandler.ServeError(w, r, errors.New("msg_signature is empty"))
return
}
if len(msgSignature1) != 40 { // sha1
err := fmt.Errorf("the length of msg_signature mismatch, have: %d, want: 40", len(msgSignature1))
errHandler.ServeError(w, r, err)
return
}
timestampStr := queryValues.Get("timestamp")
if timestampStr == "" {
errHandler.ServeError(w, r, errors.New("timestamp is empty"))
return
}
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
err = errors.New("can not parse timestamp to int64: " + timestampStr)
errHandler.ServeError(w, r, err)
return
}
nonce := queryValues.Get("nonce")
if nonce == "" {
errHandler.ServeError(w, r, errors.New("nonce is empty"))
return
}
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
corp.LogInfoln("[WECHAT_DEBUG] request msg http body:\r\n", string(reqBody))
// 解析 RequestHttpBody
var requestHttpBody RequestHttpBody
if err := xml.Unmarshal(reqBody, &requestHttpBody); err != nil {
errHandler.ServeError(w, r, err)
return
}
haveSuiteId := requestHttpBody.SuiteId
wantSuiteId := srv.SuiteId()
if wantSuiteId != "" {
if len(haveSuiteId) != len(wantSuiteId) {
err = fmt.Errorf("the RequestHttpBody's ToUserName mismatch, have: %s, want: %s", haveSuiteId, wantSuiteId)
errHandler.ServeError(w, r, err)
return
}
if subtle.ConstantTimeCompare([]byte(haveSuiteId), []byte(wantSuiteId)) != 1 {
err = fmt.Errorf("the RequestHttpBody's ToUserName mismatch, have: %s, want: %s", haveSuiteId, wantSuiteId)
errHandler.ServeError(w, r, err)
return
}
}
suiteToken := srv.SuiteToken()
// 验证签名
msgSignature2 := util.MsgSign(suiteToken, timestampStr, nonce, requestHttpBody.EncryptedMsg)
if subtle.ConstantTimeCompare([]byte(msgSignature1), []byte(msgSignature2)) != 1 {
err = fmt.Errorf("check msg_signature failed, input: %s, local: %s", msgSignature1, msgSignature2)
errHandler.ServeError(w, r, err)
return
}
// 解密
encryptedMsgBytes, err := base64.StdEncoding.DecodeString(requestHttpBody.EncryptedMsg)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
aesKey := srv.CurrentAESKey()
random, rawMsgXML, aesSuiteId, err := util.AESDecryptMsg(encryptedMsgBytes, aesKey)
if err != nil {
// 尝试用上一次的 AESKey 来解密
lastAESKey, isLastAESKeyValid := srv.LastAESKey()
if !isLastAESKeyValid {
errHandler.ServeError(w, r, err)
return
}
aesKey = lastAESKey // NOTE
random, rawMsgXML, aesSuiteId, err = util.AESDecryptMsg(encryptedMsgBytes, aesKey)
if err != nil {
errHandler.ServeError(w, r, err)
//.........这里部分代码省略.........
示例8: ServeHTTP
// ServeHTTP 处理 http 消息请求
// NOTE: 调用者保证所有参数有效
func ServeHTTP(w http.ResponseWriter, r *http.Request, queryValues url.Values, srv Server, errHandler ErrorHandler) {
switch r.Method {
case "POST": // 消息处理
switch encryptType := queryValues.Get("encrypt_type"); encryptType {
case "aes": // 安全模式, 兼容模式
signature := queryValues.Get("signature") // 只读取, 不做校验
msgSignature1 := queryValues.Get("msg_signature")
if msgSignature1 == "" {
errHandler.ServeError(w, r, errors.New("msg_signature is empty"))
return
}
timestampStr := queryValues.Get("timestamp")
if timestampStr == "" {
errHandler.ServeError(w, r, errors.New("timestamp is empty"))
return
}
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
err = errors.New("can not parse timestamp to int64: " + timestampStr)
errHandler.ServeError(w, r, err)
return
}
nonce := queryValues.Get("nonce")
if nonce == "" {
errHandler.ServeError(w, r, errors.New("nonce is empty"))
return
}
var requestHttpBody RequestHttpBody
if err := xml.NewDecoder(r.Body).Decode(&requestHttpBody); err != nil {
errHandler.ServeError(w, r, err)
return
}
// 安全考虑验证下 ToUserName
haveToUserName := requestHttpBody.ToUserName
wantToUserName := srv.OriId()
if wantToUserName != "" && !security.SecureCompareString(haveToUserName, wantToUserName) {
err := fmt.Errorf("the RequestHttpBody's ToUserName mismatch, have: %s, want: %s", haveToUserName, wantToUserName)
errHandler.ServeError(w, r, err)
return
}
token := srv.Token()
// 验证签名
msgSignature2 := util.MsgSign(token, timestampStr, nonce, requestHttpBody.EncryptedMsg)
if !security.SecureCompareString(msgSignature1, msgSignature2) {
err := fmt.Errorf("check msg_signature failed, input: %s, local: %s", msgSignature1, msgSignature2)
errHandler.ServeError(w, r, err)
return
}
// 解密
encryptedMsgBytes, err := base64.StdEncoding.DecodeString(requestHttpBody.EncryptedMsg)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
aesKey := srv.CurrentAESKey()
random, rawMsgXML, haveAppIdBytes, err := util.AESDecryptMsg(encryptedMsgBytes, aesKey)
if err != nil {
// 尝试用上一次的 AESKey 来解密
lastAESKey, isLastAESKeyValid := srv.LastAESKey()
if !isLastAESKeyValid {
errHandler.ServeError(w, r, err)
return
}
aesKey = lastAESKey // NOTE
random, rawMsgXML, haveAppIdBytes, err = util.AESDecryptMsg(encryptedMsgBytes, aesKey)
if err != nil {
errHandler.ServeError(w, r, err)
return
}
}
haveAppId := string(haveAppIdBytes)
wantAppId := srv.AppId()
if wantAppId != "" && wantAppId != haveAppId {
err := fmt.Errorf("the message's appid mismatch, have: %s, want: %s", haveAppId, wantAppId)
errHandler.ServeError(w, r, err)
return
}
// 解密成功, 解析 MixedMessage
var mixedMsg MixedMessage
if err := xml.Unmarshal(rawMsgXML, &mixedMsg); err != nil {
errHandler.ServeError(w, r, err)
return
}
// 安全考虑再次验证 ToUserName
//.........这里部分代码省略.........