From 08a3fb8a2b0281c3c329b33215ec7f8866add606 Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Mon, 14 Jul 2025 01:24:40 +0100 Subject: Add authentication and ability to change host --- web/command/middleware/authenticate.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 web/command/middleware/authenticate.go (limited to 'web/command/middleware') diff --git a/web/command/middleware/authenticate.go b/web/command/middleware/authenticate.go new file mode 100644 index 0000000..08e3552 --- /dev/null +++ b/web/command/middleware/authenticate.go @@ -0,0 +1,25 @@ +package middleware + +import ( + "net/http" + + "github.com/LMBishop/scrapbook/pkg/auth" +) + +func MustAuthenticate(authenticator *auth.Authenticator, next http.HandlerFunc) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + jwt, err := r.Cookie("session") + if err != nil { + http.Redirect(w, r, "/authenticate", 302) + return + } + + err = authenticator.VerifyJwt(jwt.Value) + if err != nil { + http.Redirect(w, r, "/authenticate", 302) + return + } + + next.ServeHTTP(w, r) + }) +} -- cgit v1.2.3-70-g09d2