[#293] pkg/token: Implement and use generator of BearerToken

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-08 14:39:17 +03:00 committed by Alex Vanin
parent 122a31cadb
commit 00778cc9ba
2 changed files with 37 additions and 2 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
tokentest "github.com/nspcc-dev/neofs-api-go/pkg/token/test"
"github.com/nspcc-dev/neofs-crypto/test"
"github.com/stretchr/testify/require"
)
@ -32,8 +33,7 @@ func TestBearerToken_Issuer(t *testing.T) {
}
func TestFilterEncoding(t *testing.T) {
f := token.NewBearerToken()
f.SetLifetime(1, 2, 3)
f := tokentest.Generate()
t.Run("binary", func(t *testing.T) {
data, err := f.Marshal()

View file

@ -0,0 +1,35 @@
package tokentest
import (
eacltest "github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl/test"
ownertest "github.com/nspcc-dev/neofs-api-go/pkg/owner/test"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
"github.com/nspcc-dev/neofs-crypto/test"
)
// Generate returns random token.BearerToken.
//
// Resulting token is unsigned.
func Generate() *token.BearerToken {
x := token.NewBearerToken()
x.SetLifetime(3, 2, 1)
x.SetOwner(ownertest.Generate())
x.SetEACLTable(eacltest.Table())
return x
}
// GenerateSigned returns signed random token.BearerToken.
//
// Panics if token could not be signed (actually unexpected).
func GenerateSigned() *token.BearerToken {
tok := Generate()
err := tok.SignToken(test.DecodeKey(0))
if err != nil {
panic(err)
}
return tok
}