From c6971d20042aef04f302cb14befe60e14a8eeb31 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 28 Apr 2020 18:40:21 +0300 Subject: [PATCH] service: access Token message fields through getters and setters --- object/types.go | 16 ++++++++-------- object/types_test.go | 23 +++++++++++------------ object/verification.go | 2 +- object/verification_test.go | 10 +++------- service/verify_test.go | 2 +- session/store.go | 26 ++++++++++++-------------- 6 files changed, 36 insertions(+), 43 deletions(-) diff --git a/object/types.go b/object/types.go index 4acffbe..cbc50f5 100644 --- a/object/types.go +++ b/object/types.go @@ -334,14 +334,14 @@ func Stringify(dst io.Writer, obj *Object) error { val = fmt.Sprintf("{"+ "ID=%s OwnerID=%s Verb=%s Address=%s Created=%d ValidUntil=%d SessionKey=%02x Signature=%02x"+ "}", - t.Token.Token_Info.ID, - t.Token.Token_Info.OwnerID, - t.Token.Token_Info.Verb, - t.Token.Token_Info.Address, - t.Token.Token_Info.Created, - t.Token.Token_Info.ValidUntil, - t.Token.Token_Info.SessionKey, - t.Token.Signature) + t.Token.GetID(), + t.Token.GetOwnerID(), + t.Token.GetVerb(), + t.Token.GetAddress(), + t.Token.CreationEpoch(), + t.Token.ExpirationEpoch(), + t.Token.GetSessionKey(), + t.Token.GetSignature()) case *Header_HomoHash: key = "HomoHash" val = t.HomoHash diff --git a/object/types_test.go b/object/types_test.go index 50e4e0b..95f328b 100644 --- a/object/types_test.go +++ b/object/types_test.go @@ -111,21 +111,20 @@ Object: }, }) + token := new(Token) + token.SetID(oid) + token.SetOwnerID(uid) + token.SetVerb(service.Token_Info_Search) + token.SetAddress(Address{ObjectID: oid, CID: refs.CID{}}) + token.SetCreationEpoch(1) + token.SetExpirationEpoch(2) + token.SetSessionKey([]byte{1, 2, 3, 4, 5, 6}) + token.SetSignature([]byte{1, 2, 3, 4, 5, 6}) + // *Header_Token obj.Headers = append(obj.Headers, Header{ Value: &Header_Token{ - Token: &Token{ - Signature: []byte{1, 2, 3, 4, 5, 6}, - Token_Info: service.Token_Info{ - ID: oid, - OwnerID: uid, - Verb: service.Token_Info_Search, - Address: service.Address{ObjectID: oid, CID: refs.CID{}}, - Created: 1, - ValidUntil: 2, - SessionKey: []byte{1, 2, 3, 4, 5, 6}, - }, - }, + Token: token, }, }) diff --git a/object/verification.go b/object/verification.go index 5694316..0bcbc7c 100644 --- a/object/verification.go +++ b/object/verification.go @@ -85,7 +85,7 @@ func (m Object) Verify() error { } pubkey = pkh.Value.(*Header_PublicKey).PublicKey.Value } else { - pubkey = vh.Value.(*Header_Token).Token.SessionKey + pubkey = vh.Value.(*Header_Token).Token.GetSessionKey() } // Verify signature diff --git a/object/verification_test.go b/object/verification_test.go index 004f969..95a6d32 100644 --- a/object/verification_test.go +++ b/object/verification_test.go @@ -6,7 +6,6 @@ import ( "github.com/google/uuid" "github.com/nspcc-dev/neofs-api-go/container" "github.com/nspcc-dev/neofs-api-go/refs" - "github.com/nspcc-dev/neofs-api-go/service" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-crypto/test" "github.com/stretchr/testify/require" @@ -77,12 +76,9 @@ func TestObject_Verify(t *testing.T) { dataPK := crypto.MarshalPublicKey(&sessionkey.PublicKey) signature, err = crypto.Sign(key, dataPK) - tok := &service.Token{ - Token_Info: service.Token_Info{ - SessionKey: dataPK, - }, - Signature: signature, - } + tok := new(Token) + tok.SetSignature(signature) + tok.SetSessionKey(dataPK) obj.AddHeader(&Header{Value: &Header_Token{Token: tok}}) // validation header is not last diff --git a/service/verify_test.go b/service/verify_test.go index c192f0f..107416b 100644 --- a/service/verify_test.go +++ b/service/verify_test.go @@ -194,7 +194,7 @@ func TestRequestVerificationHeader_SetToken(t *testing.T) { require.NoError(t, err) token := new(Token) - token.ID = id + token.SetID(id) h := new(RequestVerificationHeader) diff --git a/session/store.go b/session/store.go index e46afde..7c56c39 100644 --- a/session/store.go +++ b/session/store.go @@ -7,7 +7,6 @@ import ( "sync" "github.com/nspcc-dev/neofs-api-go/refs" - "github.com/nspcc-dev/neofs-api-go/service" crypto "github.com/nspcc-dev/neofs-crypto" ) @@ -46,24 +45,23 @@ func (s *simpleStore) New(p TokenParams) *PToken { return nil } + token := new(Token) + token.SetID(tid) + token.SetOwnerID(p.OwnerID) + token.SetVerb(p.Verb) + token.SetAddress(p.Address) + token.SetCreationEpoch(p.FirstEpoch) + token.SetExpirationEpoch(p.LastEpoch) + token.SetSessionKey(crypto.MarshalPublicKey(&key.PublicKey)) + t := &PToken{ - mtx: new(sync.Mutex), - Token: Token{ - Token_Info: service.Token_Info{ - ID: tid, - OwnerID: p.OwnerID, - Verb: p.Verb, - Address: p.Address, - Created: p.FirstEpoch, - ValidUntil: p.LastEpoch, - SessionKey: crypto.MarshalPublicKey(&key.PublicKey), - }, - }, + mtx: new(sync.Mutex), + Token: *token, PrivateKey: key, } s.Lock() - s.tokens[t.ID] = t + s.tokens[tid] = t s.Unlock() return t