From bef4618cd6b914a78c3d7f51f81a4c512e98d405 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 25 May 2022 19:04:09 +0300 Subject: [PATCH] [#252] session: Add `Object.Issuer` method Add method to get `Object` session's issuer similar to `Container`. Signed-off-by: Leonard Lyubich --- session/object.go | 17 +++++++++++++++++ session/object_test.go | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/session/object.go b/session/object.go index 74b1345d..d8a7fa19 100644 --- a/session/object.go +++ b/session/object.go @@ -377,3 +377,20 @@ func (x Object) AssertAuthKey(key neofscrypto.PublicKey) bool { return bytes.Equal(bKey, x.body.GetSessionKey()) } + +// Issuer returns user ID of the session issuer. +// +// Makes sense only for signed Object instances. For unsigned instances, +// Issuer returns zero user.ID. +// +// See also Sign. +func (x Object) Issuer() user.ID { + var issuer user.ID + + issuerV2 := x.body.GetOwnerID() + if issuerV2 != nil { + _ = issuer.ReadFromV2(*issuerV2) + } + + return issuer +} diff --git a/session/object_test.go b/session/object_test.go index dd7f5efa..c702c1c2 100644 --- a/session/object_test.go +++ b/session/object_test.go @@ -16,6 +16,7 @@ import ( addresstest "github.com/nspcc-dev/neofs-sdk-go/object/address/test" "github.com/nspcc-dev/neofs-sdk-go/session" sessiontest "github.com/nspcc-dev/neofs-sdk-go/session/test" + "github.com/nspcc-dev/neofs-sdk-go/user" "github.com/stretchr/testify/require" ) @@ -294,3 +295,18 @@ func TestObjectSignature(t *testing.T) { require.True(t, x.VerifySignature()) } } + +func TestObject_Issuer(t *testing.T) { + var token session.Object + signer := randSigner() + + require.Zero(t, token.Issuer()) + + require.NoError(t, token.Sign(signer)) + + var issuer user.ID + + user.IDFromKey(&issuer, signer.PublicKey) + + require.True(t, token.Issuer().Equals(issuer)) +}