simple_token.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "sync"
  22. "time"
  23. )
  24. const (
  25. letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  26. defaultSimpleTokenLength = 16
  27. )
  28. // var for testing purposes
  29. var (
  30. simpleTokenTTL = 5 * time.Minute
  31. simpleTokenTTLResolution = 1 * time.Second
  32. )
  33. type simpleTokenTTLKeeper struct {
  34. tokens map[string]time.Time
  35. donec chan struct{}
  36. stopc chan struct{}
  37. deleteTokenFunc func(string)
  38. mu *sync.Mutex
  39. }
  40. func (tm *simpleTokenTTLKeeper) stop() {
  41. select {
  42. case tm.stopc <- struct{}{}:
  43. case <-tm.donec:
  44. }
  45. <-tm.donec
  46. }
  47. func (tm *simpleTokenTTLKeeper) addSimpleToken(token string) {
  48. tm.tokens[token] = time.Now().Add(simpleTokenTTL)
  49. }
  50. func (tm *simpleTokenTTLKeeper) resetSimpleToken(token string) {
  51. if _, ok := tm.tokens[token]; ok {
  52. tm.tokens[token] = time.Now().Add(simpleTokenTTL)
  53. }
  54. }
  55. func (tm *simpleTokenTTLKeeper) deleteSimpleToken(token string) {
  56. delete(tm.tokens, token)
  57. }
  58. func (tm *simpleTokenTTLKeeper) run() {
  59. tokenTicker := time.NewTicker(simpleTokenTTLResolution)
  60. defer func() {
  61. tokenTicker.Stop()
  62. close(tm.donec)
  63. }()
  64. for {
  65. select {
  66. case <-tokenTicker.C:
  67. nowtime := time.Now()
  68. tm.mu.Lock()
  69. for t, tokenendtime := range tm.tokens {
  70. if nowtime.After(tokenendtime) {
  71. tm.deleteTokenFunc(t)
  72. delete(tm.tokens, t)
  73. }
  74. }
  75. tm.mu.Unlock()
  76. case <-tm.stopc:
  77. return
  78. }
  79. }
  80. }
  81. func (as *authStore) enable() {
  82. delf := func(tk string) {
  83. if username, ok := as.simpleTokens[tk]; ok {
  84. plog.Infof("deleting token %s for user %s", tk, username)
  85. delete(as.simpleTokens, tk)
  86. }
  87. }
  88. as.simpleTokenKeeper = &simpleTokenTTLKeeper{
  89. tokens: make(map[string]time.Time),
  90. donec: make(chan struct{}),
  91. stopc: make(chan struct{}),
  92. deleteTokenFunc: delf,
  93. mu: &as.simpleTokensMu,
  94. }
  95. go as.simpleTokenKeeper.run()
  96. }
  97. func (as *authStore) GenSimpleToken() (string, error) {
  98. ret := make([]byte, defaultSimpleTokenLength)
  99. for i := 0; i < defaultSimpleTokenLength; i++ {
  100. bInt, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
  101. if err != nil {
  102. return "", err
  103. }
  104. ret[i] = letters[bInt.Int64()]
  105. }
  106. return string(ret), nil
  107. }
  108. func (as *authStore) assignSimpleTokenToUser(username, token string) {
  109. as.simpleTokensMu.Lock()
  110. _, ok := as.simpleTokens[token]
  111. if ok {
  112. plog.Panicf("token %s is alredy used", token)
  113. }
  114. as.simpleTokens[token] = username
  115. as.simpleTokenKeeper.addSimpleToken(token)
  116. as.simpleTokensMu.Unlock()
  117. }
  118. func (as *authStore) invalidateUser(username string) {
  119. if as.simpleTokenKeeper == nil {
  120. return
  121. }
  122. as.simpleTokensMu.Lock()
  123. for token, name := range as.simpleTokens {
  124. if strings.Compare(name, username) == 0 {
  125. delete(as.simpleTokens, token)
  126. as.simpleTokenKeeper.deleteSimpleToken(token)
  127. }
  128. }
  129. as.simpleTokensMu.Unlock()
  130. }