-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
api_auth.go
109 lines (96 loc) · 2.92 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
package lark
import (
"sync/atomic"
"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() error {
return bot.startHeartbeat(10 * time.Second)
}
func (bot *Bot) startHeartbeat(defaultInterval time.Duration) error {
if !bot.requireType(ChatBot) {
return ErrBotTypeError
}
// First initialize the token in blocking mode
_, err := bot.GetTenantAccessTokenInternal(true)
if err != nil {
bot.httpErrorLog("Heartbeat", "failed to get tenant access token", err)
return err
}
atomic.AddInt64(&bot.heartbeatCounter, 1)
interval := defaultInterval
bot.heartbeat = make(chan bool)
go func() {
for {
t := time.NewTimer(interval)
select {
case <-bot.heartbeat:
return
case <-t.C:
interval = defaultInterval
resp, err := bot.GetTenantAccessTokenInternal(true)
if err != nil {
bot.httpErrorLog("Heartbeat", "failed to get tenant access token", err)
}
atomic.AddInt64(&bot.heartbeatCounter, 1)
if resp != nil && resp.Expire-20 > 0 {
interval = time.Duration(resp.Expire-20) * time.Second
}
}
}
}()
return nil
}