-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie.go
More file actions
279 lines (242 loc) · 9.12 KB
/
Copy pathcookie.go
File metadata and controls
279 lines (242 loc) · 9.12 KB
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
// Copyright 2014 Robert W. Johnstone. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package httpauth
import (
"container/heap"
"errors"
"html"
"net/http"
"sync"
"time"
)
// The following variables are used to specify error conditions for this
// package.
var (
ErrBadUsernameOrPassword = errors.New("Bad username or password.")
ErrInvalidToken = errors.New("The session token was invalid.")
)
type cookieClientInfo struct {
username string // username for this authorized connection
lastContact int64 // time of last communication with this client (unix nanoseconds)
nonce string // unique per client salt
}
type cookiePriorityQueue []*cookieClientInfo
func (pq cookiePriorityQueue) Len() int {
return len(pq)
}
func (pq cookiePriorityQueue) Less(i, j int) bool {
return pq[i].lastContact < pq[j].lastContact
}
func (pq cookiePriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
func (pq *cookiePriorityQueue) Push(x interface{}) {
*pq = append(*pq, x.(*cookieClientInfo))
}
func (pq *cookiePriorityQueue) Pop() interface{} {
n := len(*pq)
ret := (*pq)[n-1]
*pq = (*pq)[:n-1]
return ret
}
func (pq cookiePriorityQueue) MinValue() int64 {
n := len(pq)
return pq[n-1].lastContact
}
// A Cookie is a policy for authenticating users that uses a cookie stored
// on the client to verify authorized clients. This authentication scheme
// is more involved than the others, as callers will need to implement URLs
// for login and logout pages.
//
// When a user successfully logs in, a token (nonce) is saved in a cookie.
// The presence and validity of that token is verified to authorize future
// HTTP requests. The tokens can also be invalidated to logout a users.
type Cookie struct {
// Realm provides a 'namespace' where the authentication will be considered.
Realm string
// Auth provides a function or closure that can validate if a username/password combination is valid
Auth Authenticator
// Clients are redirected to the LoginPage when they don't have authorization
LoginPage string
// Path sets the scope of the authorization cookie
Path string
// RequireXsrfHeader adds an additional verification. See function VerifyXsrfHeader.
RequireXsrfHeader bool
// CientCacheResidence controls how long client information is retained
ClientCacheResidence time.Duration
mutex sync.Mutex
clientsByNonce map[string]*cookieClientInfo
clientsByUser map[string]*cookieClientInfo
lru cookiePriorityQueue
}
// NewCookie creates a new authentication policy that uses the cookie authentication scheme.
func NewCookie(realm, loginPageUrl string, auth Authenticator) *Cookie {
return &Cookie{
realm,
auth,
loginPageUrl,
"/",
false,
DefaultClientCacheResidence,
sync.Mutex{},
make(map[string]*cookieClientInfo),
make(map[string]*cookieClientInfo),
nil}
}
func (a *Cookie) evictLeastRecentlySeen() {
now := time.Now().UnixNano()
// Remove all entries from the client cache older than the
// residence time.
for len(a.lru) > 0 && a.lru.MinValue()+a.ClientCacheResidence.Nanoseconds() <= now {
client := heap.Pop(&a.lru).(*cookieClientInfo)
delete(a.clientsByNonce, client.nonce)
delete(a.clientsByUser, client.username)
}
}
// Authorize retrieves the credientials from the HTTP request, and
// returns the username only if the credientials could be validated.
// If the return value is blank, then the credentials are missing,
// invalid, or a system error prevented verification.
func (a *Cookie) Authorize(r *http.Request) (username string) {
// Verify XSRF header
if a.RequireXsrfHeader && !VerifyXsrfHeader(r) {
return ""
}
// Find the nonce used to identify a client
token, err := r.Cookie("Authorization")
if err != nil || token.Value == "" {
return ""
}
if len(token.Value) != nonceLen {
return ""
}
// Lock before mutating the fields of the policy
a.mutex.Lock()
defer a.mutex.Unlock()
// Do we have a client with that nonce?
if client, ok := a.clientsByNonce[token.Value]; ok {
client.lastContact = time.Now().UnixNano()
return client.username
}
return ""
}
// NotifyAuthRequired adds the headers to the HTTP response to
// inform the client of the failed authorization, and which scheme
// must be used to gain authentication.
//
// Caller's should consider adding sending an HTML response with a link
// to the login page for GET requests.
func (a *Cookie) NotifyAuthRequired(w http.ResponseWriter, r *http.Request) {
// This code is derived from http.Redirect
w.Header().Set("Location", a.LoginPage)
w.WriteHeader(http.StatusTemporaryRedirect)
// RFC2616 recommends that a short note "SHOULD" be included in the
// response because older user agents may not understand 301/307.
// Shouldn't send the response for POST or HEAD; that leaves GET.
if r.Method == "GET" {
note := "<a href=\"" + html.EscapeString(a.LoginPage) + "\">" + http.StatusText(http.StatusTemporaryRedirect) + "</a>.\n"
w.Write([]byte(note))
}
// Lock before mutating the fields of the policy
a.mutex.Lock()
defer a.mutex.Unlock()
// Check for old clientInfo, and evict those older than
// residence time.
a.evictLeastRecentlySeen()
}
// The function createSession checks the credentials of a client, and, if
// valid, creates a client entry. The nonce can be used by the client to
// identify the session.
//
// This functions handles internal details of creating the session only.
// The caller is still responsible for creating the HTTP response, which
// will need to save the returned nonce.
//
// If the credentials cannot be verified, an error will be returned (ErrBadUsernameOrPassword).
func (a *Cookie) createSession(username, password string) (nonce string, err error) {
// Authorize the user
if !a.Auth(username, password) {
return "", ErrBadUsernameOrPassword
}
// Create an entry for this user
nonce, err = createNonce()
if err != nil {
return "", err
}
// Lock before mutating the fields of the policy
a.mutex.Lock()
defer a.mutex.Unlock()
// Check if there is already a session for this username
if ci, ok := a.clientsByUser[username]; ok {
ci.lastContact = time.Now().UnixNano()
return ci.nonce, nil
}
ci := &cookieClientInfo{username, time.Now().UnixNano(), nonce}
a.clientsByNonce[nonce] = ci
a.clientsByUser[username] = ci
return nonce, nil
}
// Login checks the credentials (a username/password pair) of the client.
// If successful, a session is created, and then a cookie is set on the
// HTTP response so that the client can access the session in future
// HTTP requests.
//
// The caller is responsable for creating an appropriate response body for
// the HTTP request. For successful validation, redirection (http.StatusTemporaryRedirect)
// to the protected content is most likely the correct response.
//
// If the credentials cannot be verified, an error (ErrBadUsernameOrPassword)
// is returned. Other errors are possible. The caller is then responsable
// for creating an appropriate reponse to the HTTP request.
func (a *Cookie) Login(w http.ResponseWriter, username, password string) error {
nonce, err := a.createSession(username, password)
if err != nil {
return err
}
// There is no reason for client-side code to access the nonce. Therefore,
// we will set the cookie as HttpOnly.
// We should also consider setting the cookie as secure, and restrict
// it to HTTPS connections. However, some library users might be
// using HTTP, and the nonce should (at minimum) be safe against
// replay attacks.
http.SetCookie(w, &http.Cookie{Name: "Authorization", Value: nonce, Path: a.Path, HttpOnly: true})
return nil
}
// The function destroySession ensures that the nonce is no longer valid.
func (a *Cookie) destroySession(nonce string) {
a.mutex.Lock()
defer a.mutex.Unlock()
// Do we have a client with that nonce?
if client, ok := a.clientsByNonce[nonce]; ok {
// remove client info from maps
delete(a.clientsByNonce, nonce)
delete(a.clientsByUser, client.username)
// client info is still in the priority queue
// however, it will be removed in due time when it expires
}
}
// Logout ensures that the session associated with the HTTP request
// is no longer valid. It then sets a header on the response to erase any
// cookies used by the client to identify the session. However, even if
// future HTTP requests contains the cookie, the call to Authorize will
// fail.
//
// The caller is responsable for create an appropriate response body for the HTTP request.
// When the function is successful, redirection (http.StatusTemporaryRedirect) to the
// a login page or public content is most likely the correct response.
//
// If the credentials cannot be verified, an error is
// returned. The caller is then responsable for creating an appropriate reponse to
// the HTTP request.
func (a *Cookie) Logout(w http.ResponseWriter, r *http.Request) error {
// Find the nonce used to identify a client
token, err := r.Cookie("Authorization")
if err == nil || token.Value != "" {
// Invalidate the nonce
a.destroySession(token.Value)
}
// Clear the cookie from the client
http.SetCookie(w, &http.Cookie{Name: "Authorization", Value: "", Path: a.Path, Expires: time.Unix(0, 0)})
return nil
}