-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
826 lines (708 loc) · 21.5 KB
/
http.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/satori/uuid"
"html/template"
"log"
"math/rand"
"net/http"
"strings"
"time"
"unfoldedip/satsql"
"unfoldedip/sattypes"
)
// general template function
func executeGlobalAgainstTemplate(writer http.ResponseWriter, templateName string, g sattypes.Global) {
err := templates.ExecuteTemplate(writer, templateName, g)
if err != nil {
log.Println(err)
}
return
}
// takes the /login - handle, tries to create a session with a username
// and a password, that is given by the user by submitting the html - form
func login(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
// local error and g for templating
var err error
var g sattypes.Global
// login is a tiny bit complicated function
// that's why we defined helper functions
// ahead, that can be called from every local context
// to template the login page, but also to clean up sessions
// deletes pending session and generates a new one
genAndDestroySession := func() {
// Default: We will print out the template
// add a new CSRF token and session cookie
// for protecting the login area
// destroy old session from database
cookieOld, err := request.Cookie("Session")
// destroy old session if existed
if err == nil && cookieOld.Value != "" {
_ = satsql.DestroySession(H, cookieOld.Value)
}
// create cookie
c := &http.Cookie{
Name: "Session",
Value: uuid.NewV4().String(),
}
// create csrf token
g.CSRF = uuid.NewV4().String()
if H.Debug {
log.Println(c.Value, "is the new session leader with CSRF", g.CSRF)
}
// create a new session, that is not connected with a valid user
err = satsql.NewSession(H, sattypes.UnfoldedUser{UserID: 0}, c.Value, g.CSRF)
if err != nil {
log.Println(err)
return
}
// write cookies
http.SetCookie(writer, c)
}
// clean up from an error, set an error code
// in global, destroy the current session for safety reason
// return template
cleanAndTemplate := func(state int, err error) {
if H.Debug && err != nil {
log.Println(err)
}
g.State = state
genAndDestroySession()
executeGlobalAgainstTemplate(writer, "login.html", g)
}
// now, it is time to check if the FORM is posted
// else template the GET - page
if request.Method == http.MethodPost {
// parse form ahead
err = request.ParseForm()
if err != nil {
cleanAndTemplate(0, err)
return
}
// check if session cookie exists and csrf is right
cookieOld, err := request.Cookie("Session")
// something very strange, better return early
if err != nil || cookieOld.Value == "" {
cleanAndTemplate(0, err)
return
}
// get current CSRF for empty session
csrf, err := satsql.CheckEmptySession(H, cookieOld.Value)
if err != nil || csrf == "" {
cleanAndTemplate(0, err)
return
}
// stored CSRF compare equal to submitted CSRF
// if not, it could be a break in attempt
if csrf != request.FormValue("logincsrf") {
if H.Debug {
log.Println("CSRF expected", csrf, "but got", request.FormValue("logincsrf"))
}
cleanAndTemplate(0, err)
return
}
// at this point CSRF for login submission is validated
// finally do some basic input sanitize
email := strings.ToLower(template.HTMLEscapeString(request.Form.Get("email")))
password := request.Form.Get("password")
// check if user exists
user, err := satsql.SelectUser(H, "email", email)
if err != nil {
cleanAndTemplate(2, err)
return
}
// check password matches with given user
if g.State == 0 && user.CheckPassword(password) {
log.Println(email, user.UserID, "logged into the web console")
// destroy old session from database
_ = satsql.DestroySession(H, cookieOld.Value)
// create a new cookie /
c := &http.Cookie{
Name: "Session",
Value: uuid.NewV4().String(),
}
// create a new csrf token for cookie lifecycle
csrf := uuid.NewV4().String()
// create a new session in db
err = satsql.NewSession(H, user, c.Value, csrf)
if err != nil {
cleanAndTemplate(2, err)
return
}
// write cookies and redirect to service dashboard
http.SetCookie(writer, c)
http.Redirect(writer, request, "/services", http.StatusFound)
return
}
// login did not work, password mismatch
// delay
time.Sleep(time.Second * 3)
// clean session and jump back to main template
cleanAndTemplate(2, err)
return
}
// Are we getting a redirect from register?
if request.Method == http.MethodGet {
if request.URL.Query().Get("register") == "success" {
// set registration was successful state
g.State = 1
} else if request.URL.Query().Get("session") == "pwdrecovered" {
// set registration was successful state
g.State = 3
}
}
cleanAndTemplate(g.State, err)
return
}
// handle register page
func register(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
var U sattypes.UnfoldedUser
var g sattypes.Global
// handles the register page, if we will any error, we will bail out directly
// by setting a global error code and then jumping into the template parse function
if request.Method == http.MethodPost {
// Parse form arguments
err := request.ParseForm()
if err != nil {
g.State = 1
goto ExitAndDefault
}
// fill our user object
if U.SetEmail(template.HTMLEscapeString(request.Form.Get("email"))) != nil {
g.State = 2
goto ExitAndDefault
}
_, err = satsql.SelectUser(H, "email", U.Email)
// nil == User already exists and was pulled
// err != sql.ErrNoRows (other error, then user does not exist)
if err == nil {
g.State = 4
goto ExitAndDefault
} else if err != sql.ErrNoRows {
g.State = 3
goto ExitAndDefault
}
// bail out, if password is zero length
password := request.Form.Get("password")
if len(password) == 0 {
g.State = 5
goto ExitAndDefault
}
// hash password
_, err = U.GeneratePassword(password)
if err != nil {
g.State = 6
goto ExitAndDefault
}
// insert user into db, rewrite U struct
err = satsql.InsertUser(H, &U)
if err != nil {
g.State = 7
goto ExitAndDefault
}
// Registration worked? Then redirect
if U.UserID != 0 && g.State == 0 {
http.Redirect(writer, request, "/login?register=success", http.StatusFound)
return
}
}
ExitAndDefault:
// Default is GET method where we will print out the template
executeGlobalAgainstTemplate(writer, "register.html", g)
}
// forget2 step2 will present a button to the user, that he needs no click for accepting the sent password
// as new user password
func forget2(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
var garbageU sattypes.UnfoldedUser
var g sattypes.Global
var err error
var resetHash string
// handles the forget2 submit, if we will any error, we will bail out directly
// by setting a global error code and then jumping into the template parse function
if request.Method == http.MethodPost {
// Parse form arguments
err = request.ParseForm()
if err != nil {
g.State = 1
goto ExitAndDefault
}
// read hash from query
resetHash = request.Form.Get("hash")
if len(resetHash) == 0 {
g.State = 2
goto ExitAndDefault
}
garbageU, err = satsql.SelectUser(H, "reset", resetHash)
// nil == User exists and was pulled
// err != sql.ErrNoRows (other error, then user does not exist)
if err != nil {
g.State = 3
goto ExitAndDefault
}
// hash was right, so lets reset the password flipping the column
err = satsql.UpdateUser(H, garbageU, "password", garbageU.PasswordHashNext)
if err != nil {
g.State = 4
goto ExitAndDefault
}
// delete hash and NextPassword afterwards with randomness
err = satsql.UpdateUser(H, garbageU, "reset", genrandom(8))
if err != nil {
g.State = 5
goto ExitAndDefault
}
// delete hash and NextPassword afterwards with randomness
err = satsql.UpdateUser(H, garbageU, "passwordnext", genrandom(8))
if err != nil {
g.State = 6
goto ExitAndDefault
}
// we are here? Great..., signal 1 for "everything fine"
http.Redirect(writer, request, "/login?session=pwdrecovered", http.StatusSeeOther)
return
}
if request.Method == http.MethodGet {
// check hash in query url
resetHash = request.URL.Query().Get("hash")
if len(resetHash) == 0 {
g.State = 7
goto ExitAndDefault
}
// is there any valid user for this hash?
garbageU, err = satsql.SelectUser(H, "reset", resetHash)
// nil == User exists and was pulled
// err != sql.ErrNoRows (other error, then user does not exist)
if err != nil {
g.State = 8
goto ExitAndDefault
}
// if there is a user, template the user back
g.U = garbageU
}
ExitAndDefault:
// print out error if any
if err != nil {
log.Println(err)
}
// Safe we, if state is not 0 zero, throw forbidden
if g.State != 0 {
writer.WriteHeader(http.StatusForbidden)
return
}
// Default is GET method where we will print out the template
executeGlobalAgainstTemplate(writer, "forget2.html", g)
}
// forget will send out a possible new password with a hash to submit
func forget(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
var U sattypes.UnfoldedUser
var g sattypes.Global
var err error
// code is a bit copied from func login
// because we need to take, that third parties do not generate
// forget panels // - maybe kind of middleware would be cool
// deletes pending session and generates a new one
genAndDestroySession := func() {
// Default: We will print out the template
// add a new CSRF token and session cookie
// for protecting the login area
// destroy old session from database
cookieOld, err := request.Cookie("Session")
// destroy old session if existed
if err == nil && cookieOld.Value != "" {
_ = satsql.DestroySession(H, cookieOld.Value)
}
// create cookie
c := &http.Cookie{
Name: "Session",
Value: uuid.NewV4().String(),
}
// create csrf token
g.CSRF = uuid.NewV4().String()
if H.Debug {
log.Println(c.Value, "is the new session leader with CSRF", g.CSRF)
}
// create a new session, that is not connected with a valid user
err = satsql.NewSession(H, sattypes.UnfoldedUser{UserID: 0}, c.Value, g.CSRF)
if err != nil {
log.Println(err)
return
}
// write cookies
http.SetCookie(writer, c)
}
// clean up from an error, set an error code
// in global, destroy the current session for safety reason
// return template
cleanAndTemplate := func(state int, err error) {
if H.Debug && err != nil {
log.Println(err)
}
g.State = state
genAndDestroySession()
executeGlobalAgainstTemplate(writer, "forget1.html", g)
}
// handles the forget1 page, if we will any error, we will bail out directly
// by setting a global error code and then jumping into the template parse function
if request.Method == http.MethodPost {
// Parse form arguments
err = request.ParseForm()
if err != nil {
cleanAndTemplate(2, err)
return
}
// check if session cookie exists and csrf is right
cookieOld, err := request.Cookie("Session")
// get current CSRF for empty session
csrf, err := satsql.CheckEmptySession(H, cookieOld.Value)
if err != nil || csrf == "" {
cleanAndTemplate(0, err)
return
}
// get current CSRF for empty session
csrf, err = satsql.CheckEmptySession(H, cookieOld.Value)
if err != nil || csrf == "" {
cleanAndTemplate(0, err)
return
}
// stored CSRF compare equal to submitted CSRF
// if not, it could be a break in attempt
if csrf != request.FormValue("logincsrf") {
if H.Debug {
log.Println("CSRF expected", csrf, "but got", request.FormValue("logincsrf"))
}
cleanAndTemplate(0, err)
return
}
// at this point CSRF for login submission is validated
// finally do some basic input sanitize
// fill our user object
if U.SetEmail(strings.ToLower(template.HTMLEscapeString(request.Form.Get("email")))) != nil {
cleanAndTemplate(2, err)
return
}
U, err = satsql.SelectUser(H, "email", U.Email)
// nil == User exists and was pulled
// err != sql.ErrNoRows (other error, then user does not exist)
if err != nil {
cleanAndTemplate(2, err)
return
}
// bail out, if password is zero length
password := genrandom(8)
// hash password
_, err = U.GeneratePassword(password)
if err != nil {
cleanAndTemplate(2, err)
return
}
resetHash := uuid.NewV4().String()
// update user with hash and a possibile new password
err = satsql.UpdateUser(H, U, "reset", resetHash)
if err != nil {
cleanAndTemplate(2, err)
return
}
// insert user into db, rewrite U struct
err = satsql.UpdateUser(H, U, "passwordnext", U.PasswordHash)
if err != nil {
cleanAndTemplate(2, err)
return
}
err = H.SendPasswordForget(U.Email, password, resetHash, H.URL)
if err != nil {
cleanAndTemplate(2, err)
return
}
cleanAndTemplate(1, err)
return
}
// collect error if there is some
if err != nil {
log.Println(err)
}
cleanAndTemplate(0, err)
return
}
// isLoggedIn checks if a user cookie exists and the user session is logged in
func isLoggedIn(request *http.Request, H sattypes.BaseHandler) (sattypes.UnfoldedUser, bool) {
// pull session cookie
c, err := request.Cookie("Session")
if err != nil {
log.Printf("Cookie not found for ip %s, uri %s", request.RemoteAddr, request.URL)
return sattypes.UnfoldedUser{}, false
}
// check session cookie
userSession, err := satsql.CheckUserSession(H, c.Value)
if err != nil {
log.Printf("Cookie or session not found for ip %s, uri %s", request.RemoteAddr, request.URL)
return sattypes.UnfoldedUser{}, false
}
// select user from db
U, err := satsql.SelectUser(H, "id", fmt.Sprintf("%d", userSession.UserID))
if err != nil {
return sattypes.UnfoldedUser{}, false
}
// update session in database
err = satsql.UpdateSession(H, userSession)
if H.Debug && err != nil {
log.Println(err)
}
// embedding session into user object
U.UserSession = userSession
// return User object
return U, true
}
// handle profile page and updates to email and password
func profile(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
var g sattypes.Global
// retrieve session
if g.U, g.U.LoggedIn = isLoggedIn(request, H); !g.U.LoggedIn {
http.Redirect(writer, request, "/login?session=expired3", http.StatusSeeOther)
return
}
// check if submission was done
if request.Method == http.MethodPost {
err := request.ParseForm()
if err != nil {
g.State = 1
goto ExitAndDefault
}
// check if csrf token is valid
if !CheckCSRFToken(writer, request, H, g.U.UserSession) {
return
}
// read, what action needs to be done
if template.HTMLEscapeString(request.Form.Get("changepassword")) == "1" {
// change password?
// check incoming old password
if !g.U.CheckPassword(template.HTMLEscapeString(request.Form.Get("password"))) {
g.State = 5
goto ExitAndDefault
}
// bail out, if new password is zero length
newpassword := request.Form.Get("newpassword")
if len(newpassword) == 0 {
g.State = 6
goto ExitAndDefault
}
// hash/generate new password inside user object
_, err = g.U.GeneratePassword(newpassword)
if err != nil {
g.State = 6
goto ExitAndDefault
}
if H.Debug {
log.Println("Chaning password for", g.U.Email, g.U.UserID)
}
// if ok, then
// change to new password
err = satsql.UpdateUser(H, g.U, "password", g.U.PasswordHash)
if err != nil {
g.State = 7
goto ExitAndDefault
}
// set to success for template engine
g.State = 8
} else if template.HTMLEscapeString(request.Form.Get("changeemail")) == "1" {
// create new user object
V := g.U
// or change email address
if V.SetEmail(template.HTMLEscapeString(request.Form.Get("emailnew"))) != nil {
g.State = 2
goto ExitAndDefault
}
_, err = satsql.SelectUser(H, "email", V.Email)
// nil == User exists and was pulled
// err != sql.ErrNoRows (other error), else user does not exist
if err == nil {
g.State = 4
goto ExitAndDefault
} else if err != sql.ErrNoRows {
g.State = 3
goto ExitAndDefault
}
// update User in db
err := satsql.UpdateUser(H, g.U, "email", V.Email)
if err != nil {
log.Println(err)
goto ExitAndDefault
}
// set to success for template engine
g.State = 8
g.U = V
}
}
// Default is GET method where we will print out the template
ExitAndDefault:
executeGlobalAgainstTemplate(writer, "profile.html", g)
}
// logout takes care of the logout process
func logout(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
// read current cookie
c, err := request.Cookie("Session")
// cookie does exist
if err == nil {
if c.Value != "" {
err := satsql.DestroySession(H, c.Value)
if err != nil {
log.Println(err)
}
}
}
// create new useless cookie
c = &http.Cookie{
Name: "Session",
Value: uuid.NewV4().String(),
MaxAge: -1,
}
// overwrite session cookie and redirect to log in
http.SetCookie(writer, c)
http.Redirect(writer, request, "/login", http.StatusSeeOther)
return
}
// agentsConfig pushes configuration from services
func agentsConfig(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
// Only accept GET
if request.Method != http.MethodGet {
writer.WriteHeader(http.StatusForbidden)
return
}
// check agents access key
satAgent, allowed := CheckAgentAccessKey(request, H)
if !allowed {
writer.WriteHeader(http.StatusForbidden)
return
}
// return matching services from DB
dbServices, err := satsql.ReadServices(H, 0, satAgent.SatAgentLocation, satAgent.SatOnlyLocation)
if err != nil {
writer.WriteHeader(http.StatusForbidden)
return
}
// new json encoder
err = json.NewEncoder(writer).Encode(dbServices)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
dbServices = nil
}
// agentsResults takes services results from agents
func agentsResults(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler) {
// only accept POST
if request.Method != http.MethodPost {
writer.WriteHeader(http.StatusForbidden)
return
}
// check agents access key
_, allowed := CheckAgentAccessKey(request, H)
if !allowed {
writer.WriteHeader(http.StatusForbidden)
return
}
// Read response and try to parse
var agentResults []sattypes.ServiceResult
err := json.NewDecoder(request.Body).Decode(&agentResults)
if err != nil {
log.Println(err)
}
defer request.Body.Close()
// for every parsed result, ...
for i := range agentResults {
// ... send result to analytics via golang channel
sattypes.ResultsChannel <- agentResults[i]
}
agentResults = nil
// thank back the sender with a statusOK
writer.WriteHeader(http.StatusOK)
return
}
// CheckAgentAccessKey retrieves the agent node and key of satagent and tries to match it
// against a sql entry
func CheckAgentAccessKey(request *http.Request, H sattypes.BaseHandler) (sattypes.SatAgentSql, bool) {
var newAgent bool
// retrieve access-key from header
accessKey := request.Header.Get("agent-key")
if len(accessKey) == 0 {
return sattypes.SatAgentSql{}, false
}
// retrieve agent-name and location from header
accessNode := request.Header.Get("agent-name")
locationNode := request.Header.Get("agent-location")
// try to match node in database , if nil == token found
switch err := satsql.SearchAgentAccessKey(H, accessNode, accessKey); err {
case nil:
// accessNode was found, continue
log.Println("Matched global server key for", accessKey, accessNode, request.RemoteAddr)
break
case sql.ErrNoRows:
if accessKey != H.SatKey {
log.Println("Invalid key for client, stopping operation for", accessNode, request.RemoteAddr)
return sattypes.SatAgentSql{}, false
}
// nothing was found, continue to global check
break
default:
return sattypes.SatAgentSql{}, false
}
satAgent, err := satsql.SelectAgent(H, "satagent_name", accessNode)
if err != nil {
log.Println("SatAgentSql not known", err)
err = nil
newAgent = true
}
if newAgent {
if H.Debug {
log.Println("Creating SQL entry for sat agent", accessNode, accessKey, locationNode)
}
err = satsql.InsertAgent(H, accessNode, accessKey, locationNode)
if err != nil {
log.Println(err)
return sattypes.SatAgentSql{}, false
}
satAgent, err = satsql.SelectAgent(H, "satagent_name", accessNode)
if err != nil {
log.Println("SatAgentSql still not known", err)
return sattypes.SatAgentSql{}, false
}
}
err = satsql.UpdateAgentLocation(H, locationNode, satAgent.SatAgentID)
if err != nil {
log.Println(err)
return sattypes.SatAgentSql{}, false
}
if H.Debug {
log.Println("Allowing access to satagent-node", accessNode)
}
// check if only location is set for this host
onlyLocation := request.Header.Get("agent-onlylocation")
if onlyLocation != "" && onlyLocation == "YES" {
satAgent.SatOnlyLocation = true
}
return satAgent, true
}
// CheckCSRFToken check CSRF token from a form against expected CSRF from the usersession
func CheckCSRFToken(writer http.ResponseWriter, request *http.Request, H sattypes.BaseHandler, US sattypes.Session) bool {
// check csrf against current session
if request.FormValue("csrf") != US.CSRF {
log.Println("CSRF is wrong, shall be", US.CSRF)
writer.WriteHeader(http.StatusForbidden)
return false
}
if H.Debug {
log.Println("CSRF is right", US.CSRF, request.FormValue("csrf"))
}
return true
}
// generate random string from rune slice
func genrandom(length int) string {
rand.Seed(time.Now().UnixNano())
charAndSpecial := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
var tempString strings.Builder
for i := 0; i < length; i++ {
tempString.WriteRune(charAndSpecial[rand.Intn(len(charAndSpecial))])
}
return tempString.String()
}