-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
176 lines (155 loc) · 4.85 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
package secure
import (
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"net/http"
"time"
)
const (
sessionCookie = "session"
recordField = "ddf77ee1-6a23-4980-8edc-ff4139e98f22"
createdField = "45595a0b-7756-428e-bae0-5f7ded324e92"
validatedField = "fe6f1315-9aa1-4083-89a0-dcb6c198654b"
returnField = "eb8cacdd-d65f-441e-a63d-e4da69c2badc"
)
/*
A Session value is stored in the Config to manage session cryptography.
*/
type Session struct {
// Keys encapsulates the rotating key data & functionality.
*Keys
// LogInPath is the URL where Authentication() redirects to; a log in form
// should be served here.
// Default value is "/session".
LogInPath string
// LogOutPath is the URL where LogOut() redirects to.
// Default value is "/".
LogOutPath string
// ValidateTimeOut determines whether it's time to have the
// cookie data checked by the ValidateCookie function.
// Default value is 5 minutes.
ValidateTimeOut time.Duration
store *sessions.CookieStore
}
func (s *Session) getCookie(r *http.Request) (session *sessions.Session) {
s.freshen()
if s.store == nil {
s.store = sessions.NewCookieStore(s.KeyPairs...)
s.store.Options = &sessions.Options{
MaxAge: int(s.TimeOut / time.Second),
Secure: true,
Path: "/",
}
}
session, _ = s.store.Get(r, sessionCookie)
return
}
func create(w http.ResponseWriter, r *http.Request, record interface{}, redirect bool) (err error) {
session := sessionKeys.getCookie(r)
if session.Values[createdField] == nil {
session.Values[createdField] = time.Now()
}
session.Values[recordField] = record
session.Values[validatedField] = time.Now()
if r.TLS == nil {
err = ErrNoTLS
} else if e := session.Save(r, w); e != nil {
err = ErrTokenNotSaved
} else if redirect {
path := session.Values[returnField]
if path == nil {
path = sessionKeys.LogOutPath
}
http.Redirect(w, r, path.(string), http.StatusSeeOther)
}
return
}
// LogIn creates the cookie and sets the cookie. It redirects back to the path
// where Authenticate() was called.
//
// 'record' is the authentication data to store in the cookie, as returned by
// Authentication()
func LogIn(w http.ResponseWriter, r *http.Request, record interface{}) (err error) {
return create(w, r, record, true)
}
// Update updates the authentication data in the cookie.
func Update(w http.ResponseWriter, r *http.Request, record interface{}) (err error) {
return create(w, r, record, false)
}
func sessionCurrent(session *sessions.Session) (current bool) {
if created := session.Values[createdField]; created == nil {
} else {
current = time.Since(created.(time.Time)) < sessionKeys.TimeOut
}
return
}
func accountCurrent(session *sessions.Session, w http.ResponseWriter, r *http.Request) (current bool) {
if validated := session.Values[validatedField]; validated == nil {
} else if cur := time.Since(validated.(time.Time)) < sessionKeys.ValidateTimeOut; cur {
current = true
} else if record, cur := validate(session.Values[recordField]); cur {
session.Values[recordField] = record
session.Values[validatedField] = time.Now()
_ = session.Save(r, w)
current = true
}
return
}
type contextKey int
const authKey contextKey = 0
func authenticate(w http.ResponseWriter, r *http.Request, optional ...bool) (authenticated bool) {
enforce := true
if len(optional) > 0 {
enforce = !optional[0]
}
session := sessionKeys.getCookie(r)
if !session.IsNew && sessionCurrent(session) && accountCurrent(session, w, r) {
context.Set(r, authKey, session.Values[recordField])
authenticated = true
} else if enforce {
session = clearCookie(r)
session.Values[returnField] = r.URL.Path
_ = session.Save(r, w)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; url=` + sessionKeys.LogInPath + `">
</head>
<body>
<h2>Forbidden</h2>
<a id="location" href="` + sessionKeys.LogInPath + `">Log in</a>
</body>
</html>
`))
}
return
}
/*
Authentication returns the record that was stored in the cookie on LogIn().
Call from a Handle wrapped in secure.Handle or secure.IfHandle.
*/
func Authentication(r *http.Request) interface{} {
return context.Get(r, authKey)
}
func clearCookie(r *http.Request) (session *sessions.Session) {
session = sessionKeys.getCookie(r)
delete(session.Values, recordField)
delete(session.Values, createdField)
delete(session.Values, validatedField)
return
}
// LogOut deletes the cookie. If 'redirect' is 'true', the request is redirected
// to config.LogOutPath.
func LogOut(w http.ResponseWriter, r *http.Request, redirect bool) {
session := clearCookie(r)
session.Options = &sessions.Options{
MaxAge: -1,
}
_ = session.Save(r, w)
if redirect {
http.Redirect(w, r, sessionKeys.LogOutPath, http.StatusSeeOther)
}
}