From bba6e8618aa09a5a5b1df0f1789bb7531d57229e Mon Sep 17 00:00:00 2001 From: bronku Date: Fri, 28 Mar 2025 20:33:34 +0100 Subject: [PATCH] Refactor authentication handlers and middleware --- TODO.md | 1 + internal/auth/auth.go | 48 ++++++++---------------------------- internal/auth/credentials.go | 11 +++++++++ internal/auth/get.go | 17 +++++++++++++ internal/auth/post.go | 28 +++++++++++++++++++++ 5 files changed, 67 insertions(+), 38 deletions(-) create mode 100644 internal/auth/credentials.go create mode 100644 internal/auth/get.go create mode 100644 internal/auth/post.go diff --git a/TODO.md b/TODO.md index ea98acd..f1edd0d 100644 --- a/TODO.md +++ b/TODO.md @@ -117,6 +117,7 @@ - [ ] Error Handling - [ ] Proper error logging - [ ] User-friendly error messages + - [ ] A better way to handle the error page - [ ] Performance Optimization - [ ] API Documentation - [ ] Unit Tests diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 08f1b81..635a870 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -11,9 +11,6 @@ import ( "github.com/Bronku/iroon/internal/store" ) -//go:embed templates/login.html -var loginPage string - //go:embed templates/wrongPassword.html var wrongPassword string @@ -34,42 +31,9 @@ func New(s *store.Store) *Authenticator { return &out } -func (a *Authenticator) login(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - w.Header().Set("content-type", "text/html") - fmt.Fprint(w, loginPage) - return - } - err := r.ParseForm() - if err != nil { - w.Header().Set("content-type", "text/html") - fmt.Fprint(w, loginPage) - return - } - login := r.PostFormValue("login") - password := r.PostFormValue("password") - if login != "admin" || password != "secret" { - w.Header().Set("content-type", "text/html") - fmt.Fprint(w, wrongPassword) - return - } - cookie, err := a.newSession(login) - // #todo: change to a some sort of internal server error - if err != nil { - w.Header().Set("content-type", "text/html") - fmt.Fprint(w, "internal server error") - return - } - http.SetCookie(w, &cookie) - http.Redirect(w, r, "/", http.StatusFound) -} - -func (a *Authenticator) Middleware(in http.Handler) http.Handler { +func (a *Authenticator) ensureAuth(in http.Handler) http.Handler { + fmt.Println("ensureAuth called") return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.String() == "/login" { - a.login(w, r) - return - } c, err := r.Cookie("token") if err != nil { http.Redirect(w, r, "/login", http.StatusSeeOther) @@ -92,3 +56,11 @@ func (a *Authenticator) Middleware(in http.Handler) http.Handler { in.ServeHTTP(w, r) }) } + +func (a *Authenticator) Middleware(in http.Handler) http.Handler { + handler := http.NewServeMux() + handler.HandleFunc("GET /login", getLogin) + handler.HandleFunc("POST /login", a.login) + handler.Handle("/", a.ensureAuth(in)) + return handler +} diff --git a/internal/auth/credentials.go b/internal/auth/credentials.go new file mode 100644 index 0000000..0d23c66 --- /dev/null +++ b/internal/auth/credentials.go @@ -0,0 +1,11 @@ +package auth + +import "errors" + +func (a *Authenticator) verifyCredentials(login, password string) error { + + if login == "admin" && password == "secret" { + return nil + } + return errors.New("wrong credentials") +} diff --git a/internal/auth/get.go b/internal/auth/get.go new file mode 100644 index 0000000..bc80064 --- /dev/null +++ b/internal/auth/get.go @@ -0,0 +1,17 @@ +package auth + +import ( + "fmt" + "net/http" + + _ "embed" +) + +//go:embed templates/login.html +var loginHTML string + +func getLogin(w http.ResponseWriter, r *http.Request) { + w.Header().Set("content-type", "text/html") + fmt.Fprint(w, loginHTML) + return +} diff --git a/internal/auth/post.go b/internal/auth/post.go new file mode 100644 index 0000000..0e87595 --- /dev/null +++ b/internal/auth/post.go @@ -0,0 +1,28 @@ +package auth + +import ( + "fmt" + "net/http" +) + +func (a *Authenticator) login(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + http.Redirect(w, r, "/login", http.StatusSeeOther) + return + } + login := r.PostFormValue("login") + password := r.PostFormValue("password") + if a.verifyCredentials(login, password) != nil { + http.Redirect(w, r, "/login", http.StatusSeeOther) + return + } + cookie, err := a.newSession(login) + if err != nil { + w.Header().Set("content-type", "text/html") + fmt.Fprint(w, "internal server error") + return + } + http.SetCookie(w, &cookie) + http.Redirect(w, r, "/", http.StatusFound) +}