-
Notifications
You must be signed in to change notification settings - Fork 390
/
access.go
50 lines (42 loc) · 1.34 KB
/
access.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package consolewasm
import (
"encoding/base64"
"storj.io/common/encryption"
"storj.io/common/grant"
"storj.io/common/macaroon"
"storj.io/common/storj"
)
// GenAccessGrant creates a new access grant and returns it serialized form.
func GenAccessGrant(satelliteNodeURL, apiKey, encryptionPassphrase, base64EncodedSalt string) (string, error) {
parsedAPIKey, err := macaroon.ParseAPIKey(apiKey)
if err != nil {
return "", err
}
key, err := DeriveRootKey(encryptionPassphrase, base64EncodedSalt)
if err != nil {
return "", err
}
encAccess := grant.NewEncryptionAccessWithDefaultKey(key)
encAccess.SetDefaultPathCipher(storj.EncAESGCM)
encAccess.LimitTo(parsedAPIKey)
accessString, err := (&grant.Access{
SatelliteAddress: satelliteNodeURL,
APIKey: parsedAPIKey,
EncAccess: encAccess,
}).Serialize()
if err != nil {
return "", err
}
return accessString, nil
}
// DeriveRootKey derives the root key portion of the access grant.
func DeriveRootKey(encryptionPassphrase, base64EncodedSalt string) (*storj.Key, error) {
const concurrency = 8
saltBytes, err := base64.StdEncoding.DecodeString(base64EncodedSalt)
if err != nil {
return nil, err
}
return encryption.DeriveRootKey([]byte(encryptionPassphrase), saltBytes, "", concurrency)
}