forked from dilutedev/doppler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.go
142 lines (119 loc) · 3.44 KB
/
share.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package doppler
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"golang.org/x/crypto/pbkdf2"
)
const (
algorithm = "aes-256-gcm"
keyLength = 256 / 8
ivLength = 12
saltLength = 16
saltRounds = 100000
authTagLength = 16
)
type PlainTextArgs struct {
Secret string `json:"secret"`
ExpireViews int32 `json:"expire_views"`
ExpireDays int32 `json:"expire_days"`
}
type PlainTextResp struct {
Url string `json:"url"`
AuthenticatedUrl string `json:"authenticated_url"`
Password string `json:"password"`
Success bool `json:"success"`
}
/*
Generate a Doppler Share link by sending a plain text secret. This endpoint is not end-to-end encrypted as you are sending the secret in plain text. At no point do we store the plain text secret or the password in our systems. The receive flow the user goes through will be end-to-end encrypted where the encrypted secret will be decrypted on the browser.
*/
func (dp *doppler) SharePlainTextSecret(args PlainTextArgs) (data *PlainTextResp, err error) {
payload, err := json.Marshal(args)
if err != nil {
return
}
req, _ := http.NewRequest(
http.MethodPost,
"/v1/share/secrets/plain",
bytes.NewReader(payload))
body, err := dp.makeApiRequest(req)
if err != nil {
return
}
data = new(PlainTextResp)
err = json.Unmarshal(body, &data)
return
}
type EncryptedSecretArgs struct {
Secret string `json:"secret"`
ExpireViews int32 `json:"expire_views"`
ExpireDays int32 `json:"expire_days"`
}
/*
Generate a Doppler Share link by sending an encrypted secret. The receive flow the user goes through will be end-to-end encrypted where the encrypted secret will be decrypted on the browser.
*/
func (dp *doppler) ShareEncryptedSecret(args interface{}) (data interface{}, err error) {
payload, err := json.Marshal(args)
if err != nil {
return
}
req, _ := http.NewRequest(
http.MethodPost,
"/v1/share/secrets/encrypted",
bytes.NewReader(payload))
body, err := dp.makeApiRequest(req)
if err != nil {
return
}
data = new(interface{})
err = json.Unmarshal(body, &data)
return
}
/*
Go implementation of the Encryption Example here: https://docs.doppler.com/reference/share-secret-encrypted
*/
func EncryptSecret(secret string) (map[string]interface{}, error) {
password := make([]byte, 32)
_, err := rand.Read(password)
if err != nil {
return nil, err
}
passwordHex := fmt.Sprintf("%x", password)
hashedPassword := fmt.Sprintf("%x", sha256.Sum256([]byte(passwordHex)))
salt := make([]byte, saltLength)
_, err = rand.Read(salt)
if err != nil {
return nil, err
}
key := pbkdf2.Key([]byte(passwordHex), salt, saltRounds, keyLength, sha256.New)
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
iv := make([]byte, ivLength)
_, err = rand.Read(iv)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
encryptedData := aesgcm.Seal(nil, iv, []byte(secret), nil)
encrypted_secret := append(append(salt, iv...), encryptedData...)
return map[string]interface{}{
"encrypted_secret": base64.StdEncoding.EncodeToString(encrypted_secret),
"password": passwordHex,
"hashed_password": hashedPassword,
"expire_views": 1,
"expire_days": 1,
"encryption_kdf": "pbkdf2",
"encryption_salt_rounds": 1000000},
nil
}