frostfs-api-go/session/create.go
Leonard Lyubich 74e917810a service: support broken apart signable payload of the requests
In previous implementation service package provided types and functions
that wrapped signing/verification of data with session token.
This allowed us to use these functions for signing / verification of
service requests of other packages. To support the expansion of messages
with additional parts that need to be signed, you must be able to easily
expand the signed data with new parts.

To achieve the described goal, this commit makes the following changes:

  * adds GroupSignedPayloads and GroupVerifyPayloads functions;

  * renames SignedDataWithToken to RequestData, DataWithTokenSignAccumulator
    to RequestSignedData, DataWithTokenSignSource to RequestVerifyData;

  * renames SignDataWithSessionToken/VerifyAccumulatedSignaturesWithToken
    function to SignRequestData/VerifyRequestData and makes it to use
    GroupSignedPayloads/GroupVerifyPayloads internally.
2020-06-10 20:37:10 +03:00

62 lines
1.5 KiB
Go

package session
import (
"context"
"crypto/ecdsa"
"github.com/nspcc-dev/neofs-api-go/service"
crypto "github.com/nspcc-dev/neofs-crypto"
"google.golang.org/grpc"
)
type gRPCCreator struct {
conn *grpc.ClientConn
key *ecdsa.PrivateKey
clientFunc func(*grpc.ClientConn) SessionClient
}
// NewGRPCCreator unites virtual gRPC client with private ket and returns Creator interface.
//
// If passed ClientConn is nil, ErrNilGPRCClientConn returns.
// If passed private key is nil, crypto.ErrEmptyPrivateKey returns.
func NewGRPCCreator(conn *grpc.ClientConn, key *ecdsa.PrivateKey) (Creator, error) {
if conn == nil {
return nil, ErrNilGPRCClientConn
} else if key == nil {
return nil, crypto.ErrEmptyPrivateKey
}
return &gRPCCreator{
conn: conn,
key: key,
clientFunc: NewSessionClient,
}, nil
}
// Create constructs message, signs it with private key and sends it to a gRPC client.
//
// If passed CreateParamsSource is nil, ErrNilCreateParamsSource returns.
// If message could not be signed, an error returns.
func (s gRPCCreator) Create(ctx context.Context, p CreateParamsSource) (CreateResult, error) {
if p == nil {
return nil, ErrNilCreateParamsSource
}
// create and fill a message
req := new(CreateRequest)
req.SetOwnerID(p.GetOwnerID())
req.SetCreationEpoch(p.CreationEpoch())
req.SetExpirationEpoch(p.ExpirationEpoch())
// sign with private key
if err := service.SignRequestData(s.key, req); err != nil {
return nil, err
}
// make gRPC call
return s.clientFunc(s.conn).Create(ctx, req)
}