From ec957be60c62d8129f453430e80dff7ffd660c8a Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 15:08:37 +0300 Subject: [PATCH] [#168] acl: Implement binary/JSON encoders/decoders on BearerToken Signed-off-by: Leonard Lyubich --- pkg/token/bearer.go | 44 +++++++++++++++++++++++++++++++++++ pkg/token/bearer_test.go | 25 ++++++++++++++++++++ v2/acl/json.go | 50 ++++++++++++++++------------------------ v2/acl/json_test.go | 14 +++++------ v2/acl/marshal.go | 11 +++++++++ v2/acl/marshal_test.go | 8 ++----- 6 files changed, 108 insertions(+), 44 deletions(-) diff --git a/pkg/token/bearer.go b/pkg/token/bearer.go index 87ecbc5..6dbb457 100644 --- a/pkg/token/bearer.go +++ b/pkg/token/bearer.go @@ -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 +} diff --git a/pkg/token/bearer_test.go b/pkg/token/bearer_test.go index a06510b..353a1b7 100644 --- a/pkg/token/bearer_test.go +++ b/pkg/token/bearer_test.go @@ -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) + }) +} diff --git a/v2/acl/json.go b/v2/acl/json.go index fcbd335..91f2d32 100644 --- a/v2/acl/json.go +++ b/v2/acl/json.go @@ -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 +} diff --git a/v2/acl/json_test.go b/v2/acl/json_test.go index 1962c1d..6d95755 100644 --- a/v2/acl/json_test.go +++ b/v2/acl/json_test.go @@ -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) { diff --git a/v2/acl/marshal.go b/v2/acl/marshal.go index ac378cc..f477848 100644 --- a/v2/acl/marshal.go +++ b/v2/acl/marshal.go @@ -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 +} diff --git a/v2/acl/marshal_test.go b/v2/acl/marshal_test.go index bb1ba98..8b85411 100644 --- a/v2/acl/marshal_test.go +++ b/v2/acl/marshal_test.go @@ -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) }) }