From 01da46b81ebbaf54097d70eebd54658d99fa3787 Mon Sep 17 00:00:00 2001 From: bronku Date: Fri, 28 Mar 2025 21:06:12 +0100 Subject: [PATCH] Refactor auth session management into separate function --- internal/auth/auth.go | 22 ++------------------ internal/auth/{post.go => session.go} | 24 ++++++++++++++++++++++ internal/auth/templates/wrongPassword.html | 12 ----------- 3 files changed, 26 insertions(+), 32 deletions(-) rename internal/auth/{post.go => session.go} (52%) delete mode 100644 internal/auth/templates/wrongPassword.html diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 635a870..3dc20b6 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -5,15 +5,11 @@ import ( "fmt" "log" "net/http" - "time" "github.com/Bronku/iroon/internal/models" "github.com/Bronku/iroon/internal/store" ) -//go:embed templates/wrongPassword.html -var wrongPassword string - type Authenticator struct { sessions map[string]models.Token s *store.Store @@ -24,7 +20,7 @@ func New(s *store.Store) *Authenticator { var err error out.s = s out.sessions, err = s.GetSessions() - fmt.Println(out.sessions) + //fmt.Println(out.sessions) if err != nil { log.Fatal(err) } @@ -34,25 +30,11 @@ func New(s *store.Store) *Authenticator { func (a *Authenticator) ensureAuth(in http.Handler) http.Handler { fmt.Println("ensureAuth called") return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - c, err := r.Cookie("token") + _, err := a.getSession(r) if err != nil { http.Redirect(w, r, "/login", http.StatusSeeOther) return } - value, ok := a.sessions[c.Value] - if !ok { - http.Redirect(w, r, "/login", http.StatusSeeOther) - return - } - if time.Since(value.Expiration) > 0 { - delete(a.sessions, c.Value) - err := a.s.CleanSessions() - if err != nil { - fmt.Println("error cleaning the sessions", err) - } - http.Redirect(w, r, "/login", http.StatusSeeOther) - return - } in.ServeHTTP(w, r) }) } diff --git a/internal/auth/post.go b/internal/auth/session.go similarity index 52% rename from internal/auth/post.go rename to internal/auth/session.go index 0e87595..f4afa1c 100644 --- a/internal/auth/post.go +++ b/internal/auth/session.go @@ -1,10 +1,34 @@ package auth import ( + "errors" "fmt" "net/http" + "time" + + "github.com/Bronku/iroon/internal/models" ) +func (a *Authenticator) getSession(r *http.Request) (models.Token, error) { + c, err := r.Cookie("token") + if err != nil { + return models.Token{}, err + } + session, ok := a.sessions[c.Value] + if !ok { + return models.Token{}, errors.New("session not found") + } + if time.Since(session.Expiration) > 0 { + delete(a.sessions, c.Value) + err := a.s.CleanSessions() + if err != nil { + fmt.Println("error cleaning the sessions", err) + } + return models.Token{}, errors.New("session expired") + } + return session, nil +} + func (a *Authenticator) login(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { diff --git a/internal/auth/templates/wrongPassword.html b/internal/auth/templates/wrongPassword.html deleted file mode 100644 index 47e8f79..0000000 --- a/internal/auth/templates/wrongPassword.html +++ /dev/null @@ -1,12 +0,0 @@ -
-

Wrong Password

-
-
-
- - - - - -
-