-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
49 lines (43 loc) · 1.32 KB
/
client.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
package oauth
import (
"github.com/EricJSanchez/wecom/cache"
"github.com/EricJSanchez/wecom/config"
"github.com/EricJSanchez/wecom/context"
"github.com/EricJSanchez/wecom/credential"
)
// Client OAuth2实例
type Client struct {
corpID string // 企业ID
agentID string // agent_id
secret string // Secret是用于校验开发者身份的访问密钥
token string // 用于生成签名校验回调请求的合法性
encodingAESKey string // 回调消息加解密参数是AES密钥的Base64编码,用于解密回调消息内容对应的密文
cache cache.Cache
ctx *context.Context
}
// NewClient 初始化实例
func NewClient(cfg *config.Config) (client *Client, err error) {
if cfg.Cache == nil {
return nil, NewSDKErr(50001)
}
//初始化 AccessToken Handle
defaultAkHandle := credential.NewWorkAccessToken(
cfg.CorpID,
cfg.AgentSecret,
credential.CacheKeyWorkPrefix+"oauth:"+cfg.CorpID+":"+cfg.AgentID,
cfg.Cache)
ctx := &context.Context{
Config: cfg,
AccessTokenHandle: defaultAkHandle,
}
client = &Client{
corpID: cfg.CorpID,
agentID: cfg.AgentID,
secret: cfg.AgentSecret,
token: cfg.Token,
encodingAESKey: cfg.EncodingAESKey,
cache: cfg.Cache,
ctx: ctx,
}
return client, nil
}