rework to lighten the main, refactor wrappers. Rehandle csrf and pull config items.

This commit is contained in:
2025-04-15 22:19:55 +01:00
parent 0a5d61ea1e
commit 0a21973237
7 changed files with 142 additions and 11 deletions

26
handlers/security/csrf.go Normal file
View File

@@ -0,0 +1,26 @@
package security
import (
"fmt"
"net/http"
"github.com/gorilla/csrf"
)
var CSRFMiddleware func(http.Handler) http.Handler
func InitCSRFProtection(csrfKey []byte, isProduction bool) error {
if len(csrfKey) != 32 {
return fmt.Errorf("csrf key must be 32 bytes, got %d", len(csrfKey))
}
CSRFMiddleware = csrf.Protect(
csrfKey,
csrf.Secure(isProduction),
csrf.SameSite(csrf.SameSiteStrictMode),
csrf.Path("/"),
csrf.HttpOnly(true),
)
return nil
}