2020-09-29 15:05:22 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-12-11 11:59:16 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2020-09-29 15:08:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2020-09-29 15:05:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommonPrm struct {
|
|
|
|
local bool
|
2020-09-29 15:08:16 +00:00
|
|
|
|
2020-12-11 11:59:16 +00:00
|
|
|
token *token.SessionToken
|
|
|
|
|
2020-10-20 13:44:45 +00:00
|
|
|
bearer *token.BearerToken
|
2020-12-11 11:59:16 +00:00
|
|
|
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
callOpts []client.CallOption
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommonPrm) WithLocalOnly(v bool) *CommonPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.local = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommonPrm) LocalOnly() bool {
|
|
|
|
if p != nil {
|
|
|
|
return p.local
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:08:16 +00:00
|
|
|
func (p *CommonPrm) WithSessionToken(token *token.SessionToken) *CommonPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.token = token
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:44:45 +00:00
|
|
|
func (p *CommonPrm) WithBearerToken(token *token.BearerToken) *CommonPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.bearer = token
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-11 11:59:16 +00:00
|
|
|
// WithPrivateKey sets private key to use during execution.
|
|
|
|
func (p *CommonPrm) WithPrivateKey(key *ecdsa.PrivateKey) *CommonPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.key = key
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrivateKey returns private key to use during execution.
|
|
|
|
func (p *CommonPrm) PrivateKey() *ecdsa.PrivateKey {
|
|
|
|
if p != nil {
|
|
|
|
return p.key
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithRemoteCallOptions sets call options remote remote client calls.
|
|
|
|
func (p *CommonPrm) WithRemoteCallOptions(opts ...client.CallOption) *CommonPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.callOpts = opts
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteCallOptions return call options for remote client calls.
|
|
|
|
func (p *CommonPrm) RemoteCallOptions() []client.CallOption {
|
|
|
|
if p != nil {
|
|
|
|
return p.callOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:08:16 +00:00
|
|
|
func (p *CommonPrm) SessionToken() *token.SessionToken {
|
|
|
|
if p != nil {
|
|
|
|
return p.token
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:44:45 +00:00
|
|
|
func (p *CommonPrm) BearerToken() *token.BearerToken {
|
|
|
|
if p != nil {
|
|
|
|
return p.bearer
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:05:22 +00:00
|
|
|
func CommonPrmFromV2(req interface {
|
|
|
|
GetMetaHeader() *session.RequestMetaHeader
|
|
|
|
}) *CommonPrm {
|
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
2020-12-11 11:59:16 +00:00
|
|
|
prm := &CommonPrm{
|
|
|
|
local: meta.GetTTL() <= 1, // FIXME: use constant
|
|
|
|
token: nil,
|
|
|
|
bearer: nil,
|
|
|
|
callOpts: make([]client.CallOption, 0, 3),
|
|
|
|
}
|
|
|
|
|
|
|
|
prm.callOpts = append(prm.callOpts, client.WithTTL(meta.GetTTL()-1))
|
|
|
|
|
|
|
|
if tok := meta.GetSessionToken(); tok != nil {
|
|
|
|
prm.token = token.NewSessionTokenFromV2(tok)
|
|
|
|
prm.callOpts = append(prm.callOpts, client.WithSession(prm.token))
|
|
|
|
}
|
|
|
|
|
|
|
|
if tok := meta.GetBearerToken(); tok != nil {
|
|
|
|
prm.bearer = token.NewBearerTokenFromV2(tok)
|
|
|
|
prm.callOpts = append(prm.callOpts, client.WithBearer(prm.bearer))
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|
2020-12-11 11:59:16 +00:00
|
|
|
|
|
|
|
return prm
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|