-
Notifications
You must be signed in to change notification settings - Fork 0
/
override.go
70 lines (66 loc) · 1.99 KB
/
override.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package gcfaccess
import (
"crypto/rand"
"encoding/json"
"net/http"
"storj.io/uplink"
)
func OverrideEncryption(w http.ResponseWriter, r *http.Request) {
if handleCORS(w, r) {
return
}
var request struct {
AccessGrant string `json:"access_grant"`
Passphrase string `json:"passphrase"`
SaltBytes []byte `json:"salt"`
Path uplink.SharePrefix `json:"path"`
}
// input parsing and validation
if parseBodyJson(w, r, &request) {
return
}
access, err := uplink.ParseAccess(request.AccessGrant)
if err != nil {
http.Error(w, "Error while parsing access grant", http.StatusBadRequest)
return
}
if len(request.Path.Bucket) == 0 || len(request.Path.Prefix) == 0 {
http.Error(w, "Error: no or invalid path specified", http.StatusBadRequest)
return
}
if len(request.Passphrase) == 0 {
http.Error(w, "Error: no passphrase specified", http.StatusBadRequest)
return
}
if len(request.SaltBytes) == 0 {
request.SaltBytes = make([]byte, 32)
_, err = rand.Read(request.SaltBytes)
if err != nil {
http.Error(w, "Error generating salt bytes", http.StatusInternalServerError)
return
}
}
saltedUserKey, err := uplink.DeriveEncryptionKey(request.Passphrase, request.SaltBytes)
if err != nil {
http.Error(w, "Error deriving salted encryption key", http.StatusInternalServerError)
return
}
err = access.OverrideEncryptionKey(request.Path.Bucket, request.Path.Prefix, saltedUserKey)
if err != nil {
http.Error(w, "Error overriding encryption key", http.StatusInternalServerError)
return
}
var response struct {
AccessGrant string `json:"access_grant"`
}
serializedAccess, err := access.Serialize()
if err != nil {
http.Error(w, "Error while serializing new access grant", http.StatusInternalServerError)
return
}
response.AccessGrant = serializedAccess
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(response)
}