-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
185 lines (154 loc) · 3.94 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
package session
import (
"crypto/rand"
"encoding/hex"
"github.com/Unknwon/com"
"github.com/codinl/go-logger"
"github.com/codinl/martini"
"github.com/codinl/ttlmap"
"io"
"net/http"
"net/url"
"sync"
"time"
)
const (
SESSION_USER = "session_user"
COOKIE_NAME = "sessionid"
MAX_AGE time.Duration = 3600
)
var sessions *ttlmap.TtlMap
// Handler is a middleware that maps a Session service into the Martini handler chain.
func Handler(newUser func() User, options ...Option) martini.Handler {
var option Option
if len(options) == 0 {
option = Option{
CookieName: COOKIE_NAME,
MaxAge: MAX_AGE,
}
}
if sessions == nil {
sessions = ttlmap.NewTtlMap(time.Second * option.MaxAge)
}
return func(resp http.ResponseWriter, req *http.Request, ctx martini.Context) {
session, err := getSession(resp, req, option)
if err != nil {
logger.Error(err)
ctx.Next()
return
}
ctx.MapTo(session, (*Session)(nil))
u, found := session.Get(SESSION_USER)
if !found {
u = newUser()
session.Set(SESSION_USER, u)
}
ctx.MapTo(u, (*User)(nil))
ctx.Next()
}
}
func getSession(resp http.ResponseWriter, req *http.Request, option Option) (Session, error) {
var sid string
c, err := req.Cookie(option.CookieName)
if err == nil {
sid, _ = url.QueryUnescape(c.Value)
s, found := sessions.Get(sid)
if found {
return s.(*session), nil
}
} else {
logger.Error(err)
sid = genSessionId()
setCookie(resp, req, sid, option)
}
session := NewSession(req, resp, sid)
sessions.Set(sid, session)
return session, nil
}
func setCookie(resp http.ResponseWriter, req *http.Request, sid string, option Option) {
cookie := &http.Cookie{
Name: option.CookieName,
Value: sid,
HttpOnly: option.HttpOnly,
Secure: option.Secure,
MaxAge: int(option.MaxAge),
}
http.SetCookie(resp, cookie)
}
func genSessionId() string {
return hex.EncodeToString(generateRandomKey(16))
}
// generateRandomKey creates a random key with the given strength.
func generateRandomKey(strength int) []byte {
k := make([]byte, strength)
if n, err := io.ReadFull(rand.Reader, k); n != strength || err != nil {
return com.RandomCreateBytes(strength)
}
return k
}
// Option --------------------------------------------------------------------
// Option stores configuration for a session or session store.
//
// Fields are a subset of http.Cookie fields.
type Option struct {
CookiePath string
Domain string
CookieName string
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'.
// MaxAge>0 means Max-Age attribute present and given in seconds.
MaxAge time.Duration
Secure bool
HttpOnly bool
}
// Session --------------------------------------------------------------------
type Session interface {
Set(key string, value interface{})
Get(key string) (value interface{}, found bool)
Delete(key string)
Authenticate(user User) error
UnAuthenticate() error
}
// Session stores the values and optional configuration for a session.
type session struct {
mutex sync.RWMutex
Req *http.Request
Resp http.ResponseWriter
Sid string
Option *Option
data map[string]interface{}
}
func NewSession(req *http.Request, resp http.ResponseWriter, sid string) Session {
return &session{
Req: req,
Resp: resp,
Sid: sid,
data: make(map[string]interface{}),
}
}
func (s *session) Set(key string, value interface{}) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.data[key] = value
}
func (s *session) Get(key string) (value interface{}, found bool) {
s.mutex.RLock()
defer s.mutex.RUnlock()
if value, ok := s.data[key]; ok {
return value, ok
}
return nil, false
}
func (s *session) Delete(key string) {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.data, key)
}
func (s *session) Authenticate(user User) error {
s.Set(SESSION_USER, user)
return nil
}
func (s *session) UnAuthenticate() error {
s.Delete(SESSION_USER)
return nil
}