Skip to content

Commit

Permalink
新增isv类
Browse files Browse the repository at this point in the history
  • Loading branch information
icepy committed Apr 21, 2018
1 parent f2dad73 commit eb10391
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 41 deletions.
2 changes: 1 addition & 1 deletion __test__/GetCompanyDingTalkClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func GetCompanyDingTalkClient() *dingtalk.DingTalkClient {
SSOSecret := os.Getenv("SSOSecret")
SNSAppID := os.Getenv("SNSAppID")
SNSSecret := os.Getenv("SNSSecret")
config := &dingtalk.DTCompanyConfig{
config := &dingtalk.DTConfig{
CorpID: CorpID,
CorpSecret: CorpSecret,
AgentID: AgentID,
Expand Down
70 changes: 38 additions & 32 deletions src/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ import (
*/

type DingTalkClient struct {
CompanyConfig *DTCompanyConfig
MiniConfig *DTMiniConfig
IsvConfig *DTIsvConfig
TopConfig *TopConfig
HTTPClient *http.Client
AccessToken string
SSOAccessToken string
SNSAccessToken string
AccessTokenCache Cache
TicketCache Cache
SSOAccessTokenCache Cache
SNSAccessTokenCache Cache
DTConfig *DTConfig
TopConfig *TopConfig
HTTPClient *http.Client
AccessToken string
SSOAccessToken string
SNSAccessToken string
SuiteAccessToken string
AccessTokenCache Cache
TicketCache Cache
SSOAccessTokenCache Cache
SNSAccessTokenCache Cache
SuiteAccessTokenCache Cache
}

type TopConfig struct {
Expand All @@ -51,28 +51,23 @@ type TopConfig struct {
TopSignMethod string
}

type DTMiniConfig struct {
TopConfig
}

type DTIsvConfig struct {
TopConfig
}

type DTCompanyConfig struct {
type DTConfig struct {
TopConfig
CorpID string
CorpSecret string
AgentID string
SuiteKey string
SuiteSecret string
SuiteTicket string
ChannelSecret string
SSOSecret string
SNSAppID string
SNSSecret string
}

func NewDingTalkCompanyClient(config *DTCompanyConfig) *DingTalkClient {
func NewDingTalkClient(devType string, config *DTConfig) *DingTalkClient {
c := &DingTalkClient{
CompanyConfig: &DTCompanyConfig{},
DTConfig: &DTConfig{},
HTTPClient: &http.Client{
Timeout: 10 * time.Second,
},
Expand All @@ -83,10 +78,11 @@ func NewDingTalkCompanyClient(config *DTCompanyConfig) *DingTalkClient {
TopSimplify: topSimplify,
TopV: topV,
},
AccessTokenCache: NewFileCache(".company_access_token_file"),
TicketCache: NewFileCache(".company_ticket_file"),
SSOAccessTokenCache: NewFileCache(".company_sso_acess_token_file"),
SNSAccessTokenCache: NewFileCache(".company_sns_access_token_file"),
AccessTokenCache: NewFileCache("." + devType + "_access_token_file"),
TicketCache: NewFileCache("." + devType + "_ticket_file"),
SSOAccessTokenCache: NewFileCache("." + devType + "_sso_acess_token_file"),
SNSAccessTokenCache: NewFileCache("." + devType + "_sns_access_token_file"),
SuiteAccessTokenCache: NewFileCache("." + devType + "_suite_access_token_file"),
}
if config != nil {
if config.TopFormat != "" {
Expand All @@ -104,16 +100,26 @@ func NewDingTalkCompanyClient(config *DTCompanyConfig) *DingTalkClient {
if config.TopSimplify {
c.TopConfig.TopSimplify = config.TopSimplify
}
c.CompanyConfig.CorpID = config.CorpID
c.CompanyConfig.AgentID = config.AgentID
c.CompanyConfig.CorpSecret = config.CorpSecret
c.CompanyConfig.SSOSecret = config.SSOSecret
c.DTConfig.CorpID = config.CorpID
c.DTConfig.AgentID = config.AgentID
c.DTConfig.CorpSecret = config.CorpSecret
c.DTConfig.SSOSecret = config.SSOSecret
c.DTConfig.ChannelSecret = config.ChannelSecret
c.DTConfig.SNSAppID = config.SNSAppID
c.DTConfig.SNSSecret = config.SNSSecret
c.DTConfig.SuiteKey = config.SuiteKey
c.DTConfig.SuiteSecret = config.SuiteSecret
c.DTConfig.SuiteTicket = config.SuiteTicket
}
return c
}

func NewDingTalkISVClient() {
func NewDingTalkISVClient(config *DTConfig) *DingTalkClient {
return NewDingTalkClient("isv", config)
}

func NewDingTalkCompanyClient(config *DTConfig) *DingTalkClient {
return NewDingTalkClient("company", config)
}

func NewDingTalkMiniClient() {
Expand Down
55 changes: 47 additions & 8 deletions src/open_api_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ type SNSAccessTokenResponse struct {
Created int64
}

type SuiteAccessTokenResponse struct {
OpenAPIResponse
SuiteAccessToken string `json:"suite_access_token"`
Expires int `json:"expires_in"`
Created int64
}

type TicketResponse struct {
OpenAPIResponse
Ticket string `json:"ticket"`
Expand Down Expand Up @@ -79,6 +86,14 @@ func (e *SNSAccessTokenResponse) ExpiresIn() int {
return e.Expires
}

func (e *SuiteAccessTokenResponse) CreatedAt() int64 {
return e.Created
}

func (e *SuiteAccessTokenResponse) ExpiresIn() int {
return e.Expires
}

// 刷新企业获取的access_token
func (dtc *DingTalkClient) RefreshCompanyAccessToken() error {
var data AccessTokenResponse
Expand All @@ -89,8 +104,8 @@ func (dtc *DingTalkClient) RefreshCompanyAccessToken() error {
return nil
}
params := url.Values{}
params.Add("corpid", dtc.CompanyConfig.CorpID)
params.Add("corpsecret", dtc.CompanyConfig.CorpSecret)
params.Add("corpid", dtc.DTConfig.CorpID)
params.Add("corpsecret", dtc.DTConfig.CorpSecret)
err = dtc.httpRPC("gettoken", params, nil, &data)
if err == nil {
dtc.AccessToken = data.AccessToken
Expand All @@ -111,8 +126,8 @@ func (dtc *DingTalkClient) RefreshCompanySSOAccessToken() error {
return nil
}
params := url.Values{}
params.Add("corpid", dtc.CompanyConfig.CorpID)
params.Add("corpsecret", dtc.CompanyConfig.SSOSecret)
params.Add("corpid", dtc.DTConfig.CorpID)
params.Add("corpsecret", dtc.DTConfig.SSOSecret)
err = dtc.httpSSO("sso/gettoken", params, nil, &data)
if err == nil {
dtc.SSOAccessToken = data.SSOAccessToken
Expand All @@ -133,8 +148,8 @@ func (dtc *DingTalkClient) RefreshSNSAccessToken() error {
return nil
}
params := url.Values{}
params.Add("appid", dtc.CompanyConfig.SNSAppID)
params.Add("appsecret", dtc.CompanyConfig.SNSSecret)
params.Add("appid", dtc.DTConfig.SNSAppID)
params.Add("appsecret", dtc.DTConfig.SNSSecret)
err = dtc.httpSNS("sns/gettoken", params, nil, &data)
if err == nil {
dtc.SNSAccessToken = data.SNSAccessToken
Expand All @@ -145,6 +160,30 @@ func (dtc *DingTalkClient) RefreshSNSAccessToken() error {
return err
}

// 刷新 isv suite_access_token
func (dtc *DingTalkClient) RefreshSuiteAccessToken() error {
var data SuiteAccessTokenResponse
err := dtc.SuiteAccessTokenCache.Get(&data)
if err == nil {
dtc.SuiteAccessToken = data.SuiteAccessToken
fmt.Printf("Get suite_access_token To Local Cache=%s\n", dtc.SuiteAccessToken)
return nil
}
info := map[string]string{
"suite_key": dtc.DTConfig.SuiteKey,
"suite_secret": dtc.DTConfig.SuiteSecret,
"suite_ticket": dtc.DTConfig.SuiteTicket,
}
err = dtc.httpSNS("service/get_suite_token", nil, info, &data)
if err == nil {
dtc.SuiteAccessToken = data.SuiteAccessToken
data.Expires = data.Expires | 7200
data.Created = time.Now().Unix()
err = dtc.SuiteAccessTokenCache.Set(&data)
}
return err
}

// 获取Ticket
func (dtc *DingTalkClient) GetJSAPITicket() (ticket string, err error) {
var data TicketResponse
Expand All @@ -167,9 +206,9 @@ func (dtc *DingTalkClient) GetConfig(nonceStr string, timestamp string, url stri
config = map[string]string{
"url": url,
"nonceStr": nonceStr,
"agentId": dtc.CompanyConfig.AgentID,
"agentId": dtc.DTConfig.AgentID,
"timeStamp": timestamp,
"corpId": dtc.CompanyConfig.CorpID,
"corpId": dtc.DTConfig.CorpID,
"ticket": ticket,
"signature": sign(ticket, nonceStr, timestamp, url),
}
Expand Down

0 comments on commit eb10391

Please sign in to comment.