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/mux.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'web/mux.go') diff --git a/web/mux.go b/web/mux.go index 0821723..439eaa0 100644 --- a/web/mux.go +++ b/web/mux.go @@ -3,21 +3,28 @@ package web import ( "net/http" + "github.com/LMBishop/scrapbook/pkg/auth" "github.com/LMBishop/scrapbook/pkg/config" "github.com/LMBishop/scrapbook/pkg/index" "github.com/LMBishop/scrapbook/web/command/handler" + "github.com/LMBishop/scrapbook/web/command/middleware" ) -func NewMux(cfg *config.MainConfig, siteIndex *index.SiteIndex) *http.ServeMux { +func NewMux(cfg *config.MainConfig, siteIndex *index.SiteIndex, authenticator *auth.Authenticator) *http.ServeMux { mux := http.NewServeMux() - mux.HandleFunc("GET /", handler.GetHome(cfg, siteIndex)) - mux.HandleFunc("GET /create", handler.GetCreate()) - mux.HandleFunc("POST /create", handler.PostCreate(cfg, siteIndex)) - mux.HandleFunc("GET /site/{site}/", handler.GetSite(cfg, siteIndex)) - mux.HandleFunc("GET /site/{site}/upload", handler.GetUpload(siteIndex)) - mux.HandleFunc("POST /site/{site}/upload", handler.PostUpload(cfg, siteIndex)) - mux.HandleFunc("GET /site/{site}/flags", handler.GetFlags(siteIndex)) - mux.HandleFunc("POST /site/{site}/flags", handler.PostFlags(cfg, siteIndex)) + mux.HandleFunc("GET /authenticate", handler.GetAuthenticate()) + mux.HandleFunc("POST /authenticate", handler.PostAuthenticate(cfg, authenticator)) + + mux.HandleFunc("GET /", middleware.MustAuthenticate(authenticator, handler.GetHome(cfg, siteIndex))) + mux.HandleFunc("GET /create", middleware.MustAuthenticate(authenticator, handler.GetCreate())) + mux.HandleFunc("POST /create", middleware.MustAuthenticate(authenticator, handler.PostCreate(cfg, siteIndex))) + mux.HandleFunc("GET /site/{site}/", middleware.MustAuthenticate(authenticator, handler.GetSite(cfg, siteIndex))) + mux.HandleFunc("GET /site/{site}/upload", middleware.MustAuthenticate(authenticator, handler.GetUpload(siteIndex))) + mux.HandleFunc("POST /site/{site}/upload", middleware.MustAuthenticate(authenticator, handler.PostUpload(cfg, siteIndex))) + mux.HandleFunc("GET /site/{site}/flags", middleware.MustAuthenticate(authenticator, handler.GetFlags(siteIndex))) + mux.HandleFunc("POST /site/{site}/flags", middleware.MustAuthenticate(authenticator, handler.PostFlags(cfg, siteIndex))) + mux.HandleFunc("GET /site/{site}/host", middleware.MustAuthenticate(authenticator, handler.GetHost(siteIndex))) + mux.HandleFunc("POST /site/{site}/host", middleware.MustAuthenticate(authenticator, handler.PostHost(cfg, siteIndex))) return mux } -- cgit v1.2.3-70-g09d2