-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_session.go
239 lines (192 loc) · 5.2 KB
/
node_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package node
import (
"github.com/godaddy-x/freego/cache"
"github.com/godaddy-x/freego/utils"
"github.com/godaddy-x/freego/utils/jwt"
)
type Session interface {
GetId() string
GetStartTimestamp() int64
GetLastAccessTime() int64
GetTimeout() (int64, error) // 抛出校验会话异常
SetTimeout(t int64) error // 抛出校验会话异常
SetHost(host string)
GetHost() string
Touch() error // 刷新最后授权时间,抛出校验会话异常
Stop() error // 抛出校验会话异常
GetAttributeKeys() ([]string, error) // 抛出校验会话异常
GetAttribute(k string) (interface{}, error) // 抛出校验会话异常
SetAttribute(k string, v interface{}) error // 抛出校验会话异常
RemoveAttribute(k string) error // 抛出校验会话异常
Validate(accessToken, secretKey string) (int64, error) // 校验会话
Invalid() bool // 判断会话是否有效
IsTimeout() bool // 判断会话是否已超时
}
type SessionAware interface {
CreateSession(s Session) error // 保存session
ReadSession(s string) (Session, error) // 通过id读取session,抛出未知session异常
UpdateSession(s Session) error // 更新session,抛出未知session异常
DeleteSession(s Session) error // 删除session,抛出未知session异常
GetActiveSessions() []Session // 获取活动的session集合
}
type JWTSession struct {
Id string
StartTimestamp int64
LastAccessTime int64
Timeout int64
StopTime int64
Host string
Expire bool
Attributes map[string]interface{}
}
func (self *JWTSession) GetId() string {
return self.Id
}
func (self *JWTSession) GetStartTimestamp() int64 {
return self.StartTimestamp
}
func (self *JWTSession) GetLastAccessTime() int64 {
return self.LastAccessTime
}
func (self *JWTSession) GetTimeout() (int64, error) {
return self.Timeout, nil
}
func (self *JWTSession) SetTimeout(t int64) error {
self.Timeout = t
return nil
}
func (self *JWTSession) SetHost(host string) {
self.Host = host
}
func (self *JWTSession) GetHost() string {
return self.Host
}
func (self *JWTSession) Touch() error {
self.LastAccessTime = utils.UnixMilli()
return nil
}
func (self *JWTSession) Stop() error {
self.StopTime = utils.UnixMilli()
self.Expire = true
return nil
}
func (self *JWTSession) Validate(accessToken, secretKey string) (int64, error) {
if self.Expire {
return 0, utils.Error("session[", self.Id, "] expired")
} else if self.IsTimeout() {
return 0, utils.Error("session[", self.Id, "] timeout invalid")
}
// JWT二次校验
subject := &jwt.Subject{}
if len(self.GetHost()) > 0 {
subject.Payload = &jwt.Payload{Aud: self.GetHost()}
}
//if err := subject.Valid(accessToken, secretKey); err != nil {
// return "", err
//}
return utils.StrToInt64(subject.Payload.Sub)
}
func (self *JWTSession) Invalid() bool {
if len(self.Id) > 0 && !self.Expire && self.StopTime == 0 {
return false
}
return true
}
func (self *JWTSession) IsTimeout() bool {
if utils.UnixMilli() > (self.LastAccessTime + self.Timeout) {
self.Stop()
return true
}
return false
}
func (self *JWTSession) GetAttributeKeys() ([]string, error) {
keys := []string{}
for k, _ := range self.Attributes {
keys = append(keys, k)
}
return keys, nil
}
func (self *JWTSession) GetAttribute(k string) (interface{}, error) {
if len(k) == 0 {
return nil, nil
}
if v, b := self.Attributes[k]; b {
return v, nil
}
return nil, nil
}
func (self *JWTSession) SetAttribute(k string, v interface{}) error {
self.Attributes[k] = v
return nil
}
func (self *JWTSession) RemoveAttribute(k string) error {
if len(k) == 0 {
return nil
}
if _, b := self.Attributes[k]; b {
delete(self.Attributes, k)
}
return nil
}
/********************************* Session Cache Impl *********************************/
func NewLocalCacheSessionAware() *DefaultCacheSessionAware {
return &DefaultCacheSessionAware{
c: new(cache.LocalMapManager).NewCache(30, 10),
}
}
type DefaultCacheSessionAware struct {
c cache.Cache
}
func (self *DefaultCacheSessionAware) CreateSession(s Session) error {
if s.Invalid() {
return utils.Error("session[", s.GetId(), "] create invalid")
}
if err := s.Touch(); err != nil {
return err
}
//if err := s.Validate(); err != nil {
// return err
//}
if t, err := s.GetTimeout(); err != nil {
return err
} else {
self.c.Put(s.GetId(), s, int(t/1000))
}
return nil
}
func (self *DefaultCacheSessionAware) ReadSession(s string) (Session, error) {
var session Session
if v, b, err := self.c.Get(s, session); err != nil {
return nil, utils.Error("session[", s, "] read err: ", err)
} else if b && v != nil {
if r, ok := v.(Session); ok {
if r.Invalid() {
return nil, utils.Error("session[", s, "] read invalid")
}
return r, nil
}
}
return nil, nil
}
func (self *DefaultCacheSessionAware) UpdateSession(s Session) error {
if err := s.Touch(); err != nil {
return err
}
if t, err := s.GetTimeout(); err != nil {
return err
} else {
self.c.Put(s.GetId(), s, int(t/1000))
}
return nil
}
func (self *DefaultCacheSessionAware) DeleteSession(s Session) error {
if s == nil {
return nil
}
s.Stop()
self.c.Del(s.GetId())
return nil
}
func (self *DefaultCacheSessionAware) GetActiveSessions() []Session {
return nil
}