From 09f8ee52d0b15e808126227d349947b4d7a9687f Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 28 Apr 2020 15:39:09 +0300 Subject: [PATCH 1/3] service: implement Raw field setter on RequestMetaHeader struct After recent changes Raw field is presented in RequestMetaHeader. There is a need to provide an interface of field getter/setter. This commit: * defines RawHeader interface of raw value container; * embeds RawHeader into MetaHeader interface; * implements Raw field setter on RequestMetaHeader. --- service/meta.go | 14 ++++++++++++++ service/meta_test.go | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/service/meta.go b/service/meta.go index 5e9886d..8602dca 100644 --- a/service/meta.go +++ b/service/meta.go @@ -25,6 +25,9 @@ type ( // VersionHeader allows get or set version of protocol request VersionHeader + + // RawHeader allows to get and set raw option of request + RawHeader } // EpochHeader interface gives possibility to get or set epoch in RPC Requests. @@ -39,6 +42,12 @@ type ( SetVersion(uint32) } + // RawHeader is an interface of the container of a boolean Raw value + RawHeader interface { + GetRaw() bool + SetRaw(bool) + } + // TTLCondition is closure, that allows to validate request with ttl. TTLCondition func(ttl uint32) error ) @@ -77,6 +86,11 @@ func (m *RequestMetaHeader) SetTTL(v uint32) { m.TTL = v } // SetEpoch sets Epoch to RequestMetaHeader. func (m *RequestMetaHeader) SetEpoch(v uint64) { m.Epoch = v } +// SetRaw is a Raw field setter. +func (m *RequestMetaHeader) SetRaw(raw bool) { + m.Raw = raw +} + // ResetMeta returns current value and sets RequestMetaHeader to empty value. func (m *RequestMetaHeader) ResetMeta() RequestMetaHeader { cp := *m diff --git a/service/meta_test.go b/service/meta_test.go index 083ccd6..388b6ce 100644 --- a/service/meta_test.go +++ b/service/meta_test.go @@ -102,3 +102,13 @@ func TestRequestMetaHeader_SetVersion(t *testing.T) { m.SetVersion(version) require.Equal(t, version, m.GetVersion()) } + +func TestRequestMetaHeader_SetRaw(t *testing.T) { + m := new(RequestMetaHeader) + + m.SetRaw(true) + require.True(t, m.GetRaw()) + + m.SetRaw(false) + require.False(t, m.GetRaw()) +} From 942bedb8ed92fbb6587a19cbcbc1f79377260080 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 28 Apr 2020 16:02:40 +0300 Subject: [PATCH 2/3] service: implement Token field setter on RequestVerificationHeader After recent changes Token field is presented in RequestVerificationHeader. There is a need to provide an interface of field getter/setter. This commit: * defines TokenHeader interface of token value container; * implements Token field setter on RequestVerificationHeader. --- service/verify.go | 11 +++++++++++ service/verify_test.go | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/service/verify.go b/service/verify.go index ade13ef..182685d 100644 --- a/service/verify.go +++ b/service/verify.go @@ -27,6 +27,12 @@ type ( SetOwner(*ecdsa.PublicKey, []byte) GetLastPeer() (*ecdsa.PublicKey, error) } + + // TokenHeader is an interface of the container of a Token pointer value + TokenHeader interface { + GetToken() *Token + SetToken(*Token) + } ) const ( @@ -97,6 +103,11 @@ func (m *RequestVerificationHeader) GetLastPeer() (*ecdsa.PublicKey, error) { } } +// SetToken is a Token field setter. +func (m *RequestVerificationHeader) SetToken(token *Token) { + m.Token = token +} + func newSignature(key *ecdsa.PrivateKey, data []byte) (*RequestVerificationHeader_Signature, error) { sign, err := crypto.Sign(key, data) if err != nil { diff --git a/service/verify_test.go b/service/verify_test.go index ce333aa..c192f0f 100644 --- a/service/verify_test.go +++ b/service/verify_test.go @@ -188,3 +188,17 @@ func TestVerifyAndSignRequestHeaderWithoutCloning(t *testing.T) { require.Contains(t, buf.String(), "proto: don't know how to copy") } + +func TestRequestVerificationHeader_SetToken(t *testing.T) { + id, err := refs.NewUUID() + require.NoError(t, err) + + token := new(Token) + token.ID = id + + h := new(RequestVerificationHeader) + + h.SetToken(token) + + require.Equal(t, token, h.GetToken()) +} From d327d836c417f7971bcb3d5f7867f73ff6e1e3cf Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 28 Apr 2020 16:58:07 +0300 Subject: [PATCH 3/3] service: define and implement Token field composing interface This commit: * defines SessionToken interface of Token field getters/setters group; * implements SessionToken on Token message. --- refs/types.go | 12 ++++ service/token.go | 125 ++++++++++++++++++++++++++++++++++++++++++ service/token_test.go | 88 +++++++++++++++++++++++++++++ 3 files changed, 225 insertions(+) create mode 100644 service/token.go create mode 100644 service/token_test.go diff --git a/refs/types.go b/refs/types.go index 117aa03..a29424e 100644 --- a/refs/types.go +++ b/refs/types.go @@ -37,6 +37,18 @@ type ( OwnerID chain.WalletAddress ) +// OwnerIDContainer is an interface of the container of an OwnerID value. +type OwnerIDContainer interface { + GetOwnerID() OwnerID + SetOwnerID(OwnerID) +} + +// AddressContainer is an interface of the container of object address value. +type AddressContainer interface { + GetAddress() Address + SetAddress(Address) +} + const ( // UUIDSize contains size of UUID. UUIDSize = 16 diff --git a/service/token.go b/service/token.go new file mode 100644 index 0000000..71ea655 --- /dev/null +++ b/service/token.go @@ -0,0 +1,125 @@ +package service + +import ( + "github.com/nspcc-dev/neofs-api-go/refs" +) + +// VerbContainer is an interface of the container of a token verb value. +type VerbContainer interface { + GetVerb() Token_Info_Verb + SetVerb(Token_Info_Verb) +} + +// TokenIDContainer is an interface of the container of a token ID value. +type TokenIDContainer interface { + GetID() TokenID + SetID(TokenID) +} + +// CreationEpochContainer is an interface of the container of a creation epoch number. +type CreationEpochContainer interface { + CreationEpoch() uint64 + SetCreationEpoch(uint64) +} + +// ExpirationEpochContainer is an interface of the container of an expiration epoch number. +type ExpirationEpochContainer interface { + ExpirationEpoch() uint64 + SetExpirationEpoch(uint64) +} + +// SessionKeyContainer is an interface of the container of session key bytes. +type SessionKeyContainer interface { + GetSessionKey() []byte + SetSessionKey([]byte) +} + +// SignatureContainer is an interface of the container of signature bytes. +type SignatureContainer interface { + GetSignature() []byte + SetSignature([]byte) +} + +// SessionTokenInfo is an interface that determines the information scope of session token. +type SessionTokenInfo interface { + TokenIDContainer + refs.OwnerIDContainer + VerbContainer + refs.AddressContainer + CreationEpochContainer + ExpirationEpochContainer + SessionKeyContainer +} + +// SessionToken is an interface of token information and signature pair. +type SessionToken interface { + SessionTokenInfo + SignatureContainer +} + +var _ SessionToken = (*Token)(nil) + +// GetID is an ID field getter. +func (m Token_Info) GetID() TokenID { + return m.ID +} + +// SetID is an ID field setter. +func (m *Token_Info) SetID(id TokenID) { + m.ID = id +} + +// GetOwnerID is an OwnerID field getter. +func (m Token_Info) GetOwnerID() OwnerID { + return m.OwnerID +} + +// SetOwnerID is an OwnerID field setter. +func (m *Token_Info) SetOwnerID(id OwnerID) { + m.OwnerID = id +} + +// SetVerb is a Verb field setter. +func (m *Token_Info) SetVerb(verb Token_Info_Verb) { + m.Verb = verb +} + +// GetAddress is an Address field getter. +func (m Token_Info) GetAddress() Address { + return m.Address +} + +// SetAddress is an Address field setter. +func (m *Token_Info) SetAddress(addr Address) { + m.Address = addr +} + +// CreationEpoch is a Created field getter. +func (m Token_Info) CreationEpoch() uint64 { + return m.Created +} + +// SetCreationEpoch is a Created field setter. +func (m *Token_Info) SetCreationEpoch(e uint64) { + m.Created = e +} + +// ExpirationEpoch is a ValidUntil field getter. +func (m Token_Info) ExpirationEpoch() uint64 { + return m.ValidUntil +} + +// SetExpirationEpoch is a ValidUntil field setter. +func (m *Token_Info) SetExpirationEpoch(e uint64) { + m.ValidUntil = e +} + +// SetSessionKey is a SessionKey field setter. +func (m *Token_Info) SetSessionKey(key []byte) { + m.SessionKey = key +} + +// SetSignature is a Signature field setter. +func (m *Token) SetSignature(sig []byte) { + m.Signature = sig +} diff --git a/service/token_test.go b/service/token_test.go new file mode 100644 index 0000000..1a55406 --- /dev/null +++ b/service/token_test.go @@ -0,0 +1,88 @@ +package service + +import ( + "crypto/rand" + "testing" + + "github.com/nspcc-dev/neofs-api-go/refs" + "github.com/stretchr/testify/require" +) + +func TestTokenGettersSetters(t *testing.T) { + var tok SessionToken = new(Token) + + { // ID + id, err := refs.NewUUID() + require.NoError(t, err) + + tok.SetID(id) + + require.Equal(t, id, tok.GetID()) + } + + { // OwnerID + ownerID := OwnerID{} + _, err := rand.Read(ownerID[:]) + require.NoError(t, err) + + tok.SetOwnerID(ownerID) + + require.Equal(t, ownerID, tok.GetOwnerID()) + } + + { // Verb + verb := Token_Info_Verb(3) + + tok.SetVerb(verb) + + require.Equal(t, verb, tok.GetVerb()) + } + + { // Address + addr := Address{} + _, err := rand.Read(addr.CID[:]) + require.NoError(t, err) + _, err = rand.Read(addr.ObjectID[:]) + require.NoError(t, err) + + tok.SetAddress(addr) + + require.Equal(t, addr, tok.GetAddress()) + } + + { // Created + e := uint64(5) + + tok.SetCreationEpoch(e) + + require.Equal(t, e, tok.CreationEpoch()) + } + + { // ValidUntil + e := uint64(5) + + tok.SetExpirationEpoch(e) + + require.Equal(t, e, tok.ExpirationEpoch()) + } + + { // SessionKey + key := make([]byte, 10) + _, err := rand.Read(key) + require.NoError(t, err) + + tok.SetSessionKey(key) + + require.Equal(t, key, tok.GetSessionKey()) + } + + { // Signature + sig := make([]byte, 10) + _, err := rand.Read(sig) + require.NoError(t, err) + + tok.SetSignature(sig) + + require.Equal(t, sig, tok.GetSignature()) + } +}