forked from naoina/kocha
/
session.go
299 lines (267 loc) · 6.99 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package kocha
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha1"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/naoina/kocha/util"
"github.com/ugorji/go/codec"
)
// SessionConfig represents a configuration of session.
type SessionConfig struct {
// Name of cookie (key)
Name string
// Implementation of session store
Store SessionStore
// Expiration of session cookie, in seconds, from now. (not session expiration)
// 0 is for persistent.
CookieExpires time.Duration
// Expiration of session data, in seconds, from now. (not cookie expiration)
// 0 is for persistent.
SessionExpires time.Duration
HttpOnly bool
}
func (config *SessionConfig) Validate() error {
var sm *SessionMiddleware
for _, m := range appConfig.Middlewares {
if middleware, ok := m.(*SessionMiddleware); ok {
sm = middleware
}
}
if sm != nil {
if config == nil {
return fmt.Errorf("Because %T is nil, %T cannot be used", config, *sm)
}
if config.Store == nil {
return fmt.Errorf("Because %T.Store is nil, %T cannot be used", *config, *sm)
}
}
if config == nil {
return nil
}
if config.Name == "" {
return fmt.Errorf("%T.Name must be specify", *config)
}
if config.Store != nil {
if err := config.Store.Validate(); err != nil {
return err
}
}
return nil
}
// SessionStore is the interface that session store.
type SessionStore interface {
Save(sess Session) (key string)
Load(key string) (sess Session)
// Validate calls in boot time.
// Validate the session store specific values if you want. But highly recommended.
Validate() error
}
// Session represents a session data store.
type Session map[string]string
const (
SessionExpiresKey = "_kocha._sess._expires"
)
// Clear clear the all session data.
func (sess Session) Clear() {
for k, _ := range sess {
delete(sess, k)
}
}
func newSessionCookie(c *Controller) *http.Cookie {
expires, maxAge := expiresFromDuration(appConfig.Session.CookieExpires)
return &http.Cookie{
Name: appConfig.Session.Name,
Value: "",
Path: "/",
Expires: expires,
MaxAge: maxAge,
Secure: c.Request.IsSSL(),
HttpOnly: appConfig.Session.HttpOnly,
}
}
func expiresFromDuration(d time.Duration) (expires time.Time, maxAge int) {
switch d {
case -1:
// persistent
expires = util.Now().UTC().AddDate(20, 0, 0)
case 0:
expires = time.Time{}
default:
expires = util.Now().UTC().Add(d)
maxAge = int(d.Seconds())
}
return expires, maxAge
}
type ErrSession struct {
msg string
}
func (e ErrSession) Error() string {
return e.msg
}
func NewErrSession(msg string) error {
return ErrSession{
msg: msg,
}
}
type ErrSessionExpected struct {
msg string
}
func (e ErrSessionExpected) Error() string {
return e.msg
}
func NewErrSessionExpected(msg string) error {
return ErrSessionExpected{
msg: msg,
}
}
// Implementation of cookie store.
//
// This session store will be a session save to client-side cookie.
// Session cookie for save is encoded, encrypted and signed.
type SessionCookieStore struct {
// key for the encryption.
SecretKey string
// Key for the cookie singing.
SigningKey string
}
// Save saves and returns the key of session cookie.
// Actually, key is session cookie data itself.
func (store *SessionCookieStore) Save(sess Session) (key string) {
var buf bytes.Buffer
if err := codec.NewEncoder(&buf, &codec.MsgpackHandle{}).Encode(sess); err != nil {
panic(err)
}
encrypted, err := store.encrypt(buf.Bytes())
if err != nil {
panic(err)
}
return store.encode(store.sign(encrypted))
}
// Load returns the session data that extract from cookie value.
// The key is stored session cookie value.
func (store *SessionCookieStore) Load(key string) (sess Session) {
defer func() {
if err := recover(); err != nil {
if err, ok := err.(error); ok {
panic(NewErrSession(err.Error()))
}
panic(err)
}
}()
decoded, err := store.decode(key)
if err != nil {
panic(err)
}
unsigned, err := store.verify(decoded)
if err != nil {
panic(err)
}
decrypted, err := store.decrypt(unsigned)
if err != nil {
panic(err)
}
if err := codec.NewDecoderBytes(decrypted, &codec.MsgpackHandle{}).Decode(&sess); err != nil {
panic(err)
}
return sess
}
// Validate validates SecretKey size.
func (store *SessionCookieStore) Validate() error {
switch len(store.SecretKey) {
case 16, 24, 32:
return nil
}
return fmt.Errorf("%T.SecretKey size must be 16, 24 or 32, but %v", *store, len(store.SecretKey))
}
// encrypt returns encrypted data by AES-256-CBC.
func (store *SessionCookieStore) encrypt(buf []byte) ([]byte, error) {
block, err := aes.NewCipher([]byte(store.SecretKey))
if err != nil {
return nil, err
}
// padding for CBC
rem := (aes.BlockSize - len(buf)%aes.BlockSize) % aes.BlockSize
for i := 0; i < rem; i++ {
buf = append(buf, byte(rem))
}
encrypted := make([]byte, aes.BlockSize+len(buf))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encrypted[aes.BlockSize:], buf)
return encrypted, nil
}
// decrypt returns decrypted data from crypted data by AES-256-CBC.
func (store *SessionCookieStore) decrypt(buf []byte) ([]byte, error) {
block, err := aes.NewCipher([]byte(store.SecretKey))
if err != nil {
return nil, err
}
iv := buf[:aes.BlockSize]
decrypted := buf[aes.BlockSize:]
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(decrypted, decrypted)
return decrypted, nil
}
// encode returns encoded string by Base64 with URLEncoding.
// However, encoded string will stripped the padding character of Base64.
func (store *SessionCookieStore) encode(src []byte) string {
buf := make([]byte, base64.URLEncoding.EncodedLen(len(src)))
base64.URLEncoding.Encode(buf, src)
for {
if buf[len(buf)-1] != '=' {
break
}
buf = buf[:len(buf)-1]
}
return string(buf)
}
// decode returns decoded data from encoded data by Base64 with URLEncoding.
func (store *SessionCookieStore) decode(src string) ([]byte, error) {
size := len(src)
rem := (4 - size%4) % 4
buf := make([]byte, size+rem)
copy(buf, src)
for i := 0; i < rem; i++ {
buf[size+i] = '='
}
n, err := base64.URLEncoding.Decode(buf, buf)
if err != nil {
return nil, err
}
return buf[:n], nil
}
// sign returns signed data.
func (store *SessionCookieStore) sign(src []byte) []byte {
sign := store.hash(src)
return append(sign, src...)
}
// verify verify signed data and returns unsigned data if valid.
func (store *SessionCookieStore) verify(src []byte) (unsigned []byte, err error) {
if len(src) <= sha1.Size {
return nil, errors.New("session cookie value too short")
}
sign := src[:sha1.Size]
unsigned = src[sha1.Size:]
if !hmac.Equal(store.hash(unsigned), sign) {
return nil, errors.New("session cookie verification failed")
}
return unsigned, nil
}
// hash returns hashed data by HMAC-SHA1.
func (store *SessionCookieStore) hash(src []byte) []byte {
hash := hmac.New(sha1.New, []byte(store.SigningKey))
hash.Write(src)
return hash.Sum(nil)
}