@@ -22,7 +22,7 @@ package engine
22
22
23
23
// Usage:
24
24
// import(
25
- // "github.com/bhojpur/session/pkg/engine"
25
+ // session "github.com/bhojpur/session/pkg/engine"
26
26
// )
27
27
//
28
28
// func init() {
@@ -47,12 +47,12 @@ import (
47
47
48
48
// Store contains all data for one session process with specific id.
49
49
type Store interface {
50
- Set (ctx context.Context , key , value interface {}) error //set session value
51
- Get (ctx context.Context , key interface {}) interface {} //get session value
52
- Delete (ctx context.Context , key interface {}) error //delete session value
53
- SessionID (ctx context.Context ) string //back current sessionID
50
+ Set (ctx context.Context , key , value interface {}) error // set session value
51
+ Get (ctx context.Context , key interface {}) interface {} // get session value
52
+ Delete (ctx context.Context , key interface {}) error // delete session value
53
+ SessionID (ctx context.Context ) string // back current sessionID
54
54
SessionRelease (ctx context.Context , w http.ResponseWriter ) // release the resource & save data to provider & return the data
55
- Flush (ctx context.Context ) error //delete all data
55
+ Flush (ctx context.Context ) error // delete all data
56
56
}
57
57
58
58
// Provider contains global session methods and saved SessionStores.
@@ -63,7 +63,7 @@ type Provider interface {
63
63
SessionExist (ctx context.Context , sid string ) (bool , error )
64
64
SessionRegenerate (ctx context.Context , oldsid , sid string ) (Store , error )
65
65
SessionDestroy (ctx context.Context , sid string ) error
66
- SessionAll (ctx context.Context ) int //get all active session
66
+ SessionAll (ctx context.Context ) int // get all active session
67
67
SessionGC (ctx context.Context )
68
68
}
69
69
@@ -73,45 +73,23 @@ var provides = make(map[string]Provider)
73
73
var SLogger = NewSessionLog (os .Stderr )
74
74
75
75
// Register makes a session provide available by the provided name.
76
- // If Register is called twice with the same name or if driver is nil,
77
- // it panics.
76
+ // If provider is nil, it panic
78
77
func Register (name string , provide Provider ) {
79
78
if provide == nil {
80
- panic ("session: Register provider is nil" )
81
- }
82
- if _ , dup := provides [name ]; dup {
83
- panic ("session: Register called twice for provider " + name )
79
+ panic ("session: Register provide is nil" )
84
80
}
85
81
provides [name ] = provide
86
82
}
87
83
88
- //GetProvider
84
+ // GetProvider
89
85
func GetProvider (name string ) (Provider , error ) {
90
86
provider , ok := provides [name ]
91
87
if ! ok {
92
- return nil , fmt .Errorf ("session: unknown provider %q (forgotten import?)" , name )
88
+ return nil , fmt .Errorf ("session: unknown provide %q (forgotten import?)" , name )
93
89
}
94
90
return provider , nil
95
91
}
96
92
97
- // ManagerConfig define the session config
98
- type ManagerConfig struct {
99
- CookieName string `json:"cookieName"`
100
- EnableSetCookie bool `json:"enableSetCookie,omitempty"`
101
- Gclifetime int64 `json:"gclifetime"`
102
- Maxlifetime int64 `json:"maxLifetime"`
103
- DisableHTTPOnly bool `json:"disableHTTPOnly"`
104
- Secure bool `json:"secure"`
105
- CookieLifeTime int `json:"cookieLifeTime"`
106
- ProviderConfig string `json:"providerConfig"`
107
- Domain string `json:"domain"`
108
- SessionIDLength int64 `json:"sessionIDLength"`
109
- EnableSidInHTTPHeader bool `json:"EnableSidInHTTPHeader"`
110
- SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"`
111
- EnableSidInURLQuery bool `json:"EnableSidInURLQuery"`
112
- SessionIDPrefix string `json:"sessionIDPrefix"`
113
- }
114
-
115
93
// Manager contains Provider and its configuration.
116
94
type Manager struct {
117
95
provider Provider
@@ -133,7 +111,7 @@ type Manager struct {
133
111
func NewManager (provideName string , cf * ManagerConfig ) (* Manager , error ) {
134
112
provider , ok := provides [provideName ]
135
113
if ! ok {
136
- return nil , fmt .Errorf ("session: unknown provider %q (forgotten import?)" , provideName )
114
+ return nil , fmt .Errorf ("session: unknown provide %q (forgotten import?)" , provideName )
137
115
}
138
116
139
117
if cf .Maxlifetime == 0 {
@@ -242,6 +220,7 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
242
220
HttpOnly : ! manager .config .DisableHTTPOnly ,
243
221
Secure : manager .isSecure (r ),
244
222
Domain : manager .config .Domain ,
223
+ SameSite : manager .config .CookieSameSite ,
245
224
}
246
225
if manager .config .CookieLifeTime > 0 {
247
226
cookie .MaxAge = manager .config .CookieLifeTime
@@ -276,12 +255,15 @@ func (manager *Manager) SessionDestroy(w http.ResponseWriter, r *http.Request) {
276
255
manager .provider .SessionDestroy (nil , sid )
277
256
if manager .config .EnableSetCookie {
278
257
expiration := time .Now ()
279
- cookie = & http.Cookie {Name : manager .config .CookieName ,
258
+ cookie = & http.Cookie {
259
+ Name : manager .config .CookieName ,
280
260
Path : "/" ,
281
261
HttpOnly : ! manager .config .DisableHTTPOnly ,
282
262
Expires : expiration ,
283
263
MaxAge : - 1 ,
284
- Domain : manager .config .Domain }
264
+ Domain : manager .config .Domain ,
265
+ SameSite : manager .config .CookieSameSite ,
266
+ }
285
267
286
268
http .SetCookie (w , cookie )
287
269
}
@@ -311,17 +293,19 @@ func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Reque
311
293
312
294
cookie , err := r .Cookie (manager .config .CookieName )
313
295
if err != nil || cookie .Value == "" {
314
- //delete old cookie
296
+ // delete old cookie
315
297
session , err = manager .provider .SessionRead (nil , sid )
316
298
if err != nil {
317
299
return nil , err
318
300
}
319
- cookie = & http.Cookie {Name : manager .config .CookieName ,
301
+ cookie = & http.Cookie {
302
+ Name : manager .config .CookieName ,
320
303
Value : url .QueryEscape (sid ),
321
304
Path : "/" ,
322
305
HttpOnly : ! manager .config .DisableHTTPOnly ,
323
306
Secure : manager .isSecure (r ),
324
307
Domain : manager .config .Domain ,
308
+ SameSite : manager .config .CookieSameSite ,
325
309
}
326
310
} else {
327
311
oldsid , err := url .QueryUnescape (cookie .Value )
0 commit comments