-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
92 lines (79 loc) · 1.87 KB
/
options.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
package cookie
import (
"crypto/cipher"
"hash"
)
// Define default options
var defaultOptions = options{
cookieName: "cookie_session_id",
maxLength: -1,
maxAge: -1,
minAge: -1,
}
type options struct {
cookieName string
secure bool
hashKey []byte
hashFunc func() hash.Hash
blockKey []byte
blockFunc func([]byte) (cipher.Block, error)
maxLength int
maxAge int
minAge int
}
// Option A cookie parameter options
type Option func(*options)
// SetCookieName Set the cookie name
func SetCookieName(cookieName string) Option {
return func(o *options) {
o.cookieName = cookieName
}
}
// SetSecure Set cookie security
func SetSecure(secure bool) Option {
return func(o *options) {
o.secure = secure
}
}
// SetHashKey used to authenticate values using HMAC
func SetHashKey(hashKey []byte) Option {
return func(o *options) {
o.hashKey = hashKey
}
}
// SetHashFunc sets the hash function used to create HMAC
func SetHashFunc(hashFunc func() hash.Hash) Option {
return func(o *options) {
o.hashFunc = hashFunc
}
}
// SetBlockKey used to encrypt values
func SetBlockKey(blockKey []byte) Option {
return func(o *options) {
o.blockKey = blockKey
}
}
// SetBlockFunc sets the encryption function used to create a cipher.Block
func SetBlockFunc(blockFunc func([]byte) (cipher.Block, error)) Option {
return func(o *options) {
o.blockFunc = blockFunc
}
}
// SetMaxLength restricts the maximum length, in bytes, for the cookie value
func SetMaxLength(maxLength int) Option {
return func(o *options) {
o.maxLength = maxLength
}
}
// SetMaxAge restricts the maximum age, in seconds, for the cookie value
func SetMaxAge(maxAge int) Option {
return func(o *options) {
o.maxAge = maxAge
}
}
// SetMinAge restricts the minimum age, in seconds, for the cookie value
func SetMinAge(minAge int) Option {
return func(o *options) {
o.minAge = minAge
}
}