Repository navigation
Expand file tree
/
Copy pathcode_test.go
More file actions
343 lines (291 loc) · 12.8 KB
/
Copy pathcode_test.go
File metadata and controls
343 lines (291 loc) · 12.8 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
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
package magiclink_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xraph/forge"
"github.com/xraph/authsome/plugins/magiclink"
"github.com/xraph/authsome/store/memory"
)
// codeMailer is a CodeMailer that records every message.
type codeMailer struct {
mu sync.Mutex
msgs []magiclink.MagicLinkMessage
plain int
}
func (m *codeMailer) SendMagicLink(_ context.Context, _, _ string) error {
m.mu.Lock()
defer m.mu.Unlock()
m.plain++
return nil
}
func (m *codeMailer) SendMagicLinkWithCode(_ context.Context, msg magiclink.MagicLinkMessage) error {
m.mu.Lock()
defer m.mu.Unlock()
m.msgs = append(m.msgs, msg)
return nil
}
func (m *codeMailer) last(t *testing.T) magiclink.MagicLinkMessage {
t.Helper()
m.mu.Lock()
defer m.mu.Unlock()
require.NotEmpty(t, m.msgs, "no magic link email was sent")
return m.msgs[len(m.msgs)-1]
}
func (m *codeMailer) count() int {
m.mu.Lock()
defer m.mu.Unlock()
return len(m.msgs)
}
type codeFixture struct {
p *magiclink.Plugin
s *memory.Store
mailer *codeMailer
h http.Handler
}
func newCodeFixture(t *testing.T, ttl time.Duration) codeFixture {
t.Helper()
mailer := &codeMailer{}
p := magiclink.New(magiclink.Config{
Mailer: mailer,
EmailCode: true,
TokenTTL: ttl,
})
s := memory.New()
p.SetStore(s)
p.SetAppID(testAppIDStr)
mux := forge.NewRouter()
require.NoError(t, p.RegisterRoutes(mux))
return codeFixture{p: p, s: s, mailer: mailer, h: mux.Handler()}
}
func post(t *testing.T, h http.Handler, path string, body any) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, path, jsonBody(t, body))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
func decode(t *testing.T, rec *httptest.ResponseRecorder) map[string]any {
t.Helper()
var out map[string]any
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &out), "body=%s", rec.Body.String())
return out
}
func wrongCodeFor(code string) string {
if code == "000000" {
return "111111"
}
return "000000"
}
func TestSendCode_MailCarriesLinkAndCode(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
u := createTestUser(t, f.s)
rec := post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email})
require.Equal(t, http.StatusOK, rec.Code, "body=%s", rec.Body.String())
resp := decode(t, rec)
assert.Equal(t, "magic link sent", resp["status"])
assert.InDelta(t, 300, resp["expires_in"], 0)
assert.InDelta(t, 6, resp["code_length"], 0)
msg := f.mailer.last(t)
assert.Equal(t, u.Email, msg.Email)
assert.Len(t, msg.Token, 64)
assert.Regexp(t, `^[0-9]{6}$`, msg.Code)
assert.Equal(t, magiclink.PurposeSignIn, msg.Purpose)
assert.Equal(t, "Test User", msg.Name)
assert.Equal(t, testAppIDStr, msg.AppID)
assert.WithinDuration(t, time.Now().Add(5*time.Minute), msg.ExpiresAt, 5*time.Second)
assert.Zero(t, f.mailer.plain, "a CodeMailer is sent the full message, not the plain link")
}
// The response is decided before the address is looked up, so an unknown
// address gets exactly the body a registered one does.
func TestSendCode_UnknownEmailGetsSameResponse(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
u := createTestUser(t, f.s)
known := post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email})
unknown := post(t, f.h, "/v1/magic-link/send", map[string]string{"email": "nobody@example.com"})
require.Equal(t, http.StatusOK, known.Code)
require.Equal(t, http.StatusOK, unknown.Code)
assert.JSONEq(t, known.Body.String(), unknown.Body.String())
assert.Equal(t, 1, f.mailer.count(), "nothing is sent to an unknown address")
}
// A mailer that can't carry a code gets the plain link, and the response
// doesn't promise a code that will never arrive.
func TestSendCode_PlainMailerGetsNoCode(t *testing.T) {
mailer := &mockMailer{}
p := magiclink.New(magiclink.Config{Mailer: mailer, EmailCode: true, TokenTTL: 5 * time.Minute})
s := memory.New()
p.SetStore(s)
p.SetAppID(testAppIDStr)
u := createTestUser(t, s)
mux := forge.NewRouter()
require.NoError(t, p.RegisterRoutes(mux))
rec := post(t, mux.Handler(), "/v1/magic-link/send", map[string]string{"email": u.Email})
require.Equal(t, http.StatusOK, rec.Code)
assert.NotContains(t, decode(t, rec), "code_length")
require.Len(t, mailer.sent, 1)
}
func TestVerifyCode_SignsIn(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
u := createTestUser(t, f.s)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email}).Code)
msg := f.mailer.last(t)
// The address is matched however it is cased.
rec := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": " TEST@example.com ", "code": msg.Code})
require.Equal(t, http.StatusOK, rec.Code, "body=%s", rec.Body.String())
resp := decode(t, rec)
assert.NotEmpty(t, resp["session_token"])
assert.NotEmpty(t, resp["refresh_token"])
assert.NotNil(t, resp["user"])
stored, err := f.s.GetUser(context.Background(), u.ID)
require.NoError(t, err)
assert.True(t, stored.EmailVerified, "the code proves the inbox")
}
func TestVerifyCode_MissingFields(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
assert.Equal(t, http.StatusBadRequest, post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": "a@example.com"}).Code)
assert.Equal(t, http.StatusBadRequest, post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"code": "123456"}).Code)
}
// Five tries per code, wrong or not. The fifth may still be the right one;
// after five wrong ones the code is gone, right or not.
func TestVerifyCode_AttemptsAreBounded(t *testing.T) {
t.Run("right on the last try", func(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
u := createTestUser(t, f.s)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email}).Code)
code := f.mailer.last(t).Code
for i := 0; i < magiclink.MaxCodeAttempts-1; i++ {
rec := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": wrongCodeFor(code)})
require.Equal(t, http.StatusUnauthorized, rec.Code)
}
rec := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": code})
assert.Equal(t, http.StatusOK, rec.Code, "body=%s", rec.Body.String())
})
t.Run("dead after five wrong", func(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
u := createTestUser(t, f.s)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email}).Code)
msg := f.mailer.last(t)
for i := 0; i < magiclink.MaxCodeAttempts; i++ {
rec := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": wrongCodeFor(msg.Code)})
require.Equal(t, http.StatusUnauthorized, rec.Code)
}
rec := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": msg.Code})
assert.Equal(t, http.StatusUnauthorized, rec.Code, "the right code no longer works")
assert.NotContains(t, rec.Body.String(), "session_token")
// The link is a 256-bit secret and isn't charged for code guesses.
link := post(t, f.h, "/v1/magic-link/verify", map[string]string{"token": msg.Token})
assert.Equal(t, http.StatusOK, link.Code, "body=%s", link.Body.String())
})
}
func TestVerifyCode_Expired(t *testing.T) {
f := newCodeFixture(t, 150*time.Millisecond)
u := createTestUser(t, f.s)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email}).Code)
msg := f.mailer.last(t)
time.Sleep(300 * time.Millisecond)
rec := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": msg.Code})
assert.Equal(t, http.StatusUnauthorized, rec.Code)
assert.Equal(t, "invalid or expired code", decode(t, rec)["error"])
}
func TestVerifyCode_SingleUseAcrossLinkAndCode(t *testing.T) {
t.Run("code first", func(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
u := createTestUser(t, f.s)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email}).Code)
msg := f.mailer.last(t)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": msg.Code}).Code)
again := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": msg.Code})
assert.Equal(t, http.StatusUnauthorized, again.Code, "a code works once")
link := post(t, f.h, "/v1/magic-link/verify", map[string]string{"token": msg.Token})
assert.Equal(t, http.StatusUnauthorized, link.Code, "the link went with the code; body=%s", link.Body.String())
v, err := f.s.GetVerification(context.Background(), msg.Token)
require.NoError(t, err)
assert.True(t, v.Consumed, "the link's row is marked used as well")
})
t.Run("link first", func(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
u := createTestUser(t, f.s)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email}).Code)
msg := f.mailer.last(t)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/verify", map[string]string{"token": msg.Token}).Code)
code := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": msg.Code})
assert.Equal(t, http.StatusUnauthorized, code.Code, "the code went with the link")
})
t.Run("concurrent", func(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
u := createTestUser(t, f.s)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email}).Code)
msg := f.mailer.last(t)
reqs := []*http.Request{
httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/v1/magic-link/verify",
jsonBody(t, map[string]string{"token": msg.Token})),
httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/v1/magic-link/verify-code",
jsonBody(t, map[string]string{"email": u.Email, "code": msg.Code})),
}
var wg sync.WaitGroup
codes := make([]int, len(reqs))
for i, req := range reqs {
req.Header.Set("Content-Type", "application/json")
wg.Add(1)
go func() {
defer wg.Done()
rec := httptest.NewRecorder()
f.h.ServeHTTP(rec, req)
codes[i] = rec.Code
}()
}
wg.Wait()
ok := 0
for _, c := range codes {
if c == http.StatusOK {
ok++
}
}
assert.Equal(t, 1, ok, "exactly one of link and code signs in; got %v", codes)
})
}
// Only the newest code works: a resend retires the one before it.
func TestVerifyCode_ResendReplacesCode(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
u := createTestUser(t, f.s)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email}).Code)
first := f.mailer.last(t)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email}).Code)
second := f.mailer.last(t)
if first.Code != second.Code {
rec := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": first.Code})
assert.Equal(t, http.StatusUnauthorized, rec.Code)
}
rec := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": second.Code})
assert.Equal(t, http.StatusOK, rec.Code, "body=%s", rec.Body.String())
}
// An unknown address, a wrong code and a never-sent code all get the same
// answer, so /verify-code can't be used to find accounts.
func TestVerifyCode_AnswersDoNotRevealAccounts(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
u := createTestUser(t, f.s)
noCodeYet := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": "123456"})
unknown := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": "nobody@example.com", "code": "123456"})
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": u.Email}).Code)
wrong := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": u.Email, "code": wrongCodeFor(f.mailer.last(t).Code)})
for name, rec := range map[string]*httptest.ResponseRecorder{"no code yet": noCodeYet, "unknown": unknown, "wrong": wrong} {
assert.Equal(t, http.StatusUnauthorized, rec.Code, name)
assert.JSONEq(t, unknown.Body.String(), rec.Body.String(), name)
}
}
// The code is bound to the account it was sent for.
func TestVerifyCode_OtherAccountsCodeRefused(t *testing.T) {
f := newCodeFixture(t, 5*time.Minute)
victim := createTestUser(t, f.s)
require.Equal(t, http.StatusOK, post(t, f.h, "/v1/magic-link/send", map[string]string{"email": victim.Email}).Code)
code := f.mailer.last(t).Code
rec := post(t, f.h, "/v1/magic-link/verify-code", map[string]string{"email": "someone-else@example.com", "code": code})
assert.Equal(t, http.StatusUnauthorized, rec.Code)
}