forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
137 lines (114 loc) · 3.18 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
package revel
import (
"fmt"
"github.com/streadway/simpleuuid"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// A signed cookie (and thus limited to 4kb in size).
// Restriction: Keys may not have a colon in them.
type Session map[string]string
const (
SESSION_ID_KEY = "_ID"
TS_KEY = "_TS"
)
var expireAfterDuration time.Duration
func init() {
// Set expireAfterDuration, default to 30 days if no value in config
OnAppStart(func() {
var err error
if expiresString, ok := Config.String("session.expires"); !ok {
expireAfterDuration = 30 * 24 * time.Hour
} else if expireAfterDuration, err = time.ParseDuration(expiresString); err != nil {
panic(fmt.Errorf("session.expires invalid: %s", err))
}
})
}
// Return a UUID identifying this session.
func (s Session) Id() string {
if uuidStr, ok := s[SESSION_ID_KEY]; ok {
return uuidStr
}
uuid, err := simpleuuid.NewTime(time.Now())
if err != nil {
panic(err) // I don't think this can actually happen.
}
s[SESSION_ID_KEY] = uuid.String()
return s[SESSION_ID_KEY]
}
// Return a time.Time with session expiration date
func getSessionExpiration() time.Time {
return time.Now().Add(expireAfterDuration)
}
// Returns an http.Cookie containing the signed session.
func (s Session) cookie() *http.Cookie {
var sessionValue string
ts := getSessionExpiration()
s[TS_KEY] = getSessionExpirationCookie(ts)
for key, value := range s {
if strings.ContainsAny(key, ":\x00") {
panic("Session keys may not have colons or null bytes")
}
if strings.Contains(value, "\x00") {
panic("Session values may not have null bytes")
}
sessionValue += "\x00" + key + ":" + value + "\x00"
}
sessionData := url.QueryEscape(sessionValue)
return &http.Cookie{
Name: CookiePrefix + "_SESSION",
Value: Sign(sessionData) + "-" + sessionData,
Path: "/",
Expires: ts.UTC(),
}
}
func sessionTimeoutExpiredOrMissing(session Session) bool {
if exp, present := session[TS_KEY]; !present {
return true
} else if expInt, _ := strconv.Atoi(exp); int64(expInt) < time.Now().Unix() {
return true
}
return false
}
// Returns a Session pulled from signed cookie.
func getSessionFromCookie(cookie *http.Cookie) Session {
session := make(Session)
// Separate the data from the signature.
hyphen := strings.Index(cookie.Value, "-")
if hyphen == -1 || hyphen >= len(cookie.Value)-1 {
return session
}
sig, data := cookie.Value[:hyphen], cookie.Value[hyphen+1:]
// Verify the signature.
if !Verify(data, sig) {
INFO.Println("Session cookie signature failed")
return session
}
ParseKeyValueCookie(data, func(key, val string) {
session[key] = val
})
if sessionTimeoutExpiredOrMissing(session) {
session = make(Session)
}
return session
}
func SessionFilter(c *Controller, fc []Filter) {
c.Session = restoreSession(c.Request.Request)
fc[0](c, fc[1:])
// Store the session (and sign it).
c.SetCookie(c.Session.cookie())
}
func restoreSession(req *http.Request) Session {
session := make(Session)
cookie, err := req.Cookie(CookiePrefix + "_SESSION")
if err != nil {
return session
}
return getSessionFromCookie(cookie)
}
func getSessionExpirationCookie(t time.Time) string {
return strconv.FormatInt(t.Unix(), 10)
}