Skip to content

Commit

Permalink
session: Add getter of binary-encoded public key from signature
Browse files Browse the repository at this point in the history
Signature is used for user authentication so access to public key is
required. Digital signature bytes may still be unexported because they
are only useful for `VerifySignature` method.

Add `IssuerPublicKeyBytes` method returning binary-encoded public key of
the session issuer.

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Aug 3, 2023
1 parent ea6c536 commit 1e1ae8f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
11 changes: 11 additions & 0 deletions session/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,14 @@ func (x commonData) Issuer() user.ID {

return user.ID{}
}

// IssuerPublicKeyBytes returns binary-encoded public key of the session issuer.
//
// IssuerPublicKeyBytes MUST NOT be called before ReadFromV2 or Sign methods.
func (x *commonData) IssuerPublicKeyBytes() []byte {
if x.sigSet {
return x.sig.GetKey()
}

return nil
}
17 changes: 15 additions & 2 deletions session/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,9 @@ func TestIssuedBy(t *testing.T) {
}

func TestContainer_Issuer(t *testing.T) {
var token session.Container

t.Run("signer", func(t *testing.T) {
var token session.Container

signer := test.RandomSignerRFC6979(t)

require.Zero(t, token.Issuer())
Expand All @@ -536,12 +536,25 @@ func TestContainer_Issuer(t *testing.T) {
})

t.Run("external", func(t *testing.T) {
var token session.Container

signer := test.RandomSignerRFC6979(t)
issuer := signer.UserID()

token.SetIssuer(issuer)
require.True(t, token.Issuer().Equals(issuer))
})

t.Run("public key", func(t *testing.T) {
var token session.Container

signer := test.RandomSignerRFC6979(t)

require.Nil(t, token.IssuerPublicKeyBytes())
require.NoError(t, token.Sign(signer))

require.Equal(t, neofscrypto.PublicKeyBytes(signer.Public()), token.IssuerPublicKeyBytes())
})
}

func TestContainer_Sign(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions session/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,14 @@ func TestObject_Issuer(t *testing.T) {
signer := test.RandomSignerRFC6979(t)

require.Zero(t, token.Issuer())
require.Nil(t, token.IssuerPublicKeyBytes())

require.NoError(t, token.Sign(signer))

issuer := signer.UserID()

require.True(t, token.Issuer().Equals(issuer))
require.Equal(t, neofscrypto.PublicKeyBytes(signer.Public()), token.IssuerPublicKeyBytes())
}

func TestObject_Sign(t *testing.T) {
Expand Down

0 comments on commit 1e1ae8f

Please sign in to comment.