- more dingding callback func
This commit is contained in:
@@ -12,6 +12,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
CBTagCheckURL = "check_url"
|
||||
|
||||
CBTagUserAddOrg = "user_add_org"
|
||||
CBTagUserModifyOrg = "user_modify_org"
|
||||
CBTagUserLeaveOrg = "user_leave_org"
|
||||
@@ -21,6 +23,18 @@ const (
|
||||
KeyMsgSignature = "msg_signature"
|
||||
)
|
||||
|
||||
func (a *API) GetCallbackToken() string {
|
||||
a.locker.RLock()
|
||||
defer a.locker.RUnlock()
|
||||
return a.callbackToken
|
||||
}
|
||||
|
||||
func (a *API) GetCallbackAESKey() []byte {
|
||||
a.locker.RLock()
|
||||
defer a.locker.RUnlock()
|
||||
return a.callbackAESKey
|
||||
}
|
||||
|
||||
func (a *API) PackCallbackResult(result string) (resultMap map[string]interface{}) {
|
||||
encryptedResult, err := a.Encrypt(result)
|
||||
if err == nil {
|
||||
@@ -36,7 +50,7 @@ func (a *API) PackCallbackResult(result string) (resultMap map[string]interface{
|
||||
|
||||
func (a *API) calculateCallbackSign(data map[string]interface{}) (sign string) {
|
||||
strList := []string{
|
||||
a.callbackToken,
|
||||
a.GetCallbackToken(),
|
||||
}
|
||||
for k, v := range data {
|
||||
if k != KeyMsgSignature {
|
||||
@@ -48,7 +62,8 @@ func (a *API) calculateCallbackSign(data map[string]interface{}) (sign string) {
|
||||
}
|
||||
|
||||
func (a *API) Encrypt(msg string) (encryptedMsg string, err error) {
|
||||
binResult, err := utils.AESCBCEncpryt([]byte(msg), a.callbackAesKey, a.callbackAesKey[:16])
|
||||
aesKey := a.GetCallbackAESKey()
|
||||
binResult, err := utils.AESCBCEncpryt([]byte(msg), aesKey, aesKey[:16])
|
||||
encryptedMsg = base64.StdEncoding.EncodeToString(binResult)
|
||||
return encryptedMsg, err
|
||||
}
|
||||
@@ -56,7 +71,8 @@ func (a *API) Encrypt(msg string) (encryptedMsg string, err error) {
|
||||
func (a *API) Decrypt(msg string) (decryptedMsg string, err error) {
|
||||
binMsg, err := base64.StdEncoding.DecodeString(msg)
|
||||
if err == nil {
|
||||
binResult, err2 := utils.AESCBCDecpryt(binMsg, a.callbackAesKey, a.callbackAesKey[:16])
|
||||
aesKey := a.GetCallbackAESKey()
|
||||
binResult, err2 := utils.AESCBCDecpryt(binMsg, aesKey, aesKey[:16])
|
||||
if err = err2; err == nil {
|
||||
decryptedMsg = string(binResult)
|
||||
}
|
||||
@@ -74,8 +90,41 @@ func (a *API) RegisterCallback(callbackTagList []string, token, aesKey, urlStr s
|
||||
})
|
||||
}
|
||||
if err == nil {
|
||||
a.locker.Lock()
|
||||
defer a.locker.Unlock()
|
||||
a.callbackToken = token
|
||||
a.callbackAesKey, _ = base64.StdEncoding.DecodeString(aesKey + "=")
|
||||
a.callbackAESKey, _ = base64.StdEncoding.DecodeString(aesKey + "=")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *API) GetCallback() (callbackInfo map[string]interface{}, err error) {
|
||||
result, err := a.AccessAPI("call_back/get_call_back", nil, nil)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (a *API) UpdateCallback(callbackTagList []string, token, aesKey, urlStr string) (err error) {
|
||||
if len(callbackTagList) > 0 { // 为0做测试用
|
||||
_, err = a.AccessAPI("call_back/update_call_back", nil, map[string]interface{}{
|
||||
"call_back_tag": callbackTagList,
|
||||
"token": token,
|
||||
"aes_key": aesKey,
|
||||
"url": urlStr,
|
||||
})
|
||||
}
|
||||
if err == nil {
|
||||
a.locker.Lock()
|
||||
defer a.locker.Unlock()
|
||||
a.callbackToken = token
|
||||
a.callbackAESKey, _ = base64.StdEncoding.DecodeString(aesKey + "=")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *API) DeleteCallback() (err error) {
|
||||
_, err = a.AccessAPI("call_back/delete_call_back", nil, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -14,3 +14,13 @@ func TestPackCallbackResult(t *testing.T) {
|
||||
success := api.PackCallbackResult("success")
|
||||
t.Log(success)
|
||||
}
|
||||
|
||||
func TestRegisterCallback(t *testing.T) {
|
||||
err := api.RegisterCallback([]string{CBTagUserAddOrg}, "token", "M3Z1b1FIXjlAWW84bEVxNENHSlZOUFJEbkAlRUZQXnE", "http://callback.test.jxc4.com/dingding/msg")
|
||||
t.Log(err)
|
||||
}
|
||||
|
||||
func TestDeleteCallback(t *testing.T) {
|
||||
err := api.DeleteCallback()
|
||||
t.Log(err)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,10 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
ResponseCodeSuccess = 0
|
||||
ResponseCodeSuccess = 0
|
||||
ResponseCodeCallbackAlreadyExist = 71006
|
||||
ResponseCodeCallbackNotExist = 71007
|
||||
ResponseCodeCallbackURLNotExist = 71012
|
||||
)
|
||||
|
||||
type API struct {
|
||||
@@ -36,7 +39,7 @@ type API struct {
|
||||
locker sync.RWMutex
|
||||
|
||||
callbackToken string
|
||||
callbackAesKey []byte
|
||||
callbackAESKey []byte
|
||||
}
|
||||
|
||||
func New(appID, secret string, config ...*platformapi.APIConfig) *API {
|
||||
|
||||
Reference in New Issue
Block a user