fix middleware, add diagnostics to router
This commit is contained in:
parent
0e5a81f27b
commit
7c0d0c864a
9 changed files with 59 additions and 8 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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")))
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
|
0
testData/static/style.css
Normal file
0
testData/static/style.css
Normal file
3
testData/templates/cms_list.html
Normal file
3
testData/templates/cms_list.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
{{template "header"}}
|
||||
<h1>It works!</h1>
|
||||
{{template "footer"}}
|
|
@ -6,7 +6,6 @@
|
|||
<meta charset='utf-8'>
|
||||
|
||||
<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'>
|
||||
<title>test — error</title>
|
||||
</head>
|
||||
|
|
11
testData/templates/header.html
Normal file
11
testData/templates/header.html
Normal 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 — Test</title>
|
||||
</head>
|
||||
<body>
|
||||
{{end}}
|
21
testData/templates/login.html
Normal file
21
testData/templates/login.html
Normal 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 — 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>
|
Loading…
Reference in a new issue