This repository has been archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsession.go
167 lines (148 loc) · 4.1 KB
/
session.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 🚀 Fiber is an Express inspired web framework written in Go with 💖
// 📌 API Documentation: https://docs.gofiber.io
// 📝 Github Repository: https://github.com/gofiber/fiber
// 🙏 Special thanks to @thomasvvugt & @savsgio (fasthttp/session)
package session
import (
"fmt"
"log"
"reflect"
"strings"
"sync"
"time"
fsession "github.com/fasthttp/session/v2"
"github.com/fasthttp/session/v2/providers/memory"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
)
// Config defines the config for RequestID middleware
type Config struct {
// Lookup 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".
Lookup string
// Secure attribute for cookie
// Optional. Default: false
Secure bool
// Domain attribute for cookie
// Optional. Default: ""
Domain string
// SameSite attribute for cookie
// Possible values: "Lax", "Strict", "None"
// Optional. Default: "Lax"
SameSite string
// 0 means no expire, (24 years)
// -1 means when browser closes
// >0 is the time.Duration which the session cookies should expire.
// Optional. Default: 12 hours
Expiration time.Duration
// Holds the provider interface
// Optional. Default: memory.New()
Provider fsession.Provider
// Generator is a function that generates an unique id
// Optional.
Generator func() []byte
// gc life time to execute it
// Optional. 1 minute
GCInterval time.Duration
// fasthttp/session always sets a cookie response
// we want to disable this if no cookie lookup is set
noCookie bool
}
// Session ...
type Session struct {
config Config
core *fsession.Session
storePool *sync.Pool
}
// New ...
func New(config ...Config) *Session {
// Init session config
var cfg Config
if len(config) > 0 {
cfg = config[0]
}
// Fiber Config
if cfg.Lookup == "" {
cfg.Lookup = "cookie:session_id"
}
if cfg.Expiration == 0 {
cfg.Expiration = 12 * time.Hour
}
if cfg.GCInterval == 0 {
cfg.GCInterval = 1 * time.Minute
}
if cfg.Generator == nil {
cfg.Generator = defaultGenerator
}
if cfg.Provider == nil {
provider, err := memory.New(memory.Config{})
if err != nil {
log.Fatalf("session: memory %v", err)
}
cfg.Provider = provider
}
// Private fasthttp config
var scfg fsession.Config
scfg.GCLifetime = cfg.GCInterval
scfg.SessionIDGeneratorFunc = cfg.Generator
// Split lookup key <method>:<key>
parts := strings.Split(cfg.Lookup, ":")
// Cookie configuration for fasthttp
scfg.CookieName = parts[1]
scfg.Domain = cfg.Domain
scfg.Expiration = cfg.Expiration
scfg.Secure = cfg.Secure
switch strings.ToLower(cfg.SameSite) {
case "strict":
scfg.CookieSameSite = fasthttp.CookieSameSiteStrictMode
case "none":
scfg.CookieSameSite = fasthttp.CookieSameSiteNoneMode
default:
scfg.CookieSameSite = fasthttp.CookieSameSiteLaxMode
}
// Set configuration for header and query lookups
scfg.SessionIDInHTTPHeader = parts[0] == "header"
scfg.SessionNameInHTTPHeader = parts[1]
scfg.SessionIDInURLQuery = parts[0] == "query"
scfg.SessionNameInURLQuery = parts[1]
// fasthttp/session always sets a cookie response
// we want to disable this if no cookie lookup is set
if parts[0] != "cookie" {
cfg.noCookie = true
}
// Create fiber session
sessions := &Session{
config: cfg,
core: fsession.New(scfg),
}
provider := fmt.Sprintf("%v", reflect.TypeOf(cfg.Provider))
switch provider {
case "*mysql.Provider", "*sqlite3.Provider":
scfg.EncodeFunc = fsession.Base64Encode
scfg.DecodeFunc = fsession.Base64Decode
default:
// redis / memcache / memory
scfg.EncodeFunc = fsession.MSGPEncode
scfg.DecodeFunc = fsession.MSGPDecode
}
// Set default provider
if err := sessions.core.SetProvider(cfg.Provider); err != nil {
log.Fatal(err)
}
return sessions
}
// Get store
func (s *Session) Get(ctx *fiber.Ctx) *Store {
fstore, _ := s.core.Get(ctx.Context())
return &Store{
ctx: ctx,
sess: s,
core: fstore,
}
}
func defaultGenerator() []byte {
return utils.GetBytes(utils.UUID())
}