2020-07-15 13:48:25 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-07-20 17:23:16 +00:00
|
|
|
"bytes"
|
2020-07-20 23:43:40 +00:00
|
|
|
"context"
|
2020-07-15 20:16:27 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rsa"
|
2020-07-20 17:23:16 +00:00
|
|
|
"encoding/hex"
|
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"
|
|
|
|
"time"
|
2020-07-15 13:48:25 +00:00
|
|
|
|
2020-07-20 23:43:40 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
|
2020-07-15 13:48:25 +00:00
|
|
|
"github.com/klauspost/compress/zstd"
|
2020-07-15 20:16:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/refs"
|
2020-07-15 13:48:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/service"
|
2020-07-15 20:16:27 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
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-07-21 08:35:30 +00:00
|
|
|
var authorizationFieldRegexp = regexp.MustCompile(`AWS4-HMAC-SHA256 Credential=(?P<access_key_id>[^/]+)/(?P<date>[^/]+)/(?P<region>[^/]*)/(?P<service>[^/]+)/aws4_request, SignedHeaders=(?P<signed_header_fields>.*), Signature=(?P<v4_signature>.*)`)
|
2020-07-16 15:33:47 +00:00
|
|
|
|
2020-07-20 17:23:16 +00:00
|
|
|
const emptyStringSHA256 = `e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`
|
|
|
|
|
2020-07-15 13:48:25 +00:00
|
|
|
// Center is a central app's authentication/authorization management unit.
|
|
|
|
type Center struct {
|
2020-07-20 17:23:16 +00:00
|
|
|
log *zap.Logger
|
2020-07-16 15:33:47 +00:00
|
|
|
submatcher *regexpSubmatcher
|
2020-07-15 13:48:25 +00:00
|
|
|
zstdEncoder *zstd.Encoder
|
|
|
|
zstdDecoder *zstd.Decoder
|
2020-07-15 20:16:27 +00:00
|
|
|
neofsKeys struct {
|
|
|
|
PrivateKey *ecdsa.PrivateKey
|
|
|
|
PublicKey *ecdsa.PublicKey
|
|
|
|
}
|
|
|
|
ownerID refs.OwnerID
|
|
|
|
wifString string
|
|
|
|
userAuthKeys struct {
|
|
|
|
PrivateKey *rsa.PrivateKey
|
|
|
|
PublicKey *rsa.PublicKey
|
|
|
|
}
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
// NewCenter creates an instance of AuthCenter.
|
2020-07-20 17:23:16 +00:00
|
|
|
func NewCenter(log *zap.Logger) *Center {
|
2020-07-15 13:48:25 +00:00
|
|
|
zstdEncoder, _ := zstd.NewWriter(nil)
|
|
|
|
zstdDecoder, _ := zstd.NewReader(nil)
|
2020-07-15 20:16:27 +00:00
|
|
|
return &Center{
|
2020-07-20 17:23:16 +00:00
|
|
|
log: log,
|
2020-07-21 08:35:30 +00:00
|
|
|
submatcher: ®expSubmatcher{re: authorizationFieldRegexp},
|
2020-07-15 15:20:45 +00:00
|
|
|
zstdEncoder: zstdEncoder,
|
|
|
|
zstdDecoder: zstdDecoder,
|
|
|
|
}
|
2020-07-15 20:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (center *Center) SetNeoFSKeys(key *ecdsa.PrivateKey) error {
|
|
|
|
publicKey := &key.PublicKey
|
|
|
|
oid, err := refs.NewOwnerID(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get OwnerID")
|
|
|
|
}
|
|
|
|
center.neofsKeys.PrivateKey = key
|
|
|
|
wif, err := crypto.WIFEncode(key)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get WIF string from given key")
|
|
|
|
}
|
|
|
|
center.neofsKeys.PublicKey = publicKey
|
|
|
|
center.ownerID = oid
|
|
|
|
center.wifString = wif
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:44:25 +00:00
|
|
|
func (center *Center) GetNeoFSPrivateKey() *ecdsa.PrivateKey {
|
2020-07-15 20:16:27 +00:00
|
|
|
return center.neofsKeys.PrivateKey
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:44:25 +00:00
|
|
|
func (center *Center) GetNeoFSPublicKey() *ecdsa.PublicKey {
|
2020-07-15 20:16:27 +00:00
|
|
|
return center.neofsKeys.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (center *Center) GetOwnerID() refs.OwnerID {
|
|
|
|
return center.ownerID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (center *Center) GetWIFString() string {
|
|
|
|
return center.wifString
|
|
|
|
}
|
|
|
|
|
|
|
|
func (center *Center) SetUserAuthKeys(key *rsa.PrivateKey) {
|
|
|
|
center.userAuthKeys.PrivateKey = key
|
|
|
|
center.userAuthKeys.PublicKey = &key.PublicKey
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:23:16 +00:00
|
|
|
func (center *Center) packBearerToken(bearerToken *service.BearerTokenMsg) (string, string, error) {
|
2020-07-15 13:48:25 +00:00
|
|
|
data, err := bearerToken.Marshal()
|
|
|
|
if err != nil {
|
2020-07-20 17:23:16 +00:00
|
|
|
return "", "", errors.Wrap(err, "failed to marshal bearer token")
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
2020-07-15 20:16:27 +00:00
|
|
|
encryptedKeyID, err := encrypt(center.userAuthKeys.PublicKey, center.compress(data))
|
2020-07-15 13:48:25 +00:00
|
|
|
if err != nil {
|
2020-07-20 17:23:16 +00:00
|
|
|
return "", "", errors.Wrap(err, "failed to encrypt bearer token bytes")
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
2020-07-20 17:23:16 +00:00
|
|
|
accessKeyID := hex.EncodeToString(encryptedKeyID)
|
|
|
|
secretAccessKey := hex.EncodeToString(sha256Hash(data))
|
|
|
|
return accessKeyID, secretAccessKey, nil
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 23:43:40 +00:00
|
|
|
func (center *Center) unpackBearerToken(accessKeyID string) (*service.BearerTokenMsg, string, error) {
|
2020-07-20 17:23:16 +00:00
|
|
|
encryptedKeyID, err := hex.DecodeString(accessKeyID)
|
2020-07-15 13:48:25 +00:00
|
|
|
if err != nil {
|
2020-07-20 23:43:40 +00:00
|
|
|
return nil, "", errors.Wrap(err, "failed to decode HEX string")
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
2020-07-20 17:23:16 +00:00
|
|
|
compressedKeyID, err := decrypt(center.userAuthKeys.PrivateKey, encryptedKeyID)
|
2020-07-15 13:48:25 +00:00
|
|
|
if err != nil {
|
2020-07-20 23:43:40 +00:00
|
|
|
return nil, "", errors.Wrap(err, "failed to decrypt key ID")
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
2020-07-20 17:23:16 +00:00
|
|
|
data, err := center.decompress(compressedKeyID)
|
|
|
|
if err != nil {
|
2020-07-20 23:43:40 +00:00
|
|
|
return nil, "", errors.Wrap(err, "failed to decompress key ID")
|
2020-07-20 17:23:16 +00:00
|
|
|
}
|
2020-07-15 13:48:25 +00:00
|
|
|
bearerToken := new(service.BearerTokenMsg)
|
2020-07-20 17:23:16 +00:00
|
|
|
if err := bearerToken.Unmarshal(data); err != nil {
|
2020-07-20 23:43:40 +00:00
|
|
|
return nil, "", errors.Wrap(err, "failed to unmarshal embedded bearer token")
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
2020-07-20 23:43:40 +00:00
|
|
|
secretAccessKey := hex.EncodeToString(sha256Hash(data))
|
|
|
|
return bearerToken, secretAccessKey, nil
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:23:16 +00:00
|
|
|
func (center *Center) AuthenticationPassed(request *http.Request) (*service.BearerTokenMsg, error) {
|
2020-07-20 23:43:40 +00:00
|
|
|
queryValues := request.URL.Query()
|
|
|
|
if queryValues.Get("X-Amz-Algorithm") == "AWS4-HMAC-SHA256" {
|
|
|
|
return nil, errors.New("pre-signed form of request is not supported")
|
|
|
|
}
|
2020-07-20 17:23:16 +00:00
|
|
|
authHeaderField := request.Header["Authorization"]
|
2020-07-16 15:33:47 +00:00
|
|
|
if len(authHeaderField) != 1 {
|
2020-07-20 17:23:16 +00:00
|
|
|
return nil, errors.New("unsupported request: wrong length of Authorization header field")
|
2020-07-16 15:33:47 +00:00
|
|
|
}
|
2020-07-20 23:43:40 +00:00
|
|
|
sms1 := center.submatcher.getSubmatches(authHeaderField[0])
|
|
|
|
if len(sms1) != 6 {
|
2020-07-16 15:33:47 +00:00
|
|
|
return nil, errors.New("bad Authorization header field")
|
|
|
|
}
|
2020-07-20 23:43:40 +00:00
|
|
|
signedHeaderFieldsNames := strings.Split(sms1["signed_header_fields"], ";")
|
|
|
|
if len(signedHeaderFieldsNames) == 0 {
|
|
|
|
return nil, errors.New("wrong format of signed headers part")
|
|
|
|
}
|
|
|
|
signatureDateTime, err := time.Parse("20060102T150405Z", request.Header.Get("X-Amz-Date"))
|
2020-07-16 15:33:47 +00:00
|
|
|
if err != nil {
|
2020-07-21 09:28:00 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to parse x-amz-date header field")
|
2020-07-20 23:43:40 +00:00
|
|
|
}
|
|
|
|
accessKeyID := sms1["access_key_id"]
|
|
|
|
bearerToken, secretAccessKey, err := center.unpackBearerToken(accessKeyID)
|
|
|
|
if err != nil {
|
2020-07-21 10:07:09 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to unpack bearer token")
|
2020-07-16 15:33:47 +00:00
|
|
|
}
|
2020-07-20 23:43:40 +00:00
|
|
|
otherRequest := request.Clone(context.TODO())
|
|
|
|
otherRequest.Header = map[string][]string{}
|
|
|
|
for hfn, hfvs := range request.Header {
|
|
|
|
for _, shfn := range signedHeaderFieldsNames {
|
|
|
|
if strings.EqualFold(hfn, shfn) {
|
|
|
|
otherRequest.Header[hfn] = hfvs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
awsCreds := credentials.NewStaticCredentials(accessKeyID, secretAccessKey, "")
|
|
|
|
signer := v4.NewSigner(awsCreds)
|
|
|
|
body, err := readAndKeepBody(request)
|
|
|
|
if err != nil {
|
2020-07-21 09:28:00 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to read out request body")
|
2020-07-20 23:43:40 +00:00
|
|
|
}
|
|
|
|
_, err = signer.Sign(otherRequest, body, sms1["service"], sms1["region"], signatureDateTime)
|
|
|
|
if err != nil {
|
2020-07-21 09:28:00 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to sign temporary HTTP request")
|
2020-07-20 23:43:40 +00:00
|
|
|
}
|
|
|
|
sms2 := center.submatcher.getSubmatches(otherRequest.Header.Get("Authorization"))
|
|
|
|
if sms1["v4_signature"] != sms2["v4_signature"] {
|
2020-07-21 10:07:09 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to pass authentication procedure")
|
2020-07-20 23:43:40 +00:00
|
|
|
}
|
|
|
|
return bearerToken, 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
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
func (center *Center) compress(data []byte) []byte {
|
2020-07-20 17:23:16 +00:00
|
|
|
return center.zstdEncoder.EncodeAll(data, make([]byte, 0, len(data)))
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
func (center *Center) decompress(data []byte) ([]byte, error) {
|
2020-07-20 17:23:16 +00:00
|
|
|
return center.zstdDecoder.DecodeAll(data, nil)
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|