-
Notifications
You must be signed in to change notification settings - Fork 208
/
remember_test.go
356 lines (271 loc) · 8.25 KB
/
remember_test.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package remember
import (
"bytes"
"context"
"crypto/sha512"
"encoding/base64"
"net/http"
"net/http/httptest"
"testing"
"github.com/volatiletech/authboss/v3"
"github.com/volatiletech/authboss/v3/mocks"
)
func TestInit(t *testing.T) {
t.Parallel()
ab := authboss.New()
r := &Remember{}
err := r.Init(ab)
if err != nil {
t.Fatal(err)
}
}
type testHarness struct {
remember *Remember
ab *authboss.Authboss
session *mocks.ClientStateRW
cookies *mocks.ClientStateRW
storer *mocks.ServerStorer
}
func testSetup() *testHarness {
harness := &testHarness{}
harness.ab = authboss.New()
harness.session = mocks.NewClientRW()
harness.cookies = mocks.NewClientRW()
harness.storer = mocks.NewServerStorer()
harness.ab.Config.Core.Logger = mocks.Logger{}
harness.ab.Config.Storage.SessionState = harness.session
harness.ab.Config.Storage.CookieState = harness.cookies
harness.ab.Config.Storage.Server = harness.storer
harness.remember = &Remember{harness.ab}
return harness
}
func TestRememberAfterAuth(t *testing.T) {
t.Parallel()
h := testSetup()
user := &mocks.User{Email: "test@test.com"}
r := mocks.Request("POST")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyValues, mocks.Values{Remember: true}))
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
rec := httptest.NewRecorder()
w := h.ab.NewResponse(rec)
if handled, err := h.remember.RememberAfterAuth(w, r, false); err != nil {
t.Fatal(err)
} else if handled {
t.Error("should never be handled")
}
// Force flush of headers so cookies are written
w.WriteHeader(http.StatusOK)
if len(h.storer.RMTokens["test@test.com"]) != 1 {
t.Error("token was not persisted:", h.storer.RMTokens)
}
if cookie, ok := h.cookies.ClientValues[authboss.CookieRemember]; !ok || len(cookie) == 0 {
t.Error("remember me cookie was not set")
}
}
func TestRememberAfterAuthSkip(t *testing.T) {
t.Parallel()
h := testSetup()
r := mocks.Request("POST")
rec := httptest.NewRecorder()
w := h.ab.NewResponse(rec)
if handled, err := h.remember.RememberAfterAuth(w, r, false); err != nil {
t.Fatal(err)
} else if handled {
t.Error("should never be handled")
}
if len(h.storer.RMTokens["test@test.com"]) != 0 {
t.Error("expected no tokens to be created")
}
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyValues, mocks.Values{Remember: false}))
if handled, err := h.remember.RememberAfterAuth(w, r, false); err != nil {
t.Fatal(err)
} else if handled {
t.Error("should never be handled")
}
if len(h.storer.RMTokens["test@test.com"]) != 0 {
t.Error("expected no tokens to be created")
}
}
func TestMiddlewareAuth(t *testing.T) {
t.Parallel()
h := testSetup()
user := &mocks.User{Email: "test@test.com"}
hash, token, _ := GenerateToken(user.Email)
h.storer.Users[user.Email] = user
h.storer.RMTokens[user.Email] = []string{hash}
h.cookies.ClientValues[authboss.CookieRemember] = token
r := mocks.Request("POST")
rec := httptest.NewRecorder()
w := h.ab.NewResponse(rec)
var err error
r, err = h.ab.LoadClientState(w, r)
if err != nil {
t.Fatal(err)
}
called := false
server := Middleware(h.ab)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
w.WriteHeader(http.StatusOK)
}))
server.ServeHTTP(w, r)
if !called {
t.Error("it should have called the underlying handler")
}
if h.session.ClientValues[authboss.SessionKey] != user.Email {
t.Error("should have saved the pid in the session")
}
// Elided the rest of the checks, authenticate tests do this
}
func TestAuthenticateSuccess(t *testing.T) {
t.Parallel()
h := testSetup()
user := &mocks.User{Email: "test@test.com"}
hash, token, _ := GenerateToken(user.Email)
h.storer.Users[user.Email] = user
h.storer.RMTokens[user.Email] = []string{hash}
h.cookies.ClientValues[authboss.CookieRemember] = token
r := mocks.Request("POST")
rec := httptest.NewRecorder()
w := h.ab.NewResponse(rec)
var err error
r, err = h.ab.LoadClientState(w, r)
if err != nil {
t.Fatal(err)
}
if err = Authenticate(h.ab, w, &r); err != nil {
t.Fatal(err)
}
w.WriteHeader(http.StatusOK)
if cookie := h.cookies.ClientValues[authboss.CookieRemember]; cookie == token {
t.Error("the cookie should have been replaced with a new token")
}
if len(h.storer.RMTokens[user.Email]) != 1 {
t.Error("one token should have been removed, and one should have been added")
} else if h.storer.RMTokens[user.Email][0] == token {
t.Error("a new token should have been saved")
}
if h.session.ClientValues[authboss.SessionKey] != user.Email {
t.Error("should have saved the pid in the session")
}
if h.session.ClientValues[authboss.SessionHalfAuthKey] != "true" {
t.Error("it should have become a half-authed session")
}
if r.Context().Value(authboss.CTXKeyPID).(string) != "test@test.com" {
t.Error("should have set the context value to log the user in")
}
}
func TestAuthenticateTokenNotFound(t *testing.T) {
t.Parallel()
h := testSetup()
user := &mocks.User{Email: "test@test.com"}
_, token, _ := GenerateToken(user.Email)
h.storer.Users[user.Email] = user
h.cookies.ClientValues[authboss.CookieRemember] = token
r := mocks.Request("POST")
rec := httptest.NewRecorder()
w := h.ab.NewResponse(rec)
var err error
r, err = h.ab.LoadClientState(w, r)
if err != nil {
t.Fatal(err)
}
if err = Authenticate(h.ab, w, &r); err != nil {
t.Fatal(err)
}
w.WriteHeader(http.StatusOK)
if len(h.cookies.ClientValues[authboss.CookieRemember]) != 0 {
t.Error("there should be no remember cookie left")
}
if len(h.session.ClientValues[authboss.SessionKey]) != 0 {
t.Error("it should have not logged the user in")
}
if r.Context().Value(authboss.CTXKeyPID) != nil {
t.Error("the context's pid should be empty")
}
}
func TestAuthenticateBadTokens(t *testing.T) {
t.Parallel()
h := testSetup()
doTest := func(t *testing.T) {
t.Helper()
r := mocks.Request("POST")
rec := httptest.NewRecorder()
w := h.ab.NewResponse(rec)
var err error
r, err = h.ab.LoadClientState(w, r)
if err != nil {
t.Fatal(err)
}
if err = Authenticate(h.ab, w, &r); err != nil {
t.Fatal(err)
}
w.WriteHeader(http.StatusOK)
if len(h.cookies.ClientValues[authboss.CookieRemember]) != 0 {
t.Error("there should be no remember cookie left")
}
if len(h.session.ClientValues[authboss.SessionKey]) != 0 {
t.Error("it should have not logged the user in")
}
if r.Context().Value(authboss.CTXKeyPID) != nil {
t.Error("the context's pid should be empty")
}
}
t.Run("base64", func(t *testing.T) {
h.cookies.ClientValues[authboss.CookieRemember] = "a"
doTest(t)
})
t.Run("cookieformat", func(t *testing.T) {
h.cookies.ClientValues[authboss.CookieRemember] = `aGVsbG8=` // hello
doTest(t)
})
}
func TestAfterPasswordReset(t *testing.T) {
t.Parallel()
h := testSetup()
user := &mocks.User{Email: "test@test.com"}
hash1, _, _ := GenerateToken(user.Email)
hash2, token2, _ := GenerateToken(user.Email)
h.storer.Users[user.Email] = user
h.storer.RMTokens[user.Email] = []string{hash1, hash2}
h.cookies.ClientValues[authboss.CookieRemember] = token2
r := mocks.Request("POST")
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
rec := httptest.NewRecorder()
w := h.ab.NewResponse(rec)
if handled, err := h.remember.AfterPasswordReset(w, r, false); err != nil {
t.Error(err)
} else if handled {
t.Error("it should never be handled")
}
w.WriteHeader(http.StatusOK) // Force header flush
if len(h.storer.RMTokens[user.Email]) != 0 {
t.Error("all remember me tokens should have been removed")
}
if len(h.cookies.ClientValues[authboss.CookieRemember]) != 0 {
t.Error("there should be no remember cookie left")
}
}
func TestGenerateToken(t *testing.T) {
t.Parallel()
hash, tok, err := GenerateToken("test")
if err != nil {
t.Fatal(err)
}
rawToken, err := base64.URLEncoding.DecodeString(tok)
if err != nil {
t.Error(err)
}
index := bytes.IndexByte(rawToken, ';')
if index < 0 {
t.Fatalf("problem with the token format: %v", rawToken)
}
bytPID := rawToken[:index]
if string(bytPID) != "test" {
t.Errorf("pid wrong: %s", bytPID)
}
sum := sha512.Sum512(rawToken)
gotHash := base64.StdEncoding.EncodeToString(sum[:])
if hash != gotHash {
t.Errorf("hash wrong, want: %s, got: %s", hash, gotHash)
}
}