-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
194 lines (138 loc) · 3.66 KB
/
sessions.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
package sessions
import (
"encoding/gob"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"github.com/cj123/sessions"
"github.com/cj123/formulate"
)
func init() {
gob.Register([]formulate.ValidationError{})
}
// Store implements formulate.ValidationStore using a combination of both HTTP session and filesystem.
// ValidationErrors are stored in the HTTP session, but the FormValue is stored in the os.TempDir() in a
// JSON encoded blob, with the filename formulate_val_* where * is replaced by a random string. The filesystem
// storage is used as the session storage is limited to 4096 bytes in most browsers.
type Store struct {
r *http.Request
w http.ResponseWriter
sessionsStore sessions.Store
sessionName string
}
// NewStore creates a session store for saving validation. The sessionName provided must be unique to each form instance.
func NewStore(r *http.Request, w http.ResponseWriter, store sessions.Store, sessionName string) *Store {
return &Store{
r: r,
w: w,
sessionsStore: store,
sessionName: sessionName,
}
}
func (s *Store) getSession() (*sessions.Session, error) {
x, err := s.sessionsStore.Get(s.r, s.sessionName)
if err != nil {
return nil, err
}
x.Options.SameSite = http.SameSiteLaxMode
return x, nil
}
var ErrValidationErrorTypeAssertionFailed = errors.New("sessions: validation error type assertion failed")
func (s *Store) GetValidationErrors(field string) ([]formulate.ValidationError, error) {
sess, err := s.getSession()
if err != nil {
return nil, err
}
vals, ok := sess.Values[field]
if !ok {
return nil, nil
}
validationErrors, ok := vals.([]formulate.ValidationError)
if !ok {
return nil, ErrValidationErrorTypeAssertionFailed
}
return validationErrors, nil
}
func (s *Store) AddValidationError(field string, validationError formulate.ValidationError) error {
vals, err := s.GetValidationErrors(field)
if err != nil {
return err
}
vals = append(vals, validationError)
sess, err := s.getSession()
if err != nil {
return err
}
sess.Values[field] = vals
return s.sessionsStore.Save(s.r, s.w, sess)
}
func (s *Store) ClearValidationErrors() error {
sess, err := s.getSession()
if err != nil {
return err
}
sess.Values = nil
sess.Options.MaxAge = -1 // delete
return s.sessionsStore.Save(s.r, s.w, sess)
}
func (s *Store) SetFormValue(i interface{}) error {
sess, err := s.getSession()
if err != nil {
return err
}
filename, err := s.persistFormValue(i)
if err != nil {
return err
}
sess.Values["form_value"] = filename
return s.sessionsStore.Save(s.r, s.w, sess)
}
var ErrInvalidValue = errors.New("sessions: invalid value")
func (s *Store) GetFormValue(out interface{}) (err error) {
sess, err := s.getSession()
if err != nil {
return err
}
defer func() {
delete(sess.Values, "form_value")
saveErr := s.sessionsStore.Save(s.r, s.w, sess)
if err == nil {
err = saveErr
}
}()
val, ok := sess.Values["form_value"]
if !ok {
return ErrInvalidValue
}
name, ok := val.(string)
if !ok {
return ErrInvalidValue
}
return s.readFormValue(name, out)
}
func (s *Store) persistFormValue(val interface{}) (name string, err error) {
file, err := ioutil.TempFile("", "formulate_val_*")
if err != nil {
return "", err
}
defer file.Close()
if err := json.NewEncoder(file).Encode(val); err != nil {
return "", err
}
return file.Name(), nil
}
func (s *Store) readFormValue(name string, out interface{}) (err error) {
file, err := os.Open(name)
if err != nil {
return err
}
defer func() {
err = file.Close()
if err == nil {
err = os.Remove(name)
}
}()
return json.NewDecoder(file).Decode(out)
}