This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (162 loc) · 4.53 KB
/
main.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
package dalstore
import (
"net/http"
"time"
"github.com/goincremental/dal"
nSessions "github.com/goincremental/negroni-sessions"
"github.com/gorilla/securecookie"
gSessions "github.com/gorilla/sessions"
)
// New is returns a store object using the provided dal.Connection
func New(connection dal.Connection, database string, collection string, maxAge int,
ensureTTL bool, keyPairs ...[]byte) nSessions.Store {
if ensureTTL {
conn := connection.Clone()
defer conn.Close()
db := conn.DB(database)
c := db.C(collection)
c.EnsureIndex(dal.Index{
Key: []string{"modified"},
Background: true,
Sparse: true,
ExpireAfter: time.Duration(maxAge) * time.Second,
})
}
return &dalStore{
Codecs: securecookie.CodecsFromPairs(keyPairs...),
Token: nSessions.NewCookieToken(),
connection: connection,
database: database,
collection: collection,
options: &gSessions.Options{
MaxAge: maxAge,
},
}
}
func (d *dalStore) Options(options nSessions.Options) {
d.options = &gSessions.Options{
Path: options.Path,
Domain: options.Domain,
MaxAge: options.MaxAge,
Secure: options.Secure,
HttpOnly: options.HTTPOnly,
}
}
type dalSession struct {
ID dal.ObjectID `bson:"_id,omitempty"`
Data string
Modified time.Time
}
type dalStore struct {
Codecs []securecookie.Codec
Token nSessions.TokenGetSetter
connection dal.Connection
database string
collection string
options *gSessions.Options
}
//Implementation of gorilla/sessions.Store interface
// Get registers and returns a session for the given name and session store.
// It returns a new session if there are no sessions registered for the name.
func (d *dalStore) Get(r *http.Request, name string) (*gSessions.Session, error) {
return gSessions.GetRegistry(r).Get(d, name)
}
// New returns a session for the given name without adding it to the registry.
func (d *dalStore) New(r *http.Request, name string) (*gSessions.Session, error) {
var err error
session := gSessions.NewSession(d, name)
options := *d.options
session.Options = &options
session.IsNew = true
if cook, errToken := d.Token.GetToken(r, name); errToken == nil {
err = securecookie.DecodeMulti(name, cook, &session.ID, d.Codecs...)
if err == nil {
ok, err := d.load(session)
session.IsNew = !(err == nil && ok) // not new if no error and data available
}
}
return session, err
}
func (d *dalStore) Save(r *http.Request, w http.ResponseWriter, session *gSessions.Session) error {
if session.Options.MaxAge < 0 {
if err := d.delete(session); err != nil {
return err
}
d.Token.SetToken(w, session.Name(), "", session.Options)
return nil
}
if session.ID == "" {
session.ID = dal.NewObjectID().Hex()
}
if err := d.save(session); err != nil {
return err
}
//save just the id to the cookie, the rest will be saved in the dal store
encoded, err := securecookie.EncodeMulti(session.Name(), session.ID, d.Codecs...)
if err != nil {
return err
}
d.Token.SetToken(w, session.Name(), encoded, session.Options)
return err
}
func (d *dalStore) load(session *gSessions.Session) (bool, error) {
if !dal.IsObjectIDHex(session.ID) {
return false, nSessions.ErrInvalidId
}
conn := d.connection.Clone()
defer conn.Close()
db := conn.DB(d.database)
c := db.C(d.collection)
s := dalSession{}
err := c.FindID(dal.ObjectIDHex(session.ID)).One(&s)
if err != nil {
return false, err
}
if err := securecookie.DecodeMulti(session.Name(), s.Data, &session.Values, d.Codecs...); err != nil {
return false, err
}
return true, nil
}
func (d *dalStore) save(session *gSessions.Session) error {
if !dal.IsObjectIDHex(session.ID) {
return nSessions.ErrInvalidId
}
conn := d.connection.Clone()
defer conn.Close()
db := conn.DB(d.database)
c := db.C(d.collection)
var modified time.Time
if val, ok := session.Values["modified"]; ok {
modified, ok = val.(time.Time)
if !ok {
return nSessions.ErrInvalidModified
}
} else {
modified = time.Now()
}
encoded, err := securecookie.EncodeMulti(session.Name(), session.Values, d.Codecs...)
if err != nil {
return err
}
id := dal.ObjectIDHex(session.ID)
s := dalSession{
ID: id,
Data: encoded,
Modified: modified,
}
_, err = c.SaveID(id, &s)
if err != nil {
return err
}
return nil
}
func (d *dalStore) delete(session *gSessions.Session) error {
if !dal.IsObjectIDHex(session.ID) {
return nSessions.ErrInvalidId
}
conn := d.connection.Clone()
defer conn.Close()
db := conn.DB(d.database)
c := db.C(d.collection)
return c.RemoveID(dal.ObjectIDHex(session.ID))
}