2020-08-24 15:50:57 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
2020-08-24 15:50:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type key struct {
|
|
|
|
tokenID string
|
|
|
|
ownerID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type TokenStore struct {
|
|
|
|
mtx *sync.RWMutex
|
|
|
|
|
|
|
|
tokens map[key]*PrivateToken
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrNotFound = errors.New("private token not found")
|
|
|
|
|
|
|
|
// New creates, initializes and returns a new TokenStore instance.
|
|
|
|
//
|
|
|
|
// The elements of the instance are stored in the map.
|
|
|
|
func New() *TokenStore {
|
|
|
|
return &TokenStore{
|
|
|
|
mtx: new(sync.RWMutex),
|
|
|
|
tokens: make(map[key]*PrivateToken),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns private token corresponding to the given identifiers.
|
|
|
|
//
|
|
|
|
// Returns nil is there is no element in storage.
|
2020-09-21 09:38:13 +00:00
|
|
|
func (s *TokenStore) Get(ownerID *owner.ID, tokenID []byte) *PrivateToken {
|
2020-11-16 10:26:35 +00:00
|
|
|
ownerBytes, err := ownerID.Marshal()
|
2020-08-24 15:50:57 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mtx.RLock()
|
|
|
|
t := s.tokens[key{
|
|
|
|
tokenID: base58.Encode(tokenID),
|
|
|
|
ownerID: base58.Encode(ownerBytes),
|
|
|
|
}]
|
|
|
|
s.mtx.RUnlock()
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
2022-02-03 11:43:05 +00:00
|
|
|
|
|
|
|
// RemoveOld removes all tokens expired since provided epoch.
|
|
|
|
func (s *TokenStore) RemoveOld(epoch uint64) {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
|
|
|
for k, tok := range s.tokens {
|
|
|
|
if tok.ExpiredAt() <= epoch {
|
|
|
|
delete(s.tokens, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|