From f71d64435ebab15c283faff9895bfd386d2c2cd3 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 24 Aug 2020 13:05:10 +0300 Subject: [PATCH] [#11] services: Implement universal Sign/Verify service Signed-off-by: Leonard Lyubich --- pkg/services/accounting/sign.go | 30 ++++++++-------------- pkg/services/util/sign.go | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 pkg/services/util/sign.go diff --git a/pkg/services/accounting/sign.go b/pkg/services/accounting/sign.go index 97abda2e6..6867e4f51 100644 --- a/pkg/services/accounting/sign.go +++ b/pkg/services/accounting/sign.go @@ -5,39 +5,29 @@ import ( "crypto/ecdsa" "github.com/nspcc-dev/neofs-api-go/v2/accounting" - "github.com/nspcc-dev/neofs-api-go/v2/signature" - "github.com/pkg/errors" + "github.com/nspcc-dev/neofs-node/pkg/services/util" ) type signService struct { - key *ecdsa.PrivateKey - - svc accounting.Service + unarySigService *util.UnarySignService } func NewSignService(key *ecdsa.PrivateKey, svc accounting.Service) accounting.Service { return &signService{ - key: key, - svc: svc, + unarySigService: util.NewUnarySignService( + key, + func(ctx context.Context, req interface{}) (interface{}, error) { + return svc.Balance(ctx, req.(*accounting.BalanceRequest)) + }, + ), } } func (s *signService) Balance(ctx context.Context, req *accounting.BalanceRequest) (*accounting.BalanceResponse, error) { - // verify request signatures - if err := signature.VerifyServiceMessage(req); err != nil { - return nil, errors.Wrap(err, "could not verify request") - } - - // process request - resp, err := s.svc.Balance(ctx, req) + resp, err := s.unarySigService.HandleUnaryRequest(ctx, req) if err != nil { return nil, err } - // sign the response - if err := signature.SignServiceMessage(s.key, resp); err != nil { - return nil, errors.Wrap(err, "could not sign response") - } - - return resp, nil + return resp.(*accounting.BalanceResponse), nil } diff --git a/pkg/services/util/sign.go b/pkg/services/util/sign.go new file mode 100644 index 000000000..28e58d8d4 --- /dev/null +++ b/pkg/services/util/sign.go @@ -0,0 +1,44 @@ +package util + +import ( + "context" + "crypto/ecdsa" + + "github.com/nspcc-dev/neofs-api-go/v2/signature" + "github.com/pkg/errors" +) + +type UnaryHandler func(context.Context, interface{}) (interface{}, error) + +type UnarySignService struct { + key *ecdsa.PrivateKey + + unaryHandler UnaryHandler +} + +func NewUnarySignService(key *ecdsa.PrivateKey, handler UnaryHandler) *UnarySignService { + return &UnarySignService{ + key: key, + unaryHandler: handler, + } +} + +func (s *UnarySignService) HandleUnaryRequest(ctx context.Context, req interface{}) (interface{}, error) { + // verify request signatures + if err := signature.VerifyServiceMessage(req); err != nil { + return nil, errors.Wrap(err, "could not verify request") + } + + // process request + resp, err := s.unaryHandler(ctx, req) + if err != nil { + return nil, errors.Wrap(err, "could not handle request") + } + + // sign the response + if err := signature.SignServiceMessage(s.key, resp); err != nil { + return nil, errors.Wrap(err, "could not sign response") + } + + return resp, nil +}