-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.go
228 lines (185 loc) · 5.13 KB
/
app.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
package app
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/gofiber/template/html/v2"
"github.com/google/uuid"
"github.com/cds-snc/secret/encryption"
"github.com/cds-snc/secret/storage"
)
const MAX_AGE_IN_DAYS = 7
const MAX_SECRET_LENGTH = 64_000
func CreateApp(encryption encryption.EncryptionBackend, storage storage.StorageBackend) *fiber.App {
engine := html.New("./views", ".html")
locales := loadLocales()
engine.AddFunc("t", func(toTranslate string, lang string) string {
if locales[lang][toTranslate] != "" {
return locales[lang][toTranslate]
}
return toTranslate
})
app := fiber.New(fiber.Config{
Views: engine,
})
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"Lang": "en",
"OtherLang": getOtherLanguage("en"),
}, "base")
})
app.Get("/version", func(c *fiber.Ctx) error {
version := os.Getenv("GIT_SHA")
if version == "" {
version = "dev"
}
return c.JSON(fiber.Map{
"version": version,
})
})
app.Get("/:language", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"Lang": c.Params("language"),
"OtherLang": getOtherLanguage(c.Params("language")),
}, "base")
})
app.Get("/:language/view/:id", func(c *fiber.Ctx) error {
//Convert the id to a UUID
id, err := uuid.Parse(c.Params("id"))
if err != nil {
log.Warn(err)
return c.Status(fiber.StatusBadRequest).SendString("Invalid UUID")
}
return c.Render("view", fiber.Map{
"Lang": c.Params("language"),
"OtherLang": getOtherLanguage(c.Params("language")),
"ViewId": id,
}, "base")
})
app.Get("/decrypt/:id", func(c *fiber.Ctx) error {
//Convert the id to a UUID
id, err := uuid.Parse(c.Params("id"))
if err != nil {
log.Warn(err)
return c.Status(fiber.StatusBadRequest).SendString("Invalid UUID")
}
//Get the encrypted data from the storage backend
encryptedData, key, err := storage.Retrieve(id)
if err != nil {
log.Warn(err)
return c.Status(fiber.StatusBadRequest).SendString("Invalid UUID")
}
//Decrypt the data
decryptedData, err := encryption.Decrypt(encryptedData, key)
if err != nil {
log.Error(err)
return c.Status(fiber.StatusBadRequest).SendString("Invalid UUID")
}
//Delete the data from the storage backend
err = storage.Delete(id)
if err != nil {
log.Error(err)
return c.Status(fiber.StatusBadRequest).SendString("Invalid UUID")
}
// Return a JSON response with the decrypted data
return c.JSON(fiber.Map{
"body": string(decryptedData),
})
})
app.Delete("/delete/:id", func(c *fiber.Ctx) error {
//Convert the id to a UUID
id, err := uuid.Parse(c.Params("id"))
if err != nil {
log.Warn(err)
return c.Status(fiber.StatusBadRequest).SendString("Invalid UUID")
}
//Delete the data from the storage backend
err = storage.Delete(id)
if err != nil {
log.Error(err)
return c.Status(fiber.StatusBadRequest).SendString("Invalid UUID")
}
return c.JSON(fiber.Map{
"status": "OK",
})
})
app.Post("/encrypt", func(c *fiber.Ctx) error {
c.Accepts("application/json")
// Parse the JSON body from the request
type RequestBody struct {
Body string `json:"body"`
TTL int64 `json:"ttl"`
}
var body RequestBody
err := c.BodyParser(&body)
if err != nil {
log.Error(err)
return c.Status(fiber.StatusBadRequest).SendString("Invalid JSON")
}
// Encrypt and save the data
id, err := encryptAndSave(body.Body, body.TTL, encryption, storage)
if err != nil {
log.Error(err)
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
// Return a JSON response with the UUID
return c.JSON(fiber.Map{
"id": id,
})
})
return app
}
func encryptAndSave(body string, ttl int64, encryption encryption.EncryptionBackend, storage storage.StorageBackend) (string, error) {
// Check the TTL is in range
currentTimestamp := time.Now().Unix()
if ttl < currentTimestamp || ttl > currentTimestamp+(MAX_AGE_IN_DAYS*24*60*60) {
log.Error("Invalid TTL")
return "", fmt.Errorf("invalid TTL")
}
// Validate the body
if len(body) > MAX_SECRET_LENGTH {
log.Error("Secret too long")
return "", fmt.Errorf("secret too long")
}
// Encrypt the data
encryptedData, key, err := encryption.Encrypt([]byte(body))
if err != nil {
return "", err
}
// Store the encrypted data
id, err := storage.Store(encryptedData, key, ttl)
if err != nil {
return "", err
}
return id.String(), nil
}
func getOtherLanguage(language string) string {
if language == "en" {
return "fr"
} else {
return "en"
}
}
func loadLocales() map[string]map[string]string {
locales := make([]string, 2)
locales[0] = "en"
locales[1] = "fr"
translations := make(map[string]map[string]string)
for _, locale := range locales {
translations[locale] = make(map[string]string)
file := fmt.Sprintf("./locales/%s.json", locale)
byteValue, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
}
var result map[string]interface{}
json.Unmarshal([]byte(byteValue), &result)
for key, value := range result {
translations[locale][key] = value.(string)
}
}
return translations
}