-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
280 lines (238 loc) · 7.2 KB
/
manager.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package session
import (
"context"
"errors"
"log/slog"
"net/http"
"time"
"google.golang.org/protobuf/proto"
)
// sessionMetadata tracks additional information for the session manager to use,
// alongside the session data itself.
type sessionMetadata struct {
CreatedAt time.Time
UpdatedAt time.Time
}
type Store interface {
// GetSession loads the encoded data for a session from the request. If there is no
// session data, it should return nil.
GetSession(r *http.Request) ([]byte, error)
// PutSession saves a session. If a session exists it should be updated,
// otherwise a new session should be created. expiresAt indicates the time
// the data can be considered to be no longer used, and can be garbage
// collected.
PutSession(w http.ResponseWriter, r *http.Request, expiresAt time.Time, data []byte) error
// DeleteSession deletes the session.
DeleteSession(w http.ResponseWriter, r *http.Request) error
}
// Manager is used to automatically manage a typed session. It wraps handlers,
// and loads/saves the session type as needed. It provides methods to interact
// with the session.
type Manager[T any] struct {
store Store
codec codec
newEmpty func() T
opts ManagerOpts[T]
}
var DefaultIdleTimeout = 24 * time.Hour
type ManagerOpts[T any] struct {
MaxLifetime time.Duration
IdleTimeout time.Duration
// Onload is called when a session is retrieved from the Store. It can make
// any changes as needed, returning the session that should be used.
Onload func(T) T
}
func NewManager[T any, PtrT interface {
*T
}](s Store, opts *ManagerOpts[PtrT]) (*Manager[PtrT], error) {
// TODO - options with expiry
m := &Manager[PtrT]{
store: s,
newEmpty: func() PtrT {
return PtrT(new(T))
},
opts: ManagerOpts[PtrT]{
IdleTimeout: DefaultIdleTimeout,
},
}
if opts != nil {
m.opts = *opts
}
if m.opts.IdleTimeout == 0 && m.opts.MaxLifetime == 0 {
return nil, errors.New("at least one of idle timeout or max lifetime must be specified")
}
if _, ok := any(m.newEmpty()).(proto.Message); ok {
m.codec = &protoCodec{}
} else {
m.codec = &jsonCodec{}
}
return m, nil
}
func (m *Manager[T]) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, ok := r.Context().Value(mgrSessCtxKey[T]{inst: m}).(*sessCtx[T]); ok {
// already wrapped for this instance, noop
next.ServeHTTP(w, r)
return
}
sctx := &sessCtx[T]{
metadata: &sessionMetadata{
CreatedAt: time.Now(),
},
data: m.newEmpty(),
}
data, err := m.store.GetSession(r)
if err != nil {
m.handleErr(w, r, err)
return
}
if data != nil {
md, err := m.codec.Decode(data, sctx.data)
if err != nil {
m.handleErr(w, r, err)
return
}
sctx.metadata = md
// track the original data if we have an idle timeout, so we can
// short path re-save it.
if m.opts.IdleTimeout != 0 {
sctx.datab = data
}
if m.opts.Onload != nil {
sctx.data = m.opts.Onload(sctx.data)
}
}
r = r.WithContext(context.WithValue(r.Context(), mgrSessCtxKey[T]{inst: m}, sctx))
hw := &hookRW{
ResponseWriter: w,
hook: m.saveHook(r, sctx),
}
next.ServeHTTP(hw, r)
// if the handler doesn't write anything, make sure we fire the hook
// anyway.
hw.hookOnce.Do(func() {
hw.hook(hw.ResponseWriter)
})
})
}
// Get returns a pointer to the current session. exist indicates if an existing
// session was loaded, otherwise a new session was started
func (m *Manager[T]) Get(ctx context.Context) (_ T) {
sessCtx, ok := ctx.Value(mgrSessCtxKey[T]{inst: m}).(*sessCtx[T])
if !ok {
panic("context contained no or invalid session")
}
return sessCtx.data
}
// Save sets the session data, and marks it to be saved at the end of the
// request.
func (m *Manager[T]) Save(ctx context.Context, sess T) {
sessCtx, ok := ctx.Value(mgrSessCtxKey[T]{inst: m}).(*sessCtx[T])
if !ok {
panic("context contained no or invalid session")
}
sessCtx.delete = false
sessCtx.save = true
sessCtx.data = sess
}
// Delete marks the session for deletion at the end of the request, and discards
// the current session's data.
func (m *Manager[T]) Delete(ctx context.Context) {
sessCtx, ok := ctx.Value(mgrSessCtxKey[T]{inst: m}).(*sessCtx[T])
if !ok {
panic("context contained no or invalid session")
}
sessCtx.datab = nil
sessCtx.data = m.newEmpty()
sessCtx.delete = true
sessCtx.save = false
sessCtx.reset = false
}
// Reset rotates the session ID. Used to avoid session fixation, should be
// called on privilege elevation. This should be called at the end of a request.
// If this is not supported by the store, this will no-op.
func (m *Manager[T]) Reset(ctx context.Context, sess T) {
sessCtx, ok := ctx.Value(mgrSessCtxKey[T]{inst: m}).(*sessCtx[T])
if !ok {
panic("context contained no or invalid session")
}
sessCtx.data = sess
sessCtx.datab = nil
sessCtx.save = false
sessCtx.delete = false
sessCtx.reset = true
}
func (m *Manager[T]) handleErr(w http.ResponseWriter, r *http.Request, err error) {
slog.ErrorContext(r.Context(), "error in session manager", "err", err)
http.Error(w, "Internal Error", http.StatusInternalServerError)
}
func (m *Manager[T]) saveHook(r *http.Request, sctx *sessCtx[T]) func(w http.ResponseWriter) bool {
return func(w http.ResponseWriter) bool {
sctx.metadata.UpdatedAt = time.Now()
// if we have delete or reset, delete the session
if sctx.delete || sctx.reset {
if err := m.store.DeleteSession(w, r); err != nil {
m.handleErr(w, r, err)
return false
}
}
// if we have reset or save, save the session
if sctx.save || sctx.reset {
sb, err := m.codec.Encode(sctx.data, sctx.metadata)
if err != nil {
m.handleErr(w, r, err)
return false
}
if err := m.store.PutSession(w, r, m.calculateExpiry(sctx.metadata), sb); err != nil {
m.handleErr(w, r, err)
return false
}
} else if m.opts.IdleTimeout != 0 && len(sctx.datab) != 0 {
// always need to bump the last access time. If we weren't marked to
// save, do this with the original data.
if err := m.store.PutSession(w, r, m.calculateExpiry(sctx.metadata), sctx.datab); err != nil {
m.handleErr(w, r, err)
return false
}
}
return true
}
}
func (m *Manager[T]) calculateExpiry(md *sessionMetadata) time.Time {
var invalidTimes []time.Time
if m.opts.MaxLifetime != 0 {
maxInvalidAt := md.CreatedAt.Add(m.opts.MaxLifetime)
invalidTimes = append(invalidTimes, maxInvalidAt)
}
if m.opts.IdleTimeout != 0 {
var idleInvalidAt time.Time
if !md.UpdatedAt.IsZero() {
idleInvalidAt = md.UpdatedAt.Add(m.opts.IdleTimeout)
} else {
idleInvalidAt = md.CreatedAt.Add(m.opts.IdleTimeout)
}
invalidTimes = append(invalidTimes, idleInvalidAt)
}
if len(invalidTimes) == 0 {
return time.Time{}
}
earliestInvalidAt := invalidTimes[0]
for _, t := range invalidTimes[1:] {
if t.Before(earliestInvalidAt) {
earliestInvalidAt = t
}
}
return earliestInvalidAt
}
type mgrSessCtxKey[T any] struct{ inst *Manager[T] }
type sessCtx[T any] struct {
metadata *sessionMetadata
// data is the actual session data
data T
// datab is the original loaded data bytes. Used for idle timeout, when a
// save may happen without data modification
datab []byte
delete bool
save bool
reset bool
}