2021-11-09 08:07:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
2021-11-16 18:15:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
|
2021-11-09 08:07:49 +00:00
|
|
|
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errMalformedResponseBody = errors.New("malformed response body")
|
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
type CreateSessionRes struct {
|
|
|
|
statusRes
|
|
|
|
|
|
|
|
id []byte
|
|
|
|
|
|
|
|
sessionKey []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *CreateSessionRes) setID(id []byte) {
|
|
|
|
x.id = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x CreateSessionRes) ID() []byte {
|
|
|
|
return x.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *CreateSessionRes) setSessionKey(key []byte) {
|
|
|
|
x.sessionKey = key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x CreateSessionRes) SessionKey() []byte {
|
|
|
|
return x.sessionKey
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:55:29 +00:00
|
|
|
// CreateSession creates session through NeoFS API call.
|
|
|
|
//
|
|
|
|
// Any client's internal or transport errors are returned as error,
|
|
|
|
// NeoFS status codes are included in the returned results.
|
2021-12-29 13:18:09 +00:00
|
|
|
func (c *Client) CreateSession(ctx context.Context, expiration uint64, opts ...CallOption) (*CreateSessionRes, error) {
|
2021-11-09 08:07:49 +00:00
|
|
|
// apply all available options
|
|
|
|
callOptions := c.defaultCallOptions()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := owner.NEO3WalletFromPublicKey(&callOptions.key.PublicKey)
|
|
|
|
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))
|
|
|
|
|
|
|
|
err = v2signature.SignServiceMessage(callOptions.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := rpcapi.CreateSession(c.Raw(), req, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("transport error: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
var (
|
|
|
|
res = new(CreateSessionRes)
|
|
|
|
procPrm processResponseV2Prm
|
|
|
|
procRes processResponseV2Res
|
|
|
|
)
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
procPrm.callOpts = callOptions
|
|
|
|
procPrm.resp = resp
|
|
|
|
|
|
|
|
procRes.statusRes = res
|
|
|
|
|
|
|
|
// process response in general
|
|
|
|
if c.processResponseV2(&procRes, procPrm) {
|
|
|
|
if procRes.cliErr != nil {
|
|
|
|
return nil, procRes.cliErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body := resp.GetBody()
|
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
res.setID(body.GetID())
|
|
|
|
res.setSessionKey(body.GetSessionKey())
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
return res, nil
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|