2020-11-11 13:45:03 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2020-12-14 16:33:33 +00:00
|
|
|
package consolewasm
|
2020-11-11 13:45:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
|
|
|
|
"storj.io/common/encryption"
|
2021-03-25 22:57:25 +00:00
|
|
|
"storj.io/common/grant"
|
2020-11-11 13:45:03 +00:00
|
|
|
"storj.io/common/macaroon"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GenAccessGrant creates a new access grant and returns it serialized form.
|
|
|
|
func GenAccessGrant(satelliteNodeURL, apiKey, encryptionPassphrase, projectID string) (string, error) {
|
|
|
|
parsedAPIKey, err := macaroon.ParseAPIKey(apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-02-14 20:06:35 +00:00
|
|
|
key, err := DeriveRootKey(encryptionPassphrase, projectID)
|
2020-11-11 13:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-03-25 22:57:25 +00:00
|
|
|
encAccess := grant.NewEncryptionAccessWithDefaultKey(key)
|
2020-11-11 13:45:03 +00:00
|
|
|
encAccess.SetDefaultPathCipher(storj.EncAESGCM)
|
2021-02-11 22:52:32 +00:00
|
|
|
encAccess.LimitTo(parsedAPIKey)
|
|
|
|
|
2021-03-25 22:57:25 +00:00
|
|
|
accessString, err := (&grant.Access{
|
2020-11-11 13:45:03 +00:00
|
|
|
SatelliteAddress: satelliteNodeURL,
|
|
|
|
APIKey: parsedAPIKey,
|
|
|
|
EncAccess: encAccess,
|
2021-02-11 22:52:32 +00:00
|
|
|
}).Serialize()
|
2020-11-11 13:45:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return accessString, nil
|
|
|
|
}
|
2022-02-14 20:06:35 +00:00
|
|
|
|
|
|
|
// DeriveRootKey derives the root key portion of the access grant.
|
|
|
|
func DeriveRootKey(encryptionPassphrase, projectID string) (*storj.Key, error) {
|
|
|
|
id, err := uuid.FromString(projectID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const concurrency = 8
|
|
|
|
salt := sha256.Sum256(id[:])
|
|
|
|
|
|
|
|
return encryption.DeriveRootKey([]byte(encryptionPassphrase), salt[:], "", concurrency)
|
|
|
|
}
|