This repository was archived by the owner on Jun 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandlers.go
338 lines (284 loc) · 9.36 KB
/
handlers.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
337
338
package main
import (
"encoding/json"
"errors"
"html/template"
"io/fs"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/spf13/viper"
"gopkg.in/go-playground/validator.v9"
)
var ActiveAlerts []interface{}
type contextKey string
var (
contextKeyUserID contextKey = "userId"
apiKeyHeaderName string = "X-API-Key"
contextKeyOrgRole contextKey = "orgRole"
contextKeyDepartmentRole contextKey = "departmentRole"
contextKeyTeamRole contextKey = "teamRole"
)
type userAccount struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
Password1 string `json:"password1" validate:"required,min=6,max=72"`
Password2 string `json:"password2" validate:"required,min=6,max=72,eqfield=Password1"`
}
type userPassword struct {
Password1 string `json:"password1" validate:"required,min=6,max=72"`
Password2 string `json:"password2" validate:"required,min=6,max=72,eqfield=Password1"`
}
// ValidateUserAccount makes sure user name, email, and password are valid before creating the account
func ValidateUserAccount(name string, email string, pwd1 string, pwd2 string) (UserName string, UserEmail string, UserPassword string, validateErr error) {
v := validator.New()
a := userAccount{
Name: name,
Email: email,
Password1: pwd1,
Password2: pwd2,
}
err := v.Struct(a)
return name, email, pwd1, err
}
// ValidateUserPassword makes sure user password is valid before updating the password
func ValidateUserPassword(pwd1 string, pwd2 string) (UserPassword string, validateErr error) {
v := validator.New()
a := userPassword{
Password1: pwd1,
Password2: pwd2,
}
err := v.Struct(a)
return pwd1, err
}
// respondWithJSON takes a payload and writes the response
func (s *server) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
// getJSONRequestBody gets a JSON request body broken into a key/value map
func (s *server) getJSONRequestBody(r *http.Request, w http.ResponseWriter) map[string]interface{} {
body, _ := ioutil.ReadAll(r.Body) // check for errors
keyVal := make(map[string]interface{})
jsonErr := json.Unmarshal(body, &keyVal) // check for errors
if jsonErr != nil {
w.WriteHeader(http.StatusBadRequest)
return nil
}
return keyVal
}
// createUserCookie creates the users cookie
func (s *server) createUserCookie(w http.ResponseWriter, isRegistered bool, UserID string) {
var cookiedays = 365 // 356 days
if isRegistered == true {
cookiedays = 30 // 30 days
}
encoded, err := s.cookie.Encode(s.config.SecureCookieName, UserID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
cookie := &http.Cookie{
Name: s.config.SecureCookieName,
Value: encoded,
Path: s.config.PathPrefix + "/",
HttpOnly: true,
Domain: s.config.AppDomain,
MaxAge: 86400 * cookiedays,
Secure: s.config.SecureCookieFlag,
SameSite: http.SameSiteStrictMode,
}
http.SetCookie(w, cookie)
}
// clearUserCookies wipes the frontend and backend cookies
// used in the event of bad cookie reads
func (s *server) clearUserCookies(w http.ResponseWriter) {
feCookie := &http.Cookie{
Name: s.config.FrontendCookieName,
Value: "",
Path: s.config.PathPrefix + "/",
MaxAge: -1,
}
beCookie := &http.Cookie{
Name: s.config.SecureCookieName,
Value: "",
Path: s.config.PathPrefix + "/",
Domain: s.config.AppDomain,
Secure: s.config.SecureCookieFlag,
SameSite: http.SameSiteStrictMode,
MaxAge: -1,
HttpOnly: true,
}
http.SetCookie(w, feCookie)
http.SetCookie(w, beCookie)
}
// validateUserCookie returns the userID from secure cookies or errors if failures getting it
func (s *server) validateUserCookie(w http.ResponseWriter, r *http.Request) (string, error) {
var userID string
if cookie, err := r.Cookie(s.config.SecureCookieName); err == nil {
var value string
if err = s.cookie.Decode(s.config.SecureCookieName, cookie.Value, &value); err == nil {
userID = value
} else {
log.Println("error in reading user cookie : " + err.Error() + "\n")
s.clearUserCookies(w)
return "", errors.New("invalid user cookies")
}
} else {
log.Println("error in reading user cookie : " + err.Error() + "\n")
s.clearUserCookies(w)
return "", errors.New("invalid user cookies")
}
return userID, nil
}
// get the index template from embedded filesystem
func (s *server) getIndexTemplate(FSS fs.FS) *template.Template {
// get the html template from dist, have it ready for requests
tmplContent, ioErr := fs.ReadFile(FSS, "index.html")
if ioErr != nil {
log.Println("Error opening index template")
if !embedUseOS {
log.Fatal(ioErr)
}
}
tmplString := string(tmplContent)
tmpl, tmplErr := template.New("index").Parse(tmplString)
if tmplErr != nil {
log.Println("Error parsing index template")
if !embedUseOS {
log.Fatal(tmplErr)
}
}
return tmpl
}
/*
Handlers
*/
// handleIndex parses the index html file, injecting any relevant data
func (s *server) handleIndex(FSS fs.FS) http.HandlerFunc {
type AppConfig struct {
AvatarService string
ToastTimeout int
AllowGuests bool
AllowRegistration bool
DefaultLocale string
AuthMethod string
AppVersion string
CookieName string
PathPrefix string
APIEnabled bool
CleanupGuestsDaysOld int
CleanupStoryboardsDaysOld int
ShowActiveCountries bool
}
type UIConfig struct {
AnalyticsEnabled bool
AnalyticsID string
AppConfig AppConfig
ActiveAlerts []interface{}
}
tmpl := s.getIndexTemplate(FSS)
appConfig := AppConfig{
AvatarService: viper.GetString("config.avatar_service"),
ToastTimeout: viper.GetInt("config.toast_timeout"),
AllowGuests: viper.GetBool("config.allow_guests"),
AllowRegistration: viper.GetBool("config.allow_registration") && viper.GetString("auth.method") == "normal",
DefaultLocale: viper.GetString("config.default_locale"),
AuthMethod: viper.GetString("auth.method"),
APIEnabled: viper.GetBool("config.allow_external_api"),
AppVersion: s.config.Version,
CookieName: s.config.FrontendCookieName,
PathPrefix: s.config.PathPrefix,
CleanupGuestsDaysOld: viper.GetInt("config.cleanup_guests_days_old"),
CleanupStoryboardsDaysOld: viper.GetInt("config.cleanup_storyboards_days_old"),
ShowActiveCountries: viper.GetBool("config.show_active_countries"),
}
ActiveAlerts = s.database.GetActiveAlerts()
data := UIConfig{
AnalyticsEnabled: s.config.AnalyticsEnabled,
AnalyticsID: s.config.AnalyticsID,
AppConfig: appConfig,
}
return func(w http.ResponseWriter, r *http.Request) {
data.ActiveAlerts = ActiveAlerts // get latest alerts from memory
if embedUseOS {
tmpl = s.getIndexTemplate(FSS)
}
tmpl.Execute(w, data)
}
}
/*
Storyboard Handlers
*/
// handleStoryboardCreate handles creating a storyboard (arena)
func (s *server) handleStoryboardCreate() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID := r.Context().Value(contextKeyUserID).(string)
vars := mux.Vars(r)
body, bodyErr := ioutil.ReadAll(r.Body) // check for errors
if bodyErr != nil {
log.Println("error in reading request body: " + bodyErr.Error() + "\n")
w.WriteHeader(http.StatusInternalServerError)
return
}
var keyVal struct {
StoryboardName string `json:"storyboardName"`
}
json.Unmarshal(body, &keyVal) // check for errors
newStoryboard, err := s.database.CreateStoryboard(userID, keyVal.StoryboardName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// if storyboard created with team association
TeamID, ok := vars["teamId"]
if ok {
OrgRole := r.Context().Value(contextKeyOrgRole)
DepartmentRole := r.Context().Value(contextKeyDepartmentRole)
TeamRole := r.Context().Value(contextKeyTeamRole).(string)
var isAdmin bool
if DepartmentRole != nil && DepartmentRole.(string) == "ADMIN" {
isAdmin = true
}
if OrgRole != nil && OrgRole.(string) == "ADMIN" {
isAdmin = true
}
if isAdmin == true || TeamRole != "" {
err := s.database.TeamAddStoryboard(TeamID, newStoryboard.StoryboardID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
s.respondWithJSON(w, http.StatusOK, newStoryboard)
}
}
// handleStoryboardGet looks up storyboard or returns notfound status
func (s *server) handleStoryboardGet() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
StoryboardID := vars["id"]
storyboard, err := s.database.GetStoryboard(StoryboardID)
if err != nil {
http.NotFound(w, r)
return
}
s.respondWithJSON(w, http.StatusOK, storyboard)
}
}
// handleStoryboardsGet looks up storyboards associated with userID
func (s *server) handleStoryboardsGet() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID := r.Context().Value(contextKeyUserID).(string)
storyboards, err := s.database.GetStoryboardsByUser(userID)
if err != nil {
http.NotFound(w, r)
return
}
s.respondWithJSON(w, http.StatusOK, storyboards)
}
}