-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
73 lines (62 loc) · 1.57 KB
/
auth.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
71
72
73
package bfsp
import (
"context"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"time"
)
type encTokenInfo struct {
Token string `json:"token"`
EncMasterKey string `json:"encrypted_master_key"`
}
type TokenInfo struct {
Token string
MasterKey MasterKey
}
func GetToken(bigCentralURL string, dlToken string, rsaPrivKey *rsa.PrivateKey) (*TokenInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
apiDLTokenURL := bigCentralURL + "/api/v1/dl_token?t=" + dlToken
for {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiDLTokenURL, http.NoBody)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
switch resp.StatusCode {
case 404:
case 200:
var encryptedDLTokenInfo encTokenInfo
decoder := json.NewDecoder(resp.Body)
decoder.Decode(&encryptedDLTokenInfo)
encMasterKeyBin, err := base64.URLEncoding.DecodeString(encryptedDLTokenInfo.EncMasterKey)
if err != nil {
return nil, err
}
masterKey, err := rsaPrivKey.Decrypt(rand.Reader, encMasterKeyBin, nil)
if err != nil {
return nil, err
}
return &TokenInfo{
Token: encryptedDLTokenInfo.Token,
MasterKey: masterKey,
}, nil
default:
return nil, fmt.Errorf("status code %d from server", resp.StatusCode)
}
time.Sleep(1 * time.Second)
}
}
func injectAuth(msg *FileServerMessage, token string) {
msg.Auth = &FileServerMessage_Authentication{
Token: token,
}
}