-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
110 lines (95 loc) · 2.9 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
103
104
105
106
107
108
109
110
package sessions
import (
"encoding/base64"
"encoding/json"
"github.com/go-http-utils/cookie"
)
// Version is this package's version
const Version = "1.0.0"
// Store is an interface for custom session stores.
type Store interface {
// Load should load data from cookie and store, set it into session instance.
// error indicates that session validation failed, or other thing.
// Sessions.Init should be called in Load, even if error occured.
Load(name string, session Sessions, cookie *cookie.Cookies) error
// Save should persist session to the underlying store implementation.
Save(session Sessions) error
// Destroy should destroy the session.
Destroy(session Sessions) error
}
// Sessions ...
type Sessions interface {
// Init sets current cookie.Cookies and Store to the session instance.
Init(name, sid string, c *cookie.Cookies, store Store, lastValue string)
// GetSID returns the session' sid
GetSID() string
// GetName returns the session' name
GetName() string
// GetStore returns the session' store
GetStore() Store
// GetCookie returns the session' cookie
GetCookie() *cookie.Cookies
// IsChanged to check current session's value whether is changed
IsChanged(val string) bool
// IsNew to check the current session whether it's new user
IsNew() bool
}
// Meta stores the values and optional configuration for a session.
type Meta struct {
// Values map[string]interface{}
sid string
store Store
name string
cookie *cookie.Cookies
lastValue string
}
// Init sets current cookie.Cookies and Store to the session instance.
func (s *Meta) Init(name, sid string, c *cookie.Cookies, store Store, lastValue string) {
s.name = name
s.sid = sid
s.cookie = c
s.store = store
s.lastValue = lastValue
}
// GetSID returns the session' sid
func (s *Meta) GetSID() string {
return s.sid
}
// GetName returns the name used to register the session
func (s *Meta) GetName() string {
return s.name
}
// GetStore returns the session store used to register the session
func (s *Meta) GetStore() Store {
return s.store
}
// GetCookie returns the session' cookie
func (s *Meta) GetCookie() *cookie.Cookies {
return s.cookie
}
// IsChanged to check current session's value whether is changed
func (s *Meta) IsChanged(val string) bool {
return s.lastValue != val
}
// IsNew to check the current session whether it's new user
func (s *Meta) IsNew() bool {
return s.sid == ""
}
// Encode the value by Serializer and Base64
func Encode(value interface{}) (str string, err error) {
b, err := json.Marshal(value)
if err != nil {
return
}
str = base64.StdEncoding.EncodeToString(b)
return
}
// Decode the value to dst .
func Decode(value string, dst interface{}) (err error) {
b, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return err
}
err = json.Unmarshal(b, dst)
return
}