token-store.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package channel
  2. import (
  3. "message-pusher/common"
  4. "message-pusher/model"
  5. "sync"
  6. "time"
  7. )
  8. type TokenStoreItem interface {
  9. Key() string
  10. Token() string
  11. Refresh()
  12. }
  13. type tokenStore struct {
  14. Map map[string]*TokenStoreItem
  15. Mutex sync.RWMutex
  16. ExpirationSeconds int
  17. }
  18. var s tokenStore
  19. func TokenStoreInit() {
  20. s.Map = make(map[string]*TokenStoreItem)
  21. s.ExpirationSeconds = 2 * 60 * 60
  22. go func() {
  23. users, err := model.GetAllUsersWithSecrets()
  24. if err != nil {
  25. common.FatalLog(err.Error())
  26. }
  27. var items []TokenStoreItem
  28. for _, user := range users {
  29. if user.WeChatTestAccountId != "" {
  30. item := &WeChatTestAccountTokenStoreItem{
  31. AppID: user.WeChatTestAccountId,
  32. AppSecret: user.WeChatTestAccountSecret,
  33. }
  34. items = append(items, item)
  35. }
  36. if user.WeChatCorpAccountId != "" {
  37. item := &WeChatCorpAccountTokenStoreItem{
  38. CorpId: user.WeChatCorpAccountId,
  39. AgentSecret: user.WeChatCorpAccountAgentSecret,
  40. AgentId: user.WeChatCorpAccountAgentId,
  41. }
  42. items = append(items, item)
  43. }
  44. }
  45. s.Mutex.RLock()
  46. for i := range items {
  47. // s.Map[item.Key()] = &item // This is wrong, you are getting the address of a local variable!
  48. s.Map[items[i].Key()] = &items[i]
  49. }
  50. s.Mutex.RUnlock()
  51. for {
  52. s.Mutex.RLock()
  53. var tmpMap = make(map[string]*TokenStoreItem)
  54. for k, v := range s.Map {
  55. tmpMap[k] = v
  56. }
  57. s.Mutex.RUnlock()
  58. for k := range tmpMap {
  59. (*tmpMap[k]).Refresh()
  60. }
  61. s.Mutex.RLock()
  62. // we shouldn't directly replace the old map with the new map, cause the old map's keys may already change
  63. for k := range s.Map {
  64. v, okay := tmpMap[k]
  65. if okay {
  66. s.Map[k] = v
  67. }
  68. }
  69. sleepDuration := common.Max(s.ExpirationSeconds, 60)
  70. s.Mutex.RUnlock()
  71. time.Sleep(time.Duration(sleepDuration) * time.Second)
  72. }
  73. }()
  74. }
  75. func TokenStoreAddItem(item TokenStoreItem) {
  76. item.Refresh()
  77. s.Mutex.RLock()
  78. s.Map[item.Key()] = &item
  79. s.Mutex.RUnlock()
  80. }
  81. func TokenStoreRemoveItem(item TokenStoreItem) {
  82. s.Mutex.RLock()
  83. delete(s.Map, item.Key())
  84. s.Mutex.RUnlock()
  85. }
  86. func TokenStoreUpdateUser(cleanUser *model.User, originUser *model.User) {
  87. if cleanUser.WeChatTestAccountId == originUser.WeChatTestAccountId {
  88. cleanUser.WeChatTestAccountId = ""
  89. }
  90. if cleanUser.WeChatTestAccountSecret == originUser.WeChatTestAccountSecret {
  91. cleanUser.WeChatTestAccountSecret = ""
  92. }
  93. if cleanUser.WeChatTestAccountId != "" || cleanUser.WeChatTestAccountSecret != "" {
  94. oldWeChatTestAccountTokenStoreItem := WeChatTestAccountTokenStoreItem{
  95. AppID: originUser.WeChatTestAccountId,
  96. AppSecret: originUser.WeChatTestAccountSecret,
  97. }
  98. newWeChatTestAccountTokenStoreItem := oldWeChatTestAccountTokenStoreItem
  99. if cleanUser.WeChatTestAccountId != "" {
  100. newWeChatTestAccountTokenStoreItem.AppID = cleanUser.WeChatTestAccountId
  101. }
  102. if cleanUser.WeChatTestAccountSecret != "" {
  103. newWeChatTestAccountTokenStoreItem.AppSecret = cleanUser.WeChatTestAccountSecret
  104. }
  105. if !oldWeChatTestAccountTokenStoreItem.IsShared() {
  106. TokenStoreRemoveItem(&oldWeChatTestAccountTokenStoreItem)
  107. }
  108. TokenStoreAddItem(&newWeChatTestAccountTokenStoreItem)
  109. }
  110. if cleanUser.WeChatCorpAccountId == originUser.WeChatCorpAccountId {
  111. cleanUser.WeChatCorpAccountId = ""
  112. }
  113. if cleanUser.WeChatCorpAccountAgentId == originUser.WeChatCorpAccountAgentId {
  114. cleanUser.WeChatCorpAccountAgentId = ""
  115. }
  116. if cleanUser.WeChatCorpAccountAgentSecret == originUser.WeChatCorpAccountAgentSecret {
  117. cleanUser.WeChatCorpAccountAgentSecret = ""
  118. }
  119. if cleanUser.WeChatCorpAccountId != "" || cleanUser.WeChatCorpAccountAgentId != "" || cleanUser.WeChatCorpAccountAgentSecret != "" {
  120. oldWeChatCorpAccountTokenStoreItem := WeChatCorpAccountTokenStoreItem{
  121. CorpId: cleanUser.WeChatCorpAccountId,
  122. AgentSecret: cleanUser.WeChatCorpAccountAgentSecret,
  123. AgentId: cleanUser.WeChatCorpAccountAgentId,
  124. }
  125. newWeChatCorpAccountTokenStoreItem := oldWeChatCorpAccountTokenStoreItem
  126. if cleanUser.WeChatCorpAccountId != "" {
  127. newWeChatCorpAccountTokenStoreItem.CorpId = cleanUser.WeChatCorpAccountId
  128. }
  129. if cleanUser.WeChatCorpAccountAgentSecret != "" {
  130. newWeChatCorpAccountTokenStoreItem.AgentSecret = cleanUser.WeChatCorpAccountAgentSecret
  131. }
  132. if cleanUser.WeChatCorpAccountAgentId != "" {
  133. newWeChatCorpAccountTokenStoreItem.AgentId = cleanUser.WeChatCorpAccountAgentId
  134. }
  135. if !oldWeChatCorpAccountTokenStoreItem.IsShared() {
  136. TokenStoreRemoveItem(&oldWeChatCorpAccountTokenStoreItem)
  137. }
  138. TokenStoreAddItem(&newWeChatCorpAccountTokenStoreItem)
  139. }
  140. }
  141. // TokenStoreRemoveUser user must be filled
  142. func TokenStoreRemoveUser(user *model.User) {
  143. testAccountTokenStoreItem := WeChatTestAccountTokenStoreItem{
  144. AppID: user.WeChatTestAccountId,
  145. AppSecret: user.WeChatTestAccountSecret,
  146. }
  147. if !testAccountTokenStoreItem.IsShared() {
  148. TokenStoreRemoveItem(&testAccountTokenStoreItem)
  149. }
  150. corpAccountTokenStoreItem := WeChatCorpAccountTokenStoreItem{
  151. CorpId: user.WeChatCorpAccountId,
  152. AgentSecret: user.WeChatCorpAccountAgentSecret,
  153. AgentId: user.WeChatCorpAccountAgentId,
  154. }
  155. if !corpAccountTokenStoreItem.IsShared() {
  156. TokenStoreRemoveItem(&corpAccountTokenStoreItem)
  157. }
  158. }
  159. func TokenStoreGetToken(key string) string {
  160. s.Mutex.RLock()
  161. defer s.Mutex.RUnlock()
  162. item, ok := s.Map[key]
  163. if ok {
  164. return (*item).Token()
  165. }
  166. common.SysError("token for " + key + " is blank!")
  167. return ""
  168. }