-
Notifications
You must be signed in to change notification settings - Fork 37
/
api_auth.go
121 lines (102 loc) · 3.93 KB
/
api_auth.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
/*
* Copyright 2020 zhaoyunxing.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dingtalk
import (
"net/http"
"net/url"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/zhaoyunxing92/dingtalk/v2/constant"
"github.com/zhaoyunxing92/dingtalk/v2/crypto"
"github.com/zhaoyunxing92/dingtalk/v2/request"
"github.com/zhaoyunxing92/dingtalk/v2/response"
)
// GetAuthInfo 获取企业授权信息
func (ding *DingTalk) GetAuthInfo(corpId string) (rsp response.CorpAuthInfo, err error) {
if !ding.isv() {
return response.CorpAuthInfo{}, errors.New("ticket or corpId is null")
}
timestamp := strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
sign := crypto.GetSignature(timestamp, ding.secret, ding.ticket)
query := url.Values{}
query.Set("accessKey", ding.key)
query.Set("suiteTicket", ding.ticket)
query.Set("timestamp", timestamp)
query.Set("signature", sign)
return rsp, ding.Request(http.MethodPost, constant.GetAuthInfo, query,
request.NewAuthInfo(ding.key, corpId), &rsp)
}
// ActivateSuite 激活应用
func (ding *DingTalk) ActivateSuite(corpId, code string) (rsp response.CorpAuthInfo, err error) {
token, err := ding.GetSuiteAccessToken()
if err != nil {
return response.CorpAuthInfo{}, err
}
query := url.Values{}
query.Set("suite_access_token", token)
return rsp, ding.Request(http.MethodPost, constant.ActivateSuiteKey, query,
request.NewActivateSuite(ding.key, corpId, code), &rsp)
}
// GetAgentInfo 获取授权应用的基本信息
func (ding *DingTalk) GetAgentInfo(agentId int, corpId string) (rsp response.AgentInfo, err error) {
timestamp := strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
sign := crypto.GetSignature(timestamp, ding.secret, ding.ticket)
token, err := ding.GetSuiteAccessToken()
if err != nil {
return response.AgentInfo{}, err
}
query := url.Values{}
query.Set("suite_access_token", token)
query.Set("timestamp", timestamp)
query.Set("accessKey", ding.key)
query.Set("signature", sign)
query.Set("suiteTicket", ding.ticket)
return rsp, ding.Request(http.MethodPost, constant.GetAgentKey, query,
request.NewAgentInfo(agentId, ding.key, corpId), &rsp)
}
// GetCorpPermanentCode 获取授权企业的永久授权码
func (ding *DingTalk) GetCorpPermanentCode(code string) (rsp response.CorpPermanentCode, err error) {
token, err := ding.GetSuiteAccessToken()
if err != nil {
return response.CorpPermanentCode{}, err
}
query := url.Values{}
query.Set("suite_access_token", token)
return rsp, ding.Request(http.MethodPost, constant.GetCorpPermanentCodeKey, query,
request.NewCorpPermanentCode(code), &rsp)
}
// GetUnactiveCorp 获取应用未激活的企业列表
func (ding *DingTalk) GetUnactiveCorp(appId int) (rsp response.UnactiveCorp, err error) {
token, err := ding.GetSuiteAccessToken()
if err != nil {
return response.UnactiveCorp{}, err
}
query := url.Values{}
query.Set("suite_access_token", token)
return rsp, ding.Request(http.MethodPost, constant.GetUnactiveCorpKey, query, request.NewUnactiveCorp(appId), &rsp)
}
// ReauthCorp 重新授权未激活应用的企业
func (ding *DingTalk) ReauthCorp(appId int, corpId string, corpIds ...string) (rsp response.Response, err error) {
token, err := ding.GetSuiteAccessToken()
if err != nil {
return response.Response{}, err
}
query := url.Values{}
query.Set("suite_access_token", token)
return rsp, ding.Request(http.MethodPost, constant.ReauthCorpKey, query,
request.NewReauthCorp(appId, append(corpIds, corpId)), &rsp)
}