From 010e49c35cd8af310c43b7bef1c5ab2a919c318d Mon Sep 17 00:00:00 2001 From: mahongran <54567253+mahongran@users.noreply.github.com> Date: Tue, 14 Jan 2025 17:24:42 +0800 Subject: [PATCH] fix: work js-api signature (#818) * 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(work): add JsSdk method for JavaScript SDK integration - Introduced a new JsSdk method in the Work struct to facilitate the creation of a Js instance. - This addition enhances the functionality of the Work module by enabling JavaScript SDK support. This change improves the integration capabilities for developers working with the WeChat Work API. * fix gofmt * refactor(jsapi): simplify signature generation in GetConfig and GetAgentConfig methods - Replaced the use of strconv to format the signature string with fmt.Sprintf for improved readability. - Updated the signature generation logic in both GetConfig and GetAgentConfig methods to use a single formatted string. This change enhances code clarity and maintains functionality in the signature generation process. * fix gofmt * fix gofmt * fix gofmt * fix gofmt * refactor(js): correct initialization of Config in GetConfig method - Removed redundant initialization of the Config variable and ensured it is instantiated correctly before use. - This change improves code clarity and maintains the intended functionality of the GetConfig method. --- officialaccount/js/js.go | 4 +--- work/jsapi/jsapi.go | 8 +++++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/officialaccount/js/js.go b/officialaccount/js/js.go index e1c749fbc..53c86d37e 100644 --- a/officialaccount/js/js.go +++ b/officialaccount/js/js.go @@ -39,7 +39,6 @@ func (js *Js) SetJsTicketHandle(ticketHandle credential.JsTicketHandle) { // GetConfig 获取jssdk需要的配置参数 // uri 为当前网页地址 func (js *Js) GetConfig(uri string) (config *Config, err error) { - config = new(Config) var accessToken string accessToken, err = js.GetAccessToken() if err != nil { @@ -50,12 +49,11 @@ func (js *Js) GetConfig(uri string) (config *Config, err error) { if err != nil { return } - nonceStr := util.RandomStr(16) timestamp := util.GetCurrTS() str := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%d&url=%s", ticketStr, nonceStr, timestamp, uri) sigStr := util.Signature(str) - + config = new(Config) config.AppID = js.AppID config.NonceStr = nonceStr config.Timestamp = timestamp diff --git a/work/jsapi/jsapi.go b/work/jsapi/jsapi.go index 395837818..53814057a 100644 --- a/work/jsapi/jsapi.go +++ b/work/jsapi/jsapi.go @@ -1,7 +1,7 @@ package jsapi import ( - "strconv" + "fmt" "github.com/silenceper/wechat/v2/credential" "github.com/silenceper/wechat/v2/util" @@ -49,7 +49,8 @@ func (js *Js) GetConfig(uri string) (config *Config, err error) { } config.NonceStr = util.RandomStr(16) config.Timestamp = util.GetCurrTS() - config.Signature = util.Signature(ticketStr, config.NonceStr, strconv.FormatInt(config.Timestamp, 10), uri) + str := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%d&url=%s", ticketStr, config.NonceStr, config.Timestamp, uri) + config.Signature = util.Signature(str) return } @@ -68,6 +69,7 @@ func (js *Js) GetAgentConfig(uri string) (config *Config, err error) { } config.NonceStr = util.RandomStr(16) config.Timestamp = util.GetCurrTS() - config.Signature = util.Signature(ticketStr, config.NonceStr, strconv.FormatInt(config.Timestamp, 10), uri) + str := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%d&url=%s", ticketStr, config.NonceStr, config.Timestamp, uri) + config.Signature = util.Signature(str) return }