From 767811eec651b86798866485fc7a504334fd49a3 Mon Sep 17 00:00:00 2001 From: spsobole Date: Mon, 18 Jan 2021 22:33:12 -0700 Subject: [PATCH] Fix auth user on non auth paths --- go.sum | 1 + server.go | 9 +++++++-- server_test.go | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/go.sum b/go.sum index 194628d..818e77b 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,7 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/server.go b/server.go index b389919..99a2844 100644 --- a/server.go +++ b/server.go @@ -67,9 +67,14 @@ func (s *Server) authenticated(auth auth.Authenticator, redirect string, handle func (s *Server) wrapper(handle func(c *Context)) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { log.Debug("request: ", r.RequestURI) - c := s.makeContext(nil, w, r) - handle(c) + c := s.makeContext(nil, w, r) + if s.auth != nil { + if rec, ok := s.auth.DoAuth(w, r); ok { + c.auth = rec + } + } + handle(c) // discard the rest of the body content io.Copy(ioutil.Discard, r.Body) defer r.Body.Close() diff --git a/server_test.go b/server_test.go index ef2fe46..8290ac6 100644 --- a/server_test.go +++ b/server_test.go @@ -72,7 +72,7 @@ func TestServer_BasicAuth(t *testing.T) { s.WithTheme("skin/") s.HandleFunc("/", handlerRoot) s.HandleFuncAuthenticated("/login", "", handlerLogin) - s.HandleFuncAuthenticated("/test", "", handlerTest) + s.HandleFunc("/test", handlerTest) s.Dump() ts := httptest.NewServer(s.router)