From c512d8d645f7d9bb81af5b9649628f8dc8f85d12 Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Tue, 22 Aug 2023 09:35:38 +0400 Subject: [PATCH] client: Add SessionCreate example Signed-off-by: Evgenii Baidakov --- client/example_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/client/example_test.go b/client/example_test.go index 885f7750..60651820 100644 --- a/client/example_test.go +++ b/client/example_test.go @@ -1,12 +1,21 @@ package client_test import ( + "context" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" "time" + "github.com/google/uuid" rpcClient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" "github.com/nspcc-dev/neofs-api-go/v2/rpc/common" "github.com/nspcc-dev/neofs-api-go/v2/rpc/grpc" "github.com/nspcc-dev/neofs-sdk-go/client" + cid "github.com/nspcc-dev/neofs-sdk-go/container/id" + neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa" + "github.com/nspcc-dev/neofs-sdk-go/session" + "github.com/nspcc-dev/neofs-sdk-go/user" ) func ExampleClient_createInstance() { @@ -100,3 +109,57 @@ func Example_customService() { // // ... // } } + +// Session created for the one node, and it will work only for this node. Other nodes don't have info about this session. +// That is why session can't be created with Pool API. +func ExampleClient_SessionCreate() { + // import "github.com/google/uuid" + + var prmInit client.PrmInit + // ... + c, _ := client.New(prmInit) + + // Epoch when session will expire. + // Note that expiration starts since exp+1 epoch. + // For instance, now you have 8 epoch. You set exp=10. The session will be still valid during 10th epoch. + // Expiration starts since 11 epoch. + var exp uint64 + var prm client.PrmSessionCreate + prm.SetExp(exp) + + // The key is generated to simplify the example, in reality it's likely to come from configuration/wallet. + pk, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + signer := user.NewAutoIDSignerRFC6979(*pk) + + res, _ := c.SessionCreate(context.Background(), signer, prm) + + var id uuid.UUID + _ = id.UnmarshalBinary(res.ID()) + + // Public key for separate private key, which was created inside node for this session. + var key neofsecdsa.PublicKey + _ = key.Decode(res.PublicKey()) + + // Fill session parameters + var sessionObject session.Object + sessionObject.SetID(id) + sessionObject.SetAuthKey(&key) + sessionObject.SetExp(exp) + + // Attach verb and container. Session allows to do just one action by time. In this example it is a VerbObjectPut. + // If you need Get, Delete, etc you should create another session. + sessionObject.ForVerb(session.VerbObjectPut) + // Session works only with one container. + sessionObject.BindContainer(cid.ID{}) + + // Finally, token must be signed by container owner or someone who allowed to do the Verb action. In our example + // it is VerbObjectPut. + _ = sessionObject.Sign(signer) + + // ... + + // This token will be used in object put operation + var prmPut client.PrmObjectPutInit + prmPut.WithinSession(sessionObject) + // ... +}