fix middleware, add diagnostics to router

This commit is contained in:
Iris Lightshard 2022-05-17 22:29:40 -06:00
parent 0e5a81f27b
commit 7c0d0c864a
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
9 changed files with 59 additions and 8 deletions

View file

@ -2,12 +2,13 @@ package middleware
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"nilfm.cc/git/quartzgun/auth" "nilfm.cc/git/quartzgun/auth"
"nilfm.cc/git/quartzgun/cookie" "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) { handlerFunc := func(w http.ResponseWriter, req *http.Request) {
user, err := cookie.GetToken("user", req) user, err := cookie.GetToken("user", req)
if err == nil { if err == nil {
@ -15,13 +16,17 @@ func Protected(next http.Handler, userStore auth.UserStore) http.Handler {
if err == nil { if err == nil {
login, err := userStore.ValidateUser(user, session) login, err := userStore.ValidateUser(user, session)
if err == nil && login { if err == nil && login {
fmt.Printf("authorized!\n")
fmt.Printf("user: %s, session: %s\n", user, session)
req.Method = method
next.ServeHTTP(w, req) next.ServeHTTP(w, req)
return return
} }
} }
} }
fmt.Printf("unauthorized...\n")
req.Method = http.MethodGet req.Method = http.MethodGet
http.Redirect(w, req, "/login", http.StatusTemporaryRedirect) http.Redirect(w, req, "/login", http.StatusSeeOther)
} }
return http.HandlerFunc(handlerFunc) return http.HandlerFunc(handlerFunc)
@ -37,15 +42,17 @@ func Authorize(next string, userStore auth.UserStore) http.Handler {
24*7*52) 24*7*52)
if err == nil { if err == nil {
req.Method = http.MethodGet 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 { } else {
*req = *req.WithContext( *req = *req.WithContext(
context.WithValue( context.WithValue(
req.Context(), req.Context(),
"message", "message",
"Incorrect credentials")) "Incorrect credentials"))
fmt.Printf("login failed!\n")
req.Method = http.MethodGet req.Method = http.MethodGet
http.Redirect(w, req, "/login", http.StatusTemporaryRedirect) http.Redirect(w, req, "/login", http.StatusSeeOther)
} }
} }

View file

@ -6,6 +6,7 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"nilfm.cc/git/quartzgun/indentalUserDB" "nilfm.cc/git/quartzgun/indentalUserDB"
"nilfm.cc/git/quartzgun/middleware"
"nilfm.cc/git/quartzgun/renderer" "nilfm.cc/git/quartzgun/renderer"
"nilfm.cc/git/quartzgun/router" "nilfm.cc/git/quartzgun/router"
"testing" "testing"
@ -43,7 +44,14 @@ func TestMain(m *testing.M) {
Fallback: *template.Must(template.ParseFiles("testData/templates/error.html", "testData/templates/footer.html")), 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"))) rtr.Get("/json", ApiSomething(renderer.JSON("apiData")))

View file

@ -3,6 +3,7 @@ package router
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"html/template" "html/template"
"log" "log"
"net/http" "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 */ /* Otherwise, this is a normal route */
for _, r := range self.routes { for _, r := range self.routes {

View file

View file

@ -0,0 +1,3 @@
{{template "header"}}
<h1>It works!</h1>
{{template "footer"}}

View file

@ -6,7 +6,6 @@
<meta charset='utf-8'> <meta charset='utf-8'>
<meta name='viewport' content='width=device-width,initial-scale=1'> <meta name='viewport' content='width=device-width,initial-scale=1'>
<link rel='stylesheet' type='text/css' href='/style.css'>
<link rel='shortcut icon' href='/favicon.ico'> <link rel='shortcut icon' href='/favicon.ico'>
<title>test &mdash; error</title> <title>test &mdash; error</title>
</head> </head>

View file

@ -1,4 +1,4 @@
{{ define "footer" }} {{define "footer"}}
</body> </body>
</html> </html>
{{ end }} {{end}}

View file

@ -0,0 +1,11 @@
{{define "header"}}
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='description' content='Nirvash CMS'/>
<meta name='viewport' content='width=device-width,initial-scale=1'>
<title>Nirvash &mdash; Test</title>
</head>
<body>
{{end}}

View file

@ -0,0 +1,21 @@
{{ $errorMsg := (.Context).Value "message" }}
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='description' content='Nirvash CMS'/>
<meta name='viewport' content='width=device-width,initial-scale=1'>
<title>Nirvash &mdash; Login</title>
</head>
<body>
{{ if $errorMsg }}
<div class="error">{{ $errorMsg }}</div>
{{ end }}
<form action='/login' method='post'>
<input type="text" name="user" placeholder="user">
<input type="password" name="password" placeholder="password">
<input type="submit" value="Login">
</form>
</body>
</html>