[#168] acl: Implement binary/JSON encoders/decoders on BearerToken

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 15:08:37 +03:00 committed by Alex Vanin
parent d8fa8df442
commit ec957be60c
6 changed files with 108 additions and 44 deletions

View file

@ -118,3 +118,47 @@ func sanityCheck(b *BearerToken) error {
return nil
}
// Marshal marshals BearerToken into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (b *BearerToken) Marshal(bs ...[]byte) ([]byte, error) {
var buf []byte
if len(bs) > 0 {
buf = bs[0]
}
return b.ToV2().
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of BearerToken.
func (b *BearerToken) Unmarshal(data []byte) error {
fV2 := new(acl.BearerToken)
if err := fV2.Unmarshal(data); err != nil {
return err
}
*b = *NewBearerTokenFromV2(fV2)
return nil
}
// MarshalJSON encodes BearerToken to protobuf JSON format.
func (b *BearerToken) MarshalJSON() ([]byte, error) {
return b.ToV2().
MarshalJSON()
}
// UnmarshalJSON decodes BearerToken from protobuf JSON format.
func (b *BearerToken) UnmarshalJSON(data []byte) error {
fV2 := new(acl.BearerToken)
if err := fV2.UnmarshalJSON(data); err != nil {
return err
}
*b = *NewBearerTokenFromV2(fV2)
return nil
}

View file

@ -30,3 +30,28 @@ func TestBearerToken_Issuer(t *testing.T) {
require.Equal(t, bearerToken.Issuer().String(), ownerID.String())
})
}
func TestFilterEncoding(t *testing.T) {
f := token.NewBearerToken()
f.SetLifetime(1, 2, 3)
t.Run("binary", func(t *testing.T) {
data, err := f.Marshal()
require.NoError(t, err)
f2 := token.NewBearerToken()
require.NoError(t, f2.Unmarshal(data))
require.Equal(t, f, f2)
})
t.Run("json", func(t *testing.T) {
data, err := f.MarshalJSON()
require.NoError(t, err)
d2 := token.NewBearerToken()
require.NoError(t, d2.UnmarshalJSON(data))
require.Equal(t, f, d2)
})
}

View file

@ -1,40 +1,10 @@
package acl
import (
"errors"
acl "github.com/nspcc-dev/neofs-api-go/v2/acl/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
var (
errEmptyInput = errors.New("empty input")
)
func BearerTokenToJSON(t *BearerToken) ([]byte, error) {
if t == nil {
return nil, errEmptyInput
}
msg := BearerTokenToGRPCMessage(t)
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
}
func BearerTokenFromJSON(data []byte) (*BearerToken, error) {
if len(data) == 0 {
return nil, errEmptyInput
}
msg := new(acl.BearerToken)
if err := protojson.Unmarshal(data, msg); err != nil {
return nil, err
}
return BearerTokenFromGRPCMessage(msg), nil
}
func (f *HeaderFilter) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
@ -154,3 +124,23 @@ func (bt *BearerTokenBody) UnmarshalJSON(data []byte) error {
return nil
}
func (bt *BearerToken) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
BearerTokenToGRPCMessage(bt),
)
}
func (bt *BearerToken) UnmarshalJSON(data []byte) error {
msg := new(acl.BearerToken)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*bt = *BearerTokenFromGRPCMessage(msg)
return nil
}

View file

@ -8,17 +8,15 @@ import (
)
func TestBearerTokenJSON(t *testing.T) {
exp := generateBearerToken("token")
b := generateBearerToken("id")
t.Run("non empty", func(t *testing.T) {
data, err := acl.BearerTokenToJSON(exp)
require.NoError(t, err)
data, err := b.MarshalJSON()
require.NoError(t, err)
got, err := acl.BearerTokenFromJSON(data)
require.NoError(t, err)
b2 := new(acl.BearerToken)
require.NoError(t, b2.UnmarshalJSON(data))
require.Equal(t, exp, got)
})
require.Equal(t, b, b2)
}
func TestFilterJSON(t *testing.T) {

View file

@ -468,3 +468,14 @@ func (bt *BearerToken) StableSize() (size int) {
return size
}
func (bt *BearerToken) Unmarshal(data []byte) error {
m := new(acl.BearerToken)
if err := proto.Unmarshal(data, m); err != nil {
return err
}
*bt = *BearerTokenFromGRPCMessage(m)
return nil
}

View file

@ -5,10 +5,8 @@ import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/acl"
grpc "github.com/nspcc-dev/neofs-api-go/v2/acl/grpc"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/stretchr/testify/require"
goproto "google.golang.org/protobuf/proto"
)
func generateTarget(u acl.Role, k int) *acl.Target {
@ -226,16 +224,14 @@ func TestBearerTokenBody_StableMarshal(t *testing.T) {
func TestBearerToken_StableMarshal(t *testing.T) {
bearerTokenFrom := generateBearerToken("Bearer Token")
transport := new(grpc.BearerToken)
t.Run("non empty", func(t *testing.T) {
wire, err := bearerTokenFrom.StableMarshal(nil)
require.NoError(t, err)
err = goproto.Unmarshal(wire, transport)
require.NoError(t, err)
bearerTokenTo := new(acl.BearerToken)
require.NoError(t, bearerTokenTo.Unmarshal(wire))
bearerTokenTo := acl.BearerTokenFromGRPCMessage(transport)
require.Equal(t, bearerTokenFrom, bearerTokenTo)
})
}