2020-07-29 11:29:03 +00:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"math"
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2020-07-29 11:29:03 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
// SessionToken returns session token for connection
|
|
|
|
func (p *pool) Token(ctx context.Context, conn *grpc.ClientConn) (*token.SessionToken, error) {
|
2020-07-29 11:29:03 +00:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
if tkn, ok := p.tokens[conn.Target()]; ok && tkn != nil {
|
2020-07-29 11:29:03 +00:00
|
|
|
return tkn, nil
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
// prepare session token
|
|
|
|
tkn, err := prepareToken(ctx, conn, p.key)
|
2020-07-29 11:29:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
// save token for current connection
|
|
|
|
p.tokens[conn.Target()] = tkn
|
2020-07-29 11:29:03 +00:00
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
return tkn, nil
|
2020-07-29 11:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// creates token using
|
2020-10-13 09:32:23 +00:00
|
|
|
func prepareToken(ctx context.Context, con *grpc.ClientConn, key *ecdsa.PrivateKey) (*token.SessionToken, error) {
|
|
|
|
cli, err := client.New(key, client.WithGRPCConnection(con))
|
2020-07-29 11:29:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
return cli.CreateSession(ctx, math.MaxUint64)
|
2020-07-29 11:29:03 +00:00
|
|
|
}
|