Add half arsed user separation

This commit is contained in:
Maximilian Friedersdorff 2025-07-30 13:41:03 +01:00
parent 3c792decd6
commit 25bcf4d706
6 changed files with 125 additions and 41 deletions

View file

@ -1,7 +1,6 @@
package main
import (
"crypto/rand"
"flag"
"log"
"net"
@ -17,7 +16,7 @@ import (
func main() {
var confFile string
cache := make(map[string]string, 20)
sessions := middleware.NewSessionStore()
flag.StringVar(&confFile, "c", "/etc/gonotes/conf.toml", "Specify path to config file.")
flag.Parse()
@ -39,22 +38,28 @@ func main() {
if !conf.Conf.Production {
router.HandleFunc("/login/", func(w http.ResponseWriter, r *http.Request) {
user := r.FormValue("user")
log.Printf("Trying to log in %s", user)
sessionID := rand.Text()
cache[sessionID] = user
cookie := http.Cookie{
Name: "id", Value: sessionID, MaxAge: 3600,
Secure: true, HttpOnly: true, Path: "/",
if len(user) == 0 {
user = "anon"
}
http.SetCookie(w, &cookie)
sessions.Login(user, w)
http.Redirect(w, r, "/notes/", http.StatusFound)
})
}
router.Handle("/logout/", sessions.AsMiddleware(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := r.FormValue("user")
if len(user) == 0 {
user = "anon"
}
sessions.Logout(w, r)
http.Redirect(w, r, "/notes/", http.StatusFound)
})))
router.Handle("/", middleware.LoggingMiddleware(http.RedirectHandler("/notes/", http.StatusFound)))
router.Handle("/notes/", middleware.SessionMiddleware(cache, middleware.LoggingMiddleware(http.StripPrefix("/notes", notesRouter))))
router.Handle("/notes/", sessions.AsMiddleware(middleware.LoggingMiddleware(http.StripPrefix("/notes", notesRouter))))
router.Handle(
"/static/",
middleware.LoggingMiddleware(