2020-07-15 13:48:25 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/service"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Center is a central app's authentication/authorization management unit.
|
|
|
|
type Center struct {
|
2020-07-15 15:20:45 +00:00
|
|
|
enclave *secureEnclave
|
2020-07-15 13:48:25 +00:00
|
|
|
zstdEncoder *zstd.Encoder
|
|
|
|
zstdDecoder *zstd.Decoder
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
// NewCenter creates an instance of AuthCenter.
|
|
|
|
func NewCenter(pathToRSAKey, pathToECDSAKey string) (*Center, error) {
|
2020-07-15 13:48:25 +00:00
|
|
|
zstdEncoder, _ := zstd.NewWriter(nil)
|
|
|
|
zstdDecoder, _ := zstd.NewReader(nil)
|
2020-07-15 15:20:45 +00:00
|
|
|
enclave, err := newSecureEnclave(pathToRSAKey, pathToECDSAKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create secure enclave")
|
|
|
|
}
|
|
|
|
center := &Center{
|
|
|
|
enclave: enclave,
|
|
|
|
zstdEncoder: zstdEncoder,
|
|
|
|
zstdDecoder: zstdDecoder,
|
|
|
|
}
|
|
|
|
return center, nil
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
func (center *Center) PackBearerToken(bearerToken *service.BearerTokenMsg) ([]byte, error) {
|
2020-07-15 13:48:25 +00:00
|
|
|
data, err := bearerToken.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal bearer token")
|
|
|
|
}
|
2020-07-15 15:20:45 +00:00
|
|
|
encryptedKeyID, err := center.enclave.Encrypt(gateUserAuthKey, center.compress(data))
|
2020-07-15 13:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "")
|
|
|
|
}
|
|
|
|
return append(sha256Hash(data), encryptedKeyID...), nil
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
func (center *Center) UnpackBearerToken(packedBearerToken []byte) (*service.BearerTokenMsg, error) {
|
2020-07-15 13:48:25 +00:00
|
|
|
compressedKeyID := packedBearerToken[32:]
|
2020-07-15 15:20:45 +00:00
|
|
|
encryptedKeyID, err := center.decompress(compressedKeyID)
|
2020-07-15 13:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decompress key ID")
|
|
|
|
}
|
2020-07-15 15:20:45 +00:00
|
|
|
keyID, err := center.enclave.Decrypt(gateUserAuthKey, encryptedKeyID)
|
2020-07-15 13:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decrypt key ID")
|
|
|
|
}
|
|
|
|
bearerToken := new(service.BearerTokenMsg)
|
|
|
|
if err := bearerToken.Unmarshal(keyID); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to unmarshal embedded bearer token")
|
|
|
|
}
|
|
|
|
return bearerToken, nil
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
func (center *Center) compress(data []byte) []byte {
|
|
|
|
center.zstdEncoder.Reset(nil)
|
2020-07-15 13:48:25 +00:00
|
|
|
var compressedData []byte
|
2020-07-15 15:20:45 +00:00
|
|
|
center.zstdEncoder.EncodeAll(data, compressedData)
|
2020-07-15 13:48:25 +00:00
|
|
|
return compressedData
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:20:45 +00:00
|
|
|
func (center *Center) decompress(data []byte) ([]byte, error) {
|
|
|
|
center.zstdDecoder.Reset(nil)
|
2020-07-15 13:48:25 +00:00
|
|
|
var decompressedData []byte
|
2020-07-15 15:20:45 +00:00
|
|
|
if _, err := center.zstdDecoder.DecodeAll(data, decompressedData); err != nil {
|
2020-07-15 13:48:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return decompressedData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sha256Hash(data []byte) []byte {
|
|
|
|
hash := sha256.New()
|
|
|
|
hash.Write(data)
|
|
|
|
return hash.Sum(nil)
|
|
|
|
}
|