simple_token.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "math/big"
  20. "strings"
  21. "time"
  22. )
  23. const (
  24. letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  25. defaultSimpleTokenLength = 16
  26. simpleTokenTTL = 5 * time.Minute
  27. simpleTokenTTLResolution = 1 * time.Second
  28. )
  29. type simpleTokenTTLKeeper struct {
  30. tokens map[string]time.Time
  31. addSimpleTokenCh chan string
  32. resetSimpleTokenCh chan string
  33. deleteSimpleTokenCh chan string
  34. stopCh chan chan struct{}
  35. deleteTokenFunc func(string)
  36. }
  37. func NewSimpleTokenTTLKeeper(deletefunc func(string)) *simpleTokenTTLKeeper {
  38. stk := &simpleTokenTTLKeeper{
  39. tokens: make(map[string]time.Time),
  40. addSimpleTokenCh: make(chan string, 1),
  41. resetSimpleTokenCh: make(chan string, 1),
  42. deleteSimpleTokenCh: make(chan string, 1),
  43. stopCh: make(chan chan struct{}),
  44. deleteTokenFunc: deletefunc,
  45. }
  46. go stk.run()
  47. return stk
  48. }
  49. func (tm *simpleTokenTTLKeeper) stop() {
  50. waitCh := make(chan struct{})
  51. tm.stopCh <- waitCh
  52. <-waitCh
  53. close(tm.stopCh)
  54. }
  55. func (tm *simpleTokenTTLKeeper) addSimpleToken(token string) {
  56. tm.addSimpleTokenCh <- token
  57. }
  58. func (tm *simpleTokenTTLKeeper) resetSimpleToken(token string) {
  59. tm.resetSimpleTokenCh <- token
  60. }
  61. func (tm *simpleTokenTTLKeeper) deleteSimpleToken(token string) {
  62. tm.deleteSimpleTokenCh <- token
  63. }
  64. func (tm *simpleTokenTTLKeeper) run() {
  65. tokenTicker := time.NewTicker(simpleTokenTTLResolution)
  66. defer tokenTicker.Stop()
  67. for {
  68. select {
  69. case t := <-tm.addSimpleTokenCh:
  70. tm.tokens[t] = time.Now().Add(simpleTokenTTL)
  71. case t := <-tm.resetSimpleTokenCh:
  72. if _, ok := tm.tokens[t]; ok {
  73. tm.tokens[t] = time.Now().Add(simpleTokenTTL)
  74. }
  75. case t := <-tm.deleteSimpleTokenCh:
  76. delete(tm.tokens, t)
  77. case <-tokenTicker.C:
  78. nowtime := time.Now()
  79. for t, tokenendtime := range tm.tokens {
  80. if nowtime.After(tokenendtime) {
  81. tm.deleteTokenFunc(t)
  82. delete(tm.tokens, t)
  83. }
  84. }
  85. case waitCh := <-tm.stopCh:
  86. tm.tokens = make(map[string]time.Time)
  87. waitCh <- struct{}{}
  88. return
  89. }
  90. }
  91. }
  92. func (as *authStore) GenSimpleToken() (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 (as *authStore) assignSimpleTokenToUser(username, token string) {
  104. as.simpleTokensMu.Lock()
  105. _, ok := as.simpleTokens[token]
  106. if ok {
  107. plog.Panicf("token %s is alredy used", token)
  108. }
  109. as.simpleTokens[token] = username
  110. as.simpleTokenKeeper.addSimpleToken(token)
  111. as.simpleTokensMu.Unlock()
  112. }
  113. func (as *authStore) invalidateUser(username string) {
  114. as.simpleTokensMu.Lock()
  115. defer as.simpleTokensMu.Unlock()
  116. for token, name := range as.simpleTokens {
  117. if strings.Compare(name, username) == 0 {
  118. delete(as.simpleTokens, token)
  119. as.simpleTokenKeeper.deleteSimpleToken(token)
  120. }
  121. }
  122. }