wings/router/tokens/token_store.go
2020-04-05 18:56:54 -07:00

42 lines
757 B
Go

package tokens
import (
cache2 "github.com/patrickmn/go-cache"
"sync"
"time"
)
type TokenStore struct {
cache *cache2.Cache
mutex *sync.Mutex
}
var _tokens *TokenStore
// Returns the global unqiue token store cache. This is used to validate
// 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: cache2.New(time.Minute*60, time.Minute*5),
mutex: &sync.Mutex{},
}
}
return _tokens
}
func (t *TokenStore) IsValidToken(token string) bool {
t.mutex.Lock()
defer t.mutex.Unlock()
_, exists := t.cache.Get(token)
if !exists {
t.cache.Add(token, "", time.Minute*60)
}
return !exists
}