-
Notifications
You must be signed in to change notification settings - Fork 10
/
sdk.go
133 lines (117 loc) · 2.97 KB
/
sdk.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
package sdk
import (
"errors"
"github.com/polaris-team/dingtalk-sdk-golang/encrypt"
"github.com/polaris-team/dingtalk-sdk-golang/http"
"os"
"strconv"
"time"
)
type DingTalkSDK struct {
SuiteKey string
SuiteSecret string
Token string
AesKey string
AppId int64
}
type Corp struct {
CorpId string
SuiteTicket string
SuiteKey string
SuiteSecret string
}
type DingTalkClient struct {
AccessKey string
AccessToken string
AgentId int64
}
type DingTalkOauthClient struct {
OauthAppId string
OauthAppSecret string
}
func NewDingTalkOauthClient() *DingTalkOauthClient {
return &DingTalkOauthClient{
OauthAppId: os.Getenv("OAUTH_APP_ID"),
OauthAppSecret: os.Getenv("OAUTH_APP_SECRET"),
}
}
func NewSDK() *DingTalkSDK {
appId, err := strconv.ParseInt(os.Getenv("APP_ID"), 10, 64)
if err != nil {
panic(err)
}
return &DingTalkSDK{
SuiteKey: os.Getenv("SUITE_KEY"),
SuiteSecret: os.Getenv("SUITE_SECRET"),
Token: os.Getenv("SUITE_TOKEN"),
AesKey: os.Getenv("SUITE_AES_KEY"),
AppId: appId,
}
}
func NewCorp(suiteTicket string, corpId string) *Corp {
return &Corp{
CorpId: corpId,
SuiteTicket: suiteTicket,
SuiteKey: os.Getenv("SUITE_KEY"),
SuiteSecret: os.Getenv("SUITE_SECRET"),
}
}
func NewDingTalkClient(accessToken string, agentId int64) *DingTalkClient {
return &DingTalkClient{
AccessToken: accessToken,
AgentId: agentId,
}
}
func (s *DingTalkSDK) CreateCrypto() *Crypto {
if s.SuiteKey == "" {
panic("SUITE_KEY is not config in env!")
}
if s.Token == "" {
panic("SUITE_TOKEN is not config in env!")
}
if s.AesKey == "" {
panic("SUITE_AES_KEY is not config in env!")
}
return NewCrypto(s.Token, s.AesKey, s.SuiteKey)
}
func (s *DingTalkSDK) CreateCorp(corpId string, suiteTicket string) *Corp {
if s.SuiteKey == "" {
panic("SUITE_KEY is not config in env!")
}
if s.SuiteSecret == "" {
panic("SUITE_SECRET is not config in env!")
}
return &Corp{
CorpId: corpId,
SuiteTicket: suiteTicket,
SuiteKey: s.SuiteKey,
SuiteSecret: s.SuiteSecret,
}
}
func (corp *Corp) CreateDingTalkClient() (*DingTalkClient, error) {
tokenInfo, err := corp.GetCorpToken()
if err != nil {
return nil, err
}
authInfo, err := corp.GetAuthInfo()
if err != nil {
return nil, err
}
if authInfo.ErrCode > 0 {
return nil, errors.New(authInfo.ErrMsg)
}
return NewDingTalkClient(tokenInfo.AccessToken, authInfo.AuthInfo.Agent[0].AgentId), nil
}
func ExcuteOapi(url string, oauthAppId string, oauthAppSecret string, body string) (string, error) {
timestamp := time.Now().UnixNano() / 1e6
nativeSignature := strconv.FormatInt(timestamp, 10)
afterHmacSHA256 := encrypt.SHA256(nativeSignature, oauthAppSecret)
afterBase64 := encrypt.BASE64(afterHmacSHA256)
afterUrlEncode := encrypt.URLEncode(afterBase64)
params := map[string]string{
"timestamp": strconv.FormatInt(timestamp, 10),
"accessKey": oauthAppId,
"signature": afterUrlEncode,
}
return http.Post(url, params, body)
}