-
Notifications
You must be signed in to change notification settings - Fork 10
/
api_auth_caller.go
162 lines (143 loc) · 4.78 KB
/
api_auth_caller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package sdk
import (
"github.com/polaris-team/dingtalk-sdk-golang/encrypt"
"github.com/polaris-team/dingtalk-sdk-golang/http"
"github.com/polaris-team/dingtalk-sdk-golang/json"
"strconv"
"time"
)
//CorpAuth is the common signature methods for multiple authentication interfaces
//Get a common interface for enterprise authentication information
func CorpAuth(url string, suiteKey string, suiteSecret string, suiteTicket string, authCorpId string, agentId int64) (string, error) {
timestamp := time.Now().UnixNano() / 1e6
nativeSignature := strconv.FormatInt(timestamp, 10) + "\n" + suiteTicket
afterHmacSHA256 := encrypt.SHA256(nativeSignature, suiteSecret)
afterBase64 := encrypt.BASE64(afterHmacSHA256)
afterUrlEncode := encrypt.URLEncode(afterBase64)
params := map[string]string{
"timestamp": strconv.FormatInt(timestamp, 10),
"accessKey": suiteKey,
"suiteTicket": suiteTicket,
"signature": afterUrlEncode,
}
bodyParams := map[string]string{
"auth_corpid": authCorpId,
}
if agentId != 0 {
bodyParams["agentid"] = strconv.FormatInt(agentId, 10)
bodyParams["suite_key"] = suiteKey
}
body, _ := json.ToJson(bodyParams)
return http.Post(url, params, body)
}
//GetCorpToken can get corporation's token
//Desc: 获取企业凭证
//Doc: https://open-doc.dingtalk.com/microapp/serverapi3/hv357q
func (corp *Corp) GetCorpToken() (GetCorpTokenResp, error) {
body, err := CorpAuth("https://oapi.dingtalk.com/service/get_corp_token", corp.SuiteKey, corp.SuiteSecret, corp.SuiteTicket, corp.CorpId, 0)
resp := GetCorpTokenResp{}
if err != nil {
return resp, err
}
json.FromJson(body, &resp)
return resp, err
}
//Desc: 获取企业授权信息
//Doc: https://open-doc.dingtalk.com/microapp/serverapi3/fmdqvx
func (corp *Corp) GetAuthInfo() (GetAuthInfoResp, error) {
body, err := CorpAuth("https://oapi.dingtalk.com/service/get_auth_info", corp.SuiteKey, corp.SuiteSecret, corp.SuiteTicket, corp.CorpId, 0)
resp := GetAuthInfoResp{}
if err != nil {
return resp, err
}
json.FromJson(body, &resp)
return resp, err
}
//Desc: 获取授权应用信息
//Doc: https://open-doc.dingtalk.com/microapp/serverapi3/vfitg0
func (corp *Corp) GetAgent(agentId int64) (GetAgentResp, error) {
body, err := CorpAuth("https://oapi.dingtalk.com/service/get_agent", corp.SuiteKey, corp.SuiteSecret, corp.SuiteTicket, corp.CorpId, agentId)
resp := GetAgentResp{}
if err != nil {
return resp, err
}
json.FromJson(body, &resp)
return resp, err
}
//Desc: 获取jsapi_ticket
//Doc: https://open-doc.dingtalk.com/microapp/dev/uwa7vs
func (client *DingTalkClient) GetJsApiTicket(authType string) (*GetJsApiTicketResp, error) {
params := map[string]string{
"access_token": client.AccessToken,
"type": authType,
}
body, err := http.Get("https://oapi.dingtalk.com/get_jsapi_ticket", params)
resp := GetJsApiTicketResp{}
if err != nil {
return nil, err
}
json.FromJson(body, &resp)
return &resp, err
}
func (s *DingTalkSDK) GetSuiteToken(suiteTicket string) (*GetSuiteAccessTokenResp, error) {
params := map[string]string{
"suite_key": s.SuiteKey,
"suite_secret": s.SuiteSecret,
"suiteTicket": suiteTicket,
}
jsonStr, err := json.ToJson(params)
if err != nil {
return nil, err
}
body, err := http.Post("https://oapi.dingtalk.com/service/get_suite_token", nil, jsonStr)
resp := GetSuiteAccessTokenResp{}
if err != nil {
return nil, err
}
json.FromJson(body, &resp)
return &resp, err
}
func (s *DingTalkSDK) GetPermanentCode(suiteAccessToken string, tmpAuthCode string) (*GetPermanentCodeResp, error) {
params := map[string]string{
"suite_access_token": suiteAccessToken,
}
bodyParams := map[string]string{
"tmp_auth_code": tmpAuthCode,
}
jsonStr, err := json.ToJson(bodyParams)
if err != nil {
return nil, err
}
body, err := http.Post("https://oapi.dingtalk.com/service/get_permanent_code", params, jsonStr)
resp := GetPermanentCodeResp{}
if err != nil {
return nil, err
}
json.FromJson(body, &resp)
return &resp, err
}
func (s *DingTalkSDK) ActivateSuite(suiteAccessToken string, authCorpId string, permanentCode string) (*BaseResp, error) {
params := map[string]string{
"suite_access_token": suiteAccessToken,
}
bodyParams := map[string]string{
"suite_key": s.SuiteKey,
"auth_corpid": authCorpId,
"permanent_code": permanentCode,
}
jsonStr, err := json.ToJson(bodyParams)
if err != nil {
return nil, err
}
body, err := http.Post("https://oapi.dingtalk.com/service/activate_suite", params, jsonStr)
resp := BaseResp{}
if err != nil {
return nil, err
}
json.FromJson(body, &resp)
return &resp, err
}
func CalculateJsApiSign(ticket string, nonceStr string, timestamp int64, url string) string {
plain := "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr + "×tamp=" + strconv.FormatInt(timestamp, 10) + "&url=" + url
return encrypt.SHA1(plain)
}