-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdoc.go
65 lines (53 loc) · 1.86 KB
/
doc.go
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
// Copyright 2017 Bobby Powers. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
/*
Package seshcookie enables you to associate session-state with HTTP
requests while keeping your server stateless. Because session-state
is transferred as part of the HTTP request (in a cookie), state can be
seamlessly maintained between server restarts or load balancing. It's
inspired by Beaker (http://pypi.python.org/pypi/Beaker), which
provides a similar service for Python webapps. The cookies are
authenticated and encrypted (using AES-GCM) with a key derived from a
string provided to the NewHandler function. This makes seshcookie
reliable and secure: session contents are opaque to users and not able
to be manipulated or forged by third parties.
Storing session-state in a cookie makes building some apps trivial,
like this example that tells a user how many times they have visited
the site:
package main
import (
"net/http"
"log"
"fmt"
"github.com/bpowers/seshcookie"
)
type VisitedHandler struct{}
func (h *VisitedHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
return
}
session := seshcookie.GetSession(req.Context())
count, _ := session["count"].(int)
count++
session["count"] = count
rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(200)
if count == 1 {
rw.Write([]byte("this is your first visit, welcome!"))
} else {
rw.Write([]byte(fmt.Sprintf("page view #%d", count)))
}
}
func main() {
key := "session key, preferably a sequence of data from /dev/urandom"
http.Handle("/", seshcookie.NewHandler(
&VisitedHandler{},
key,
&seshcookie.Config{HTTPOnly: true, Secure: false}))
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("ListenAndServe: %s", err)
}
}
*/
package seshcookie