-
Notifications
You must be signed in to change notification settings - Fork 0
/
dingtalk.go
175 lines (160 loc) · 3.75 KB
/
dingtalk.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
163
164
165
166
167
168
169
170
171
172
173
174
175
package dkqrlogin
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"strings"
"time"
)
type DingTalk struct {
InternalUrl string //dingtalk 内网地址
PublicUrl string //dingtalk 外网地址
App string // app 名称
Admins []string // 管理员
Secret string // 密钥
httpClient *http.Client
}
type DingQrClient struct {
Dc DingTalk
TokenCatch *TTLCache
}
func Init(client DingTalk) *DingQrClient {
client.httpClient = http.DefaultClient
return &DingQrClient{
Dc: client,
TokenCatch: NewTTL(),
}
}
func (d *DingTalk) QRUrl(redirect string) string {
uri := d.PublicUrl + "/qr_login"
var q = url.Values{}
q.Add("app_name", d.App)
q.Add("callback", redirect)
return uri + "?" + q.Encode()
}
type User struct {
Active bool
Admin bool
Avatar string
Mobile string
Name string
Title string
UserID string
IsAdmin bool
}
// UserInfo 根据登录的code获取用户信息
func (d *DingTalk) UserInfo(ctx context.Context, code, codeType string) (*User, error) {
var (
q = url.Values{}
req *http.Request
err error
)
var user User
if codeType == "mobile" {
q.Add("mobile", code)
req, err = d.buildRequest(ctx, "/user/info", q, nil)
if err != nil {
return nil, err
}
if err = d.do(req, &user); err != nil {
return nil, err
}
} else {
q.Add("code", code)
req, err = d.buildRequest(ctx, "/qr_login/user_info", q, nil)
if err != nil {
return nil, err
}
var ret struct {
User User
}
if err = d.do(req, &ret); err != nil {
return nil, err
}
user = ret.User
}
for _, v := range d.Admins {
if v == user.Mobile {
user.IsAdmin = true
break
}
}
return &user, nil
}
type ResultResponse struct {
Message string
Data interface{}
}
func (d *DingTalk) do(req *http.Request, ret interface{}) error {
resp, err := d.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
s, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
response := &ResultResponse{
Data: ret,
}
if err := json.Unmarshal(s, response); err != nil {
return err
}
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
return nil
}
return errors.New(response.Message)
}
func (d *DingTalk) buildRequest(ctx context.Context, path string, q url.Values, body interface{}) (*http.Request, error) {
q.Set("timestamp", fmt.Sprintf("%d", time.Now().Unix()))
q.Set("app_name", d.App)
uri := d.InternalUrl + path + "?" + q.Encode()
var req *http.Request
var err error
if body == nil {
req, err = http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
} else {
b, _ := json.Marshal(body)
req, err = http.NewRequestWithContext(ctx, http.MethodPost, uri, bytes.NewBuffer(b))
}
if err != nil {
return nil, err
}
s := d.sign(q)
req.Header.Set("X-Token", s)
req.Header.Set("Content-Type", "application/json")
return req, nil
}
func (d *DingTalk) sign(q url.Values) string {
q.Set("timestamp", fmt.Sprintf("%d", time.Now().Unix()))
q.Set("app_name", d.App)
// 获取所有Uri Parameters
stringSlice := make([]string, 0)
for k := range q {
// 放入string 数组
stringSlice = append(stringSlice, k)
}
// 对所有请求参数进行字典升序排列;
sort.Strings(stringSlice)
var tokenTmp string
for _, v := range stringSlice {
// 将以上排序后的参数表进行字符串连接,如key1value1key2value2key3value3...keyNvalueN;
tokenTmp += v + q.Get(v)
}
// app secret作为后缀,对该字符串进行SHA-1计算,并转换成16进制编码;
tokenTmp = tokenTmp + d.Secret
crypt := sha1.New()
crypt.Write([]byte(tokenTmp))
tmp := hex.EncodeToString(crypt.Sum(nil))
// 转换为全大写形式后即获得签名串
return strings.ToUpper(tmp)
}