-
Notifications
You must be signed in to change notification settings - Fork 18
/
session.go
171 lines (153 loc) · 3.76 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
package W
import (
"time"
"github.com/kokizzu/gotro/L"
"github.com/kokizzu/gotro/M"
"github.com/kokizzu/gotro/S"
"github.com/kokizzu/gotro/T"
"github.com/valyala/fasthttp"
)
type SessionConnector interface {
// delete key
Del(key string)
// get remaining lifespan in seconds
Expiry(key string) int64
// set string with remaining lifespan in seconds
FadeStr(key, val string, ttl int64)
// set integer with remaining lifespan in seconds
FadeInt(key string, val int64, ttl int64)
// set json with remaining lifespan in seconds
FadeMSX(key string, val M.SX, ttl int64)
// get string
GetStr(key string) string
// get integer
GetInt(key string) int64
// get string
GetMSX(key string) M.SX
// increment
Inc(key string) int64
// increment
Dec(key string) int64
// set string
SetStr(key, val string)
// set integer
SetInt(key string, val int64)
// set json
SetMSX(key string, val M.SX)
// set json
SetMSS(key string, val M.SS)
// get product name, eg: Pg, Sc, My, Rd
Product() string
Lpush(key string, val string)
Rpush(key string, val string)
Lrange(key string, first, last int64) []string
}
var SESS_KEY = `SK`
var EXPIRE_SEC int64
var RENEW_SEC int64
const NS2SEC = 1000 * 1000 * 1000
type Session struct {
UserAgent string
IpAddr string
Key string
M.SX
Changed bool
}
func (s *Session) Logout() {
Sessions.Del(s.Key)
s.Key = ``
s.SX = M.SX{}
s.RandomKey()
}
func (s *Session) RandomKey() {
for {
s.Key = s.StateCSRF() + S.RandomCB63(8)
if Sessions.GetStr(s.Key) == `` {
Sessions.FadeMSX(s.Key, M.SX{`key_at`: T.Epoch()}, EXPIRE_SEC)
break // no collision
}
}
//L.LOG.Notice(s.Key)
s.Changed = true
}
func (s *Session) Login(val M.SX) {
if s.Key == `` {
s.RandomKey()
}
val[`login_at`] = T.Epoch()
s.SX = val
s.Changed = true
L.Print(`LOGIN`, s.Key, val)
Sessions.FadeMSX(s.Key, val, EXPIRE_SEC)
}
// should be called after receiving request
func (s *Session) Load(ctx *Context) {
r := ctx.RequestCtx
s.UserAgent = string(r.Request.Header.UserAgent())
s.IpAddr = r.RemoteAddr().String()
cookie := string(r.Request.Header.Cookie(SESS_KEY))
if cookie == `` {
s.SX = M.SX{}
s.RandomKey()
} else if !S.StartsWith(cookie, s.StateCSRF()) {
s.Logout() // possible incorrect cookie stealing
} else {
s.Key = cookie
s.SX = Sessions.GetMSX(s.Key)
//L.Print(`Session.Load`, s.Key,s.SX)
if len(s.SX) == 0 {
s.Logout() // possible using expired cookie
}
}
}
// should be called before writing response
func (s *Session) Save(ctx *Context) {
//L.Print(`Session.Save?`, s.Changed)
if s.Changed {
rem := Sessions.Expiry(s.Key)
expiration := time.Now().Add(time.Second * time.Duration(rem))
//L.Print(`Session.Save`, rem, expiration, s.Key)
cookie := &fasthttp.Cookie{}
cookie.SetKey(SESS_KEY)
cookie.SetValue(s.Key)
cookie.SetPath(`/`)
cookie.SetExpire(expiration)
ctx.Response.Header.SetCookie(cookie)
}
}
func (s *Session) StateCSRF() string {
return S.HashPassword(s.UserAgent) + `|`
}
func (s *Session) Touch() {
if s.Key == `` {
return
}
if Sessions.Expiry(s.Key) < RENEW_SEC {
s.SX[`renew_at`] = T.Epoch()
s.Changed = true
Sessions.FadeMSX(s.Key, s.SX, EXPIRE_SEC)
}
}
func (s *Session) String() string {
if len(s.SX) == 0 {
return ``
}
return s.SX.Pretty(` | `)
}
func (s *Session) NewlineString() string {
if len(s.SX) == 0 {
return ``
}
return s.SX.Pretty("\n\t")
}
func (s *Session) HeaderString() string {
return s.GetStr(`id`) + "\n\tUserAgent: " + s.UserAgent + "\n\tSessionKey: " + s.Key
}
func InitSession(sess_key string, expire_ns, renew_ns time.Duration, conn SessionConnector, glob SessionConnector) {
//rand.Seed(T.UnixNano())
SESS_KEY = S.IfEmpty(sess_key, SESS_KEY)
EXPIRE_SEC = int64(expire_ns / NS2SEC)
RENEW_SEC = int64(renew_ns / NS2SEC)
Sessions = conn
Globals = glob
}