From fcbe0bbd2e31039e34be3d7b385da71f4abc20a1 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 24 May 2021 19:48:41 +0300 Subject: [PATCH] [#283] pkg/container: Add session token and signature to Container Container can be created within a session, and should be signed. Add `SessionToken` / `SetSessionToken` (`Signature` / `SetSignature`) methods to carry session token (signature) in `Container` structure. Signed-off-by: Leonard Lyubich --- pkg/container/container.go | 27 +++++++++++++++++++++++++++ pkg/container/container_test.go | 23 +++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/pkg/container/container.go b/pkg/container/container.go index f4a52a1d..f758d1c7 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -7,11 +7,16 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg/netmap" "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/nspcc-dev/neofs-api-go/pkg/token" "github.com/nspcc-dev/neofs-api-go/v2/container" ) type Container struct { v2 container.Container + + token *token.SessionToken + + sig *pkg.Signature } func New(opts ...NewOption) *Container { @@ -152,6 +157,28 @@ func (c *Container) SetPlacementPolicy(v *netmap.PlacementPolicy) { c.v2.SetPlacementPolicy(v.ToV2()) } +// SessionToken returns token of the session within +// which container was created. +func (c Container) SessionToken() *token.SessionToken { + return c.token +} + +// SetSessionToken sets token of the session within +// which container was created. +func (c *Container) SetSessionToken(t *token.SessionToken) { + c.token = t +} + +// Signature returns signature of the marshaled container. +func (c Container) Signature() *pkg.Signature { + return c.sig +} + +// SetSignature sets signature of the marshaled container. +func (c *Container) SetSignature(sig *pkg.Signature) { + c.sig = sig +} + // Marshal marshals Container into a protobuf binary form. // // Buffer is allocated when the argument is empty. diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go index f4739ab4..d1fb7bd5 100644 --- a/pkg/container/container_test.go +++ b/pkg/container/container_test.go @@ -10,6 +10,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/container" "github.com/nspcc-dev/neofs-api-go/pkg/netmap" "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/nspcc-dev/neofs-api-go/pkg/token" "github.com/nspcc-dev/neofs-crypto/test" "github.com/stretchr/testify/require" ) @@ -95,3 +96,25 @@ func TestContainerEncoding(t *testing.T) { require.Equal(t, c, c2) }) } + +func TestContainer_SessionToken(t *testing.T) { + tok := token.NewSessionToken() + tok.SetID([]byte{1, 2, 3}) + + cnr := container.New() + + cnr.SetSessionToken(tok) + + require.Equal(t, tok, cnr.SessionToken()) +} + +func TestContainer_Signature(t *testing.T) { + sig := pkg.NewSignature() + sig.SetKey([]byte{1, 2, 3}) + sig.SetSign([]byte{4, 5, 6}) + + cnr := container.New() + cnr.SetSignature(sig) + + require.Equal(t, sig, cnr.Signature()) +}