-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.go
128 lines (102 loc) · 2.63 KB
/
config.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
package fasthttpsession
import (
"github.com/satori/go.uuid"
"time"
)
var (
defaultCookieName = "fasthttpsessionid"
defaultExpires = time.Hour * 2
defaultGCLifetime = int64(3)
)
// new default config
func NewDefaultConfig() *Config {
config := &Config{
CookieName: defaultCookieName,
Domain: "",
Expires: defaultExpires,
GCLifetime: defaultGCLifetime,
SessionLifetime: 60,
Secure: true,
SessionIdInURLQuery: false,
SessionNameInUrlQuery: "",
SessionIdInHttpHeader: false,
SessionNameInHttpHeader: "",
}
// default sessionIdGeneratorFunc
config.SessionIdGeneratorFunc = config.defaultSessionIdGenerator
return config
}
type Config struct {
// cookie name
CookieName string
// cookie domain
Domain string
// If you want to delete the cookie when the browser closes, set it to -1.
//
// 0 means no expire, (24 years)
// -1 means when browser closes
// >0 is the time.Duration which the session cookies should expire.
Expires time.Duration
// gc life time(s)
GCLifetime int64
// session life time(s)
SessionLifetime int64
// set whether to pass this bar cookie only through HTTPS
Secure bool
// sessionId is in url query
SessionIdInURLQuery bool
// sessionName in url query
SessionNameInUrlQuery string
// sessionId is in http header
SessionIdInHttpHeader bool
// sessionName in http header
SessionNameInHttpHeader string
// SessionIdGeneratorFunc should returns a random session id.
SessionIdGeneratorFunc func() string
// Encode the cookie value if not nil.
EncodeFunc func(cookieValue string) (string, error)
// Decode the cookie value if not nil.
DecodeFunc func(cookieValue string) (string, error)
}
// sessionId generator
func (c *Config) SessionIdGenerator() string {
sessionIdGenerator := c.SessionIdGeneratorFunc
if sessionIdGenerator == nil {
return c.defaultSessionIdGenerator()
}
return sessionIdGenerator()
}
// default sessionId generator => uuid
func (c *Config) defaultSessionIdGenerator() string {
id := uuid.NewV4()
return id.String()
}
// encode cookie value
func (c *Config) Encode(cookieValue string) string {
encode := c.EncodeFunc
if encode != nil {
newVal, err := encode(cookieValue)
if err == nil {
cookieValue = newVal
} else {
cookieValue = ""
}
}
return cookieValue
}
// decode cookie value
func (c *Config) Decode(cookieValue string) string {
if cookieValue == "" {
return ""
}
decode := c.DecodeFunc
if decode != nil {
newVal, err := decode(cookieValue)
if err == nil {
cookieValue = newVal
} else {
cookieValue = ""
}
}
return cookieValue
}