2021-11-09 08:07:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-30 08:40:28 +00:00
|
|
|
"crypto/ecdsa"
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2022-04-11 06:30:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-11-09 08:07:49 +00:00
|
|
|
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"
|
2022-04-11 06:30:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2021-11-09 08:07:49 +00:00
|
|
|
)
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// PrmSessionCreate groups parameters of SessionCreate operation.
|
|
|
|
type PrmSessionCreate struct {
|
2022-03-03 11:04:53 +00:00
|
|
|
prmCommonMeta
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
exp uint64
|
2022-05-30 08:40:28 +00:00
|
|
|
|
|
|
|
keySet bool
|
|
|
|
key ecdsa.PrivateKey
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetExp sets number of the last NepFS epoch in the lifetime of the session after which it will be expired.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x *PrmSessionCreate) SetExp(exp uint64) {
|
2021-12-02 12:21:54 +00:00
|
|
|
x.exp = exp
|
|
|
|
}
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2022-05-30 08:40:28 +00:00
|
|
|
// UseKey specifies private key to sign the requests and compute token owner.
|
|
|
|
// If key is not provided, then Client default key is used.
|
|
|
|
func (x *PrmSessionCreate) UseKey(key ecdsa.PrivateKey) {
|
|
|
|
x.keySet = true
|
|
|
|
x.key = key
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// ResSessionCreate groups resulting values of SessionCreate operation.
|
|
|
|
type ResSessionCreate struct {
|
2021-11-16 18:17:25 +00:00
|
|
|
statusRes
|
|
|
|
|
|
|
|
id []byte
|
|
|
|
|
|
|
|
sessionKey []byte
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x *ResSessionCreate) setID(id []byte) {
|
2021-11-16 18:17:25 +00:00
|
|
|
x.id = id
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// ID returns identifier of the opened session in a binary NeoFS API protocol format.
|
|
|
|
//
|
|
|
|
// Client doesn't retain value so modification is safe.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x ResSessionCreate) ID() []byte {
|
2021-11-16 18:17:25 +00:00
|
|
|
return x.id
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x *ResSessionCreate) setSessionKey(key []byte) {
|
2021-11-16 18:17:25 +00:00
|
|
|
x.sessionKey = key
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// PublicKey returns public key of the opened session in a binary NeoFS API protocol format.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x ResSessionCreate) PublicKey() []byte {
|
2021-11-16 18:17:25 +00:00
|
|
|
return x.sessionKey
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// SessionCreate opens a session with the node server on the remote endpoint.
|
2021-12-02 12:21:54 +00:00
|
|
|
// The session lifetime coincides with the server lifetime. Results can be written
|
|
|
|
// to session token which can be later attached to the requests.
|
2021-12-29 13:55:29 +00:00
|
|
|
//
|
2022-01-24 10:33:43 +00:00
|
|
|
// Exactly one return value is non-nil. By default, server status is returned in res structure.
|
2021-12-02 12:21:54 +00:00
|
|
|
// Any client's internal or transport errors are returned as `error`.
|
2022-07-04 15:33:33 +00:00
|
|
|
// If PrmInit.ResolveNeoFSFailures has been called, unsuccessful
|
2021-12-02 12:21:54 +00:00
|
|
|
// NeoFS status codes are returned as `error`, otherwise, are included
|
|
|
|
// in the returned result structure.
|
|
|
|
//
|
2022-02-17 16:10:49 +00:00
|
|
|
// Immediately panics if parameters are set incorrectly (see PrmSessionCreate docs).
|
2021-12-02 12:21:54 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
2022-01-24 10:33:43 +00:00
|
|
|
// Return statuses:
|
2022-08-24 14:17:40 +00:00
|
|
|
// - global (see Client docs).
|
2022-02-17 16:10:49 +00:00
|
|
|
func (c *Client) SessionCreate(ctx context.Context, prm PrmSessionCreate) (*ResSessionCreate, error) {
|
2021-12-02 12:21:54 +00:00
|
|
|
// check context
|
|
|
|
if ctx == nil {
|
|
|
|
panic(panicMsgMissingContext)
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 08:40:28 +00:00
|
|
|
ownerKey := c.prm.key.PublicKey
|
|
|
|
if prm.keySet {
|
|
|
|
ownerKey = prm.key.PublicKey
|
|
|
|
}
|
2022-04-11 06:30:22 +00:00
|
|
|
var ownerID user.ID
|
2022-05-30 08:40:28 +00:00
|
|
|
user.IDFromKey(&ownerID, ownerKey)
|
2022-04-11 06:30:22 +00:00
|
|
|
|
|
|
|
var ownerIDV2 refs.OwnerID
|
|
|
|
ownerID.WriteToV2(&ownerIDV2)
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// form request body
|
2021-11-09 08:07:49 +00:00
|
|
|
reqBody := new(v2session.CreateRequestBody)
|
2022-04-11 06:30:22 +00:00
|
|
|
reqBody.SetOwnerID(&ownerIDV2)
|
2021-12-02 12:21:54 +00:00
|
|
|
reqBody.SetExpiration(prm.exp)
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// for request
|
|
|
|
var req v2session.CreateRequest
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
req.SetBody(reqBody)
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// init call context
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
var (
|
2021-12-02 12:21:54 +00:00
|
|
|
cc contextCall
|
2022-02-17 16:10:49 +00:00
|
|
|
res ResSessionCreate
|
2021-11-16 18:17:25 +00:00
|
|
|
)
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2022-08-23 17:12:12 +00:00
|
|
|
c.initCallContext(&cc)
|
2022-05-30 08:40:28 +00:00
|
|
|
if prm.keySet {
|
|
|
|
cc.key = prm.key
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:04:53 +00:00
|
|
|
cc.meta = prm.prmCommonMeta
|
2021-12-02 12:21:54 +00:00
|
|
|
cc.req = &req
|
|
|
|
cc.statusRes = &res
|
|
|
|
cc.call = func() (responseV2, error) {
|
2022-03-07 11:39:49 +00:00
|
|
|
return rpcapi.CreateSession(&c.c, &req, client.WithContext(ctx))
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
cc.result = func(r responseV2) {
|
|
|
|
resp := r.(*v2session.CreateResponse)
|
2021-11-16 18:17:25 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
body := resp.GetBody()
|
2021-11-16 18:17:25 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
res.setID(body.GetID())
|
|
|
|
res.setSessionKey(body.GetSessionKey())
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// process call
|
|
|
|
if !cc.processCall() {
|
|
|
|
return nil, cc.err
|
|
|
|
}
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
return &res, nil
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|