This repository was archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstore.go
211 lines (187 loc) · 5.94 KB
/
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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package firestoregorilla is a Firestore-backed sessions store, which can be
// used with gorilla/sessions.
//
// Encoded sessions are stored in Firestore and session IDs are stored in
// secure cookies.
//
// Sessions never expire and are never deleted or cleaned up.
package firestoregorilla
import (
"context"
"encoding/json"
"fmt"
"net/http"
"cloud.google.com/go/firestore"
"github.com/gorilla/sessions"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// maxLength is the maximum length of an encoded session that can be stored
// in a Store. See https://firebase.google.com/docs/firestore/quotas.
const maxLength = 2 << 20
// Store is a Firestore-backed sessions store.
type Store struct {
client *firestore.Client
}
var _ sessions.Store = &Store{}
// sessionDoc wraps an encoded session so it can be saved as a Firestore
// document.
type sessionDoc struct {
EncodedSession string
}
// New creates a new Store.
//
// Only string key values are supported for sessions.
func New(ctx context.Context, client *firestore.Client) (*Store, error) {
return &Store{
client: client,
}, nil
}
// Get returns a cached session, if it exists. Otherwise, Get returns a new
// session.
//
// The name is used as the cookie name and Firestore collection name, so
// different apps in the same Google Cloud project should use different names.
func (s *Store) Get(r *http.Request, name string) (*sessions.Session, error) {
return sessions.GetRegistry(r).Get(s, name)
}
// New creates and returns a new session.
//
// If the session already exists, it will be returned.
//
// The name is used as the cookie name and Firestore collection name, so
// different apps in the same Google Cloud project should use different names.
func (s *Store) New(r *http.Request, name string) (*sessions.Session, error) {
session := sessions.NewSession(s, name)
// Ignore errors in case the cookie isn't set yet.
id, _ := s.readIDFromCookie(r, name)
if id == "" {
// No ID in the cookie means the session is new.
session.IsNew = true
return session, nil
}
// ID found, check if the session already exists.
ds, err := s.client.Collection(name).Doc(id).Get(r.Context())
if status.Code(err) == codes.NotFound {
// A NotFound error means the session is new.
session.IsNew = true
return session, nil
}
if err != nil {
return session, fmt.Errorf("Get: %v", err)
}
// The session was found, get it.
encoded := sessionDoc{}
if err := ds.DataTo(&encoded); err != nil {
return session, fmt.Errorf("DataTo: %v", err)
}
cachedSession, err := s.deserialize(encoded.EncodedSession)
if err != nil {
return session, err
}
session.ID = cachedSession.ID
session.Values = cachedSession.Values
session.Options = cachedSession.Options
session.IsNew = false
return session, nil
}
// Save persists the session to Firestore and the session ID in a cookie.
func (s *Store) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
id := session.ID
if id == "" {
// Ignore errors in case the cookie isn't set yet.
id, _ = s.readIDFromCookie(r, session.Name())
}
if id == "" {
id = s.client.Collection(session.Name()).NewDoc().ID
}
session.ID = id
sessionString, err := s.serialize(session)
if err != nil {
return err
}
encoded := sessionDoc{EncodedSession: sessionString}
if _, err := s.client.Collection(session.Name()).Doc(id).Set(r.Context(), encoded); err != nil {
return fmt.Errorf("Create: %v", err)
}
s.saveIDInCookie(w, session.Name(), id)
return nil
}
// readIDFromCookie gets the ID stored int the cookie, if it exists.
// It returns an error if the cookie is not set.
func (s *Store) readIDFromCookie(r *http.Request, name string) (string, error) {
c, err := r.Cookie(name)
if err != nil {
return "", fmt.Errorf("Cookie: %v", err)
}
return c.Value, nil
}
// saveIDInCookie saves the ID in a cookie.
func (s *Store) saveIDInCookie(w http.ResponseWriter, name, id string) {
cookie := &http.Cookie{
Name: name,
Value: id,
}
http.SetCookie(w, cookie)
}
// jsonSession is an encoding/json compatible version of sessions.Session.
type jsonSession struct {
Values map[string]interface{}
ID string
Options *sessions.Options
}
// serialize serializes the session into a JSON string. Only string key values
// are supported. encoding/gob could be used to support non-string keys, but it
// is slower and leads to larger sessions.
func (s *Store) serialize(session *sessions.Session) (string, error) {
values := map[string]interface{}{}
for k, v := range session.Values {
ks, ok := k.(string)
if !ok {
return "", fmt.Errorf("only string keys supported: %v", k)
}
values[ks] = v
}
jSession := jsonSession{
Values: values,
ID: session.ID,
Options: session.Options,
}
b, err := json.Marshal(jSession)
if err != nil {
return "", fmt.Errorf("json.Marshal: %v", err)
}
if len(b) > maxLength {
return "", fmt.Errorf("max length of session exceeded: %d > %d", len(b), maxLength)
}
return string(b), nil
}
// deserialize decodes a session.
func (*Store) deserialize(s string) (*sessions.Session, error) {
jSession := jsonSession{}
if err := json.Unmarshal([]byte(s), &jSession); err != nil {
return nil, fmt.Errorf("json.Unmarshal: %v", err)
}
values := map[interface{}]interface{}{}
for k, v := range jSession.Values {
values[k] = v
}
return &sessions.Session{
Values: values,
ID: jSession.ID,
Options: jSession.Options,
}, nil
}