This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sessions.go
102 lines (81 loc) · 2.88 KB
/
sessions.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
package kwiscale
import (
"errors"
"github.com/gorilla/sessions"
)
var sessionEngine = make(map[string]SessionStore, 0)
// SessionEngineOptions set options for session engine.
type SessionEngineOptions map[string]interface{}
// RegisterSessionEngine can register session engine that implements
// ISessionStore. The name is used to let configuration to select it.
func RegisterSessionEngine(name string, engine SessionStore) {
sessionEngine[name] = engine
}
// Register cookiesessionstore by default.
func init() {
RegisterSessionEngine("default", &CookieSessionStore{})
}
// SessionStore to implement to give a session storage
type SessionStore interface {
// Init is called when store is initialized while App is initialized
Init()
// Name should set the session name
Name(string)
// SetOptions set some optionnal values to session engine
SetOptions(SessionEngineOptions)
// SetSecret should register a string to encode cookie (not mandatory
// but you should implement this to respect interface)
SetSecret([]byte)
// Get a value from storage , interface param is the key
Get(WebHandler, interface{}) (interface{}, error)
// Set a value in the storage, first interface param is the key,
// second interface is the value to store
Set(WebHandler, interface{}, interface{})
// Clean, should cleanup files
Clean(WebHandler)
}
// CookieSessionStore is a basic cookie based on gorilla.session.
type CookieSessionStore struct {
store *sessions.CookieStore
name string
secret []byte
}
// Init prepare the cookie storage.
func (s *CookieSessionStore) Init() {
s.store = sessions.NewCookieStore(s.secret)
}
// SetSecret record a string to encode cookie
func (s *CookieSessionStore) SetSecret(secret []byte) {
s.secret = secret
}
// Name set session name
func (s *CookieSessionStore) Name(name string) {
s.name = name
}
// SetOptions does nothing for the engine
func (*CookieSessionStore) SetOptions(SessionEngineOptions) {}
// Get a value from session by name.
func (s *CookieSessionStore) Get(handler WebHandler, key interface{}) (interface{}, error) {
session, err := s.store.Get(handler.getRequest(), s.name)
if err != nil {
return nil, err
}
Log("Getting session", key, session.Values[key])
if session.Values[key] == nil {
return nil, errors.New("empty session")
}
return session.Values[key], nil
}
// Set a named value in sessionstore.
func (s *CookieSessionStore) Set(handler WebHandler, key interface{}, val interface{}) {
Log("Writing session", key, val)
session, _ := s.store.Get(handler.getRequest(), s.name)
session.Values[key] = val
session.Save(handler.getRequest(), handler.getResponse())
}
// Clean removes the entire session values for current session.
func (s *CookieSessionStore) Clean(handler WebHandler) {
session, _ := s.store.Get(handler.getRequest(), s.name)
session.Values = make(map[interface{}]interface{})
session.Save(handler.getRequest(), handler.getResponse())
}