From d327d836c417f7971bcb3d5f7867f73ff6e1e3cf Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 28 Apr 2020 16:58:07 +0300 Subject: [PATCH] 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()) + } +}