Skip to content

Commit a171b44

Browse files
committed
refactoring, update GetToken to new big_central
1 parent 01a7d37 commit a171b44

File tree

5 files changed

+117
-66
lines changed

5 files changed

+117
-66
lines changed

auth.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,63 @@ package bfsp
22

33
import (
44
"context"
5+
"crypto/rand"
6+
"crypto/rsa"
7+
"encoding/base64"
8+
"encoding/json"
59
"fmt"
6-
"io"
710
"net/http"
811
"time"
912
)
1013

11-
func GetDLToken(bigCentralURL string, dlToken string) (string, error) {
14+
type encTokenInfo struct {
15+
Token string `json:"token"`
16+
EncMasterKey string `json:"encrypted_master_key"`
17+
}
18+
19+
type TokenInfo struct {
20+
Token string
21+
MasterKey MasterKey
22+
}
23+
24+
func GetToken(bigCentralURL string, dlToken string, rsaPrivKey *rsa.PrivateKey) (*TokenInfo, error) {
1225
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
1326
defer cancel()
1427

1528
apiDLTokenURL := bigCentralURL + "/api/v1/dl_token?t=" + dlToken
1629
for {
1730
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiDLTokenURL, http.NoBody)
1831
if err != nil {
19-
return "", err
32+
return nil, err
2033
}
2134
resp, err := http.DefaultClient.Do(req)
2235
if err != nil {
23-
return "", err
36+
return nil, err
2437
}
2538
defer resp.Body.Close()
2639

2740
switch resp.StatusCode {
2841
case 404:
2942
case 200:
30-
respBin, err := io.ReadAll(resp.Body)
43+
var encryptedDLTokenInfo encTokenInfo
44+
decoder := json.NewDecoder(resp.Body)
45+
decoder.Decode(&encryptedDLTokenInfo)
46+
encMasterKeyBin, err := base64.URLEncoding.DecodeString(encryptedDLTokenInfo.EncMasterKey)
47+
if err != nil {
48+
return nil, err
49+
}
50+
masterKey, err := rsaPrivKey.Decrypt(rand.Reader, encMasterKeyBin, nil)
3151
if err != nil {
32-
return "", err
52+
return nil, err
3353
}
34-
resp := string(respBin)
3554

36-
return resp, nil
55+
return &TokenInfo{
56+
Token: encryptedDLTokenInfo.Token,
57+
MasterKey: masterKey,
58+
}, nil
3759

3860
default:
39-
return "", fmt.Errorf("status code %d from server", resp.StatusCode)
61+
return nil, fmt.Errorf("status code %d from server", resp.StatusCode)
4062
}
4163

4264
time.Sleep(1 * time.Second)

bfsp.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli.go

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package bfsp
22

33
import (
4+
"context"
45
"crypto/rand"
56
"encoding/base64"
67

78
"github.com/biscuit-auth/biscuit-go/v2"
89
"github.com/biscuit-auth/biscuit-go/v2/parser"
910
"github.com/google/uuid"
1011
"github.com/klauspost/compress/zstd"
11-
"golang.org/x/crypto/chacha20poly1305"
1212
"google.golang.org/protobuf/proto"
1313
"lukechampine.com/blake3"
1414
)
@@ -22,64 +22,16 @@ type EncryptedCompressedChunk struct {
2222
chunk []byte
2323
}
2424

25-
func CompressEncryptChunk(chunkBytes []byte, chunkMetadata *ChunkMetadata, fileId string, masterKey MasterKey) (*EncryptedCompressedChunk, error) {
26-
zstdEncoder, err := zstd.NewWriter(nil)
27-
if err != nil {
28-
return nil, err
29-
}
30-
defer zstdEncoder.Close()
25+
type clientContextKeyType struct{}
3126

32-
compressedChunkBytes := zstdEncoder.EncodeAll(chunkBytes, nil)
33-
34-
fileUUID := uuid.MustParse(fileId)
35-
fileUUIDBin, err := fileUUID.MarshalBinary()
36-
fileKeyBytes := masterKey[:]
37-
fileKeyBytes = append(fileKeyBytes, fileUUIDBin...)
38-
fileKey := blake3.Sum256(fileKeyBytes)
27+
var clientContextKey = clientContextKeyType{}
3928

40-
enc, err := chacha20poly1305.NewX(fileKey[:])
41-
if err != nil {
42-
return nil, err
43-
}
44-
encryptedChunkBytes := enc.Seal(nil, chunkMetadata.Nonce, compressedChunkBytes, []byte(chunkMetadata.Id))
45-
46-
return &EncryptedCompressedChunk{
47-
chunk: encryptedChunkBytes,
48-
}, nil
29+
func ContextWithClient(ctx context.Context, cli FileServerClient) context.Context {
30+
return context.WithValue(ctx, clientContextKey, cli)
4931
}
5032

51-
func CompressEncryptChunkMetadata(chunkMetadata *ChunkMetadata, fileId string, masterKey MasterKey) ([]byte, error) {
52-
zstdEncoder, err := zstd.NewWriter(nil)
53-
if err != nil {
54-
return nil, err
55-
}
56-
defer zstdEncoder.Close()
57-
58-
b, err := proto.Marshal(chunkMetadata)
59-
compressedChunkBytes := zstdEncoder.EncodeAll(b, nil)
60-
61-
fileUUID := uuid.MustParse(fileId)
62-
fileUUIDBin, err := fileUUID.MarshalBinary()
63-
fileKeyBytes := masterKey[:]
64-
fileKeyBytes = append(fileKeyBytes, fileUUIDBin...)
65-
fileKey := blake3.Sum256(fileKeyBytes)
66-
67-
enc, err := chacha20poly1305.NewX(fileKey[:])
68-
if err != nil {
69-
return nil, err
70-
}
71-
chunkMetaUUID, err := uuid.Parse(chunkMetadata.Id)
72-
if err != nil {
73-
return nil, err
74-
}
75-
nonce, err := chunkMetaUUID.MarshalBinary()
76-
if err != nil {
77-
return nil, err
78-
}
79-
nonce = append(nonce, make([]byte, 24-len(nonce))...)
80-
81-
encryptedChunkMetaBytes := enc.Seal(nil, nonce, compressedChunkBytes, chunkMetaUUID[:])
82-
return encryptedChunkMetaBytes, nil
33+
func ClientFromContext(ctx context.Context) FileServerClient {
34+
return ctx.Value(clientContextKey).(FileServerClient)
8335
}
8436

8537
func ShareFile(fileMeta *FileMetadata, tokenStr string, masterKey MasterKey) (*ViewFileInfo, error) {

enc.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package bfsp
22

33
import (
4+
"context"
45
"encoding/base64"
56

7+
"github.com/google/uuid"
8+
"github.com/klauspost/compress/zstd"
69
"golang.org/x/crypto/argon2"
10+
"golang.org/x/crypto/chacha20poly1305"
11+
"google.golang.org/protobuf/proto"
712
"lukechampine.com/blake3"
813
)
914

@@ -27,3 +32,75 @@ func CreateMasterEncKey(password string) (MasterKey, error) {
2732

2833
return masterKey[:], nil
2934
}
35+
36+
func CompressEncryptChunk(chunkBytes []byte, chunkMetadata *ChunkMetadata, fileId string, masterKey MasterKey) (*EncryptedCompressedChunk, error) {
37+
zstdEncoder, err := zstd.NewWriter(nil)
38+
if err != nil {
39+
return nil, err
40+
}
41+
defer zstdEncoder.Close()
42+
43+
compressedChunkBytes := zstdEncoder.EncodeAll(chunkBytes, nil)
44+
45+
fileUUID := uuid.MustParse(fileId)
46+
fileUUIDBin, err := fileUUID.MarshalBinary()
47+
fileKeyBytes := masterKey[:]
48+
fileKeyBytes = append(fileKeyBytes, fileUUIDBin...)
49+
fileKey := blake3.Sum256(fileKeyBytes)
50+
51+
enc, err := chacha20poly1305.NewX(fileKey[:])
52+
if err != nil {
53+
return nil, err
54+
}
55+
encryptedChunkBytes := enc.Seal(nil, chunkMetadata.Nonce, compressedChunkBytes, []byte(chunkMetadata.Id))
56+
57+
return &EncryptedCompressedChunk{
58+
chunk: encryptedChunkBytes,
59+
}, nil
60+
}
61+
62+
func CompressEncryptChunkMetadata(chunkMetadata *ChunkMetadata, fileId string, masterKey MasterKey) ([]byte, error) {
63+
zstdEncoder, err := zstd.NewWriter(nil)
64+
if err != nil {
65+
return nil, err
66+
}
67+
defer zstdEncoder.Close()
68+
69+
b, err := proto.Marshal(chunkMetadata)
70+
compressedChunkBytes := zstdEncoder.EncodeAll(b, nil)
71+
72+
fileUUID := uuid.MustParse(fileId)
73+
fileUUIDBin, err := fileUUID.MarshalBinary()
74+
fileKeyBytes := masterKey[:]
75+
fileKeyBytes = append(fileKeyBytes, fileUUIDBin...)
76+
fileKey := blake3.Sum256(fileKeyBytes)
77+
78+
enc, err := chacha20poly1305.NewX(fileKey[:])
79+
if err != nil {
80+
return nil, err
81+
}
82+
chunkMetaUUID, err := uuid.Parse(chunkMetadata.Id)
83+
if err != nil {
84+
return nil, err
85+
}
86+
nonce, err := chunkMetaUUID.MarshalBinary()
87+
if err != nil {
88+
return nil, err
89+
}
90+
nonce = append(nonce, make([]byte, 24-len(nonce))...)
91+
92+
encryptedChunkMetaBytes := enc.Seal(nil, nonce, compressedChunkBytes, chunkMetaUUID[:])
93+
return encryptedChunkMetaBytes, nil
94+
}
95+
96+
type keyContextKeyType struct{}
97+
98+
var keyContextKey = keyContextKeyType{}
99+
100+
func ContextWithMasterKey(ctx context.Context, masterKey MasterKey) context.Context {
101+
return context.WithValue(ctx, keyContextKey, masterKey)
102+
}
103+
104+
func MasterKeyFromContext(ctx context.Context) MasterKey {
105+
return ctx.Value(keyContextKey).(MasterKey)
106+
}

http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (cli *httpClient) sendFileServerMessage(msg isFileServerMessage_Message, re
7575
// the first 4 bytes are the length of the message in uint32_le, we'll ignore that for now
7676
body = body[4:]
7777

78-
// i <3 generics
78+
// i <3 interfaces
7979
err = proto.Unmarshal(body, resp)
8080
if err != nil {
8181
return err

0 commit comments

Comments
 (0)