2020-07-15 13:48:25 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-07-20 17:23:16 +00:00
|
|
|
"bytes"
|
2020-08-06 15:23:01 +00:00
|
|
|
"context"
|
2020-11-24 06:59:01 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2020-08-19 13:28:17 +00:00
|
|
|
"fmt"
|
2020-07-15 20:16:27 +00:00
|
|
|
"io/ioutil"
|
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"
|
2020-11-24 06:59:01 +00:00
|
|
|
sdk "github.com/nspcc-dev/cdn-neofs-sdk"
|
2020-11-24 16:31:57 +00:00
|
|
|
"github.com/nspcc-dev/cdn-neofs-sdk/creds/bearer"
|
2020-11-24 06:59:01 +00:00
|
|
|
"github.com/nspcc-dev/cdn-neofs-sdk/creds/hcs"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2020-10-13 09:33:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2020-07-15 13:48:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-07-20 17:23:16 +00:00
|
|
|
"go.uber.org/zap"
|
2020-07-15 13:48:25 +00:00
|
|
|
)
|
|
|
|
|
2020-08-19 13:28:17 +00:00
|
|
|
var authorizationFieldRegexp = regexp.MustCompile(`AWS4-HMAC-SHA256 Credential=(?P<access_key_id_cid>[^/]+)/(?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 (
|
2020-11-24 06:59:01 +00:00
|
|
|
Center interface {
|
|
|
|
Authenticate(request *http.Request) (*token.BearerToken, error)
|
|
|
|
}
|
2020-07-20 17:23:16 +00:00
|
|
|
|
2020-11-24 06:59:01 +00:00
|
|
|
center struct {
|
|
|
|
reg *regexpSubmatcher
|
2020-11-24 16:31:57 +00:00
|
|
|
cli bearer.Credentials
|
2020-07-21 10:21:03 +00:00
|
|
|
}
|
2020-07-15 20:16:27 +00:00
|
|
|
|
2020-10-13 09:33:33 +00:00
|
|
|
Params struct {
|
2020-11-24 06:59:01 +00:00
|
|
|
Client sdk.Client
|
|
|
|
Logger *zap.Logger
|
|
|
|
Credential hcs.Credentials
|
2020-10-13 09:33:33 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// New creates an instance of AuthCenter.
|
2020-11-24 16:31:57 +00:00
|
|
|
func New(obj sdk.ObjectClient, key hcs.PrivateKey) Center {
|
2020-11-24 06:59:01 +00:00
|
|
|
return ¢er{
|
2020-11-24 16:31:57 +00:00
|
|
|
cli: bearer.New(obj, 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
|
|
|
}
|
|
|
|
|
2020-11-24 06:59:01 +00:00
|
|
|
func (c *center) Authenticate(r *http.Request) (*token.BearerToken, error) {
|
|
|
|
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 {
|
|
|
|
return nil, errors.New("unsupported request: wrong length of Authorization header field")
|
|
|
|
}
|
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 {
|
2020-07-24 14:03:02 +00:00
|
|
|
return nil, errors.New("bad Authorization header field")
|
|
|
|
}
|
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 {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse x-amz-date header field")
|
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2020-08-19 13:28:17 +00:00
|
|
|
accessKeyID := 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()
|
|
|
|
if err = address.Parse(accessKeyID); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not parse AccessBox address: %s", accessKeyID)
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:31:57 +00:00
|
|
|
tkn, err := c.cli.Get(r.Context(), address)
|
2020-11-24 09:17:36 +00:00
|
|
|
if err != nil {
|
2020-11-24 06:59:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-24 09:17:36 +00:00
|
|
|
data, err := tkn.Marshal()
|
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
|
|
|
|
|
|
|
hash := sha256.Sum256(data)
|
|
|
|
secret := hex.EncodeToString(hash[:])
|
|
|
|
|
|
|
|
otherRequest := r.Clone(context.TODO())
|
2020-08-06 15:23:01 +00:00
|
|
|
otherRequest.Header = map[string][]string{}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
|
|
|
for hfn, hfvs := range r.Header {
|
2020-08-06 15:23:01 +00:00
|
|
|
for _, shfn := range signedHeaderFieldsNames {
|
|
|
|
if strings.EqualFold(hfn, shfn) {
|
|
|
|
otherRequest.Header[hfn] = hfvs
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
|
|
|
awsCreds := credentials.NewStaticCredentials(accessKeyID, secret, "")
|
2020-08-06 15:23:01 +00:00
|
|
|
signer := v4.NewSigner(awsCreds)
|
2020-11-24 06:59:01 +00:00
|
|
|
|
|
|
|
body, err := readAndKeepBody(r)
|
2020-08-06 15:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to read out request body")
|
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
|
|
|
hdr, err := signer.Sign(otherRequest, body, sms1["service"], sms1["region"], signatureDateTime)
|
2020-08-06 15:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to sign temporary HTTP request")
|
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
|
|
|
sms2 := c.reg.getSubmatches(hdr.Get("Authorization"))
|
2020-08-06 15:23:01 +00:00
|
|
|
if sms1["v4_signature"] != sms2["v4_signature"] {
|
|
|
|
return nil, errors.Wrap(err, "failed to pass authentication procedure")
|
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2020-11-24 09:17:36 +00:00
|
|
|
return tkn, nil
|
2020-07-16 15:33:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:40:46 +00:00
|
|
|
// TODO: Make this write into a smart buffer backed by a file on a fast drive.
|
2020-07-20 23:43:40 +00:00
|
|
|
func readAndKeepBody(request *http.Request) (*bytes.Reader, error) {
|
2020-07-20 17:23:16 +00:00
|
|
|
if request.Body == nil {
|
2020-07-20 23:43:40 +00:00
|
|
|
var r bytes.Reader
|
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
payload, err := ioutil.ReadAll(request.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-20 17:23:16 +00:00
|
|
|
}
|
|
|
|
request.Body = ioutil.NopCloser(bytes.NewReader(payload))
|
2020-07-20 23:43:40 +00:00
|
|
|
return bytes.NewReader(payload), nil
|
2020-07-20 17:23:16 +00:00
|
|
|
}
|