本文整理匯總了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
//.........這裏部分代碼省略.........