forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
128 lines (105 loc) · 2.97 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 session
import (
"fmt"
"strings"
"time"
"github.com/boomhut/fiber/v2"
"github.com/boomhut/fiber/v2/log"
"github.com/boomhut/fiber/v2/utils"
)
// Config defines the config for middleware.
type Config struct {
// Allowed session duration
// Optional. Default value 24 * time.Hour
Expiration time.Duration
// Storage interface to store the session data
// Optional. Default value memory.New()
Storage fiber.Storage
// KeyLookup is a string in the form of "<source>:<name>" that is used
// to extract session id from the request.
// Possible values: "header:<name>", "query:<name>" or "cookie:<name>"
// Optional. Default value "cookie:session_id".
KeyLookup string
// Domain of the cookie.
// Optional. Default value "".
CookieDomain string
// Path of the cookie.
// Optional. Default value "".
CookiePath string
// Indicates if cookie is secure.
// Optional. Default value false.
CookieSecure bool
// Indicates if cookie is HTTP only.
// Optional. Default value false.
CookieHTTPOnly bool
// Value of SameSite cookie.
// Optional. Default value "Lax".
CookieSameSite string
// Decides whether cookie should last for only the browser sesison.
// Ignores Expiration if set to true
// Optional. Default value false.
CookieSessionOnly bool
// KeyGenerator generates the session key.
// Optional. Default value utils.UUIDv4
KeyGenerator func() string
// Deprecated: Please use KeyLookup
CookieName string
// Source defines where to obtain the session id
source Source
// The session name
sessionName string
}
type Source string
const (
SourceCookie Source = "cookie"
SourceHeader Source = "header"
SourceURLQuery Source = "query"
)
// ConfigDefault is the default config
var ConfigDefault = Config{
Expiration: 24 * time.Hour,
KeyLookup: "cookie:session_id",
KeyGenerator: utils.UUIDv4,
source: "cookie",
sessionName: "session_id",
}
// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
// Set default values
if int(cfg.Expiration.Seconds()) <= 0 {
cfg.Expiration = ConfigDefault.Expiration
}
if cfg.CookieName != "" {
log.Warn("[SESSION] CookieName is deprecated, please use KeyLookup")
cfg.KeyLookup = fmt.Sprintf("cookie:%s", cfg.CookieName)
}
if cfg.KeyLookup == "" {
cfg.KeyLookup = ConfigDefault.KeyLookup
}
if cfg.KeyGenerator == nil {
cfg.KeyGenerator = ConfigDefault.KeyGenerator
}
selectors := strings.Split(cfg.KeyLookup, ":")
const numSelectors = 2
if len(selectors) != numSelectors {
panic("[session] KeyLookup must in the form of <source>:<name>")
}
switch Source(selectors[0]) {
case SourceCookie:
cfg.source = SourceCookie
case SourceHeader:
cfg.source = SourceHeader
case SourceURLQuery:
cfg.source = SourceURLQuery
default:
panic("[session] source is not supported")
}
cfg.sessionName = selectors[1]
return cfg
}