wings/router/tokens/token_store.go

43 lines
743 B
Go
Raw Permalink Normal View History

2020-04-06 01:56:54 +00:00
package tokens
import (
"sync"
"time"
"github.com/patrickmn/go-cache"
2020-04-06 01:56:54 +00:00
)
type TokenStore struct {
sync.Mutex
cache *cache.Cache
2020-04-06 01:56:54 +00:00
}
var _tokens *TokenStore
// Returns the global unique token store cache. This is used to validate
2020-04-06 01:56:54 +00:00
// one time token usage by storing any received tokens in a local memory
// cache until they are ready to expire.
func getTokenStore() *TokenStore {
if _tokens == nil {
_tokens = &TokenStore{
cache: cache.New(time.Minute*60, time.Minute*5),
2020-04-06 01:56:54 +00:00
}
}
return _tokens
}
// Checks if a token is valid or not.
2020-04-06 01:56:54 +00:00
func (t *TokenStore) IsValidToken(token string) bool {
t.Lock()
defer t.Unlock()
2020-04-06 01:56:54 +00:00
_, exists := t.cache.Get(token)
if !exists {
t.cache.Add(token, "", time.Minute*60)
}
return !exists
}