-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpersistent_jar.go
299 lines (243 loc) · 6.97 KB
/
persistent_jar.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package cookiejar
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"sync"
"time"
"github.com/bool64/ctxd"
"github.com/spf13/afero"
)
const (
permReadonly = os.FileMode(0o600)
)
var _ http.CookieJar = (*PersistentJar)(nil)
// PersistentJar persists cookies to a file.
type PersistentJar struct {
jar *Jar
fs afero.Fs
serder EntrySerDer
logger ctxd.Logger
autoSync bool
filePath string
filePerm os.FileMode
lazyLoad sync.Once
}
// SetCookies implements the SetCookies method of the http.CookieJar interface.
//
// It does nothing if the URL's scheme is not HTTP or HTTPS.
func (j *PersistentJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
j.lazyLoad.Do(j.load)
j.jar.SetCookies(u, cookies)
if j.autoSync {
if err := j.Sync(); err != nil {
j.logger.Error(context.Background(), err.Error())
}
}
}
// Cookies implements the Cookies method of the http.CookieJar interface.
//
// It returns an empty slice if the URL's scheme is not HTTP or HTTPS.
func (j *PersistentJar) Cookies(u *url.URL) []*http.Cookie {
j.lazyLoad.Do(j.load)
return j.jar.Cookies(u)
}
// Sync persists cookies to the file.
func (j *PersistentJar) Sync() error {
j.jar.mu.Lock()
defer j.jar.mu.Unlock()
ctx := ctxd.AddFields(context.Background(), "cookies.file", j.filePath)
f, err := j.fs.OpenFile(filepath.Clean(j.filePath), os.O_RDWR|os.O_CREATE|os.O_TRUNC, j.filePerm)
if err != nil {
return ctxd.WrapError(ctx, err, "could not open file for persisting cookies")
}
defer func() {
_ = f.Close() //nolint: errcheck
}()
if err := j.serder.Serialize(f, mapToExport(j.jar.entries)); err != nil {
return ctxd.WrapError(ctx, err, "could not serialize cookies")
}
if err := f.Sync(); err != nil {
return ctxd.WrapError(ctx, err, "could not sync cookies file")
}
return nil
}
func (j *PersistentJar) load() {
j.jar.mu.Lock()
defer j.jar.mu.Unlock()
ctx := ctxd.AddFields(context.Background(), "cookies.file", j.filePath)
f, err := j.fs.Open(filepath.Clean(j.filePath))
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
j.logger.Error(ctx, "could not open file for loading cookies", "error", err)
}
return
}
defer func() {
_ = f.Close() //nolint: errcheck
}()
entries, err := j.serder.Deserialize(f)
if err != nil {
j.logger.Error(ctx, "could not deserialize cookies", "error", err)
return
}
j.jar.entries, j.jar.nextSeqNum = mapToImport(entries)
}
// NewPersistentJar creates new persistent cookie jar.
func NewPersistentJar(opts ...PersistentJarOption) *PersistentJar {
jar, _ := New(nil) //nolint: errcheck
j := &PersistentJar{
jar: jar,
fs: afero.NewOsFs(),
serder: jsonSerDer{},
logger: ctxd.NoOpLogger{},
autoSync: false,
filePath: "cookies.json",
filePerm: permReadonly,
}
for _, opt := range opts {
opt.applyPersistentJarOption(j)
}
return j
}
// PersistentJarOption is an option to configure PersistentJar.
type PersistentJarOption interface {
applyPersistentJarOption(j *PersistentJar)
}
type persistentJarOptionFunc func(j *PersistentJar)
func (f persistentJarOptionFunc) applyPersistentJarOption(j *PersistentJar) {
f(j)
}
// WithFs sets the file system.
func WithFs(fs afero.Fs) PersistentJarOption {
return persistentJarOptionFunc(func(j *PersistentJar) {
j.fs = fs
})
}
// WithSerDer sets the serializer/deserializer.
func WithSerDer(serder EntrySerDer) PersistentJarOption {
return persistentJarOptionFunc(func(j *PersistentJar) {
j.serder = serder
})
}
// WithAutoSync sets the auto sync mode.
func WithAutoSync(autoSync bool) PersistentJarOption {
return persistentJarOptionFunc(func(j *PersistentJar) {
j.autoSync = autoSync
})
}
// WithFilePath sets the file path.
func WithFilePath(filePath string) PersistentJarOption {
return persistentJarOptionFunc(func(j *PersistentJar) {
j.filePath = filePath
})
}
// WithFilePerm sets the file permission.
func WithFilePerm(filePerm os.FileMode) PersistentJarOption {
return persistentJarOptionFunc(func(j *PersistentJar) {
j.filePerm = filePerm
})
}
// WithLogger sets the logger.
func WithLogger(logger ctxd.Logger) PersistentJarOption {
return persistentJarOptionFunc(func(j *PersistentJar) {
j.logger = logger
})
}
// WithPublicSuffixList sets the public suffix list.
func WithPublicSuffixList(list PublicSuffixList) PersistentJarOption {
return persistentJarOptionFunc(func(j *PersistentJar) {
j.jar.psList = list
})
}
// Entry is a public presentation of the entry struct.
type Entry struct {
Name string
Value string
Quoted bool
Domain string
Path string
SameSite string
Secure bool
HttpOnly bool
Persistent bool
HostOnly bool
Expires time.Time
Creation time.Time
LastAccess time.Time
SeqNum uint64
}
func mapToExport(entries map[string]map[string]entry) map[string]map[string]Entry {
exported := make(map[string]map[string]Entry)
for domain, domainCookies := range entries {
exported[domain] = make(map[string]Entry)
for name, cookie := range domainCookies {
exported[domain][name] = Entry{
Name: cookie.Name,
Value: cookie.Value,
Domain: cookie.Domain,
Path: cookie.Path,
SameSite: cookie.SameSite,
Secure: cookie.Secure,
HttpOnly: cookie.HttpOnly,
Persistent: cookie.Persistent,
HostOnly: cookie.HostOnly,
Expires: cookie.Expires,
Creation: cookie.Creation,
LastAccess: cookie.LastAccess,
SeqNum: cookie.seqNum,
}
}
}
return exported
}
func mapToImport(entries map[string]map[string]Entry) (map[string]map[string]entry, uint64) {
nextSeqNum := uint64(0)
imported := make(map[string]map[string]entry)
for domain, domainCookies := range entries {
imported[domain] = make(map[string]entry)
for name, cookie := range domainCookies {
imported[domain][name] = entry{
Name: cookie.Name,
Value: cookie.Value,
Domain: cookie.Domain,
Path: cookie.Path,
SameSite: cookie.SameSite,
Secure: cookie.Secure,
HttpOnly: cookie.HttpOnly,
Persistent: cookie.Persistent,
HostOnly: cookie.HostOnly,
Expires: cookie.Expires,
Creation: cookie.Creation,
LastAccess: cookie.LastAccess,
seqNum: cookie.SeqNum,
}
if cookie.SeqNum > nextSeqNum {
nextSeqNum = cookie.SeqNum + 1
}
}
}
return imported, nextSeqNum
}
// EntrySerDer is an interface for serializing and deserializing entries.
type EntrySerDer interface {
Serialize(w io.Writer, entries map[string]map[string]Entry) error
Deserialize(r io.Reader) (map[string]map[string]Entry, error)
}
// jsonSerDer is a JSON serializer and deserializer.
type jsonSerDer struct{}
func (jsonSerDer) Serialize(w io.Writer, entries map[string]map[string]Entry) error {
return json.NewEncoder(w).Encode(entries)
}
func (jsonSerDer) Deserialize(r io.Reader) (map[string]map[string]Entry, error) {
var entries map[string]map[string]Entry
if err := json.NewDecoder(r).Decode(&entries); err != nil {
return nil, err
}
return entries, nil
}