-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
187 lines (157 loc) · 3.75 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package session
import (
"github.com/goal-web/contracts"
"github.com/goal-web/supports/logs"
"github.com/goal-web/supports/utils"
"net/http"
"time"
)
// Session 后期会拆成 session 和 session store ,支持用 redis 、memcached、database 等其他方式存储 session
type Session struct {
id string
name string
started bool
path string
domain string
lifetime time.Duration
attributes map[string]string
request contracts.HttpRequest
changed bool
store contracts.SessionStore
}
func New(config Config, request contracts.HttpRequest, store contracts.SessionStore) contracts.Session {
return &Session{
id: "",
name: config.Name,
started: false,
request: request,
path: request.Path(),
store: store,
domain: config.Domain,
lifetime: config.Lifetime,
attributes: map[string]string{},
}
}
func (this *Session) GetName() string {
return this.name
}
func (this *Session) SetName(name string) {
this.name = name
}
func (this *Session) GetId() string {
return this.id
}
func (this *Session) SetId(id string) {
this.id = id
}
func (this *Session) Start() bool {
cookie, err := this.request.Cookie(this.name)
if err != nil {
logs.WithError(err).Debug("Failed to load cookies")
} else {
this.id = cookie.Value
}
if this.id == "" {
this.generateSessionId()
}
this.loadSession()
this.started = true
return true
}
func (this *Session) loadSession() {
this.attributes = this.store.LoadSession(this.GetId())
}
func (this *Session) Save() {
if this.changed {
this.store.Save(this.GetId(), this.attributes)
}
}
func (this *Session) All() map[string]string {
return this.attributes
}
func (this *Session) Exists(key string) bool {
_, exists := this.attributes[key]
return exists
}
func (this *Session) Has(key string) bool {
value, exists := this.attributes[key]
return exists && value != ""
}
func (this *Session) Get(key, defaultValue string) string {
value, exists := this.attributes[key]
if !exists || value == "" {
return defaultValue
}
return value
}
func (this *Session) Pull(key, defaultValue string) string {
this.changed = true
value, exists := this.attributes[key]
if !exists || value == "" {
return defaultValue
}
delete(this.attributes, key)
return value
}
func (this *Session) Put(key, value string) {
this.changed = true
this.attributes[key] = value
}
func (this *Session) Token() string {
return this.Get("_token", "")
}
func (this *Session) RegenerateToken() {
this.id = utils.RandStr(40)
this.request.SetCookie(&http.Cookie{
Name: this.name,
Value: this.id,
Expires: time.Now().Add(time.Second * this.lifetime),
})
}
func (this *Session) Remove(key string) string {
return this.Pull(key, "")
}
func (this *Session) Forget(keys ...string) {
this.changed = true
for _, key := range keys {
delete(this.attributes, key)
}
}
func (this *Session) Flush() {
this.changed = true
this.attributes = make(map[string]string)
}
func (this *Session) Invalidate() bool {
this.Flush()
return this.Migrate(true)
}
func (this *Session) Regenerate(destroy bool) bool {
if !this.Migrate(destroy) {
this.RegenerateToken()
}
return true
}
func (this *Session) Migrate(destroy bool) bool {
if destroy {
// todo: $this->handler->destroy($this->getId());
}
this.generateSessionId()
return true
}
func (this *Session) IsStarted() bool {
return this.started
}
func (this *Session) generateSessionId() {
this.id = utils.RandStr(40)
this.request.SetCookie(&http.Cookie{
Name: this.name,
Value: this.id,
Expires: time.Now().Add(time.Second * this.lifetime),
})
}
func (this *Session) PreviousUrl() string {
return this.Get("_previous.url", "")
}
func (this *Session) SetPreviousUrl(url string) {
this.Put("_previous.url", url)
}