Skip to content

Commit

Permalink
session: Add method to set token signature without issuer
Browse files Browse the repository at this point in the history
`Sign` method sets both issuer and signature of the token. There could
be a need to set signature only, e.g. for testing.

Now signature could be set via new method `SetSignature` also used by
`Sign` itself.

Refs #546.

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Mar 25, 2024
1 parent 035b5ad commit bd31d70
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
5 changes: 1 addition & 4 deletions session/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ func (x commonData) signedData(w contextWriter) []byte {
return x.fillBody(w).StableMarshal(nil)
}

func (x *commonData) sign(signer user.Signer, w contextWriter) error {
x.issuer = signer.UserID()
x.issuerSet = true

func (x *commonData) sign(signer neofscrypto.Signer, w contextWriter) error {
var sig neofscrypto.Signature

err := sig.Calculate(signer, x.signedData(w))
Expand Down
8 changes: 8 additions & 0 deletions session/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ func (x *Container) UnmarshalJSON(data []byte) error {
//
// See also [Container.VerifySignature], [Container.SignedData].
func (x *Container) Sign(signer user.Signer) error {
x.issuer = signer.UserID()
x.issuerSet = true
return x.SetSignature(signer)
}

// SetSignature allows to sign Container like [Container.Sign] but without
// issuer setting.
func (x *Container) SetSignature(signer neofscrypto.Signer) error {
return x.sign(signer, x.writeContext)
}

Expand Down
4 changes: 4 additions & 0 deletions session/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,10 @@ func TestContainer_Issuer(t *testing.T) {
func TestContainer_Sign(t *testing.T) {
val := sessiontest.Container()

require.NoError(t, val.SetSignature(test.RandomSignerRFC6979(t)))
require.Zero(t, val.Issuer())
require.True(t, val.VerifySignature())

require.NoError(t, val.Sign(test.RandomSignerRFC6979(t)))

require.True(t, val.VerifySignature())
Expand Down
9 changes: 9 additions & 0 deletions session/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/nspcc-dev/neofs-api-go/v2/session"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/user"
)
Expand Down Expand Up @@ -181,6 +182,14 @@ func (x *Object) UnmarshalJSON(data []byte) error {
//
// See also [Object.VerifySignature], [Object.SignedData].
func (x *Object) Sign(signer user.Signer) error {
x.issuer = signer.UserID()
x.issuerSet = true
return x.SetSignature(signer)
}

// SetSignature allows to sign Object like [Object.Sign] but without issuer
// setting.
func (x *Object) SetSignature(signer neofscrypto.Signer) error {
return x.sign(signer, x.writeContext)
}

Expand Down
4 changes: 4 additions & 0 deletions session/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@ func TestObject_Issuer(t *testing.T) {
func TestObject_Sign(t *testing.T) {
val := sessiontest.Object()

require.NoError(t, val.SetSignature(test.RandomSignerRFC6979(t)))
require.Zero(t, val.Issuer())
require.True(t, val.VerifySignature())

require.NoError(t, val.Sign(test.RandomSignerRFC6979(t)))

require.True(t, val.VerifySignature())
Expand Down

0 comments on commit bd31d70

Please sign in to comment.