-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
executable file
路39 lines (32 loc) 路 925 Bytes
/
middleware.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
package main
import (
"net/http"
"github.com/jagottsicher/myGoWebApplication/internal/helpers"
"github.com/justinas/nosurf"
)
// NoSurf serves as a CSRF protection middleware
func NoSurf(next http.Handler) http.Handler {
csrfHandler := nosurf.New(next)
csrfHandler.SetBaseCookie(http.Cookie{
HttpOnly: true,
Path: "/",
Secure: app.InProduction,
SameSite: http.SameSiteLaxMode,
})
return csrfHandler
}
// SessionLoad loads saves session data for each request
func SessionLoad(next http.Handler) http.Handler {
return session.LoadAndSave(next)
}
// Auth redirects non-authenticated requests
func Auth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !helpers.IsAuthenticated(r) {
session.Put(r.Context(), "error", "Log in first!")
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
return
}
next.ServeHTTP(w, r)
})
}