Skip to content

Commit 71c8ab5

Browse files
authored
F ix work access token (#810)
* feat: enhance WorkAccessToken to include AgentID for improved token management - Added AgentID field to WorkAccessToken struct. - Updated NewWorkAccessToken function to accept AgentID as a parameter. - Modified access token cache key to incorporate AgentID, ensuring unique cache entries per agent. This change improves the handling of access tokens in a multi-agent environment. * refactor: enhance WorkAccessToken to improve cache key handling - Updated the AgentID field in WorkAccessToken struct to clarify its optional nature for distinguishing applications. - Modified the access token cache key construction to support both new and legacy formats based on the presence of AgentID. - Added comments for better understanding of the cache key logic and its compatibility with historical versions. This change improves the flexibility and clarity of access token management in multi-agent scenarios. * feat: enhance WorkAccessToken with new constructor for AgentID support - Introduced NewWorkAccessTokenWithAgentID function to maintain backward compatibility while allowing for AgentID usage. - Updated NewWorkAccessToken to call the new constructor, ensuring seamless integration. - Improved error handling in GetAccessTokenContext by checking for cache availability and handling potential errors during cache operations. This change enhances the flexibility of access token management, particularly in multi-agent scenarios, while ensuring compatibility with existing implementations.
1 parent 92bf6c7 commit 71c8ab5

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

credential/default_access_token.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,27 @@ func (ak *StableAccessToken) GetAccessTokenDirectly(ctx context.Context, forceRe
189189
type WorkAccessToken struct {
190190
CorpID string
191191
CorpSecret string
192+
AgentID string // 可选,用于区分不同应用
192193
cacheKeyPrefix string
193194
cache cache.Cache
194195
accessTokenLock *sync.Mutex
195196
}
196197

197-
// NewWorkAccessToken new WorkAccessToken
198-
func NewWorkAccessToken(corpID, corpSecret, cacheKeyPrefix string, cache cache.Cache) AccessTokenContextHandle {
198+
// NewWorkAccessToken new WorkAccessToken (保持向后兼容)
199+
func NewWorkAccessToken(corpID, corpSecret, agentID, cacheKeyPrefix string, cache cache.Cache) AccessTokenContextHandle {
200+
// 调用新方法,保持兼容性
201+
return NewWorkAccessTokenWithAgentID(corpID, corpSecret, agentID, cacheKeyPrefix, cache)
202+
}
203+
204+
// NewWorkAccessTokenWithAgentID new WorkAccessToken with agentID
205+
func NewWorkAccessTokenWithAgentID(corpID, corpSecret, agentID, cacheKeyPrefix string, cache cache.Cache) AccessTokenContextHandle {
199206
if cache == nil {
200-
panic("cache the not exist")
207+
panic("cache is needed")
201208
}
202209
return &WorkAccessToken{
203210
CorpID: corpID,
204211
CorpSecret: corpSecret,
212+
AgentID: agentID,
205213
cache: cache,
206214
cacheKeyPrefix: cacheKeyPrefix,
207215
accessTokenLock: new(sync.Mutex),
@@ -218,7 +226,17 @@ func (ak *WorkAccessToken) GetAccessTokenContext(ctx context.Context) (accessTok
218226
// 加上lock,是为了防止在并发获取token时,cache刚好失效,导致从微信服务器上获取到不同token
219227
ak.accessTokenLock.Lock()
220228
defer ak.accessTokenLock.Unlock()
221-
accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", ak.cacheKeyPrefix, ak.CorpID)
229+
230+
// 构建缓存key
231+
var accessTokenCacheKey string
232+
if ak.AgentID != "" {
233+
// 如果设置了AgentID,使用新的key格式
234+
accessTokenCacheKey = fmt.Sprintf("%s_access_token_%s_%s", ak.cacheKeyPrefix, ak.CorpID, ak.AgentID)
235+
} else {
236+
// 兼容历史版本的key格式
237+
accessTokenCacheKey = fmt.Sprintf("%s_access_token_%s", ak.cacheKeyPrefix, ak.CorpID)
238+
}
239+
222240
val := ak.cache.Get(accessTokenCacheKey)
223241
if val != nil {
224242
accessToken = val.(string)
@@ -234,6 +252,9 @@ func (ak *WorkAccessToken) GetAccessTokenContext(ctx context.Context) (accessTok
234252

235253
expires := resAccessToken.ExpiresIn - 1500
236254
err = ak.cache.Set(accessTokenCacheKey, resAccessToken.AccessToken, time.Duration(expires)*time.Second)
255+
if err != nil {
256+
return
257+
}
237258

238259
accessToken = resAccessToken.AccessToken
239260
return

work/config/config.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ import (
77

88
// Config for 企业微信
99
type Config struct {
10-
CorpID string `json:"corp_id"` // corp_id
11-
CorpSecret string `json:"corp_secret"` // corp_secret,如果需要获取会话存档实例,当前参数请填写聊天内容存档的Secret,可以在企业微信管理端--管理工具--聊天内容存档查看
12-
AgentID string `json:"agent_id"` // agent_id
13-
Cache cache.Cache
14-
RasPrivateKey string // 消息加密私钥,可以在企业微信管理端--管理工具--消息加密公钥查看对用公钥,私钥一般由自己保存
15-
10+
CorpID string `json:"corp_id"` // corp_id
11+
CorpSecret string `json:"corp_secret"` // corp_secret,如果需要获取会话存档实例,当前参数请填写聊天内容存档的Secret,可以在企业微信管理端--管理工具--聊天内容存档查看
12+
AgentID string `json:"agent_id"` // agent_id
13+
Cache cache.Cache
14+
RasPrivateKey string // 消息加密私钥,可以在企业微信管理端--管理工具--消息加密公钥查看对用公钥,私钥一般由自己保存
1615
Token string `json:"token"` // 微信客服回调配置,用于生成签名校验回调请求的合法性
1716
EncodingAESKey string `json:"encoding_aes_key"` // 微信客服回调p配置,用于解密回调消息内容对应的密文
1817
}

work/kf/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewClient(cfg *config.Config) (client *Client, err error) {
2424
}
2525

2626
// 初始化 AccessToken Handle
27-
defaultAkHandle := credential.NewWorkAccessToken(cfg.CorpID, cfg.CorpSecret, credential.CacheKeyWorkPrefix, cfg.Cache)
27+
defaultAkHandle := credential.NewWorkAccessToken(cfg.CorpID, cfg.CorpSecret, cfg.AgentID, credential.CacheKeyWorkPrefix, cfg.Cache)
2828
ctx := &context.Context{
2929
Config: cfg,
3030
AccessTokenHandle: defaultAkHandle,

work/work.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Work struct {
2424

2525
// NewWork init work
2626
func NewWork(cfg *config.Config) *Work {
27-
defaultAkHandle := credential.NewWorkAccessToken(cfg.CorpID, cfg.CorpSecret, credential.CacheKeyWorkPrefix, cfg.Cache)
27+
defaultAkHandle := credential.NewWorkAccessToken(cfg.CorpID, cfg.CorpSecret, cfg.AgentID, credential.CacheKeyWorkPrefix, cfg.Cache)
2828
ctx := &context.Context{
2929
Config: cfg,
3030
AccessTokenHandle: defaultAkHandle,

0 commit comments

Comments
 (0)