2021-06-14 13:39:25 +00:00
|
|
|
package tokens
|
2021-05-25 19:59:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-05-26 16:48:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2021-06-04 13:01:42 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2021-05-25 19:59:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-06-14 13:39:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2021-05-25 19:59:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
2021-05-28 20:48:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pkg/pool"
|
2021-05-25 19:59:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-05-26 18:23:36 +00:00
|
|
|
// Credentials is a bearer token get/put interface.
|
2021-05-25 19:59:21 +00:00
|
|
|
Credentials interface {
|
2021-06-17 16:45:50 +00:00
|
|
|
GetTokens(context.Context, *object.Address) (*accessbox.GateData, error)
|
2021-06-24 15:21:34 +00:00
|
|
|
Put(context.Context, *cid.ID, *owner.ID, *accessbox.AccessBox, ...*keys.PublicKey) (*object.Address, error)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cred struct {
|
2021-06-24 15:21:34 +00:00
|
|
|
key *keys.PrivateKey
|
2021-05-28 20:48:23 +00:00
|
|
|
pool pool.Pool
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-05-26 18:23:36 +00:00
|
|
|
// ErrEmptyPublicKeys is returned when no HCS keys are provided.
|
|
|
|
ErrEmptyPublicKeys = errors.New("HCS public keys could not be empty")
|
|
|
|
// ErrEmptyBearerToken is returned when no bearer token is provided.
|
2021-05-25 19:59:21 +00:00
|
|
|
ErrEmptyBearerToken = errors.New("Bearer token could not be empty")
|
|
|
|
)
|
|
|
|
|
|
|
|
var bufferPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = New
|
|
|
|
|
2021-05-26 18:23:36 +00:00
|
|
|
// New creates new Credentials instance using given cli and key.
|
2021-06-24 15:21:34 +00:00
|
|
|
func New(conns pool.Pool, key *keys.PrivateKey) Credentials {
|
2021-05-28 20:48:23 +00:00
|
|
|
return &cred{pool: conns, key: key}
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cred) acquireBuffer() *bytes.Buffer {
|
|
|
|
return bufferPool.Get().(*bytes.Buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cred) releaseBuffer(buf *bytes.Buffer) {
|
|
|
|
buf.Reset()
|
|
|
|
bufferPool.Put(buf)
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
func (c *cred) GetTokens(ctx context.Context, address *object.Address) (*accessbox.GateData, error) {
|
2021-06-14 13:39:25 +00:00
|
|
|
box, err := c.getAccessBox(ctx, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:45:50 +00:00
|
|
|
return box.GetTokens(c.key)
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cred) getAccessBox(ctx context.Context, address *object.Address) (*accessbox.AccessBox, error) {
|
|
|
|
var (
|
|
|
|
box accessbox.AccessBox
|
|
|
|
buf = c.acquireBuffer()
|
|
|
|
)
|
|
|
|
defer c.releaseBuffer(buf)
|
2021-05-25 19:59:21 +00:00
|
|
|
|
2021-05-26 16:48:27 +00:00
|
|
|
ops := new(client.GetObjectParams).WithAddress(address).WithPayloadWriter(buf)
|
|
|
|
|
2021-06-15 08:51:42 +00:00
|
|
|
_, err := c.pool.GetObject(
|
2021-05-26 16:48:27 +00:00
|
|
|
ctx,
|
|
|
|
ops,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
if err = box.Unmarshal(buf.Bytes()); err != nil {
|
2021-05-25 19:59:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-14 13:39:25 +00:00
|
|
|
return &box, nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
func (c *cred) Put(ctx context.Context, cid *cid.ID, issuer *owner.ID, box *accessbox.AccessBox, keys ...*keys.PublicKey) (*object.Address, error) {
|
2021-05-25 19:59:21 +00:00
|
|
|
var (
|
2021-06-15 15:48:30 +00:00
|
|
|
err error
|
2021-05-25 19:59:21 +00:00
|
|
|
created = strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(keys) == 0 {
|
|
|
|
return nil, ErrEmptyPublicKeys
|
2021-06-14 13:39:25 +00:00
|
|
|
} else if box == nil {
|
2021-05-25 19:59:21 +00:00
|
|
|
return nil, ErrEmptyBearerToken
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
|
|
|
data, err := box.Marshal()
|
|
|
|
if err != nil {
|
2021-05-25 19:59:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp := object.NewAttribute()
|
|
|
|
timestamp.SetKey(object.AttributeTimestamp)
|
|
|
|
timestamp.SetValue(created)
|
|
|
|
|
|
|
|
filename := object.NewAttribute()
|
|
|
|
filename.SetKey(object.AttributeFileName)
|
|
|
|
filename.SetValue(created + "_access.box")
|
|
|
|
|
|
|
|
raw := object.NewRaw()
|
|
|
|
raw.SetContainerID(cid)
|
2021-06-14 13:39:25 +00:00
|
|
|
raw.SetOwnerID(issuer)
|
2021-05-25 19:59:21 +00:00
|
|
|
raw.SetAttributes(filename, timestamp)
|
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
ops := new(client.PutObjectParams).WithObject(raw.Object()).WithPayloadReader(bytes.NewBuffer(data))
|
2021-06-15 08:51:42 +00:00
|
|
|
oid, err := c.pool.PutObject(
|
2021-05-26 16:48:27 +00:00
|
|
|
ctx,
|
|
|
|
ops,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
address := object.NewAddress()
|
|
|
|
address.SetObjectID(oid)
|
|
|
|
address.SetContainerID(cid)
|
|
|
|
return address, nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|