-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
82 lines (70 loc) · 2.05 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
// Package store provices an implementation of sessions.Store that also carries
// its name around with it.
package store
import (
"encoding/hex"
"fmt"
"time"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
)
var (
sessionAge = 24 * time.Hour
)
// Namer is a sessions.Store that also has its own name
type Namer interface {
sessions.Store
Name() string
}
type namedStore struct {
*sessions.CookieStore
domain string
name string
secret string
}
// Name implements the Namer interface, returning this store's name
func (s *namedStore) Name() string {
return s.name
}
var (
// ErrMissingName is returned when attempting to create a Namer with an empty name
ErrMissingName = fmt.Errorf("name cannot be empty")
// ErrMissingDomain returned when attempting to create a Namer with an empty domain
ErrMissingDomain = fmt.Errorf("domain cannot be empty")
)
// A KeyError is returned when attmpting to create a Namer with an invalid key. Keys must be
// hex-encoded strings with length 128.
type KeyError struct{}
// Error implements the error interface
func (k KeyError) Error() string {
return fmt.Sprintf(
"Session store secret should be a 128-character hex-encoded string. "+
"Here's a freshly generated one: %q",
hex.EncodeToString(securecookie.GenerateRandomKey(64)))
}
func (s *namedStore) init() error {
if s.name == "" {
return ErrMissingName
}
if s.domain == "" {
return ErrMissingDomain
}
key, err := hex.DecodeString(s.secret)
if err != nil || len(key) != 64 {
return KeyError{}
}
cookieStore := sessions.NewCookieStore(key)
cookieStore.Options.HttpOnly = true
cookieStore.Options.Secure = true
cookieStore.Options.Domain = s.domain
cookieStore.Options.MaxAge = int(sessionAge / time.Second)
s.CookieStore = cookieStore
return nil
}
// New returns a new Namer for the given name, secret key and domain. secret should be a hex-encoded
// string with length 128.
func New(name, secret, domain string) (Namer, error) {
sc := &namedStore{name: name, domain: domain, secret: secret}
err := sc.init()
return sc, err
}