/
redis_store.go
211 lines (189 loc) · 5.22 KB
/
redis_store.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
package sessions
import (
"bytes"
"context"
"encoding/base32"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
khttp "github.com/go-kratos/kratos/v2/transport/http"
"github.com/gorilla/securecookie"
"github.com/redis/go-redis/v9"
)
// Amount of time for cookies/redis keys to expire.
var sessionExpire = 86400 * 30
// SessionSerializer provides an interface hook for alternative serializers
type SessionSerializer interface {
Deserialize(d []byte, ss *Session) error
Serialize(ss *Session) ([]byte, error)
}
// JSONSerializer encode the session map to JSON.
type JSONSerializer struct{}
func (s JSONSerializer) Serialize(ss *Session) ([]byte, error) {
m := make(map[string]any, len(ss.Values))
for k, v := range ss.Values {
ks, ok := k.(string)
if !ok {
err := fmt.Errorf("non-string key value, cannot serialize session to JSON: %v", k)
return nil, err
}
m[ks] = v
}
return json.Marshal(m)
}
func (s JSONSerializer) Deserialize(d []byte, ss *Session) error {
m := make(map[string]any)
err := json.Unmarshal(d, &m)
if err != nil {
return err
}
for k, v := range m {
ss.Values[k] = v
}
return nil
}
// GobSerializer uses gob package to encode the session map
type GobSerializer struct{}
func (s GobSerializer) Serialize(ss *Session) ([]byte, error) {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
err := enc.Encode(ss.Values)
if err == nil {
return buf.Bytes(), nil
}
return nil, err
}
func (s GobSerializer) Deserialize(d []byte, ss *Session) error {
dec := gob.NewDecoder(bytes.NewBuffer(d))
return dec.Decode(&ss.Values)
}
// RedisStore stores sessions in a redis backend.
type RedisStore struct {
rdCmd redis.Cmdable
serializer SessionSerializer
Options *Options
keyPrefix string
Codecs []securecookie.Codec
DefaultMaxAge int
maxLength int
}
func (s *RedisStore) SetMaxLength(l int) {
if l >= 0 {
s.maxLength = l
}
}
// SetKeyPrefix set the prefix
func (s *RedisStore) SetKeyPrefix(p string) {
s.keyPrefix = p
}
// SetSerializer sets the serializer
func (s *RedisStore) SetSerializer(ss SessionSerializer) {
s.serializer = ss
}
func (s *RedisStore) SetMaxAge(v int) {
var c *securecookie.SecureCookie
var ok bool
s.Options.MaxAge = v
for i := range s.Codecs {
if c, ok = s.Codecs[i].(*securecookie.SecureCookie); ok {
c.MaxAge(v)
} else {
fmt.Printf("Can't change MaxAge on codec %v\n", s.Codecs[i])
}
}
}
func NewRedisStore(rdCmd redis.Cmdable, keyPairs ...[]byte) (*RedisStore, error) {
rs := &RedisStore{
rdCmd: rdCmd,
Codecs: securecookie.CodecsFromPairs(keyPairs...),
Options: &Options{
Path: "/",
MaxAge: sessionExpire,
},
DefaultMaxAge: 60 * 20, // 20 minutes seems like a reasonable default
maxLength: 4096,
keyPrefix: "pallas_session:",
serializer: GobSerializer{},
}
return rs, nil
}
func (s *RedisStore) Get(ht *khttp.Transport, name string) (*Session, error) {
return GetRegistry(ht).Get(s, name)
}
func (s *RedisStore) New(ht *khttp.Transport, name string) (*Session, error) {
var (
err error
ok bool
)
session := NewSession(s, name)
// make a copy
options := *s.Options
session.Options = &options
session.IsNew = true
if c, errCookie := ht.Request().Cookie(name); errCookie == nil {
err = securecookie.DecodeMulti(name, c.Value, &session.ID, s.Codecs...)
if err == nil {
ok, err = s.load(ht.Request().Context(), session)
session.IsNew = !(err == nil && ok) // not new if no error and data available
}
}
return session, err
}
func (s *RedisStore) Save(ht *khttp.Transport, session *Session) error {
// Marked for deletion.
if session.Options.MaxAge <= 0 {
if err := s.delete(ht.Request().Context(), session); err != nil {
return err
}
setCookie(ht, NewCookie(session.Name(), "", session.Options))
} else {
// Build an alphanumeric key for the redis store.
if session.ID == "" {
session.ID = strings.TrimRight(base32.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(32)), "=")
}
if err := s.save(ht.Request().Context(), session); err != nil {
return err
}
encoded, err := securecookie.EncodeMulti(session.Name(), session.ID, s.Codecs...)
if err != nil {
return err
}
setCookie(ht, NewCookie(session.Name(), encoded, session.Options))
}
return nil
}
// save stores the session in redis.
func (s *RedisStore) save(ctx context.Context, session *Session) error {
b, err := s.serializer.Serialize(session)
if err != nil {
return err
}
if s.maxLength != 0 && len(b) > s.maxLength {
return errors.New("SessionStore: the value to store is too big")
}
age := session.Options.MaxAge
if age == 0 {
age = s.DefaultMaxAge
}
err = s.rdCmd.SetEx(ctx, s.keyPrefix+session.ID, b, time.Duration(age)*time.Second).Err()
return err
}
func (s *RedisStore) load(ctx context.Context, session *Session) (bool, error) {
data, err := s.rdCmd.Get(ctx, s.keyPrefix+session.ID).Bytes()
if err != nil && !errors.Is(err, redis.Nil) {
return false, err
}
if data == nil {
return false, nil // no data was associated with this key
}
return true, s.serializer.Deserialize(data, session)
}
func (s *RedisStore) delete(ctx context.Context, session *Session) error {
if err := s.rdCmd.Del(ctx, s.keyPrefix+session.ID).Err(); err != nil {
return err
}
return nil
}