diff --git a/auth/center.go b/auth/center.go index 5c533254..c8f8bfad 100644 --- a/auth/center.go +++ b/auth/center.go @@ -3,16 +3,16 @@ package auth import ( "bytes" "crypto/ecdsa" - "crypto/rsa" - "encoding/hex" "io/ioutil" "net/http" "regexp" "strings" - "github.com/klauspost/compress/zstd" "github.com/nspcc-dev/neofs-api-go/refs" "github.com/nspcc-dev/neofs-api-go/service" + "github.com/nspcc-dev/neofs-authmate/accessbox/hcs" + "github.com/nspcc-dev/neofs-authmate/gates" + manager "github.com/nspcc-dev/neofs-authmate/neofsmanager" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/pkg/errors" "go.uber.org/zap" @@ -24,37 +24,28 @@ const emptyStringSHA256 = `e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca4959 // Center is a central app's authentication/authorization management unit. type Center struct { - log *zap.Logger - submatcher *regexpSubmatcher - zstdEncoder *zstd.Encoder - zstdDecoder *zstd.Decoder - neofsKeys struct { + log *zap.Logger + submatcher *regexpSubmatcher + neofsKeys struct { PrivateKey *ecdsa.PrivateKey PublicKey *ecdsa.PublicKey } - ownerID refs.OwnerID - wifString string - userAuthKeys struct { - PrivateKey *rsa.PrivateKey - PublicKey *rsa.PublicKey - } + ownerID refs.OwnerID + wifString string + manager *manager.Manager + authKeys *hcs.X25519Keys } // NewCenter creates an instance of AuthCenter. -func NewCenter(log *zap.Logger) (*Center, error) { - zstdEncoder, err := zstd.NewWriter(nil) +func NewCenter(log *zap.Logger, neofsNodeAddress string) (*Center, error) { + m, err := manager.NewManager(neofsNodeAddress) if err != nil { - return nil, errors.Wrap(err, "failed to create zstd encoder") - } - zstdDecoder, err := zstd.NewReader(nil) - if err != nil { - return nil, errors.Wrap(err, "failed to create zstd decoder") + return nil, err } return &Center{ - log: log, - submatcher: ®expSubmatcher{re: authorizationFieldRegexp}, - zstdEncoder: zstdEncoder, - zstdDecoder: zstdDecoder, + log: log, + submatcher: ®expSubmatcher{re: authorizationFieldRegexp}, + manager: m, }, nil } @@ -91,9 +82,13 @@ func (center *Center) GetWIFString() string { return center.wifString } -func (center *Center) SetUserAuthKeys(key *rsa.PrivateKey) { - center.userAuthKeys.PrivateKey = key - center.userAuthKeys.PublicKey = &key.PublicKey +func (center *Center) SetUserAuthKeys(key hcs.X25519PrivateKey) error { + keys, err := hcs.NewKeys(key) + if err != nil { + return err + } + center.authKeys = keys + return nil } func (center *Center) AuthenticationPassed(request *http.Request) (*service.BearerTokenMsg, error) { @@ -153,51 +148,31 @@ func (center *Center) AuthenticationPassed(request *http.Request) (*service.Bear return bearerToken, nil } -func (center *Center) packBearerToken(bearerToken *service.BearerTokenMsg) (string, string, error) { - data, err := bearerToken.Marshal() - if err != nil { - return "", "", errors.Wrap(err, "failed to marshal bearer token") - } - encryptedKeyID, err := encrypt(center.userAuthKeys.PublicKey, center.compress(data)) - if err != nil { - return "", "", errors.Wrap(err, "failed to encrypt bearer token bytes") - } - accessKeyID := hex.EncodeToString(encryptedKeyID) - secretAccessKey := hex.EncodeToString(sha256Hash(data)) - return accessKeyID, secretAccessKey, nil -} - -func (center *Center) unpackBearerToken(accessKeyID string) (*service.BearerTokenMsg, string, error) { - encryptedKeyID, err := hex.DecodeString(accessKeyID) - if err != nil { - return nil, "", errors.Wrap(err, "failed to decode HEX string") - } - compressedKeyID, err := decrypt(center.userAuthKeys.PrivateKey, encryptedKeyID) - if err != nil { - return nil, "", errors.Wrap(err, "failed to decrypt key ID") - } - data, err := center.decompress(compressedKeyID) - if err != nil { - return nil, "", errors.Wrap(err, "failed to decompress key ID") - } - bearerToken := new(service.BearerTokenMsg) - if err := bearerToken.Unmarshal(data); err != nil { - return nil, "", errors.Wrap(err, "failed to unmarshal embedded bearer token") - } - secretAccessKey := hex.EncodeToString(sha256Hash(data)) - return bearerToken, secretAccessKey, nil -} - -// FIXME: Reimplement this method right after finilizign a new auth scheme. func (center *Center) fetchBearerToken(accessKeyID string) (*service.BearerTokenMsg, string, error) { // TODO: Turn it into getting bearer token from NeoFS node later on. - accessKeyID = "051d729a102513387b63f2f07a5cd45ca3158b273646527916cc3f705fa0006b249e038f4e4b4986d85b18358da8692819ef6e35063e91efce17da32d956ad9e48d2674f0cab2bd5ff27b49cb9a1b0e71eb73d330b6cd8f23e85252e55afe992765b2983ee2bafc57079221fdc8e48a3f5d8b0be87a259fd12c6afd59e3ec748c8677a4211c8d6ec7b67008a006f526b22a8536effe8ecb6581ea16d7f9358ede53dddf36fb589ab9a829b81d9c69d19a4b9d1ac58d5e9311e3608eb233475bfc47a02633c5611d1bb3b9450ef00b2490924be5f3375e3eb4b6ed2f23906f183c213e77c19ec5c62b3be3a5a5b526851ea674c613b542c2861fd4a5178b65d8df8899a05b25db8b9e15f5e257467044e41b69144fe4b802aff2a56c8960e5e3c7eb004d41a6d873f927db43ed047171f36cda5be080e0df2ddfe15edb423b41491559a7a5cc70932566cdd71058913abb0a68d3c1ac50586f0a48b02e1cea24ca8a2010d5495dbc5daf0575413c06542a16288664104ad06289a5b2bf119071171562dcdc05d6b9260df2fc676540fc1d836c36f77a090cc5ce52b930f74ae625d53e1a80b7a59aa2d85e2f188c131de5739bf0e49e6d5f081e757f2b0d85a8264dfb66c4400bcd4727d7c21eca92c138d975fe51f986e80ec32ba13e8850c82dd813cd45640caa303555e0759d0d111dac6cc39cbe711dd56dc8f01a6022635" - secretAccessKey := "12775ab859000dd87b3c7586146f465efc73bbbd33ac3b36f2ab2b061df15f7b" - bearerToken, _, err := center.unpackBearerToken(accessKeyID) - if err != nil { - return nil, "", errors.Wrap(err, "failed to fetch bearer token") + // accessKeyID = "051d729a102513387b63f2f07a5cd45ca3158b273646527916cc3f705fa0006b249e038f4e4b4986d85b18358da8692819ef6e35063e91efce17da32d956ad9e48d2674f0cab2bd5ff27b49cb9a1b0e71eb73d330b6cd8f23e85252e55afe992765b2983ee2bafc57079221fdc8e48a3f5d8b0be87a259fd12c6afd59e3ec748c8677a4211c8d6ec7b67008a006f526b22a8536effe8ecb6581ea16d7f9358ede53dddf36fb589ab9a829b81d9c69d19a4b9d1ac58d5e9311e3608eb233475bfc47a02633c5611d1bb3b9450ef00b2490924be5f3375e3eb4b6ed2f23906f183c213e77c19ec5c62b3be3a5a5b526851ea674c613b542c2861fd4a5178b65d8df8899a05b25db8b9e15f5e257467044e41b69144fe4b802aff2a56c8960e5e3c7eb004d41a6d873f927db43ed047171f36cda5be080e0df2ddfe15edb423b41491559a7a5cc70932566cdd71058913abb0a68d3c1ac50586f0a48b02e1cea24ca8a2010d5495dbc5daf0575413c06542a16288664104ad06289a5b2bf119071171562dcdc05d6b9260df2fc676540fc1d836c36f77a090cc5ce52b930f74ae625d53e1a80b7a59aa2d85e2f188c131de5739bf0e49e6d5f081e757f2b0d85a8264dfb66c4400bcd4727d7c21eca92c138d975fe51f986e80ec32ba13e8850c82dd813cd45640caa303555e0759d0d111dac6cc39cbe711dd56dc8f01a6022635" + // secretAccessKey := "12775ab859000dd87b3c7586146f465efc73bbbd33ac3b36f2ab2b061df15f7b" + // bearerToken, _, err := center.unpackBearerToken(accessKeyID) + // if err != nil { + // return nil, "", errors.Wrap(err, "failed to fetch bearer token") + // } + akid := new(refs.Address) + if err := akid.Parse(accessKeyID); err != nil { + return nil, "", errors.Wrap(err, "failed to parse access key id as refs.Address") } - return bearerToken, secretAccessKey, nil + config := &gates.ObtainingConfig{ + BaseConfig: gates.BaseConfig{ + OperationalCredentials: nil, + Manager: nil, + }, + GateKeys: center.authKeys, + SecretAddress: akid, + } + res, err := gates.ObtainSecret(config) + if err != nil { + return nil, "", errors.Wrap(err, "failed to obtain secret") + } + return res.BearerToken, res.SecretAccessKey, nil } // TODO: Make this write into a smart buffer backed by a file on a fast drive. @@ -213,11 +188,3 @@ func readAndKeepBody(request *http.Request) (*bytes.Reader, error) { request.Body = ioutil.NopCloser(bytes.NewReader(payload)) return bytes.NewReader(payload), nil } - -func (center *Center) compress(data []byte) []byte { - return center.zstdEncoder.EncodeAll(data, make([]byte, 0, len(data))) -} - -func (center *Center) decompress(data []byte) ([]byte, error) { - return center.zstdDecoder.DecodeAll(data, nil) -} diff --git a/auth/rsa-utils.go b/auth/rsa-utils.go deleted file mode 100644 index cd1e7562..00000000 --- a/auth/rsa-utils.go +++ /dev/null @@ -1,42 +0,0 @@ -package auth - -import ( - "crypto/rand" - "crypto/rsa" - "crypto/sha256" - "crypto/x509" - "encoding/pem" - "io/ioutil" - - "github.com/pkg/errors" -) - -func ReadRSAPrivateKeyFromPEMFile(filePath string) (*rsa.PrivateKey, error) { - kbs, err := ioutil.ReadFile(filePath) - if err != nil { - return nil, errors.Wrapf(err, "failed to read file %s", filePath) - } - pemBlock, _ := pem.Decode(kbs) - if pemBlock == nil { - return nil, errors.Errorf("failed to decode PEM data from file %s", filePath) - } - rsaKey, err := x509.ParsePKCS1PrivateKey(pemBlock.Bytes) - if err != nil { - return nil, errors.Wrapf(err, "failed to parse private key bytes from pem data from file %s", filePath) - } - return rsaKey, nil -} - -func encrypt(key *rsa.PublicKey, data []byte) ([]byte, error) { - return rsa.EncryptOAEP(sha256.New(), rand.Reader, key, data, []byte{}) -} - -func decrypt(key *rsa.PrivateKey, data []byte) ([]byte, error) { - return rsa.DecryptOAEP(sha256.New(), rand.Reader, key, data, []byte{}) -} - -func sha256Hash(data []byte) []byte { - hash := sha256.New() - hash.Write(data) - return hash.Sum(nil) -}