2020-09-15 10:49:20 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-17 12:56:17 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-15 10:49:20 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2021-05-31 12:09:04 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
2021-03-12 13:07:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/rpc/client"
|
|
|
|
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
2020-09-15 10:49:20 +00:00
|
|
|
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
)
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// Session contains session-related methods.
|
|
|
|
type Session interface {
|
|
|
|
// CreateSession creates session using provided expiration time.
|
2021-05-31 12:09:04 +00:00
|
|
|
CreateSession(context.Context, uint64, ...CallOption) (*session.Token, error)
|
2021-03-12 15:02:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 15:10:51 +00:00
|
|
|
var errMalformedResponseBody = errors.New("malformed response body")
|
|
|
|
|
2021-05-31 12:09:04 +00:00
|
|
|
func (c *clientImpl) CreateSession(ctx context.Context, expiration uint64, opts ...CallOption) (*session.Token, error) {
|
2020-09-15 10:49:20 +00:00
|
|
|
// apply all available options
|
2020-10-27 12:54:02 +00:00
|
|
|
callOptions := c.defaultCallOptions()
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-15 10:49:20 +00:00
|
|
|
for i := range opts {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOptions)
|
2020-09-15 10:49:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 12:15:54 +00:00
|
|
|
w, err := owner.NEO3WalletFromPublicKey(&callOptions.key.PublicKey)
|
2020-09-15 10:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ownerID := new(owner.ID)
|
|
|
|
ownerID.SetNeo3Wallet(w)
|
|
|
|
|
|
|
|
reqBody := new(v2session.CreateRequestBody)
|
|
|
|
reqBody.SetOwnerID(ownerID.ToV2())
|
|
|
|
reqBody.SetExpiration(expiration)
|
|
|
|
|
|
|
|
req := new(v2session.CreateRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
2021-03-10 12:15:54 +00:00
|
|
|
err = v2signature.SignServiceMessage(callOptions.key, req)
|
2020-09-15 10:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
resp, err := rpcapi.CreateSession(c.Raw(), req, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("transport error: %w", err)
|
2020-09-15 10:49:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:42:16 +00:00
|
|
|
// handle response meta info
|
|
|
|
if err := c.handleResponseInfoV2(callOptions, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
2020-09-15 10:49:20 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("can't verify response message: %w", err)
|
2020-09-15 10:49:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
body := resp.GetBody()
|
|
|
|
if body == nil {
|
|
|
|
return nil, errMalformedResponseBody
|
|
|
|
}
|
2020-10-27 12:55:19 +00:00
|
|
|
|
2021-05-31 12:09:04 +00:00
|
|
|
sessionToken := session.NewToken()
|
2021-03-12 13:07:52 +00:00
|
|
|
sessionToken.SetID(body.GetID())
|
|
|
|
sessionToken.SetSessionKey(body.GetSessionKey())
|
|
|
|
sessionToken.SetOwnerID(ownerID)
|
2020-10-27 12:55:19 +00:00
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
return sessionToken, nil
|
2020-10-27 12:55:19 +00:00
|
|
|
}
|