simple_token.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package auth
  15. // CAUTION: This random number based token mechanism is only for testing purpose.
  16. // JWT based mechanism will be added in the near future.
  17. import (
  18. "context"
  19. "crypto/rand"
  20. "fmt"
  21. "math/big"
  22. "strconv"
  23. "strings"
  24. "sync"
  25. "time"
  26. "go.uber.org/zap"
  27. )
  28. const (
  29. letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  30. defaultSimpleTokenLength = 16
  31. )
  32. // var for testing purposes
  33. var (
  34. simpleTokenTTL = 5 * time.Minute
  35. simpleTokenTTLResolution = 1 * time.Second
  36. )
  37. type simpleTokenTTLKeeper struct {
  38. tokens map[string]time.Time
  39. donec chan struct{}
  40. stopc chan struct{}
  41. deleteTokenFunc func(string)
  42. mu *sync.Mutex
  43. }
  44. func (tm *simpleTokenTTLKeeper) stop() {
  45. select {
  46. case tm.stopc <- struct{}{}:
  47. case <-tm.donec:
  48. }
  49. <-tm.donec
  50. }
  51. func (tm *simpleTokenTTLKeeper) addSimpleToken(token string) {
  52. tm.tokens[token] = time.Now().Add(simpleTokenTTL)
  53. }
  54. func (tm *simpleTokenTTLKeeper) resetSimpleToken(token string) {
  55. if _, ok := tm.tokens[token]; ok {
  56. tm.tokens[token] = time.Now().Add(simpleTokenTTL)
  57. }
  58. }
  59. func (tm *simpleTokenTTLKeeper) deleteSimpleToken(token string) {
  60. delete(tm.tokens, token)
  61. }
  62. func (tm *simpleTokenTTLKeeper) run() {
  63. tokenTicker := time.NewTicker(simpleTokenTTLResolution)
  64. defer func() {
  65. tokenTicker.Stop()
  66. close(tm.donec)
  67. }()
  68. for {
  69. select {
  70. case <-tokenTicker.C:
  71. nowtime := time.Now()
  72. tm.mu.Lock()
  73. for t, tokenendtime := range tm.tokens {
  74. if nowtime.After(tokenendtime) {
  75. tm.deleteTokenFunc(t)
  76. delete(tm.tokens, t)
  77. }
  78. }
  79. tm.mu.Unlock()
  80. case <-tm.stopc:
  81. return
  82. }
  83. }
  84. }
  85. type tokenSimple struct {
  86. lg *zap.Logger
  87. indexWaiter func(uint64) <-chan struct{}
  88. simpleTokenKeeper *simpleTokenTTLKeeper
  89. simpleTokensMu sync.Mutex
  90. simpleTokens map[string]string // token -> username
  91. }
  92. func (t *tokenSimple) genTokenPrefix() (string, error) {
  93. ret := make([]byte, defaultSimpleTokenLength)
  94. for i := 0; i < defaultSimpleTokenLength; i++ {
  95. bInt, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
  96. if err != nil {
  97. return "", err
  98. }
  99. ret[i] = letters[bInt.Int64()]
  100. }
  101. return string(ret), nil
  102. }
  103. func (t *tokenSimple) assignSimpleTokenToUser(username, token string) {
  104. t.simpleTokensMu.Lock()
  105. defer t.simpleTokensMu.Unlock()
  106. if t.simpleTokenKeeper == nil {
  107. return
  108. }
  109. _, ok := t.simpleTokens[token]
  110. if ok {
  111. if t.lg != nil {
  112. t.lg.Panic(
  113. "failed to assign already-used simple token to a user",
  114. zap.String("user-name", username),
  115. zap.String("token", token),
  116. )
  117. } else {
  118. plog.Panicf("token %s is already used", token)
  119. }
  120. }
  121. t.simpleTokens[token] = username
  122. t.simpleTokenKeeper.addSimpleToken(token)
  123. }
  124. func (t *tokenSimple) invalidateUser(username string) {
  125. if t.simpleTokenKeeper == nil {
  126. return
  127. }
  128. t.simpleTokensMu.Lock()
  129. for token, name := range t.simpleTokens {
  130. if name == username {
  131. delete(t.simpleTokens, token)
  132. t.simpleTokenKeeper.deleteSimpleToken(token)
  133. }
  134. }
  135. t.simpleTokensMu.Unlock()
  136. }
  137. func (t *tokenSimple) enable() {
  138. delf := func(tk string) {
  139. if username, ok := t.simpleTokens[tk]; ok {
  140. if t.lg != nil {
  141. t.lg.Info(
  142. "deleted a simple token",
  143. zap.String("user-name", username),
  144. zap.String("token", tk),
  145. )
  146. } else {
  147. plog.Infof("deleting token %s for user %s", tk, username)
  148. }
  149. delete(t.simpleTokens, tk)
  150. }
  151. }
  152. t.simpleTokenKeeper = &simpleTokenTTLKeeper{
  153. tokens: make(map[string]time.Time),
  154. donec: make(chan struct{}),
  155. stopc: make(chan struct{}),
  156. deleteTokenFunc: delf,
  157. mu: &t.simpleTokensMu,
  158. }
  159. go t.simpleTokenKeeper.run()
  160. }
  161. func (t *tokenSimple) disable() {
  162. t.simpleTokensMu.Lock()
  163. tk := t.simpleTokenKeeper
  164. t.simpleTokenKeeper = nil
  165. t.simpleTokens = make(map[string]string) // invalidate all tokens
  166. t.simpleTokensMu.Unlock()
  167. if tk != nil {
  168. tk.stop()
  169. }
  170. }
  171. func (t *tokenSimple) info(ctx context.Context, token string, revision uint64) (*AuthInfo, bool) {
  172. if !t.isValidSimpleToken(ctx, token) {
  173. return nil, false
  174. }
  175. t.simpleTokensMu.Lock()
  176. username, ok := t.simpleTokens[token]
  177. if ok && t.simpleTokenKeeper != nil {
  178. t.simpleTokenKeeper.resetSimpleToken(token)
  179. }
  180. t.simpleTokensMu.Unlock()
  181. return &AuthInfo{Username: username, Revision: revision}, ok
  182. }
  183. func (t *tokenSimple) assign(ctx context.Context, username string, rev uint64) (string, error) {
  184. // rev isn't used in simple token, it is only used in JWT
  185. index := ctx.Value(AuthenticateParamIndex{}).(uint64)
  186. simpleTokenPrefix := ctx.Value(AuthenticateParamSimpleTokenPrefix{}).(string)
  187. token := fmt.Sprintf("%s.%d", simpleTokenPrefix, index)
  188. t.assignSimpleTokenToUser(username, token)
  189. return token, nil
  190. }
  191. func (t *tokenSimple) isValidSimpleToken(ctx context.Context, token string) bool {
  192. splitted := strings.Split(token, ".")
  193. if len(splitted) != 2 {
  194. return false
  195. }
  196. index, err := strconv.Atoi(splitted[1])
  197. if err != nil {
  198. return false
  199. }
  200. select {
  201. case <-t.indexWaiter(uint64(index)):
  202. return true
  203. case <-ctx.Done():
  204. }
  205. return false
  206. }
  207. func newTokenProviderSimple(lg *zap.Logger, indexWaiter func(uint64) <-chan struct{}) *tokenSimple {
  208. return &tokenSimple{
  209. lg: lg,
  210. simpleTokens: make(map[string]string),
  211. indexWaiter: indexWaiter,
  212. }
  213. }