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)
|
|
|
|
|
2020-08-26 09:43:39 +00:00
|
|
|
type SignService struct {
|
2020-08-24 10:05:10 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
type ServerStreamHandler func(context.Context, interface{}) (ResponseMessageReader, error)
|
2020-08-26 09:41:43 +00:00
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
type ResponseMessageReader func() (interface{}, error)
|
2020-08-26 09:41:43 +00:00
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
type ResponseMessageStreamer struct {
|
2020-08-26 09:41:43 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
recv ResponseMessageReader
|
|
|
|
}
|
|
|
|
|
|
|
|
type RequestMessageWriter func(interface{}) error
|
|
|
|
|
|
|
|
type ClientStreamCloser func() (interface{}, error)
|
|
|
|
|
|
|
|
type RequestMessageStreamer struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
send RequestMessageWriter
|
|
|
|
|
|
|
|
close ClientStreamCloser
|
2020-08-26 09:41:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:43:39 +00:00
|
|
|
func NewUnarySignService(key *ecdsa.PrivateKey) *SignService {
|
|
|
|
return &SignService{
|
2020-08-26 09:20:26 +00:00
|
|
|
key: key,
|
2020-08-24 10:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
func (s *RequestMessageStreamer) Send(req interface{}) error {
|
|
|
|
// verify request signatures
|
|
|
|
if err := signature.VerifyServiceMessage(req); err != nil {
|
|
|
|
return errors.Wrap(err, "could not verify request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.send(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestMessageStreamer) CloseAndRecv() (interface{}, error) {
|
|
|
|
resp, err := s.close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not close stream and receive response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := signature.SignServiceMessage(s.key, resp); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not sign response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SignService) CreateRequestStreamer(sender RequestMessageWriter, closer ClientStreamCloser) *RequestMessageStreamer {
|
|
|
|
return &RequestMessageStreamer{
|
|
|
|
key: s.key,
|
|
|
|
send: sender,
|
|
|
|
close: closer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ResponseMessageStreamer) Recv() (interface{}, error) {
|
2020-08-26 09:41:43 +00:00
|
|
|
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 10:00:41 +00:00
|
|
|
func (s *SignService) HandleServerStreamRequest(ctx context.Context, req interface{}, handler ServerStreamHandler) (*ResponseMessageStreamer, error) {
|
2020-08-26 09:41:43 +00:00
|
|
|
// 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 10:00:41 +00:00
|
|
|
return &ResponseMessageStreamer{
|
2020-08-26 09:41:43 +00:00
|
|
|
key: s.key,
|
|
|
|
recv: msgRdr,
|
|
|
|
}, nil
|
2020-08-25 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:43:39 +00:00
|
|
|
func (s *SignService) 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
|
|
|
|
}
|