From 5c477c92d4103ff40f57c36110a4e27c172d86a6 Mon Sep 17 00:00:00 2001 From: theandrew168 Date: Sun, 13 Oct 2024 18:02:58 -0500 Subject: [PATCH] Create a more specific var for enabling debug auth --- Makefile | 4 ++-- README.md | 1 + backend/web/handler.go | 7 +++++-- backend/web/login.go | 6 ++---- backend/web/page/login.go | 3 ++- backend/web/page/login.html | 2 +- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index f4a0a12..c895278 100644 --- a/Makefile +++ b/Makefile @@ -11,12 +11,12 @@ build: # use wgo to watch for code changes and subsequently rebuild the app .PHONY: run run: - DEBUG=1 go run github.com/bokwoon95/wgo@latest run -file .html -file .css main.go + ENABLE_DEBUG_AUTH=1 go run github.com/bokwoon95/wgo@latest run -file .html -file .css main.go # run the app using the local-only config file .PHONY: run-local run-local: - DEBUG=1 go run github.com/bokwoon95/wgo@latest run -file .html -file .css main.go -conf bloggulus.local.conf + ENABLE_DEBUG_AUTH=1 go run github.com/bokwoon95/wgo@latest run -file .html -file .css main.go -conf bloggulus.local.conf .PHONY: migrate migrate: diff --git a/README.md b/README.md index 8d24a81..17cfec9 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ make run-local ``` Otherwise, you can simply run the application normally (without OAuth configured) and use the local-only debug login. +This is enabled when `ENABLE_DEBUG_AUTH` is set (which the already Makefile includes for `run` and `run-local`). ### Testing diff --git a/backend/web/handler.go b/backend/web/handler.go index ac9e422..0fbaa8e 100644 --- a/backend/web/handler.go +++ b/backend/web/handler.go @@ -92,8 +92,11 @@ func Handler( // The main application routes start here. mux.Handle("GET /{$}", HandleIndexPage(find)) + // Check if the debug auth method should be enabled. + enableDebugAuth := os.Getenv("ENABLE_DEBUG_AUTH") != "" + // Authenication routes. - mux.Handle("GET /signin", HandleLogin()) + mux.Handle("GET /signin", HandleLogin(enableDebugAuth)) mux.Handle("GET /github/signin", HandleOAuthLogin(&githubConf)) mux.Handle("GET /github/callback", HandleOAuthCallback(&githubConf, repo, FetchGithubUserID)) mux.Handle("GET /google/signin", HandleOAuthLogin(&googleConf)) @@ -101,7 +104,7 @@ func Handler( mux.Handle("POST /logout", HandleLogoutForm(repo)) // Debug-only auth routes. - if os.Getenv("DEBUG") != "" { + if enableDebugAuth { mux.Handle("POST /debug/signin", HandleDebugLogin(repo)) } diff --git a/backend/web/login.go b/backend/web/login.go index 4a34c74..440b001 100644 --- a/backend/web/login.go +++ b/backend/web/login.go @@ -9,7 +9,6 @@ import ( "io" "log/slog" "net/http" - "os" "github.com/theandrew168/bloggulus/backend/model" "github.com/theandrew168/bloggulus/backend/postgres" @@ -101,7 +100,7 @@ func FetchGoogleUserID(client *http.Client) (string, error) { return username, nil } -func HandleLogin() http.Handler { +func HandleLogin(enableDebugAuth bool) http.Handler { tmpl := page.NewLogin() return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Check for a "next" query param for post-auth redirecting. @@ -114,11 +113,10 @@ func HandleLogin() http.Handler { cookie := util.NewSessionCookie(util.NextCookieName, next) http.SetCookie(w, &cookie) - isDebug := os.Getenv("DEBUG") != "" data := page.LoginData{ BaseData: util.TemplateBaseData(r, w), - IsDebug: isDebug, + EnableDebugAuth: enableDebugAuth, } util.Render(w, r, http.StatusOK, func(w io.Writer) error { return tmpl.Render(w, data) diff --git a/backend/web/page/login.go b/backend/web/page/login.go index 33aabed..f36a991 100644 --- a/backend/web/page/login.go +++ b/backend/web/page/login.go @@ -15,9 +15,10 @@ var LoginHTML string type LoginData struct { layout.BaseData - IsDebug bool GithubConf *oauth2.Config Errors map[string]string + + EnableDebugAuth bool } type LoginPage struct { diff --git a/backend/web/page/login.html b/backend/web/page/login.html index 3861e71..6c3b72d 100644 --- a/backend/web/page/login.html +++ b/backend/web/page/login.html @@ -6,7 +6,7 @@

Welcome!


- {{if .IsDebug}} + {{if .EnableDebugAuth}}