2022-03-18 08:46:07 +00:00
|
|
|
package temporary
|
2020-08-24 15:50:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/session/storage"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2020-08-24 15:50:57 +00:00
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
)
|
|
|
|
|
|
|
|
type key struct {
|
|
|
|
tokenID string
|
|
|
|
ownerID string
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:46:07 +00:00
|
|
|
// TokenStore is an in-memory session token store.
|
|
|
|
// It allows creating (storing), retrieving and
|
|
|
|
// expiring (removing) session tokens.
|
|
|
|
// Must be created only via calling NewTokenStore.
|
2020-08-24 15:50:57 +00:00
|
|
|
type TokenStore struct {
|
2023-05-30 07:14:37 +00:00
|
|
|
mtx sync.RWMutex
|
2020-08-24 15:50:57 +00:00
|
|
|
|
2022-03-18 08:46:07 +00:00
|
|
|
tokens map[key]*storage.PrivateToken
|
2020-08-24 15:50:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:46:07 +00:00
|
|
|
// NewTokenStore creates, initializes and returns a new TokenStore instance.
|
2020-08-24 15:50:57 +00:00
|
|
|
//
|
|
|
|
// The elements of the instance are stored in the map.
|
2022-03-18 08:46:07 +00:00
|
|
|
func NewTokenStore() *TokenStore {
|
2020-08-24 15:50:57 +00:00
|
|
|
return &TokenStore{
|
2022-03-18 08:46:07 +00:00
|
|
|
tokens: make(map[key]*storage.PrivateToken),
|
2020-08-24 15:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns private token corresponding to the given identifiers.
|
|
|
|
//
|
|
|
|
// Returns nil is there is no element in storage.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (s *TokenStore) Get(ownerID user.ID, tokenID []byte) *storage.PrivateToken {
|
2020-08-24 15:50:57 +00:00
|
|
|
s.mtx.RLock()
|
|
|
|
t := s.tokens[key{
|
|
|
|
tokenID: base58.Encode(tokenID),
|
2022-05-17 13:59:46 +00:00
|
|
|
ownerID: base58.Encode(ownerID.WalletBytes()),
|
2020-08-24 15:50:57 +00:00
|
|
|
}]
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|