Rename app->internal
This commit is contained in:
21
internal/middleware/csrf.go
Normal file
21
internal/middleware/csrf.go
Normal file
@ -0,0 +1,21 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"GoWeb/security"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Csrf validates the CSRF token and returns the handler function if it succeeded
|
||||
func Csrf(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := security.VerifyCsrfToken(r)
|
||||
if err != nil {
|
||||
slog.Info("error verifying csrf token")
|
||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
f(w, r)
|
||||
}
|
||||
}
|
16
internal/middleware/wrapper.go
Normal file
16
internal/middleware/wrapper.go
Normal file
@ -0,0 +1,16 @@
|
||||
package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
type MiddlewareFunc func(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
// ProcessGroup is a wrapper function for the http.HandleFunc function
|
||||
// that takes the function you want to execute (f) and the middleware you want
|
||||
// to execute (m) this should be used when processing multiple groups of middleware at a time
|
||||
func ProcessGroup(f func(w http.ResponseWriter, r *http.Request), m []MiddlewareFunc) func(w http.ResponseWriter, r *http.Request) {
|
||||
for _, middleware := range m {
|
||||
_ = middleware(f)
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
Reference in New Issue
Block a user