2020-08-24 10:05:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
type ServerStreamHandler func(context.Context, interface{}) (MessageReader, error)
|
|
|
|
|
|
|
|
type MessageReader func() (interface{}, error)
|
|
|
|
|
|
|
|
type MessageStreamer struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
recv MessageReader
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:20:26 +00:00
|
|
|
func NewUnarySignService(key *ecdsa.PrivateKey) *UnarySignService {
|
2020-08-24 10:05:10 +00:00
|
|
|
return &UnarySignService{
|
2020-08-26 09:20:26 +00:00
|
|
|
key: key,
|
2020-08-24 10:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
func (s *MessageStreamer) Recv() (interface{}, error) {
|
|
|
|
m, err := s.recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not receive response message for signing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := signature.SignServiceMessage(s.key, m); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not sign response message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
2020-08-25 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
func (s *UnarySignService) HandleServerStreamRequest(ctx context.Context, req interface{}, handler ServerStreamHandler) (*MessageStreamer, error) {
|
|
|
|
// verify request signatures
|
|
|
|
if err := signature.VerifyServiceMessage(req); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not verify request")
|
2020-08-25 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
msgRdr, err := handler(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not create message reader")
|
2020-08-25 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
return &MessageStreamer{
|
|
|
|
key: s.key,
|
|
|
|
recv: msgRdr,
|
|
|
|
}, nil
|
2020-08-25 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
func (s *UnarySignService) HandleUnaryRequest(ctx context.Context, req interface{}, handler UnaryHandler) (interface{}, error) {
|
2020-08-24 10:05:10 +00:00
|
|
|
// verify request signatures
|
|
|
|
if err := signature.VerifyServiceMessage(req); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not verify request")
|
|
|
|
}
|
|
|
|
|
|
|
|
// process request
|
2020-08-26 09:20:26 +00:00
|
|
|
resp, err := handler(ctx, req)
|
2020-08-24 10:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not handle request")
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
// sign the response
|
|
|
|
if err := signature.SignServiceMessage(s.key, resp); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not sign response")
|
|
|
|
}
|
|
|
|
|
2020-08-24 10:05:10 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|