[#56] object/transformer: Write session token to object body

Add session token argument to object formatter constructor which is written
to the object. Pass session token from trusted object Put.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-29 15:41:29 +03:00 committed by Alex Vanin
parent a4b9560ef6
commit 8cddbe58a6
2 changed files with 10 additions and 2 deletions

View file

@ -69,7 +69,7 @@ func (p *Streamer) initTarget(prm *PutInitPrm) error {
p.target = transformer.NewPayloadSizeLimiter( p.target = transformer.NewPayloadSizeLimiter(
p.maxSizeSrc.MaxObjectSize(), p.maxSizeSrc.MaxObjectSize(),
func() transformer.ObjectTarget { func() transformer.ObjectTarget {
return transformer.NewFormatTarget(pToken.SessionKey(), p.newCommonTarget(prm)) return transformer.NewFormatTarget(pToken.SessionKey(), p.newCommonTarget(prm), prm.token)
}, },
) )

View file

@ -5,6 +5,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
"github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/core/object"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -17,6 +18,8 @@ type formatter struct {
obj *object.RawObject obj *object.RawObject
sz uint64 sz uint64
token *token.SessionToken
} }
// NewFormatTarget returns ObjectTarget instance that finalizes object structure // NewFormatTarget returns ObjectTarget instance that finalizes object structure
@ -27,11 +30,13 @@ type formatter struct {
// Object changes: // Object changes:
// - sets version to current SDK version; // - sets version to current SDK version;
// - sets payload size to the total length of all written chunks; // - sets payload size to the total length of all written chunks;
// - sets session token;
// - calculates and sets verification fields (ID, Signature). // - calculates and sets verification fields (ID, Signature).
func NewFormatTarget(key *ecdsa.PrivateKey, nextTarget ObjectTarget) ObjectTarget { func NewFormatTarget(key *ecdsa.PrivateKey, nextTarget ObjectTarget, token *token.SessionToken) ObjectTarget {
return &formatter{ return &formatter{
nextTarget: nextTarget, nextTarget: nextTarget,
key: key, key: key,
token: token,
} }
} }
@ -52,12 +57,15 @@ func (f *formatter) Write(p []byte) (n int, err error) {
func (f *formatter) Close() (*AccessIdentifiers, error) { func (f *formatter) Close() (*AccessIdentifiers, error) {
f.obj.SetVersion(pkg.SDKVersion()) f.obj.SetVersion(pkg.SDKVersion())
f.obj.SetPayloadSize(f.sz) f.obj.SetPayloadSize(f.sz)
f.obj.SetSessionToken(f.token)
var parID *objectSDK.ID var parID *objectSDK.ID
if par := f.obj.GetParent(); par != nil && par.ToV2().GetHeader() != nil { if par := f.obj.GetParent(); par != nil && par.ToV2().GetHeader() != nil {
rawPar := objectSDK.NewRawFromV2(par.ToV2()) rawPar := objectSDK.NewRawFromV2(par.ToV2())
rawPar.SetSessionToken(f.token)
if err := objectSDK.SetIDWithSignature(f.key, rawPar); err != nil { if err := objectSDK.SetIDWithSignature(f.key, rawPar); err != nil {
return nil, errors.Wrap(err, "could not finalize parent object") return nil, errors.Wrap(err, "could not finalize parent object")
} }