[#238] session: Add container.IssuedBy method

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2022-05-04 17:49:55 +03:00 committed by LeL
parent 539ac9915e
commit 12ea1e8d74
2 changed files with 38 additions and 0 deletions

View file

@ -376,3 +376,25 @@ func (x Container) AssertAuthKey(key neofscrypto.PublicKey) bool {
return bytes.Equal(bKey, x.body.GetSessionKey())
}
// IssuedBy returns true if session token is signed
// and, therefore, owned by specified user.
//
// See also Sign.
func (x Container) IssuedBy(id user.ID) bool {
var (
tokenOwner user.ID
v2TokenOwner = x.body.GetOwnerID()
)
if v2TokenOwner == nil {
return false
}
err := tokenOwner.ReadFromV2(*v2TokenOwner)
if err != nil {
return false
}
return tokenOwner.Equals(id)
}

View file

@ -11,6 +11,7 @@ import (
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/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"
)
@ -285,3 +286,18 @@ func TestContainerSignature(t *testing.T) {
require.True(t, x.VerifySignature())
}
}
func TestContainer_IssuedBy(t *testing.T) {
var (
token session.Container
issuer user.ID
signer = randSigner()
)
user.IDFromKey(&issuer, signer.PublicKey)
require.False(t, token.IssuedBy(issuer))
require.NoError(t, token.Sign(signer))
require.True(t, token.IssuedBy(issuer))
}