-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
336 lines (259 loc) · 8.45 KB
/
server.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"bytes"
"crypto/rand"
"embed"
b64 "encoding/base64"
"encoding/json"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/google/uuid"
"golang.org/x/crypto/chacha20poly1305"
)
// -------------------------------------------------------------------------------
// Custom Types used in the API
// -------------------------------------------------------------------------------
// IDResponse is used to indicate the client where his new note is created/located
type IDResponse struct {
ID string `json:"id"`
}
// -------------------------------------------------------------------------------
// Global variables
// -------------------------------------------------------------------------------
// aws s3 bucket name
const bucketName = "burning-letter"
// build aws session, assumption is that aws credentials are in environment
// check aws docs for more info
var sess = session.Must(session.NewSession(&aws.Config{
Region: aws.String("eu-central-1"),
},
))
// interface to s3 build upon previously created session
var svc = s3.New(sess)
// -------------------------------------------------------------------------------
// Utility functions
// -------------------------------------------------------------------------------
// dclose is used to log errors on defered closing
func dclose(f io.Closer) {
if err := f.Close(); err != nil {
log.Print(err.Error())
}
}
// generateSecret is generating a pseudo-random 32 byte key
func generateSecret() []byte {
key := make([]byte, chacha20poly1305.KeySize)
if _, err := rand.Read(key); err != nil {
panic(err)
}
return key
}
func deleteNoteFromAWS(id string) {
_, err := svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(id),
})
if err != nil {
log.Println(err.Error())
}
}
// notes are only allowed to persist for max 3 days (72h)
func isValidExpiration(expiration time.Duration) bool {
return expiration <= time.Hour*72
}
// -------------------------------------------------------------------------------
// HTTP Handler functions
// -------------------------------------------------------------------------------
// handleGetNote retreives a note from amazon and requests removing the note (TODO)
// It does not decrypt the message. We want to give the user the option to decrypt
// his own messages.
// url param for uuid: /notes/{id}
// output:
// - 404, if invalid uuid or already cleaned up uuid is provided
// - 500, aws answer is not able to fit buffer
// - 200, returns content of message as plain text
func handleGetNote(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
id := chi.URLParam(r, "id")
resp, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(id),
})
if err != nil {
http.Error(w, "Key not found", http.StatusNotFound)
return
}
defer dclose(r.Body)
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
if err != nil {
log.Print(err.Error())
http.Error(w, "Could not read aws response into buffer", http.StatusInternalServerError)
return
}
defer deleteNoteFromAWS(id)
fmt.Fprint(w, b64.URLEncoding.EncodeToString(buf.Bytes()))
}
// handleCreateNote is encrypting a note and saving the result to s3
// a fresh uuid is generated for every note therefore making guessing
// of notes infeasible
// outputs:
// - 400, no body therefore no note to create
// - 400, invalid json provided (e.g syntax error)
// - 400, message or secret is not b64 encoded
// - 400, encryption was not successful (most likely because of invalid key e.g not 32 byte)
// - 500, upload to s3 failed
// - 500, marshaling failed for id response (unlikely unless aws provides weird answer)
// - 200, json which includes generated uuid for note
func handleCreateNote(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
http.Error(w, "No Body was attached", http.StatusBadRequest)
return
}
defer dclose(r.Body)
dec := json.NewDecoder(r.Body)
var data Note
err := dec.Decode(&data)
if err != nil {
log.Print(err.Error())
http.Error(w, "Invalid json provided", http.StatusBadRequest)
return
}
expiresIn, err := time.ParseDuration(data.DeleteAfter)
if err != nil || !isValidExpiration(expiresIn) {
http.Error(w, "invalid expiration time given", http.StatusBadRequest)
return
}
// Secret is b64 decoded, revert for encryption
data.Secret, err = b64.URLEncoding.DecodeString(data.SecretB64)
if err != nil {
http.Error(w, "secret is not a valid base64 encryption", http.StatusBadRequest)
return
}
id := uuid.New().String()
err = data.encryptMessage()
if err != nil {
http.Error(w, "encryption was not successful", http.StatusBadRequest)
return
}
_, err = svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(id),
Body: bytes.NewReader(data.Message),
})
if err != nil {
log.Print(err.Error())
http.Error(w, "Upload to s3 failed", http.StatusInternalServerError)
}
out, err := json.Marshal(&IDResponse{ID: id})
if err != nil {
log.Print(err.Error())
http.Error(w, "Could not build response", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(out)
if err != nil {
log.Print(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go func() {
log.Println("started timer")
time.Sleep(time.Second)
deleteNoteFromAWS("random")
log.Println("deleted")
}()
}
// offer a handler for options to handle browser option requests
func handleOptions(w http.ResponseWriter, r *http.Request) {
// never expose server to public
}
// handleRandomKey write a randomly generated 32 byte key
// keys are not stored at any time
// output, base64 string
func handleRandomKey(w http.ResponseWriter, r *http.Request) {
key := generateSecret()
fmt.Fprint(w, b64.URLEncoding.EncodeToString(key))
}
// handleDecrypt decrypts a given message with a given secret
// query parameters:
// - message, base64 string
// - secret, base64 string
// outputs:
// - 400, secret is not in base64 format
// - 400, message is not in base64 format
// - 400, tampered message or invalid secret (decryption fails or nonce invalid)
// - 200, returns encrypted message
func handleDecrypt(w http.ResponseWriter, r *http.Request) {
messageB64 := r.URL.Query().Get("message")
secretB64 := r.URL.Query().Get("secret")
secret, err := b64.URLEncoding.DecodeString(secretB64)
if err != nil {
http.Error(w, "secret is not base64", http.StatusBadRequest)
return
}
message, err := b64.URLEncoding.DecodeString(messageB64)
if err != nil {
http.Error(w, "message is not base64", http.StatusBadRequest)
return
}
tmpNote := Note{Message: message, Secret: secret}
err = tmpNote.decryptMessage()
tmpNote.cleanS3Quotes()
if err != nil {
http.Error(w, "message has been tampered with or secret is invalid", http.StatusBadRequest)
return
}
fmt.Fprint(w, string(tmpNote.Message))
}
// -------------------------------------------------------------------------------
// Middleware functions
// -------------------------------------------------------------------------------
// enable cors for every requests, never make this api public with these configs
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
next.ServeHTTP(w, r)
})
}
// -------------------------------------------------------------------------------
// Main function
// -------------------------------------------------------------------------------
//go:embed all:web/out
var nextFS embed.FS
func main() {
webFS, err := fs.Sub(nextFS, "web/out")
if err != nil {
log.Fatal(err)
}
r := chi.NewRouter()
// chi middleware
r.Use(middleware.Logger)
r.Use(corsMiddleware)
apiRouter := chi.NewRouter()
apiRouter.Route("/notes", func(r chi.Router) {
r.Get("/{id}", handleGetNote)
r.Post("/", handleCreateNote)
r.Options("/", handleOptions)
})
apiRouter.Get("/random/key", handleRandomKey)
apiRouter.Get("/decrypt", handleDecrypt)
r.Mount("/api", apiRouter)
r.Handle("/*", http.FileServer(http.FS(webFS)))
port := os.Getenv("PORT")
if len(port) == 0 {
port = "80"
}
log.Fatal(http.ListenAndServe(":"+port, r))
}