2022-04-11 09:35:06 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2022-04-18 08:30:34 +00:00
|
|
|
"context"
|
2022-04-11 09:35:06 +00:00
|
|
|
"encoding/base64"
|
2022-06-16 09:12:26 +00:00
|
|
|
"encoding/hex"
|
2022-07-07 12:59:38 +00:00
|
|
|
"errors"
|
2022-04-11 09:35:06 +00:00
|
|
|
"fmt"
|
2022-04-20 14:52:41 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
"github.com/go-openapi/runtime/middleware"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/acl"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/gen/models"
|
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/gen/restapi/operations"
|
2022-04-29 06:39:24 +00:00
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/internal/util"
|
2022-07-12 08:01:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/bearer"
|
2022-04-11 09:35:06 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-04-14 08:53:13 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-04-11 09:35:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
2022-04-14 08:53:13 +00:00
|
|
|
"go.uber.org/zap"
|
2022-04-11 09:35:06 +00:00
|
|
|
)
|
|
|
|
|
2022-08-11 09:47:46 +00:00
|
|
|
const (
|
|
|
|
attributeFilePath = "FilePath"
|
|
|
|
)
|
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
// PutObjects handler that uploads object to NeoFS.
|
|
|
|
func (a *API) PutObjects(params operations.PutObjectParams, principal *models.Principal) middleware.Responder {
|
2022-04-11 11:18:26 +00:00
|
|
|
errorResponse := operations.NewPutObjectBadRequest()
|
2022-04-11 09:35:06 +00:00
|
|
|
ctx := params.HTTPRequest.Context()
|
|
|
|
|
2022-08-18 15:41:33 +00:00
|
|
|
bearerHeaders, err := prepareBearerTokenHeaders(params.XBearerSignature, params.XBearerSignatureKey, *params.WalletConnect, *params.FullBearer)
|
|
|
|
if err != nil {
|
|
|
|
resp := a.logAndGetErrorResponse("invalid bearer headers", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
btoken, err := getBearerToken(principal, bearerHeaders)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid bearer token", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cnrID cid.ID
|
2022-07-12 08:01:21 +00:00
|
|
|
if err = cnrID.DecodeString(*params.Object.ContainerID); err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid container id", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := base64.StdEncoding.DecodeString(params.Object.Payload)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid object payload", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prm := PrmAttributes{
|
|
|
|
DefaultTimestamp: a.defaultTimestamp,
|
|
|
|
DefaultFileName: *params.Object.FileName,
|
|
|
|
}
|
2022-04-20 14:52:41 +00:00
|
|
|
attributes, err := GetObjectAttributes(ctx, a.pool, params.Object.Attributes, prm)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("failed to get object attributes", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
owner := bearer.ResolveIssuer(btoken)
|
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
obj := object.New()
|
2022-07-12 08:01:21 +00:00
|
|
|
obj.SetContainerID(cnrID)
|
|
|
|
obj.SetOwnerID(&owner)
|
2022-04-11 09:35:06 +00:00
|
|
|
obj.SetPayload(payload)
|
|
|
|
obj.SetAttributes(attributes...)
|
|
|
|
|
|
|
|
var prmPut pool.PrmObjectPut
|
|
|
|
prmPut.SetHeader(*obj)
|
|
|
|
prmPut.UseBearer(btoken)
|
|
|
|
|
|
|
|
objID, err := a.pool.PutObject(ctx, prmPut)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("put object", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 08:30:34 +00:00
|
|
|
var resp models.Address
|
2022-04-11 09:35:06 +00:00
|
|
|
resp.ContainerID = params.Object.ContainerID
|
2022-04-29 06:39:24 +00:00
|
|
|
resp.ObjectID = util.NewString(objID.String())
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewPutObjectOK().
|
|
|
|
WithPayload(&resp).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 11:18:26 +00:00
|
|
|
// GetObjectInfo handler that get object info.
|
|
|
|
func (a *API) GetObjectInfo(params operations.GetObjectInfoParams, principal *models.Principal) middleware.Responder {
|
|
|
|
errorResponse := operations.NewGetObjectInfoBadRequest()
|
|
|
|
ctx := params.HTTPRequest.Context()
|
|
|
|
|
2022-04-14 08:53:13 +00:00
|
|
|
addr, err := parseAddress(params.ContainerID, params.ObjectID)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid address", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-11 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 15:41:33 +00:00
|
|
|
bearerHeaders, err := prepareBearerTokenHeaders(params.XBearerSignature, params.XBearerSignatureKey, *params.WalletConnect, *params.FullBearer)
|
|
|
|
if err != nil {
|
|
|
|
resp := a.logAndGetErrorResponse("invalid bearer headers", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
btoken, err := getBearerToken(principal, bearerHeaders)
|
2022-04-11 11:18:26 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("get bearer token", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-11 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var prm pool.PrmObjectHead
|
2022-07-12 08:01:21 +00:00
|
|
|
prm.SetAddress(addr)
|
2022-04-11 11:18:26 +00:00
|
|
|
prm.UseBearer(btoken)
|
|
|
|
|
|
|
|
objInfo, err := a.pool.HeadObject(ctx, prm)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("head object", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-11 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var resp models.ObjectInfo
|
2022-04-29 06:39:24 +00:00
|
|
|
resp.ContainerID = util.NewString(params.ContainerID)
|
|
|
|
resp.ObjectID = util.NewString(params.ObjectID)
|
|
|
|
resp.OwnerID = util.NewString(objInfo.OwnerID().String())
|
2022-04-11 11:18:26 +00:00
|
|
|
resp.Attributes = make([]*models.Attribute, len(objInfo.Attributes()))
|
2022-04-29 06:39:24 +00:00
|
|
|
resp.ObjectSize = util.NewInteger(int64(objInfo.PayloadSize()))
|
|
|
|
resp.PayloadSize = util.NewInteger(0)
|
2022-04-11 11:18:26 +00:00
|
|
|
|
|
|
|
for i, attr := range objInfo.Attributes() {
|
|
|
|
resp.Attributes[i] = &models.Attribute{
|
2022-04-29 06:39:24 +00:00
|
|
|
Key: util.NewString(attr.Key()),
|
|
|
|
Value: util.NewString(attr.Value()),
|
2022-04-11 11:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:49:06 +00:00
|
|
|
if objInfo.PayloadSize() == 0 {
|
|
|
|
return operations.NewGetObjectInfoOK().WithPayload(&resp)
|
|
|
|
}
|
2022-04-20 14:10:43 +00:00
|
|
|
|
2022-10-18 09:49:06 +00:00
|
|
|
offset, length, err := prepareOffsetLength(params, objInfo.PayloadSize())
|
|
|
|
if err != nil {
|
|
|
|
errResp := a.logAndGetErrorResponse("invalid range param", err)
|
|
|
|
return errorResponse.WithPayload(errResp)
|
2022-04-20 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if uint64(*params.MaxPayloadSize) < length {
|
|
|
|
return operations.NewGetObjectInfoOK().WithPayload(&resp)
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:49:06 +00:00
|
|
|
var prmRange pool.PrmObjectRange
|
|
|
|
prmRange.SetAddress(addr)
|
|
|
|
prmRange.UseBearer(btoken)
|
|
|
|
prmRange.SetOffset(offset)
|
|
|
|
prmRange.SetLength(length)
|
|
|
|
|
2022-04-20 14:10:43 +00:00
|
|
|
rangeRes, err := a.pool.ObjectRange(ctx, prmRange)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
errResp := a.logAndGetErrorResponse("range object", err)
|
|
|
|
return errorResponse.WithPayload(errResp)
|
2022-04-20 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err = rangeRes.Close(); err != nil {
|
|
|
|
a.log.Error("close range result", zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
sb := new(strings.Builder)
|
|
|
|
encoder := base64.NewEncoder(base64.StdEncoding, sb)
|
2022-08-25 08:24:10 +00:00
|
|
|
payloadSize, err := io.Copy(encoder, &rangeRes)
|
2022-04-20 14:10:43 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
errResp := a.logAndGetErrorResponse("encode object payload", err)
|
|
|
|
return errorResponse.WithPayload(errResp)
|
2022-04-20 14:10:43 +00:00
|
|
|
}
|
|
|
|
if err = encoder.Close(); err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
errResp := a.logAndGetErrorResponse("close encoder", err)
|
|
|
|
return errorResponse.WithPayload(errResp)
|
2022-04-20 14:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.Payload = sb.String()
|
2022-04-29 06:39:24 +00:00
|
|
|
resp.PayloadSize = util.NewInteger(payloadSize)
|
2022-04-20 14:10:43 +00:00
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewGetObjectInfoOK().
|
|
|
|
WithPayload(&resp).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-11 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 08:53:13 +00:00
|
|
|
// DeleteObject handler that removes object from NeoFS.
|
|
|
|
func (a *API) DeleteObject(params operations.DeleteObjectParams, principal *models.Principal) middleware.Responder {
|
|
|
|
errorResponse := operations.NewDeleteObjectBadRequest()
|
|
|
|
ctx := params.HTTPRequest.Context()
|
|
|
|
|
|
|
|
addr, err := parseAddress(params.ContainerID, params.ObjectID)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid address", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-14 08:53:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 15:41:33 +00:00
|
|
|
bearerHeaders, err := prepareBearerTokenHeaders(params.XBearerSignature, params.XBearerSignatureKey, *params.WalletConnect, *params.FullBearer)
|
|
|
|
if err != nil {
|
|
|
|
resp := a.logAndGetErrorResponse("invalid bearer headers", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
btoken, err := getBearerToken(principal, bearerHeaders)
|
2022-04-14 08:53:13 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("failed to get bearer token", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-14 08:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var prm pool.PrmObjectDelete
|
2022-07-12 08:01:21 +00:00
|
|
|
prm.SetAddress(addr)
|
2022-04-14 08:53:13 +00:00
|
|
|
prm.UseBearer(btoken)
|
|
|
|
|
|
|
|
if err = a.pool.DeleteObject(ctx, prm); err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("failed to delete object", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-14 08:53:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewDeleteObjectOK().
|
|
|
|
WithPayload(util.NewSuccessResponse()).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-14 08:53:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 08:30:34 +00:00
|
|
|
// SearchObjects handler that removes object from NeoFS.
|
|
|
|
func (a *API) SearchObjects(params operations.SearchObjectsParams, principal *models.Principal) middleware.Responder {
|
|
|
|
errorResponse := operations.NewSearchObjectsBadRequest()
|
|
|
|
ctx := params.HTTPRequest.Context()
|
|
|
|
|
|
|
|
var cnrID cid.ID
|
2022-07-12 08:01:21 +00:00
|
|
|
if err := cnrID.DecodeString(params.ContainerID); err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("invalid container id", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-18 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 15:41:33 +00:00
|
|
|
bearerHeaders, err := prepareBearerTokenHeaders(params.XBearerSignature, params.XBearerSignatureKey, *params.WalletConnect, *params.FullBearer)
|
|
|
|
if err != nil {
|
|
|
|
resp := a.logAndGetErrorResponse("invalid bearer headers", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
btoken, err := getBearerToken(principal, bearerHeaders)
|
2022-04-18 08:30:34 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("failed to get bearer token", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-18 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 06:39:24 +00:00
|
|
|
filters, err := util.ToNativeFilters(params.SearchFilters)
|
2022-04-18 08:30:34 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("failed to transform to native", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-18 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var prm pool.PrmObjectSearch
|
|
|
|
prm.SetContainerID(cnrID)
|
|
|
|
prm.UseBearer(btoken)
|
|
|
|
prm.SetFilters(filters)
|
|
|
|
|
|
|
|
resSearch, err := a.pool.SearchObjects(ctx, prm)
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("failed to search objects", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-18 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset := int(*params.Offset)
|
|
|
|
size := int(*params.Limit)
|
|
|
|
|
|
|
|
var iterateErr error
|
|
|
|
var obj *models.ObjectBaseInfo
|
|
|
|
var objects []*models.ObjectBaseInfo
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
err = resSearch.Iterate(func(id oid.ID) bool {
|
|
|
|
if i < offset {
|
|
|
|
i++
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
if obj, iterateErr = headObjectBaseInfo(ctx, a.pool, cnrID, id, btoken); iterateErr != nil {
|
2022-04-18 08:30:34 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
objects = append(objects, obj)
|
|
|
|
|
|
|
|
return len(objects) == size
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
err = iterateErr
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-07 12:59:38 +00:00
|
|
|
resp := a.logAndGetErrorResponse("failed to search objects", err)
|
|
|
|
return errorResponse.WithPayload(resp)
|
2022-04-18 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list := &models.ObjectList{
|
2022-04-29 06:39:24 +00:00
|
|
|
Size: util.NewInteger(int64(len(objects))),
|
2022-04-18 08:30:34 +00:00
|
|
|
Objects: objects,
|
|
|
|
}
|
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewSearchObjectsOK().
|
|
|
|
WithPayload(list).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-04-18 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
func headObjectBaseInfo(ctx context.Context, p *pool.Pool, cnrID cid.ID, objID oid.ID, btoken bearer.Token) (*models.ObjectBaseInfo, error) {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnrID)
|
|
|
|
addr.SetObject(objID)
|
2022-04-18 08:30:34 +00:00
|
|
|
|
|
|
|
var prm pool.PrmObjectHead
|
2022-07-12 08:01:21 +00:00
|
|
|
prm.SetAddress(addr)
|
2022-04-18 08:30:34 +00:00
|
|
|
prm.UseBearer(btoken)
|
|
|
|
|
|
|
|
objInfo, err := p.HeadObject(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &models.ObjectBaseInfo{
|
|
|
|
Address: &models.Address{
|
2022-04-29 06:39:24 +00:00
|
|
|
ContainerID: util.NewString(cnrID.String()),
|
|
|
|
ObjectID: util.NewString(objID.String()),
|
2022-04-18 08:30:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attr := range objInfo.Attributes() {
|
2022-08-11 09:47:46 +00:00
|
|
|
switch attr.Key() {
|
|
|
|
case object.AttributeFileName:
|
2022-04-18 08:30:34 +00:00
|
|
|
resp.Name = attr.Value()
|
2022-08-11 09:47:46 +00:00
|
|
|
case attributeFilePath:
|
|
|
|
resp.FilePath = attr.Value()
|
2022-04-18 08:30:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
func parseAddress(containerID, objectID string) (oid.Address, error) {
|
2022-04-14 08:53:13 +00:00
|
|
|
var cnrID cid.ID
|
2022-07-12 08:01:21 +00:00
|
|
|
if err := cnrID.DecodeString(containerID); err != nil {
|
|
|
|
return oid.Address{}, fmt.Errorf("invalid container id: %w", err)
|
2022-04-14 08:53:13 +00:00
|
|
|
}
|
|
|
|
var objID oid.ID
|
2022-07-12 08:01:21 +00:00
|
|
|
if err := objID.DecodeString(objectID); err != nil {
|
|
|
|
return oid.Address{}, fmt.Errorf("invalid object id: %w", err)
|
2022-04-14 08:53:13 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnrID)
|
|
|
|
addr.SetObject(objID)
|
2022-04-14 08:53:13 +00:00
|
|
|
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
2022-08-18 15:41:33 +00:00
|
|
|
type BearerTokenHeaders struct {
|
|
|
|
Signature string
|
|
|
|
Key string
|
|
|
|
IsWalletConnect bool
|
|
|
|
IsFullToken bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBearerToken(token *models.Principal, hdr *BearerTokenHeaders) (bearer.Token, error) {
|
2022-04-14 08:53:13 +00:00
|
|
|
bt := &BearerToken{
|
|
|
|
Token: string(*token),
|
2022-08-18 15:41:33 +00:00
|
|
|
Signature: hdr.Signature,
|
|
|
|
Key: hdr.Key,
|
2022-04-14 08:53:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 15:41:33 +00:00
|
|
|
return prepareBearerToken(bt, hdr.IsWalletConnect, hdr.IsFullToken)
|
2022-04-14 08:53:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 15:41:33 +00:00
|
|
|
func prepareBearerToken(bt *BearerToken, isWalletConnect, isFullToken bool) (bearer.Token, error) {
|
2022-04-15 07:03:00 +00:00
|
|
|
data, err := base64.StdEncoding.DecodeString(bt.Token)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-12 08:01:21 +00:00
|
|
|
return bearer.Token{}, fmt.Errorf("can't base64-decode bearer token: %w", err)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 15:41:33 +00:00
|
|
|
if isFullToken {
|
|
|
|
var btoken bearer.Token
|
|
|
|
if err = btoken.Unmarshal(data); err != nil {
|
|
|
|
return bearer.Token{}, fmt.Errorf("couldn't unmarshall bearer token: %w", err)
|
|
|
|
}
|
|
|
|
if !btoken.VerifySignature() {
|
|
|
|
return bearer.Token{}, fmt.Errorf("invalid signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
return btoken, nil
|
|
|
|
}
|
|
|
|
|
2022-06-16 09:12:26 +00:00
|
|
|
signature, err := hex.DecodeString(bt.Signature)
|
2022-04-11 09:35:06 +00:00
|
|
|
if err != nil {
|
2022-07-12 08:01:21 +00:00
|
|
|
return bearer.Token{}, fmt.Errorf("couldn't decode bearer signature: %w", err)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ownerKey, err := keys.NewPublicKeyFromString(bt.Key)
|
|
|
|
if err != nil {
|
2022-07-12 08:01:21 +00:00
|
|
|
return bearer.Token{}, fmt.Errorf("couldn't fetch bearer token owner key: %w", err)
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
body := new(acl.BearerTokenBody)
|
|
|
|
if err = body.Unmarshal(data); err != nil {
|
2022-08-18 15:41:33 +00:00
|
|
|
return bearer.Token{}, fmt.Errorf("can't unmarshal bearer token body: %w", err)
|
2022-04-15 07:03:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
v2signature := new(refs.Signature)
|
|
|
|
v2signature.SetScheme(refs.ECDSA_SHA512)
|
2022-04-15 07:03:00 +00:00
|
|
|
if isWalletConnect {
|
2022-07-12 08:01:21 +00:00
|
|
|
v2signature.SetScheme(refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT)
|
2022-04-15 07:03:00 +00:00
|
|
|
}
|
2022-04-11 09:35:06 +00:00
|
|
|
v2signature.SetSign(signature)
|
|
|
|
v2signature.SetKey(ownerKey.Bytes())
|
|
|
|
|
2022-07-12 08:01:21 +00:00
|
|
|
var v2btoken acl.BearerToken
|
|
|
|
v2btoken.SetBody(body)
|
|
|
|
v2btoken.SetSignature(v2signature)
|
|
|
|
|
|
|
|
var btoken bearer.Token
|
|
|
|
if err = btoken.ReadFromV2(v2btoken); err != nil {
|
|
|
|
return bearer.Token{}, fmt.Errorf("read from v2 token: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !btoken.VerifySignature() {
|
|
|
|
return bearer.Token{}, fmt.Errorf("invalid signature")
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:03:00 +00:00
|
|
|
return btoken, nil
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
2022-10-18 09:49:06 +00:00
|
|
|
|
|
|
|
func prepareOffsetLength(params operations.GetObjectInfoParams, objSize uint64) (uint64, uint64, error) {
|
|
|
|
var offset, length uint64
|
|
|
|
if params.RangeOffset != nil || params.RangeLength != nil {
|
|
|
|
if params.RangeOffset == nil || params.RangeLength == nil {
|
|
|
|
return 0, 0, errors.New("both offset and length must be provided")
|
|
|
|
}
|
|
|
|
offset = uint64(*params.RangeOffset)
|
|
|
|
length = uint64(*params.RangeLength)
|
|
|
|
} else {
|
|
|
|
length = objSize
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset >= objSize {
|
|
|
|
return 0, 0, fmt.Errorf("offset '%d' must be less than object size '%d'", offset, objSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset+length > objSize {
|
|
|
|
return 0, 0, fmt.Errorf("end of range '%d' must be less or equal object size '%d'", offset+length, objSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset, length, nil
|
|
|
|
}
|