aboutsummaryrefslogtreecommitdiff
path: root/cmd/sklad/session.go
blob: 02fe0b079fe9cbb24e2f199d0e6bd04652cfbdd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main

import (
	"context"
	"encoding/hex"
	"math/rand"
	"net/http"
	"net/url"
)

// session storage indexed by a random UUID
var sessions = map[string]*Session{}

type Session struct {
	LoggedIn bool // may access the DB
}

type sessionContextKey struct{}

func sessionGenId() string {
	u := make([]byte, 16)
	if _, err := rand.Read(u); err != nil {
		panic("cannot generate random bytes")
	}
	return hex.EncodeToString(u)
}

// TODO: We don't want to keep an unlimited amount of cookies in the storage.
//  - The essential question is: how do we avoid DoS?
//  - Which cookies are worth keeping?
//     - Definitely logged-in users, only one person should know the password.
//  - Evict by FIFO? LRU?
func sessionGet(w http.ResponseWriter, r *http.Request) (session *Session) {
	if c, _ := r.Cookie("sessionid"); c != nil {
		session, _ = sessions[c.Value]
	}
	if session == nil {
		id := sessionGenId()
		session = &Session{LoggedIn: false}
		sessions[id] = session
		http.SetCookie(w, &http.Cookie{Name: "sessionid", Value: id})
	}
	return
}

func sessionWrap(inner func(http.ResponseWriter, *http.Request)) func(
	http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		// We might also try no-cache with an ETag for the whole database,
		// though I don't expect any substantial improvements of anything.
		w.Header().Set("Cache-Control", "no-store")

		redirect := "/login"
		if r.RequestURI != "/" && r.Method == http.MethodGet {
			redirect += "?redirect=" + url.QueryEscape(r.RequestURI)
		}

		session := sessionGet(w, r)
		if !session.LoggedIn {
			http.Redirect(w, r, redirect, http.StatusSeeOther)
			return
		}
		inner(w, r.WithContext(
			context.WithValue(r.Context(), sessionContextKey{}, session)))
	}
}