2021-06-14 13:39:25 +00:00
|
|
|
package tokens
|
2021-05-25 19:59:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-02-08 16:54:04 +00:00
|
|
|
"fmt"
|
2021-05-25 19:59:21 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-09-10 06:56:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/cache"
|
2021-05-25 19:59:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
2021-11-15 12:56:16 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-03-01 19:02:24 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-04-25 09:57:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
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 {
|
2022-05-25 17:25:43 +00:00
|
|
|
GetBox(context.Context, oid.Address) (*accessbox.Box, error)
|
|
|
|
Put(context.Context, cid.ID, user.ID, *accessbox.AccessBox, uint64, ...*keys.PublicKey) (*oid.Address, error)
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cred struct {
|
2021-09-08 07:08:56 +00:00
|
|
|
key *keys.PrivateKey
|
2022-03-01 19:02:24 +00:00
|
|
|
neoFS NeoFS
|
2021-09-10 06:56:56 +00:00
|
|
|
cache *cache.AccessBoxCache
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
// PrmObjectCreate groups parameters of objects created by credential tool.
|
|
|
|
type PrmObjectCreate struct {
|
|
|
|
// NeoFS identifier of the object creator.
|
2022-04-25 09:57:58 +00:00
|
|
|
Creator user.ID
|
2022-03-01 19:02:24 +00:00
|
|
|
|
|
|
|
// NeoFS container to store the object.
|
|
|
|
Container cid.ID
|
|
|
|
|
|
|
|
// File name.
|
|
|
|
Filename string
|
|
|
|
|
|
|
|
// Last NeoFS epoch of the object lifetime.
|
|
|
|
ExpirationEpoch uint64
|
|
|
|
|
|
|
|
// Object payload.
|
|
|
|
Payload []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeoFS represents virtual connection to NeoFS network.
|
|
|
|
type NeoFS interface {
|
|
|
|
// CreateObject creates and saves a parameterized object in the specified
|
2022-04-13 16:56:58 +00:00
|
|
|
// NeoFS container from a specific user. It sets 'Timestamp' attribute to the current time.
|
|
|
|
// It returns the ID of the saved object.
|
2022-03-01 19:02:24 +00:00
|
|
|
//
|
2022-04-13 16:56:58 +00:00
|
|
|
// It returns exactly one non-nil value. It returns any error encountered which
|
|
|
|
// prevented the object from being created.
|
2022-03-01 19:02:24 +00:00
|
|
|
CreateObject(context.Context, PrmObjectCreate) (*oid.ID, error)
|
|
|
|
|
|
|
|
// ReadObjectPayload reads payload of the object from NeoFS network by address
|
|
|
|
// into memory.
|
|
|
|
//
|
2022-04-13 16:56:58 +00:00
|
|
|
// It returns exactly one non-nil value. It returns any error encountered which
|
|
|
|
// prevented the object payload from being read.
|
2022-05-25 17:25:43 +00:00
|
|
|
ReadObjectPayload(context.Context, oid.Address) ([]byte, error)
|
2022-03-01 19:02:24 +00:00
|
|
|
}
|
|
|
|
|
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 _ = New
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// New creates a new Credentials instance using the given cli and key.
|
2022-03-01 19:02:24 +00:00
|
|
|
func New(neoFS NeoFS, key *keys.PrivateKey, config *cache.Config) Credentials {
|
|
|
|
return &cred{neoFS: neoFS, key: key, cache: cache.NewAccessBoxCache(config)}
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
func (c *cred) GetBox(ctx context.Context, addr oid.Address) (*accessbox.Box, error) {
|
2022-02-08 16:54:04 +00:00
|
|
|
cachedBox := c.cache.Get(addr)
|
2021-09-08 07:08:56 +00:00
|
|
|
if cachedBox != nil {
|
|
|
|
return cachedBox, nil
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
box, err := c.getAccessBox(ctx, addr)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-08 07:08:56 +00:00
|
|
|
cachedBox, err = box.GetBox(c.key)
|
2021-07-16 12:35:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
if err = c.cache.Put(addr, cachedBox); err != nil {
|
2021-09-08 07:08:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedBox, nil
|
2021-07-16 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
func (c *cred) getAccessBox(ctx context.Context, addr oid.Address) (*accessbox.AccessBox, error) {
|
|
|
|
data, err := c.neoFS.ReadObjectPayload(ctx, addr)
|
2022-02-08 16:54:04 +00:00
|
|
|
if err != nil {
|
2022-03-01 19:02:24 +00:00
|
|
|
return nil, fmt.Errorf("read payload: %w", err)
|
2021-05-26 16:48:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
// decode access box
|
|
|
|
var box accessbox.AccessBox
|
|
|
|
if err = box.Unmarshal(data); err != nil {
|
2021-05-25 19:59:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
return &box, nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
func (c *cred) Put(ctx context.Context, idCnr cid.ID, issuer user.ID, box *accessbox.AccessBox, expiration uint64, keys ...*keys.PublicKey) (*oid.Address, error) {
|
2021-05-25 19:59:21 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:02:24 +00:00
|
|
|
idObj, err := c.neoFS.CreateObject(ctx, PrmObjectCreate{
|
2022-04-25 09:57:58 +00:00
|
|
|
Creator: issuer,
|
2022-05-25 17:25:43 +00:00
|
|
|
Container: idCnr,
|
2022-03-25 10:28:39 +00:00
|
|
|
Filename: strconv.FormatInt(time.Now().Unix(), 10) + "_access.box",
|
2022-03-01 19:02:24 +00:00
|
|
|
ExpirationEpoch: expiration,
|
|
|
|
Payload: data,
|
|
|
|
})
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetObject(*idObj)
|
|
|
|
addr.SetContainer(idCnr)
|
|
|
|
|
|
|
|
return &addr, nil
|
2021-05-25 19:59:21 +00:00
|
|
|
}
|