Make start on session management

This commit is contained in:
Maximilian Friedersdorff 2025-07-30 09:35:01 +01:00
parent 352d9555ba
commit 17dd20478d
4 changed files with 65 additions and 7 deletions

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"crypto/rand"
"flag" "flag"
"log" "log"
"net" "net"
@ -16,6 +17,8 @@ import (
func main() { func main() {
var confFile string var confFile string
cache := make(map[string]string, 20)
flag.StringVar(&confFile, "c", "/etc/gonotes/conf.toml", "Specify path to config file.") flag.StringVar(&confFile, "c", "/etc/gonotes/conf.toml", "Specify path to config file.")
flag.Parse() flag.Parse()
@ -33,8 +36,28 @@ func main() {
etag := middleware.NewETag("static", cacheExpiration) etag := middleware.NewETag("static", cacheExpiration)
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
// TODO: omg remove this
log.Printf("Session id is %s", sessionID)
cookie := http.Cookie{
Name: "id", Value: sessionID, MaxAge: 3600,
Secure: true, HttpOnly: true, Path: "/",
}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/notes/", http.StatusFound)
})
}
router.Handle("/", middleware.LoggingMiddleware(http.RedirectHandler("/notes/", http.StatusFound))) router.Handle("/", middleware.LoggingMiddleware(http.RedirectHandler("/notes/", http.StatusFound)))
router.Handle("/notes/", middleware.LoggingMiddleware(http.StripPrefix("/notes", notesRouter))) router.Handle("/notes/", middleware.SessionMiddleware(cache, middleware.LoggingMiddleware(http.StripPrefix("/notes", notesRouter))))
router.Handle( router.Handle(
"/static/", "/static/",
middleware.LoggingMiddleware( middleware.LoggingMiddleware(

2
go.mod
View file

@ -1,6 +1,6 @@
module forgejo.gwairfelin.com/max/gonotes module forgejo.gwairfelin.com/max/gonotes
go 1.23.5 go 1.24.5
require github.com/yuin/goldmark v1.7.8 require github.com/yuin/goldmark v1.7.8

View file

@ -54,11 +54,12 @@ func (asset *Asset) fetchIfNotExists(staticPath string) {
} }
type Config struct { type Config struct {
Address string Address string
Protocol string Protocol string
Extension string Extension string
NotesDir string NotesDir string
LogAccess bool LogAccess bool
Production bool
} }
var ( var (

View file

@ -0,0 +1,34 @@
// Middleware to deal with sessions
package middleware
import (
"context"
"log"
"net/http"
)
func SessionMiddleware(cache map[string]string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sessionCookie, err := r.Cookie("id")
// No session yet
if err != nil {
http.Redirect(w, r, "/login/", http.StatusUnauthorized)
return
}
user, ok := cache[sessionCookie.Value]
// Session expired
if !ok {
http.Redirect(w, r, "/login/", http.StatusUnauthorized)
return
}
log.Printf("User is %s", user)
ctx := r.Context()
ctx = context.WithValue(ctx, "user", user)
next.ServeHTTP(w, r.WithContext(ctx))
})
}