|
@@ -12,6 +12,7 @@ type TokenStoreItem interface {
|
|
|
Token() string
|
|
|
Refresh()
|
|
|
IsFilled() bool
|
|
|
+ IsShared() bool
|
|
|
}
|
|
|
|
|
|
type tokenStore struct {
|
|
@@ -22,34 +23,51 @@ type tokenStore struct {
|
|
|
|
|
|
var s tokenStore
|
|
|
|
|
|
+func channel2item(channel_ *model.Channel) TokenStoreItem {
|
|
|
+ if channel_.Type == model.TypeWeChatTestAccount {
|
|
|
+ item := &WeChatTestAccountTokenStoreItem{
|
|
|
+ AppID: channel_.AppId,
|
|
|
+ AppSecret: channel_.Secret,
|
|
|
+ }
|
|
|
+ return item
|
|
|
+ } else if channel_.Type == model.TypeWeChatCorpAccount {
|
|
|
+ corpId, agentId, err := parseWechatCorpAccountAppId(channel_.AppId)
|
|
|
+ if err != nil {
|
|
|
+ common.SysError(err.Error())
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ item := &WeChatCorpAccountTokenStoreItem{
|
|
|
+ CorpId: corpId,
|
|
|
+ AgentSecret: channel_.Secret,
|
|
|
+ AgentId: agentId,
|
|
|
+ }
|
|
|
+ return item
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func channels2items(channels []*model.Channel) []TokenStoreItem {
|
|
|
+ var items []TokenStoreItem
|
|
|
+ for _, channel_ := range channels {
|
|
|
+ item := channel2item(channel_)
|
|
|
+ if item != nil {
|
|
|
+ items = append(items, item)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return items
|
|
|
+}
|
|
|
+
|
|
|
func TokenStoreInit() {
|
|
|
s.Map = make(map[string]*TokenStoreItem)
|
|
|
// https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
|
|
|
// https://developer.work.weixin.qq.com/document/path/91039
|
|
|
s.ExpirationSeconds = 2 * 55 * 60 // 2 hours - 5 minutes
|
|
|
go func() {
|
|
|
- users, err := model.GetAllUsersWithSecrets()
|
|
|
+ channels, err := model.GetTokenStoreChannels()
|
|
|
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,
|
|
|
- AgentSecret: user.WeChatCorpAccountAgentSecret,
|
|
|
- AgentId: user.WeChatCorpAccountAgentId,
|
|
|
- }
|
|
|
- items = append(items, item)
|
|
|
- }
|
|
|
- }
|
|
|
+ items := channels2items(channels)
|
|
|
s.Mutex.RLock()
|
|
|
for i := range items {
|
|
|
// s.Map[item.Key()] = &item // This is wrong, you are getting the address of a local variable!
|
|
@@ -99,99 +117,124 @@ func TokenStoreRemoveItem(item TokenStoreItem) {
|
|
|
}
|
|
|
|
|
|
func TokenStoreAddUser(user *model.User) {
|
|
|
- testItem := WeChatTestAccountTokenStoreItem{
|
|
|
- AppID: user.WeChatTestAccountId,
|
|
|
- AppSecret: user.WeChatTestAccountSecret,
|
|
|
+ channels, err := model.GetTokenStoreChannelsByUserId(user.Id)
|
|
|
+ if err != nil {
|
|
|
+ common.SysError(err.Error())
|
|
|
+ return
|
|
|
}
|
|
|
- TokenStoreAddItem(&testItem)
|
|
|
- corpItem := WeChatCorpAccountTokenStoreItem{
|
|
|
- CorpId: user.WeChatCorpAccountId,
|
|
|
- AgentSecret: user.WeChatCorpAccountAgentSecret,
|
|
|
- AgentId: user.WeChatCorpAccountAgentId,
|
|
|
+ items := channels2items(channels)
|
|
|
+ for i := range items {
|
|
|
+ TokenStoreAddItem(items[i])
|
|
|
}
|
|
|
- TokenStoreAddItem(&corpItem)
|
|
|
}
|
|
|
|
|
|
-func TokenStoreUpdateUser(cleanUser *model.User, originUser *model.User) {
|
|
|
- // WeChat Test Account
|
|
|
- // The fields of cleanUser may be incomplete!
|
|
|
- if cleanUser.WeChatTestAccountId == originUser.WeChatTestAccountId {
|
|
|
- cleanUser.WeChatTestAccountId = ""
|
|
|
- }
|
|
|
- if cleanUser.WeChatTestAccountSecret == originUser.WeChatTestAccountSecret {
|
|
|
- cleanUser.WeChatTestAccountSecret = ""
|
|
|
+// TokenStoreRemoveUser
|
|
|
+// user must be filled.
|
|
|
+// It's okay to delete a user that don't have an item here.
|
|
|
+func TokenStoreRemoveUser(user *model.User) {
|
|
|
+ channels, err := model.GetTokenStoreChannelsByUserId(user.Id)
|
|
|
+ if err != nil {
|
|
|
+ common.SysError(err.Error())
|
|
|
+ return
|
|
|
}
|
|
|
- // This means the user updated those fields.
|
|
|
- if cleanUser.WeChatTestAccountId != "" || cleanUser.WeChatTestAccountSecret != "" {
|
|
|
- oldWeChatTestAccountTokenStoreItem := WeChatTestAccountTokenStoreItem{
|
|
|
- AppID: originUser.WeChatTestAccountId,
|
|
|
- AppSecret: originUser.WeChatTestAccountSecret,
|
|
|
+ items := channels2items(channels)
|
|
|
+ for i := range items {
|
|
|
+ if items[i].IsShared() {
|
|
|
+ continue
|
|
|
}
|
|
|
- // Yeah, it's a deep copy.
|
|
|
- newWeChatTestAccountTokenStoreItem := oldWeChatTestAccountTokenStoreItem
|
|
|
- if cleanUser.WeChatTestAccountId != "" {
|
|
|
- newWeChatTestAccountTokenStoreItem.AppID = cleanUser.WeChatTestAccountId
|
|
|
- }
|
|
|
- if cleanUser.WeChatTestAccountSecret != "" {
|
|
|
- newWeChatTestAccountTokenStoreItem.AppSecret = cleanUser.WeChatTestAccountSecret
|
|
|
- }
|
|
|
- if !oldWeChatTestAccountTokenStoreItem.IsShared() {
|
|
|
- TokenStoreRemoveItem(&oldWeChatTestAccountTokenStoreItem)
|
|
|
- }
|
|
|
- TokenStoreAddItem(&newWeChatTestAccountTokenStoreItem)
|
|
|
+ TokenStoreRemoveItem(items[i])
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TokenStoreAddChannel(channel *model.Channel) {
|
|
|
+ if channel.Type != model.TypeWeChatTestAccount && channel.Type != model.TypeWeChatCorpAccount {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ item := channel2item(channel)
|
|
|
+ if item != nil {
|
|
|
+ TokenStoreAddItem(item)
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- // WeChat Corp Account
|
|
|
- if cleanUser.WeChatCorpAccountId == originUser.WeChatCorpAccountId {
|
|
|
- cleanUser.WeChatCorpAccountId = ""
|
|
|
+func TokenStoreRemoveChannel(channel *model.Channel) {
|
|
|
+ if channel.Type != model.TypeWeChatTestAccount && channel.Type != model.TypeWeChatCorpAccount {
|
|
|
+ return
|
|
|
}
|
|
|
- if cleanUser.WeChatCorpAccountAgentId == originUser.WeChatCorpAccountAgentId {
|
|
|
- cleanUser.WeChatCorpAccountAgentId = ""
|
|
|
+ item := channel2item(channel)
|
|
|
+ if item != nil {
|
|
|
+ TokenStoreRemoveItem(item)
|
|
|
}
|
|
|
- if cleanUser.WeChatCorpAccountAgentSecret == originUser.WeChatCorpAccountAgentSecret {
|
|
|
- cleanUser.WeChatCorpAccountAgentSecret = ""
|
|
|
+}
|
|
|
+
|
|
|
+func TokenStoreUpdateChannel(newChannel *model.Channel, oldChannel *model.Channel) {
|
|
|
+ if oldChannel.Type != model.TypeWeChatTestAccount && oldChannel.Type != model.TypeWeChatCorpAccount {
|
|
|
+ return
|
|
|
}
|
|
|
- if cleanUser.WeChatCorpAccountId != "" || cleanUser.WeChatCorpAccountAgentId != "" || cleanUser.WeChatCorpAccountAgentSecret != "" {
|
|
|
- oldWeChatCorpAccountTokenStoreItem := WeChatCorpAccountTokenStoreItem{
|
|
|
- CorpId: originUser.WeChatCorpAccountId,
|
|
|
- AgentSecret: originUser.WeChatCorpAccountAgentSecret,
|
|
|
- AgentId: originUser.WeChatCorpAccountAgentId,
|
|
|
+ if oldChannel.Type == model.TypeWeChatTestAccount {
|
|
|
+ // Only keep changed parts
|
|
|
+ if newChannel.AppId == oldChannel.AppId {
|
|
|
+ newChannel.AppId = ""
|
|
|
}
|
|
|
- newWeChatCorpAccountTokenStoreItem := oldWeChatCorpAccountTokenStoreItem
|
|
|
- if cleanUser.WeChatCorpAccountId != "" {
|
|
|
- newWeChatCorpAccountTokenStoreItem.CorpId = cleanUser.WeChatCorpAccountId
|
|
|
+ if newChannel.Secret == oldChannel.Secret {
|
|
|
+ newChannel.Secret = ""
|
|
|
}
|
|
|
- if cleanUser.WeChatCorpAccountAgentSecret != "" {
|
|
|
- newWeChatCorpAccountTokenStoreItem.AgentSecret = cleanUser.WeChatCorpAccountAgentSecret
|
|
|
+ oldItem := WeChatTestAccountTokenStoreItem{
|
|
|
+ AppID: oldChannel.AppId,
|
|
|
+ AppSecret: oldChannel.Secret,
|
|
|
}
|
|
|
- if cleanUser.WeChatCorpAccountAgentId != "" {
|
|
|
- newWeChatCorpAccountTokenStoreItem.AgentId = cleanUser.WeChatCorpAccountAgentId
|
|
|
+ // Yeah, it's a deep copy.
|
|
|
+ newItem := oldItem
|
|
|
+ // This means the user updated those fields.
|
|
|
+ if newChannel.AppId != "" {
|
|
|
+ newItem.AppID = newChannel.AppId
|
|
|
}
|
|
|
- if !oldWeChatCorpAccountTokenStoreItem.IsShared() {
|
|
|
- TokenStoreRemoveItem(&oldWeChatCorpAccountTokenStoreItem)
|
|
|
+ if newChannel.Secret != "" {
|
|
|
+ newItem.AppSecret = newChannel.Secret
|
|
|
}
|
|
|
- TokenStoreAddItem(&newWeChatCorpAccountTokenStoreItem)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// TokenStoreRemoveUser
|
|
|
-// user must be filled.
|
|
|
-// It's okay to delete a user that don't have an item here.
|
|
|
-func TokenStoreRemoveUser(user *model.User) {
|
|
|
- testAccountTokenStoreItem := WeChatTestAccountTokenStoreItem{
|
|
|
- AppID: user.WeChatTestAccountId,
|
|
|
- AppSecret: user.WeChatTestAccountSecret,
|
|
|
- }
|
|
|
- if !testAccountTokenStoreItem.IsShared() {
|
|
|
- TokenStoreRemoveItem(&testAccountTokenStoreItem)
|
|
|
- }
|
|
|
- corpAccountTokenStoreItem := WeChatCorpAccountTokenStoreItem{
|
|
|
- CorpId: user.WeChatCorpAccountId,
|
|
|
- AgentSecret: user.WeChatCorpAccountAgentSecret,
|
|
|
- AgentId: user.WeChatCorpAccountAgentId,
|
|
|
+ if !oldItem.IsShared() {
|
|
|
+ TokenStoreRemoveItem(&oldItem)
|
|
|
+ }
|
|
|
+ TokenStoreAddItem(&newItem)
|
|
|
+ return
|
|
|
}
|
|
|
- if !corpAccountTokenStoreItem.IsShared() {
|
|
|
- TokenStoreRemoveItem(&corpAccountTokenStoreItem)
|
|
|
+ if oldChannel.Type == model.TypeWeChatCorpAccount {
|
|
|
+ // Only keep changed parts
|
|
|
+ if newChannel.AppId == oldChannel.AppId {
|
|
|
+ newChannel.AppId = ""
|
|
|
+ }
|
|
|
+ if newChannel.Secret == oldChannel.Secret {
|
|
|
+ newChannel.Secret = ""
|
|
|
+ }
|
|
|
+ corpId, agentId, err := parseWechatCorpAccountAppId(oldChannel.AppId)
|
|
|
+ if err != nil {
|
|
|
+ common.SysError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ oldItem := WeChatCorpAccountTokenStoreItem{
|
|
|
+ CorpId: corpId,
|
|
|
+ AgentSecret: oldChannel.Secret,
|
|
|
+ AgentId: agentId,
|
|
|
+ }
|
|
|
+ // Yeah, it's a deep copy.
|
|
|
+ newItem := oldItem
|
|
|
+ // This means the user updated those fields.
|
|
|
+ if newChannel.AppId != "" {
|
|
|
+ corpId, agentId, err := parseWechatCorpAccountAppId(oldChannel.AppId)
|
|
|
+ if err != nil {
|
|
|
+ common.SysError(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newItem.CorpId = corpId
|
|
|
+ newItem.AgentId = agentId
|
|
|
+ }
|
|
|
+ if newChannel.Secret != "" {
|
|
|
+ newItem.AgentSecret = newChannel.Secret
|
|
|
+ }
|
|
|
+ if !oldItem.IsShared() {
|
|
|
+ TokenStoreRemoveItem(&oldItem)
|
|
|
+ }
|
|
|
+ TokenStoreAddItem(&newItem)
|
|
|
+ return
|
|
|
}
|
|
|
}
|
|
|
|