diff --git a/middleware/middleware.go b/middleware/middleware.go index 138ed71..cbd1998 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -2,12 +2,13 @@ package middleware import ( "context" + "fmt" "net/http" "nilfm.cc/git/quartzgun/auth" "nilfm.cc/git/quartzgun/cookie" ) -func Protected(next http.Handler, userStore auth.UserStore) http.Handler { +func Protected(next http.Handler, method string, userStore auth.UserStore) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { user, err := cookie.GetToken("user", req) if err == nil { @@ -15,13 +16,17 @@ func Protected(next http.Handler, userStore auth.UserStore) http.Handler { if err == nil { login, err := userStore.ValidateUser(user, session) if err == nil && login { + fmt.Printf("authorized!\n") + fmt.Printf("user: %s, session: %s\n", user, session) + req.Method = method next.ServeHTTP(w, req) return } } } + fmt.Printf("unauthorized...\n") req.Method = http.MethodGet - http.Redirect(w, req, "/login", http.StatusTemporaryRedirect) + http.Redirect(w, req, "/login", http.StatusSeeOther) } return http.HandlerFunc(handlerFunc) @@ -37,15 +42,17 @@ func Authorize(next string, userStore auth.UserStore) http.Handler { 24*7*52) if err == nil { req.Method = http.MethodGet - http.Redirect(w, req, next, http.StatusOK) + fmt.Printf("logged in as %s\n", req.FormValue("user")) + http.Redirect(w, req, next, http.StatusSeeOther) } else { *req = *req.WithContext( context.WithValue( req.Context(), "message", "Incorrect credentials")) + fmt.Printf("login failed!\n") req.Method = http.MethodGet - http.Redirect(w, req, "/login", http.StatusTemporaryRedirect) + http.Redirect(w, req, "/login", http.StatusSeeOther) } } diff --git a/quartzgun_test.go b/quartzgun_test.go index c325926..3c9d099 100644 --- a/quartzgun_test.go +++ b/quartzgun_test.go @@ -6,6 +6,7 @@ import ( "html/template" "net/http" "nilfm.cc/git/quartzgun/indentalUserDB" + "nilfm.cc/git/quartzgun/middleware" "nilfm.cc/git/quartzgun/renderer" "nilfm.cc/git/quartzgun/router" "testing" @@ -43,7 +44,14 @@ func TestMain(m *testing.M) { Fallback: *template.Must(template.ParseFiles("testData/templates/error.html", "testData/templates/footer.html")), } - rtr.Get("/", AddContent(renderer.Template("testData/templates/test.html"))) + rtr.Get("/login", renderer.Template( + "testData/templates/login.html")) + + rtr.Post("/login", middleware.Authorize("/", udb)) + + rtr.Get("/", middleware.Protected( + renderer.Template( + "testData/templates/test.html"), http.MethodGet, udb)) rtr.Get("/json", ApiSomething(renderer.JSON("apiData"))) diff --git a/router/router.go b/router/router.go index 2d9ea3c..f80b8f6 100644 --- a/router/router.go +++ b/router/router.go @@ -3,6 +3,7 @@ package router import ( "context" "errors" + "fmt" "html/template" "log" "net/http" @@ -100,6 +101,7 @@ func (self *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { } } + fmt.Printf("%s: %s\n", req.Method, req.URL.Path) /* Otherwise, this is a normal route */ for _, r := range self.routes { diff --git a/testData/static/style.css b/testData/static/style.css new file mode 100644 index 0000000..e69de29 diff --git a/testData/templates/cms_list.html b/testData/templates/cms_list.html new file mode 100644 index 0000000..063a450 --- /dev/null +++ b/testData/templates/cms_list.html @@ -0,0 +1,3 @@ +{{template "header"}} +