-
Notifications
You must be signed in to change notification settings - Fork 111
/
cookies.go
42 lines (37 loc) · 1.1 KB
/
cookies.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
package cookies
import (
"errors"
"fmt"
"net/http"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"github.com/rilldata/rill/runtime/pkg/observability"
"go.uber.org/zap"
)
type Store struct {
*sessions.CookieStore
logger *zap.Logger
}
func New(logger *zap.Logger, keyPairs ...[]byte) *Store {
return &Store{
CookieStore: sessions.NewCookieStore(keyPairs...),
logger: logger,
}
}
// Get returns a session for the given name.
// It returns a new session if the cookie could not be decoded and validated.
func (s *Store) Get(r *http.Request, name string) *sessions.Session {
sess, err := s.CookieStore.Get(r, name)
if err != nil {
s.logger.Error("failed to get cookie", zap.String("cookie_name", name), zap.Error(err), observability.ZapCtx(r.Context()))
var cookieErr securecookie.Error
if errors.As(err, &cookieErr) && cookieErr.IsDecode() {
session := sessions.NewSession(s.CookieStore, name)
session.Options = s.CookieStore.Options
session.IsNew = true
return session
}
panic(fmt.Errorf("unable to get cookie name %q error: %w", name, err))
}
return sess
}