simple_token.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 randum number based token mechanism is only for testing purpose.
  16. // JWT based mechanism will be added in the near future.
  17. import (
  18. "crypto/rand"
  19. "fmt"
  20. "math/big"
  21. "strconv"
  22. "strings"
  23. "sync"
  24. "time"
  25. "golang.org/x/net/context"
  26. )
  27. const (
  28. letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  29. defaultSimpleTokenLength = 16
  30. simpleTokenTTL = 5 * time.Minute
  31. simpleTokenTTLResolution = 1 * time.Second
  32. )
  33. type simpleTokenTTLKeeper struct {
  34. tokens map[string]time.Time
  35. addSimpleTokenCh chan string
  36. resetSimpleTokenCh chan string
  37. deleteSimpleTokenCh chan string
  38. stopCh chan chan struct{}
  39. deleteTokenFunc func(string)
  40. }
  41. func NewSimpleTokenTTLKeeper(deletefunc func(string)) *simpleTokenTTLKeeper {
  42. stk := &simpleTokenTTLKeeper{
  43. tokens: make(map[string]time.Time),
  44. addSimpleTokenCh: make(chan string, 1),
  45. resetSimpleTokenCh: make(chan string, 1),
  46. deleteSimpleTokenCh: make(chan string, 1),
  47. stopCh: make(chan chan struct{}),
  48. deleteTokenFunc: deletefunc,
  49. }
  50. go stk.run()
  51. return stk
  52. }
  53. func (tm *simpleTokenTTLKeeper) stop() {
  54. waitCh := make(chan struct{})
  55. tm.stopCh <- waitCh
  56. <-waitCh
  57. close(tm.stopCh)
  58. }
  59. func (tm *simpleTokenTTLKeeper) addSimpleToken(token string) {
  60. tm.addSimpleTokenCh <- token
  61. }
  62. func (tm *simpleTokenTTLKeeper) resetSimpleToken(token string) {
  63. tm.resetSimpleTokenCh <- token
  64. }
  65. func (tm *simpleTokenTTLKeeper) deleteSimpleToken(token string) {
  66. tm.deleteSimpleTokenCh <- token
  67. }
  68. func (tm *simpleTokenTTLKeeper) run() {
  69. tokenTicker := time.NewTicker(simpleTokenTTLResolution)
  70. defer tokenTicker.Stop()
  71. for {
  72. select {
  73. case t := <-tm.addSimpleTokenCh:
  74. tm.tokens[t] = time.Now().Add(simpleTokenTTL)
  75. case t := <-tm.resetSimpleTokenCh:
  76. if _, ok := tm.tokens[t]; ok {
  77. tm.tokens[t] = time.Now().Add(simpleTokenTTL)
  78. }
  79. case t := <-tm.deleteSimpleTokenCh:
  80. delete(tm.tokens, t)
  81. case <-tokenTicker.C:
  82. nowtime := time.Now()
  83. for t, tokenendtime := range tm.tokens {
  84. if nowtime.After(tokenendtime) {
  85. tm.deleteTokenFunc(t)
  86. delete(tm.tokens, t)
  87. }
  88. }
  89. case waitCh := <-tm.stopCh:
  90. tm.tokens = make(map[string]time.Time)
  91. waitCh <- struct{}{}
  92. return
  93. }
  94. }
  95. }
  96. type tokenSimple struct {
  97. indexWaiter func(uint64) <-chan struct{}
  98. simpleTokenKeeper *simpleTokenTTLKeeper
  99. simpleTokensMu sync.RWMutex
  100. simpleTokens map[string]string // token -> username
  101. }
  102. func (t *tokenSimple) genTokenPrefix() (string, error) {
  103. ret := make([]byte, defaultSimpleTokenLength)
  104. for i := 0; i < defaultSimpleTokenLength; i++ {
  105. bInt, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
  106. if err != nil {
  107. return "", err
  108. }
  109. ret[i] = letters[bInt.Int64()]
  110. }
  111. return string(ret), nil
  112. }
  113. func (t *tokenSimple) assignSimpleTokenToUser(username, token string) {
  114. t.simpleTokensMu.Lock()
  115. _, ok := t.simpleTokens[token]
  116. if ok {
  117. plog.Panicf("token %s is alredy used", token)
  118. }
  119. t.simpleTokens[token] = username
  120. t.simpleTokenKeeper.addSimpleToken(token)
  121. t.simpleTokensMu.Unlock()
  122. }
  123. func (t *tokenSimple) invalidateUser(username string) {
  124. t.simpleTokensMu.Lock()
  125. defer t.simpleTokensMu.Unlock()
  126. for token, name := range t.simpleTokens {
  127. if strings.Compare(name, username) == 0 {
  128. delete(t.simpleTokens, token)
  129. t.simpleTokenKeeper.deleteSimpleToken(token)
  130. }
  131. }
  132. }
  133. func newDeleterFunc(t *tokenSimple) func(string) {
  134. return func(tk string) {
  135. t.simpleTokensMu.Lock()
  136. defer t.simpleTokensMu.Unlock()
  137. if username, ok := t.simpleTokens[tk]; ok {
  138. plog.Infof("deleting token %s for user %s", tk, username)
  139. delete(t.simpleTokens, tk)
  140. }
  141. }
  142. }
  143. func (t *tokenSimple) enable() {
  144. t.simpleTokenKeeper = NewSimpleTokenTTLKeeper(newDeleterFunc(t))
  145. }
  146. func (t *tokenSimple) disable() {
  147. if t.simpleTokenKeeper != nil {
  148. t.simpleTokenKeeper.stop()
  149. t.simpleTokenKeeper = nil
  150. }
  151. t.simpleTokensMu.Lock()
  152. t.simpleTokens = make(map[string]string) // invalidate all tokens
  153. t.simpleTokensMu.Unlock()
  154. }
  155. func (t *tokenSimple) info(ctx context.Context, token string, revision uint64) (*AuthInfo, bool) {
  156. if !t.isValidSimpleToken(ctx, token) {
  157. return nil, false
  158. }
  159. t.simpleTokensMu.RLock()
  160. defer t.simpleTokensMu.RUnlock()
  161. username, ok := t.simpleTokens[token]
  162. if ok {
  163. t.simpleTokenKeeper.resetSimpleToken(token)
  164. }
  165. return &AuthInfo{Username: username, Revision: revision}, ok
  166. }
  167. func (t *tokenSimple) assign(ctx context.Context, username string, rev uint64) (string, error) {
  168. // rev isn't used in simple token, it is only used in JWT
  169. index := ctx.Value("index").(uint64)
  170. simpleToken := ctx.Value("simpleToken").(string)
  171. token := fmt.Sprintf("%s.%d", simpleToken, index)
  172. t.assignSimpleTokenToUser(username, token)
  173. return token, nil
  174. }
  175. func (t *tokenSimple) isValidSimpleToken(ctx context.Context, token string) bool {
  176. splitted := strings.Split(token, ".")
  177. if len(splitted) != 2 {
  178. return false
  179. }
  180. index, err := strconv.Atoi(splitted[1])
  181. if err != nil {
  182. return false
  183. }
  184. select {
  185. case <-t.indexWaiter(uint64(index)):
  186. return true
  187. case <-ctx.Done():
  188. }
  189. return false
  190. }
  191. func newTokenProviderSimple(indexWaiter func(uint64) <-chan struct{}) *tokenSimple {
  192. return &tokenSimple{
  193. simpleTokens: make(map[string]string),
  194. indexWaiter: indexWaiter,
  195. }
  196. }