-
Notifications
You must be signed in to change notification settings - Fork 8
/
gsmstub.go
139 lines (123 loc) · 3.92 KB
/
gsmstub.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gsm
import (
"encoding/base32"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"net/http"
"strings"
)
// NewDumbMemorySessionStoreWithValueStorer return a new dumb in-memory
// map and no expiration backed by a ValueStorer. Good for local
// development so you don't have to run
// memcached on your laptop just to fire up
// your app and hack away.
// A ValueStorer is used to store an encrypted sessionID. The encrypted sessionID is used to access
// the dumb in-memory map and get the session values.
func NewDumbMemorySessionStoreWithValueStorer(valueStorer ValueStorer) *DumbMemoryStore {
if valueStorer == nil {
panic("Cannot have nil ValueStorer")
}
keyPair := []byte("stub")
return &DumbMemoryStore{
Codecs: securecookie.CodecsFromPairs(keyPair),
Options: &sessions.Options{
Path: "/",
MaxAge: 86400 * 30,
},
Data: make(map[string]string),
ValueStorer: valueStorer,
}
}
// Sessions implemented with a dumb in-memory
// map and no expiration. Good for local
// development so you don't have to run
// memcached on your laptop just to fire up
// your app and hack away.
func NewDumbMemorySessionStore() *DumbMemoryStore {
return NewDumbMemorySessionStoreWithValueStorer(&CookieStorer{})
}
// DumbMemoryStore stores sessions in memcache
//
type DumbMemoryStore struct {
Codecs []securecookie.Codec
Options *sessions.Options // default configuration
Data map[string]string // session data goes here
ValueStorer ValueStorer
}
// MaxLength restricts the maximum length of new sessions to l.
// If l is 0 there is no limit to the size of a session, use with caution.
// The default for a new DumbMemoryStore is 4096.
func (s *DumbMemoryStore) MaxLength(l int) {
for _, c := range s.Codecs {
if codec, ok := c.(*securecookie.SecureCookie); ok {
codec.MaxLength(l)
}
}
}
// Get returns a session for the given name after adding it to the registry.
//
// See CookieStore.Get().
func (s *DumbMemoryStore) Get(r *http.Request, name string) (*sessions.Session, error) {
return sessions.GetRegistry(r).Get(s, name)
}
// New returns a session for the given name without adding it to the registry.
//
// See CookieStore.New().
func (s *DumbMemoryStore) New(r *http.Request, name string) (*sessions.Session, error) {
session := sessions.NewSession(s, name)
opts := *s.Options
session.Options = &opts
session.IsNew = true
var err error
if value, errCookie := s.ValueStorer.GetValueForSessionName(r, name); errCookie == nil {
err = securecookie.DecodeMulti(name, value, &session.ID, s.Codecs...)
if err == nil {
err = s.load(session)
if err == nil {
session.IsNew = false
}
}
}
return session, err
}
// Save adds a single session to the response.
func (s *DumbMemoryStore) Save(r *http.Request, w http.ResponseWriter,
session *sessions.Session) error {
if session.ID == "" {
// Because the ID is used in the filename, encode it to
// use alphanumeric characters only.
session.ID = strings.TrimRight(
base32.StdEncoding.EncodeToString(
securecookie.GenerateRandomKey(32)), "=")
}
if err := s.save(session); err != nil {
return err
}
encoded, err := securecookie.EncodeMulti(session.Name(), session.ID,
s.Codecs...)
if err != nil {
return err
}
if err := s.ValueStorer.SetValueForSessionName(w, session.Name(), encoded, session.Options); err != nil {
return err
}
return nil
}
// save writes encoded session.Values using the memcache client
func (s *DumbMemoryStore) save(session *sessions.Session) error {
encoded, err := securecookie.EncodeMulti(session.Name(), session.Values,
s.Codecs...)
if err != nil {
return err
}
s.Data[session.ID] = encoded
return nil
}
// load reads a file and decodes its content into session.Values.
func (s *DumbMemoryStore) load(session *sessions.Session) error {
if err := securecookie.DecodeMulti(session.Name(), string(s.Data[session.ID]),
&session.Values, s.Codecs...); err != nil {
return err
}
return nil
}