diff --git a/session/private.go b/session/private.go index 8ebce81b..42bb2058 100644 --- a/session/private.go +++ b/session/private.go @@ -43,3 +43,13 @@ func (t *pToken) PublicKey() []byte { func (t *pToken) Expired(epoch uint64) bool { return t.validUntil < epoch } + +// SetOwnerID is an owner ID field setter. +func (s *PrivateTokenKey) SetOwnerID(id OwnerID) { + s.owner = id +} + +// SetTokenID is a token ID field setter. +func (s *PrivateTokenKey) SetTokenID(id TokenID) { + s.token = id +} diff --git a/session/private_test.go b/session/private_test.go index 7963afb6..8097b97a 100644 --- a/session/private_test.go +++ b/session/private_test.go @@ -48,3 +48,23 @@ func TestPToken_Expired(t *testing.T) { // must be expired in the epoch after last require.True(t, token.Expired(e+1)) } + +func TestPrivateTokenKey_SetOwnerID(t *testing.T) { + ownerID := OwnerID{1, 2, 3} + + s := new(PrivateTokenKey) + + s.SetOwnerID(ownerID) + + require.Equal(t, ownerID, s.owner) +} + +func TestPrivateTokenKey_SetTokenID(t *testing.T) { + tokenID := TokenID{1, 2, 3} + + s := new(PrivateTokenKey) + + s.SetTokenID(tokenID) + + require.Equal(t, tokenID, s.token) +} diff --git a/session/store.go b/session/store.go index 79998c7d..fa33b7e1 100644 --- a/session/store.go +++ b/session/store.go @@ -7,7 +7,7 @@ import ( type mapTokenStore struct { *sync.RWMutex - tokens map[TokenID]PrivateToken + tokens map[PrivateTokenKey]PrivateToken } // NewMapTokenStore creates new PrivateTokenStore instance. @@ -16,16 +16,16 @@ type mapTokenStore struct { func NewMapTokenStore() PrivateTokenStore { return &mapTokenStore{ RWMutex: new(sync.RWMutex), - tokens: make(map[TokenID]PrivateToken), + tokens: make(map[PrivateTokenKey]PrivateToken), } } // Store adds passed token to the map. // // Resulting error is always nil. -func (s *mapTokenStore) Store(id TokenID, token PrivateToken) error { +func (s *mapTokenStore) Store(key PrivateTokenKey, token PrivateToken) error { s.Lock() - s.tokens[id] = token + s.tokens[key] = token s.Unlock() return nil @@ -34,11 +34,11 @@ func (s *mapTokenStore) Store(id TokenID, token PrivateToken) error { // Fetch returns the map element corresponding to the given key. // // Returns ErrPrivateTokenNotFound is there is no element in map. -func (s *mapTokenStore) Fetch(id TokenID) (PrivateToken, error) { +func (s *mapTokenStore) Fetch(key PrivateTokenKey) (PrivateToken, error) { s.RLock() defer s.RUnlock() - t, ok := s.tokens[id] + t, ok := s.tokens[key] if !ok { return nil, ErrPrivateTokenNotFound } diff --git a/session/store_test.go b/session/store_test.go index 123b1036..74e00233 100644 --- a/session/store_test.go +++ b/session/store_test.go @@ -15,19 +15,26 @@ func TestMapTokenStore(t *testing.T) { // create map token store s := NewMapTokenStore() - // create new storage key - id, err := refs.NewUUID() + // create test TokenID + tid, err := refs.NewUUID() require.NoError(t, err) + // create test OwnerID + ownerID := OwnerID{1, 2, 3} + + key := PrivateTokenKey{} + key.SetOwnerID(ownerID) + key.SetTokenID(tid) + // ascertain that there is no record for the key - _, err = s.Fetch(id) + _, err = s.Fetch(key) require.EqualError(t, err, ErrPrivateTokenNotFound.Error()) // save private token record - require.NoError(t, s.Store(id, pToken)) + require.NoError(t, s.Store(key, pToken)) // fetch private token by the key - res, err := s.Fetch(id) + res, err := s.Fetch(key) require.NoError(t, err) // ascertain that returned token equals to initial @@ -52,7 +59,11 @@ func TestMapTokenStore_RemoveExpired(t *testing.T) { // create token store instance s := NewMapTokenStore() - // create storage keys for tokens + // create test PrivateTokenKey + key := PrivateTokenKey{} + key.SetOwnerID(OwnerID{1, 2, 3}) + + // create IDs for tokens id1, err := refs.NewUUID() require.NoError(t, err) id2, err := refs.NewUUID() @@ -60,21 +71,25 @@ func TestMapTokenStore_RemoveExpired(t *testing.T) { assertPresence := func(ids ...TokenID) { for i := range ids { - _, err = s.Fetch(ids[i]) + key.SetTokenID(ids[i]) + _, err = s.Fetch(key) require.NoError(t, err) } } assertAbsence := func(ids ...TokenID) { for i := range ids { - _, err = s.Fetch(ids[i]) + key.SetTokenID(ids[i]) + _, err = s.Fetch(key) require.EqualError(t, err, ErrPrivateTokenNotFound.Error()) } } // store both tokens - require.NoError(t, s.Store(id1, tok1)) - require.NoError(t, s.Store(id2, tok2)) + key.SetTokenID(id1) + require.NoError(t, s.Store(key, tok1)) + key.SetTokenID(id2) + require.NoError(t, s.Store(key, tok2)) // ascertain that both tokens are available assertPresence(id1, id2) diff --git a/session/types.go b/session/types.go index 932fe38b..ee13b927 100644 --- a/session/types.go +++ b/session/types.go @@ -23,12 +23,18 @@ type PrivateToken interface { Expired(uint64) bool } +// PrivateTokenKey is a structure of private token storage key. +type PrivateTokenKey struct { + owner OwnerID + token TokenID +} + // PrivateTokenSource is an interface of private token storage with read access. type PrivateTokenSource interface { // Fetch must return the storage record corresponding to the passed key. // // Resulting error must be ErrPrivateTokenNotFound if there is no corresponding record. - Fetch(TokenID) (PrivateToken, error) + Fetch(PrivateTokenKey) (PrivateToken, error) } // EpochLifetimeStore is an interface of the storage of elements that lifetime is limited by NeoFS epoch. @@ -45,7 +51,7 @@ type PrivateTokenStore interface { // Store must save passed private token in the storage under the given key. // // Resulting error must be nil if private token was stored successfully. - Store(TokenID, PrivateToken) error + Store(PrivateTokenKey, PrivateToken) error } // KeyStore is an interface of the storage of public keys addressable by OwnerID,