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 (
|
2022-09-13 14:12:14 +01:00
|
|
|
"encoding/base64"
|
2020-11-11 13:45:03 +00:00
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GenAccessGrant creates a new access grant and returns it serialized form.
|
2022-09-13 14:12:14 +01:00
|
|
|
func GenAccessGrant(satelliteNodeURL, apiKey, encryptionPassphrase, base64EncodedSalt string) (string, error) {
|
2020-11-11 13:45:03 +00:00
|
|
|
parsedAPIKey, err := macaroon.ParseAPIKey(apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-09-13 14:12:14 +01:00
|
|
|
key, err := DeriveRootKey(encryptionPassphrase, base64EncodedSalt)
|
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.
|
2022-09-13 14:12:14 +01:00
|
|
|
func DeriveRootKey(encryptionPassphrase, base64EncodedSalt string) (*storj.Key, error) {
|
|
|
|
const concurrency = 8
|
|
|
|
saltBytes, err := base64.StdEncoding.DecodeString(base64EncodedSalt)
|
2022-02-14 20:06:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-13 14:12:14 +01:00
|
|
|
return encryption.DeriveRootKey([]byte(encryptionPassphrase), saltBytes, "", concurrency)
|
2022-02-14 20:06:35 +00:00
|
|
|
}
|