feat: token store is done but not tested
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package channel
|
||||
|
||||
import (
|
||||
"message-pusher/common"
|
||||
"message-pusher/model"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TokenStoreItem interface {
|
||||
Key() string
|
||||
Token() string
|
||||
Refresh()
|
||||
}
|
||||
|
||||
type tokenStore struct {
|
||||
Map map[string]*TokenStoreItem
|
||||
Mutex sync.RWMutex
|
||||
ExpirationSeconds int
|
||||
}
|
||||
|
||||
var s tokenStore
|
||||
|
||||
func TokenStoreInit() {
|
||||
s.Map = make(map[string]*TokenStoreItem)
|
||||
s.ExpirationSeconds = 2 * 60 * 60
|
||||
go func() {
|
||||
users, err := model.GetAllUsers()
|
||||
if err != nil {
|
||||
common.FatalLog(err.Error())
|
||||
}
|
||||
var items []TokenStoreItem
|
||||
for _, user := range users {
|
||||
if user.WeChatTestAccountId != "" {
|
||||
item := &WeChatTestAccountTokenStoreItem{
|
||||
AppID: user.WeChatTestAccountId,
|
||||
AppSecret: user.WeChatTestAccountSecret,
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if user.WeChatCorpAccountId != "" {
|
||||
item := &WeChatCorpAccountTokenStoreItem{
|
||||
CorpId: user.WeChatCorpAccountId,
|
||||
CorpSecret: user.WeChatCorpAccountSecret,
|
||||
AgentId: user.WeChatCorpAccountAgentId,
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
}
|
||||
s.Mutex.RLock()
|
||||
for _, item := range items {
|
||||
s.Map[item.Key()] = &item
|
||||
}
|
||||
s.Mutex.RUnlock()
|
||||
for {
|
||||
s.Mutex.RLock()
|
||||
var tmpMap = make(map[string]*TokenStoreItem)
|
||||
for k, v := range s.Map {
|
||||
tmpMap[k] = v
|
||||
}
|
||||
s.Mutex.RUnlock()
|
||||
for k := range tmpMap {
|
||||
(*tmpMap[k]).Refresh()
|
||||
}
|
||||
s.Mutex.RLock()
|
||||
// we shouldn't directly replace the old map with the new map, cause the old map's keys may already change
|
||||
for k := range s.Map {
|
||||
v, okay := tmpMap[k]
|
||||
if okay {
|
||||
s.Map[k] = v
|
||||
}
|
||||
}
|
||||
sleepDuration := common.Max(s.ExpirationSeconds, 60)
|
||||
s.Mutex.RUnlock()
|
||||
time.Sleep(time.Duration(sleepDuration) * time.Second)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func TokenStoreAddItem(item *TokenStoreItem) {
|
||||
(*item).Refresh()
|
||||
s.Mutex.RLock()
|
||||
s.Map[(*item).Key()] = item
|
||||
s.Mutex.RUnlock()
|
||||
}
|
||||
|
||||
func TokenStoreRemoveItem(item *TokenStoreItem) {
|
||||
s.Mutex.RLock()
|
||||
delete(s.Map, (*item).Key())
|
||||
s.Mutex.RUnlock()
|
||||
}
|
||||
|
||||
func TokenStoreGetToken(key string) string {
|
||||
s.Mutex.RLock()
|
||||
defer s.Mutex.RUnlock()
|
||||
return (*s.Map[key]).Token()
|
||||
}
|
||||
@@ -1 +1,62 @@
|
||||
package channel
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"message-pusher/common"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type wechatCorpAccountResponse struct {
|
||||
ErrorCode int `json:"errcode"`
|
||||
ErrorMessage string `json:"errmsg"`
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
type WeChatCorpAccountTokenStoreItem struct {
|
||||
CorpId string
|
||||
CorpSecret string
|
||||
AgentId string
|
||||
AccessToken string
|
||||
}
|
||||
|
||||
func (i *WeChatCorpAccountTokenStoreItem) Key() string {
|
||||
return i.CorpId + i.AgentId + i.CorpSecret
|
||||
}
|
||||
|
||||
func (i *WeChatCorpAccountTokenStoreItem) Token() string {
|
||||
return i.AccessToken
|
||||
}
|
||||
|
||||
func (i *WeChatCorpAccountTokenStoreItem) Refresh() {
|
||||
// https://work.weixin.qq.com/api/doc/90000/90135/91039
|
||||
client := http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s",
|
||||
i.CorpId, i.CorpSecret), nil)
|
||||
if err != nil {
|
||||
common.SysError(err.Error())
|
||||
return
|
||||
}
|
||||
responseData, err := client.Do(req)
|
||||
if err != nil {
|
||||
common.SysError("failed to refresh access token: " + err.Error())
|
||||
return
|
||||
}
|
||||
defer responseData.Body.Close()
|
||||
var res wechatCorpAccountResponse
|
||||
err = json.NewDecoder(responseData.Body).Decode(&res)
|
||||
if err != nil {
|
||||
common.SysError("failed to decode wechatCorpAccountResponse: " + err.Error())
|
||||
return
|
||||
}
|
||||
if res.ErrorCode != 0 {
|
||||
common.SysError(res.ErrorMessage)
|
||||
return
|
||||
}
|
||||
i.AccessToken = res.AccessToken
|
||||
common.SysLog("access token refreshed")
|
||||
}
|
||||
|
||||
@@ -1 +1,61 @@
|
||||
package channel
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"message-pusher/common"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type wechatTestAccountResponse struct {
|
||||
ErrorCode int `json:"errcode"`
|
||||
ErrorMessage string `json:"errmsg"`
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
type WeChatTestAccountTokenStoreItem struct {
|
||||
AppID string
|
||||
AppSecret string
|
||||
AccessToken string
|
||||
}
|
||||
|
||||
func (i *WeChatTestAccountTokenStoreItem) Key() string {
|
||||
return i.AppID + i.AppSecret
|
||||
}
|
||||
|
||||
func (i *WeChatTestAccountTokenStoreItem) Token() string {
|
||||
return i.AccessToken
|
||||
}
|
||||
|
||||
func (i *WeChatTestAccountTokenStoreItem) Refresh() {
|
||||
// https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
|
||||
client := http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
|
||||
i.AppID, i.AppSecret), nil)
|
||||
if err != nil {
|
||||
common.SysError(err.Error())
|
||||
return
|
||||
}
|
||||
responseData, err := client.Do(req)
|
||||
if err != nil {
|
||||
common.SysError("failed to refresh access token: " + err.Error())
|
||||
return
|
||||
}
|
||||
defer responseData.Body.Close()
|
||||
var res wechatTestAccountResponse
|
||||
err = json.NewDecoder(responseData.Body).Decode(&res)
|
||||
if err != nil {
|
||||
common.SysError("failed to decode wechatTestAccountResponse: " + err.Error())
|
||||
return
|
||||
}
|
||||
if res.ErrorCode != 0 {
|
||||
common.SysError(res.ErrorMessage)
|
||||
return
|
||||
}
|
||||
i.AccessToken = res.AccessToken
|
||||
common.SysLog("access token refreshed")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user