forked from go-lark/lark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_auth.go
101 lines (90 loc) · 2.68 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
package lark
import (
"time"
)
// URLs for auth
const (
appAccessTokenInternalURL = "/open-apis/auth/v3/app_access_token/internal"
tenantAppAccessTokenInternalURL = "/open-apis/auth/v3/tenant_access_token/internal/"
)
// AuthTokenInternalResponse .
type AuthTokenInternalResponse struct {
BaseResponse
AppAccessToken string `json:"app_access_token"`
Expire int `json:"expire"`
}
// TenantAuthTokenInternalResponse .
type TenantAuthTokenInternalResponse struct {
BaseResponse
TenantAppAccessToken string `json:"tenant_access_token"`
Expire int `json:"expire"`
}
// GetAccessTokenInternal gets AppAccessToken for internal use
func (bot *Bot) GetAccessTokenInternal(updateToken bool) (*AuthTokenInternalResponse, error) {
if !bot.requireType(ChatBot) {
return nil, ErrBotTypeError
}
params := map[string]interface{}{
"app_id": bot.appID,
"app_secret": bot.appSecret,
}
var respData AuthTokenInternalResponse
err := bot.PostAPIRequest("GetAccessTokenInternal", appAccessTokenInternalURL, false, params, &respData)
if err == nil && updateToken {
bot.accessToken.Store(respData.AppAccessToken)
}
return &respData, err
}
// GetTenantAccessTokenInternal gets AppAccessToken for internal use
func (bot *Bot) GetTenantAccessTokenInternal(updateToken bool) (*TenantAuthTokenInternalResponse, error) {
if !bot.requireType(ChatBot) {
return nil, ErrBotTypeError
}
params := map[string]interface{}{
"app_id": bot.appID,
"app_secret": bot.appSecret,
}
var respData TenantAuthTokenInternalResponse
err := bot.PostAPIRequest("GetTenantAccessTokenInternal", tenantAppAccessTokenInternalURL, false, params, &respData)
if err == nil && updateToken {
bot.tenantAccessToken.Store(respData.TenantAppAccessToken)
}
return &respData, err
}
// StopHeartbeat stop auto-renew
func (bot *Bot) StopHeartbeat() {
bot.heartbeat <- true
}
// StartHeartbeat renew auth token periodically
func (bot *Bot) StartHeartbeat() {
if !bot.requireType(ChatBot) {
bot.logger.Log(bot.ctx, LogLevelError, "Heartbeat only support Chat Bot")
return
}
bot.heartbeat = make(chan bool)
go func() {
for {
next := time.Second * 10
resp, err := bot.GetTenantAccessTokenInternal(true)
if err != nil {
bot.httpErrorLog("Heartbeat", "failed to get tenant access token", err)
}
if resp != nil && resp.Expire-20 > 0 {
next = time.Duration(resp.Expire-20) * time.Second
}
debugHeartbeat := bot.debugHeartbeat.Load().(int)
if debugHeartbeat > 0 {
next = time.Second * 1
}
t := time.NewTimer(next)
select {
case <-bot.heartbeat:
return
case <-t.C:
if debugHeartbeat > 0 {
bot.debugHeartbeat.Store(debugHeartbeat + 1)
}
}
}
}()
}