2020-07-15 13:48:25 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-08-06 15:23:01 +00:00
|
|
|
"context"
|
2021-05-20 10:14:17 +00:00
|
|
|
"errors"
|
2020-08-19 13:28:17 +00:00
|
|
|
"fmt"
|
2020-11-27 12:31:39 +00:00
|
|
|
"io"
|
2020-07-16 15:33:47 +00:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2020-07-20 23:43:40 +00:00
|
|
|
"strings"
|
2020-08-06 15:23:01 +00:00
|
|
|
"time"
|
2020-07-15 13:48:25 +00:00
|
|
|
|
2020-10-13 09:33:33 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2020-08-06 15:23:01 +00:00
|
|
|
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
|
2021-06-24 15:21:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-11-24 06:59:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-08-09 08:53:58 +00:00
|
|
|
apiErrors "github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2021-06-21 10:54:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
2021-06-14 13:39:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/tokens"
|
2021-05-28 20:48:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pkg/pool"
|
2020-07-20 17:23:16 +00:00
|
|
|
"go.uber.org/zap"
|
2020-07-15 13:48:25 +00:00
|
|
|
)
|
|
|
|
|
2021-07-16 11:16:11 +00:00
|
|
|
// authorizationFieldRegexp -- is regexp for credentials with Base58 encoded cid and oid and '0' (zero) as delimiter.
|
|
|
|
var authorizationFieldRegexp = regexp.MustCompile(`AWS4-HMAC-SHA256 Credential=(?P<access_key_id_cid>[^/]+)0(?P<access_key_id_oid>[^/]+)/(?P<date>[^/]+)/(?P<region>[^/]*)/(?P<service>[^/]+)/aws4_request,\s*SignedHeaders=(?P<signed_header_fields>.+),\s*Signature=(?P<v4_signature>.+)`)
|
2020-07-16 15:33:47 +00:00
|
|
|
|
2020-10-13 09:33:33 +00:00
|
|
|
type (
|
2021-05-13 20:25:31 +00:00
|
|
|
// Center is a user authentication interface.
|
2020-11-24 06:59:01 +00:00
|
|
|
Center interface {
|
2021-07-16 12:35:07 +00:00
|
|
|
Authenticate(request *http.Request) (*accessbox.Box, error)
|
2020-11-24 06:59:01 +00:00
|
|
|
}
|
2020-07-20 17:23:16 +00:00
|
|
|
|
2020-11-24 06:59:01 +00:00
|
|
|
center struct {
|
|
|
|
reg *regexpSubmatcher
|
2021-06-14 13:39:25 +00:00
|
|
|
cli tokens.Credentials
|
2020-07-21 10:21:03 +00:00
|
|
|
}
|
2020-07-15 20:16:27 +00:00
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// Params stores node connection parameters.
|
2020-10-13 09:33:33 +00:00
|
|
|
Params struct {
|
2021-06-16 14:07:31 +00:00
|
|
|
Pool pool.Pool
|
|
|
|
Logger *zap.Logger
|
2020-10-13 09:33:33 +00:00
|
|
|
}
|
2020-11-27 12:31:39 +00:00
|
|
|
|
|
|
|
prs int
|
2020-10-13 09:33:33 +00:00
|
|
|
)
|
|
|
|
|
2021-06-15 15:50:00 +00:00
|
|
|
// ErrNoAuthorizationHeader is returned for unauthenticated requests.
|
2021-06-11 16:29:55 +00:00
|
|
|
var ErrNoAuthorizationHeader = errors.New("no authorization header")
|
|
|
|
|
2020-11-27 12:31:39 +00:00
|
|
|
func (p prs) Read(_ []byte) (n int, err error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p prs) Seek(_ int64, _ int) (int64, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ io.ReadSeeker = prs(0)
|
|
|
|
|
2020-10-13 09:33:33 +00:00
|
|
|
// New creates an instance of AuthCenter.
|
2021-06-24 15:21:34 +00:00
|
|
|
func New(conns pool.Pool, key *keys.PrivateKey) Center {
|
2020-11-24 06:59:01 +00:00
|
|
|
return ¢er{
|
2021-06-14 13:39:25 +00:00
|
|
|
cli: tokens.New(conns, key),
|
2020-10-13 09:33:33 +00:00
|
|
|
reg: ®expSubmatcher{re: authorizationFieldRegexp},
|
2020-11-24 06:59:01 +00:00
|
|
|
}
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
func (c *center) Authenticate(r *http.Request) (*accessbox.Box, error) {
|
2020-11-24 06:59:01 +00:00
|
|
|
queryValues := r.URL.Query()
|
2020-07-24 14:03:02 +00:00
|
|
|
if queryValues.Get("X-Amz-Algorithm") == "AWS4-HMAC-SHA256" {
|
|
|
|
return nil, errors.New("pre-signed form of request is not supported")
|
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
|
|
|
authHeaderField := r.Header["Authorization"]
|
2020-07-24 14:03:02 +00:00
|
|
|
if len(authHeaderField) != 1 {
|
2021-06-11 16:29:55 +00:00
|
|
|
return nil, ErrNoAuthorizationHeader
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
|
|
|
sms1 := c.reg.getSubmatches(authHeaderField[0])
|
2020-08-19 13:28:17 +00:00
|
|
|
if len(sms1) != 7 {
|
2021-08-09 08:53:58 +00:00
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrAuthorizationHeaderMalformed)
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2020-07-24 14:03:02 +00:00
|
|
|
signedHeaderFieldsNames := strings.Split(sms1["signed_header_fields"], ";")
|
|
|
|
if len(signedHeaderFieldsNames) == 0 {
|
|
|
|
return nil, errors.New("wrong format of signed headers part")
|
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
|
|
|
signatureDateTime, err := time.Parse("20060102T150405Z", r.Header.Get("X-Amz-Date"))
|
2020-08-06 15:23:01 +00:00
|
|
|
if err != nil {
|
2021-05-20 10:14:17 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse x-amz-date header field: %w", err)
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2021-07-16 11:16:11 +00:00
|
|
|
accessKeyID := fmt.Sprintf("%s0%s", sms1["access_key_id_cid"], sms1["access_key_id_oid"])
|
2021-06-02 18:53:20 +00:00
|
|
|
accessKeyAddress := fmt.Sprintf("%s/%s", sms1["access_key_id_cid"], sms1["access_key_id_oid"])
|
2020-11-24 06:59:01 +00:00
|
|
|
|
|
|
|
address := object.NewAddress()
|
2021-06-02 18:53:20 +00:00
|
|
|
if err = address.Parse(accessKeyAddress); err != nil {
|
2021-05-20 10:14:17 +00:00
|
|
|
return nil, fmt.Errorf("could not parse AccessBox address: %s : %w", accessKeyID, err)
|
2020-11-24 06:59:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
box, err := c.cli.GetBox(r.Context(), address)
|
2020-07-24 14:03:02 +00:00
|
|
|
if err != nil {
|
2020-11-24 06:59:01 +00:00
|
|
|
return nil, err
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
|
|
|
otherRequest := r.Clone(context.TODO())
|
2021-02-01 16:59:42 +00:00
|
|
|
otherRequest.Header = make(http.Header)
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2021-02-01 16:59:42 +00:00
|
|
|
for key, val := range r.Header {
|
|
|
|
for _, name := range signedHeaderFieldsNames {
|
|
|
|
if strings.EqualFold(key, name) {
|
|
|
|
otherRequest.Header[key] = val
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
awsCreds := credentials.NewStaticCredentials(accessKeyID, box.Gate.AccessKey, "")
|
2020-08-06 15:23:01 +00:00
|
|
|
signer := v4.NewSigner(awsCreds)
|
2021-02-01 16:59:42 +00:00
|
|
|
signer.DisableURIPathEscaping = true
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2020-11-27 12:31:39 +00:00
|
|
|
// body not required
|
|
|
|
if _, err := signer.Sign(otherRequest, nil, sms1["service"], sms1["region"], signatureDateTime); err != nil {
|
2021-05-20 10:14:17 +00:00
|
|
|
return nil, fmt.Errorf("failed to sign temporary HTTP request: %w", err)
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2020-11-27 12:31:39 +00:00
|
|
|
sms2 := c.reg.getSubmatches(otherRequest.Header.Get("Authorization"))
|
2020-08-06 15:23:01 +00:00
|
|
|
if sms1["v4_signature"] != sms2["v4_signature"] {
|
2021-08-09 08:53:58 +00:00
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrSignatureDoesNotMatch)
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
return box, nil
|
2020-07-16 15:33:47 +00:00
|
|
|
}
|