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 --- main.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'main.go') diff --git a/main.go b/main.go index 7a38d82..5be14da 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "crypto/rand" "fmt" "log/slog" "net/http" @@ -8,6 +9,7 @@ import ( "path/filepath" "github.com/LMBishop/scrapbook/api" + "github.com/LMBishop/scrapbook/pkg/auth" "github.com/LMBishop/scrapbook/pkg/config" "github.com/LMBishop/scrapbook/pkg/constants" "github.com/LMBishop/scrapbook/pkg/index" @@ -41,10 +43,19 @@ func main() { slog.Warn("command interface host is empty - neither api or web interface will be accessible") } + if cfg.Command.Secret == "" { + slog.Warn("command interface secret is empty - neither api or web interface will be accessible") + } + + secretKey := make([]byte, 32) + rand.Read(secretKey) + + authenticator := auth.NewAuthenticator(secretKey) + mux := http.NewServeMux() mux.Handle(fmt.Sprintf("%s/api/", cfg.Command.Host), http.StripPrefix("/api/", api.NewMux(&cfg, siteIndex))) - mux.Handle(fmt.Sprintf("%s/", cfg.Command.Host), web.NewMux(&cfg, siteIndex)) + mux.Handle(fmt.Sprintf("%s/", cfg.Command.Host), web.NewMux(&cfg, siteIndex, authenticator)) mux.HandleFunc("/", server.ServeSite(siteIndex)) err = http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Listen.Address, cfg.Listen.Port), mux) -- cgit v1.2.3-70-g09d2