2020-09-29 15:05:22 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2022-05-18 15:20:08 +00:00
|
|
|
"fmt"
|
2021-01-12 14:55:02 +00:00
|
|
|
"strconv"
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2020-09-29 15:05:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2022-05-12 07:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/bearer"
|
2021-11-10 07:08:33 +00:00
|
|
|
sessionsdk "github.com/nspcc-dev/neofs-sdk-go/session"
|
2020-09-29 15:05:22 +00:00
|
|
|
)
|
|
|
|
|
2022-01-19 11:58:15 +00:00
|
|
|
// maxLocalTTL is maximum TTL for an operation to be considered local.
|
|
|
|
const maxLocalTTL = 1
|
|
|
|
|
2020-09-29 15:05:22 +00:00
|
|
|
type CommonPrm struct {
|
|
|
|
local bool
|
2020-09-29 15:08:16 +00:00
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
netmapEpoch, netmapLookupDepth uint64
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
token *sessionsdk.Object
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2022-05-12 07:22:02 +00:00
|
|
|
bearer *bearer.Token
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
ttl uint32
|
2020-09-29 15:05:22 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
xhdrs []string
|
2021-01-12 14:55:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
// TTL returns TTL for new requests.
|
|
|
|
func (p *CommonPrm) TTL() uint32 {
|
2020-09-29 15:05:22 +00:00
|
|
|
if p != nil {
|
2021-11-01 08:35:33 +00:00
|
|
|
return p.ttl
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 18:01:29 +00:00
|
|
|
return 1
|
2020-09-29 15:08:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 18:01:29 +00:00
|
|
|
// XHeaders returns X-Headers for new requests.
|
2022-05-18 15:20:08 +00:00
|
|
|
func (p *CommonPrm) XHeaders() []string {
|
2020-10-20 13:44:45 +00:00
|
|
|
if p != nil {
|
2021-11-01 08:35:33 +00:00
|
|
|
return p.xhdrs
|
2020-10-20 13:44:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return nil
|
2020-10-20 13:44:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
func (p *CommonPrm) WithLocalOnly(v bool) *CommonPrm {
|
2020-12-11 11:59:16 +00:00
|
|
|
if p != nil {
|
2021-11-01 08:35:33 +00:00
|
|
|
p.local = v
|
2020-12-11 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
func (p *CommonPrm) LocalOnly() bool {
|
2020-12-11 11:59:16 +00:00
|
|
|
if p != nil {
|
2021-11-01 08:35:33 +00:00
|
|
|
return p.local
|
2021-01-12 14:55:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return false
|
2021-03-13 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
func (p *CommonPrm) SessionToken() *sessionsdk.Object {
|
2020-09-29 15:08:16 +00:00
|
|
|
if p != nil {
|
|
|
|
return p.token
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-12 07:22:02 +00:00
|
|
|
func (p *CommonPrm) BearerToken() *bearer.Token {
|
2020-10-20 13:44:45 +00:00
|
|
|
if p != nil {
|
|
|
|
return p.bearer
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
func (p *CommonPrm) NetmapEpoch() uint64 {
|
|
|
|
if p != nil {
|
|
|
|
return p.netmapEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommonPrm) NetmapLookupDepth() uint64 {
|
|
|
|
if p != nil {
|
|
|
|
return p.netmapLookupDepth
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommonPrm) SetNetmapLookupDepth(v uint64) {
|
|
|
|
if p != nil {
|
|
|
|
p.netmapLookupDepth = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 17:57:56 +00:00
|
|
|
// ForgetTokens forgets all the tokens read from the request's
|
|
|
|
// meta information before.
|
|
|
|
func (p *CommonPrm) ForgetTokens() {
|
|
|
|
if p != nil {
|
|
|
|
p.token = nil
|
|
|
|
p.bearer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:05:22 +00:00
|
|
|
func CommonPrmFromV2(req interface {
|
|
|
|
GetMetaHeader() *session.RequestMetaHeader
|
2021-01-12 14:55:02 +00:00
|
|
|
}) (*CommonPrm, error) {
|
2020-09-29 15:05:22 +00:00
|
|
|
meta := req.GetMetaHeader()
|
2022-11-11 16:39:11 +00:00
|
|
|
ttl := meta.GetTTL()
|
|
|
|
|
|
|
|
// unwrap meta header to get original request meta information
|
|
|
|
for meta.GetOrigin() != nil {
|
|
|
|
meta = meta.GetOrigin()
|
|
|
|
}
|
2020-09-29 15:05:22 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
var tokenSession *sessionsdk.Object
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if tokenSessionV2 := meta.GetSessionToken(); tokenSessionV2 != nil {
|
|
|
|
tokenSession = new(sessionsdk.Object)
|
|
|
|
|
|
|
|
err = tokenSession.ReadFromV2(*tokenSessionV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid session token: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 15:55:16 +00:00
|
|
|
xHdrs := meta.GetXHeaders()
|
|
|
|
|
2020-12-11 11:59:16 +00:00
|
|
|
prm := &CommonPrm{
|
2022-05-25 16:09:12 +00:00
|
|
|
local: ttl <= maxLocalTTL,
|
|
|
|
token: tokenSession,
|
|
|
|
ttl: ttl - 1, // decrease TTL for new requests
|
|
|
|
xhdrs: make([]string, 0, 2*len(xHdrs)),
|
2020-12-11 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tok := meta.GetBearerToken(); tok != nil {
|
2022-05-12 07:22:02 +00:00
|
|
|
prm.bearer = new(bearer.Token)
|
2022-06-08 08:53:15 +00:00
|
|
|
err = prm.bearer.ReadFromV2(*tok)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid bearer token: %w", err)
|
|
|
|
}
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2020-12-28 15:55:16 +00:00
|
|
|
for i := range xHdrs {
|
2022-05-18 15:20:08 +00:00
|
|
|
switch key := xHdrs[i].GetKey(); key {
|
2021-01-12 14:55:02 +00:00
|
|
|
case session.XHeaderNetmapEpoch:
|
|
|
|
var err error
|
|
|
|
|
|
|
|
prm.netmapEpoch, err = strconv.ParseUint(xHdrs[i].GetValue(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case session.XHeaderNetmapLookupDepth:
|
|
|
|
var err error
|
|
|
|
|
|
|
|
prm.netmapLookupDepth, err = strconv.ParseUint(xHdrs[i].GetValue(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
2022-05-18 15:20:08 +00:00
|
|
|
prm.xhdrs = append(prm.xhdrs, key, xHdrs[i].GetValue())
|
2021-01-12 14:55:02 +00:00
|
|
|
}
|
2020-12-28 15:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
return prm, nil
|
2020-09-29 15:05:22 +00:00
|
|
|
}
|