[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-24 19:48:41 +03:00 committed by Alex Vanin
parent f60f7e0cdb
commit fcbe0bbd2e
2 changed files with 50 additions and 0 deletions

View file

@ -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.

View file

@ -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())
}