This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsession_store.go
168 lines (146 loc) · 4.32 KB
/
session_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
package hero
import (
"encoding/base32"
"net/http"
"strings"
"time"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"github.com/jinzhu/gorm"
)
const (
defaultSessionMaxAge = 2592000
defaultSessionPath = "/"
)
// Store is a Store implementation for gorilla session. It support mysql, postgresql and
// foundation databases
type Store struct {
q *query
codecs []securecookie.Codec
options *sessions.Options
}
// DefaultStore returns a *Store with default values.
func DefaultStore(db *gorm.DB) *Store {
keyPairs := [][]byte{
[]byte("ePAPW9vJv7gHoftvQTyNj5VkWB52mlza"),
[]byte("N8SmpJ00aSpepNrKoyYxmAJhwVuKEWZD"),
}
cfg := &Config{
SessionMaxAge: defaultSessionMaxAge,
SessionPath: defaultSessionPath,
}
return NewStore(db, cfg, keyPairs...)
}
// NewStore creates a new *Store instance.
func NewStore(db *gorm.DB, config *Config, keyPairs ...[]byte) *Store {
q := &query{}
q.DB = db
return &Store{
q: q,
codecs: securecookie.CodecsFromPairs(keyPairs...),
options: &sessions.Options{
Path: config.SessionPath,
Domain: config.SessionDomain,
MaxAge: config.SessionMaxAge,
Secure: config.SessionSecure,
HttpOnly: config.SessionHTTPOnly,
},
}
}
// Get fetches a session for a given name after it has been added to the registry.
func (s *Store) Get(r *http.Request, name string) (*sessions.Session, error) {
return sessions.GetRegistry(r).Get(s, name)
}
// New returns a new session
func (s *Store) 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 c, errCookie := r.Cookie(name); errCookie == nil {
err = securecookie.DecodeMulti(name, c.Value, &session.ID, s.codecs...)
if err == nil {
err = s.load(session)
if err == nil {
session.IsNew = false
}
}
}
return session, err
}
// Save saves the session into a postgresql database
func (s *Store) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
// Set delete if max-age is < 0
if session.Options.MaxAge < 0 {
if err := s.Delete(r, w, session); err != nil {
return err
}
http.SetCookie(w, sessions.NewCookie(session.Name(), "", session.Options))
return nil
}
if session.ID == "" {
// Generate a random session ID key suitable for storage in the DB
session.ID = strings.TrimRight(
base32.StdEncoding.EncodeToString(
securecookie.GenerateRandomKey(32)), "=")
}
if err := s.save(session); err != nil {
return err
}
// Keep the session ID key in a cookie so it can be looked up in DB later.
encoded, err := securecookie.EncodeMulti(session.Name(), session.ID, s.codecs...)
if err != nil {
return err
}
http.SetCookie(w, sessions.NewCookie(session.Name(), encoded, session.Options))
return nil
}
//load fetches a session by ID from the database and decodes its content into session.Values
func (s *Store) load(session *sessions.Session) error {
ss, err := s.q.GetSessionByKey(session.ID)
if err != nil {
return err
}
return securecookie.DecodeMulti(session.Name(), string(ss.Data),
&session.Values, s.codecs...)
}
func (s *Store) save(session *sessions.Session) error {
encoded, err := securecookie.EncodeMulti(session.Name(), session.Values,
s.codecs...)
if err != nil {
return err
}
var expiresOn time.Time
exOn := session.Values["expires_on"]
if exOn == nil {
expiresOn = time.Now().Add(time.Second * time.Duration(session.Options.MaxAge))
} else {
expiresOn = exOn.(time.Time)
if expiresOn.Sub(time.Now().Add(time.Second*time.Duration(session.Options.MaxAge))) < 0 {
expiresOn = time.Now().Add(time.Second * time.Duration(session.Options.MaxAge))
}
}
ss := &Session{
Key: session.ID,
Data: encoded,
ExpiresOn: expiresOn,
}
if session.IsNew {
return s.q.SaveSession(ss)
}
return s.q.UpdateSession(ss)
}
func (s *Store) destroy(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
options := *s.options
options.MaxAge = -1
http.SetCookie(w, sessions.NewCookie(session.Name(), "", &options))
for k := range session.Values {
delete(session.Values, k)
}
return s.q.DeleteSession(session.ID)
}
// Delete deletes session.
func (s *Store) Delete(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
return s.destroy(r, w, session)
}