This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sessions.go
136 lines (113 loc) · 2.7 KB
/
sessions.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
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package session
import (
"net/http"
"time"
"github.com/lunny/tango"
)
const (
DefaultMaxAge = 30 * time.Minute
DefaultSessionIdName = "SESSIONID"
DefaultCookiePath = "/"
)
type Sessions struct {
Options
}
type Sessionser interface {
SetSessions(*Sessions)
}
type Options struct {
MaxAge time.Duration
SessionIdName string
Store Store
Generator IdGenerator
Tracker Tracker
OnSessionNew func(*Session)
OnSessionRelease func(*Session)
}
func preOptions(opts []Options) Options {
var opt Options
if len(opts) > 0 {
opt = opts[0]
}
if opt.MaxAge == 0 {
opt.MaxAge = DefaultMaxAge
}
if opt.Store == nil {
opt.Store = NewMemoryStore(opt.MaxAge)
}
if opt.SessionIdName == "" {
opt.SessionIdName = DefaultSessionIdName
}
if opt.Generator == nil {
opt.Generator = NewSha1Generator(string(GenRandKey(16)))
}
if opt.Tracker == nil {
opt.Tracker = NewCookieTracker(opt.SessionIdName, 0, false, DefaultCookiePath)
}
return opt
}
func New(opts ...Options) *Sessions {
opt := preOptions(opts)
sessions := &Sessions{
Options: opt,
}
sessions.Run()
return sessions
}
func Default() *Sessions {
return NewWithTimeout(DefaultMaxAge)
}
func NewWithTimeout(maxAge time.Duration) *Sessions {
return New(Options{MaxAge: maxAge})
}
func (itor *Sessions) Handle(ctx *tango.Context) {
if action := ctx.Action(); action != nil {
if s, ok := action.(Sessionser); ok {
s.SetSessions(itor)
}
if s, ok := action.(Sessioner); ok {
err := s.InitSession(itor, ctx.Req(), ctx.ResponseWriter)
if err != nil {
ctx.Result = err
return
}
}
}
ctx.Next()
}
func (manager *Sessions) SessionFromID(id Id) *Session {
return &Session{
id: id,
maxAge: manager.MaxAge,
manager: manager,
}
}
func (manager *Sessions) SetMaxAge(maxAge time.Duration) {
manager.MaxAge = maxAge
manager.Tracker.SetMaxAge(maxAge)
manager.Store.SetMaxAge(maxAge)
}
func (manager *Sessions) SetIdMaxAge(id Id, maxAge time.Duration) {
manager.Store.SetIdMaxAge(id, maxAge)
}
func (manager *Sessions) Exist(id Id) bool {
return manager.Store.Exist(id)
}
func (manager *Sessions) Session(req *http.Request, rw http.ResponseWriter) *Session {
var sess Session
sess.InitSession(manager, req, rw)
return &sess
}
func (manager *Sessions) Invalidate(rw http.ResponseWriter, session *Session) {
if manager.OnSessionRelease != nil {
manager.OnSessionRelease(session)
}
manager.Store.Clear(session.id)
manager.Tracker.Clear(rw)
}
func (manager *Sessions) Run() error {
return manager.Store.Run()
}