-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_multi_auth_test.go
More file actions
577 lines (481 loc) · 19.3 KB
/
e2e_multi_auth_test.go
File metadata and controls
577 lines (481 loc) · 19.3 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package authsome_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xraph/forge"
authsome "github.com/xraph/authsome"
"github.com/xraph/authsome/account"
"github.com/xraph/authsome/apikey"
"github.com/xraph/authsome/id"
"github.com/xraph/authsome/middleware"
apikeyPlugin "github.com/xraph/authsome/plugins/apikey"
"github.com/xraph/authsome/session"
"github.com/xraph/authsome/store/memory"
"github.com/xraph/authsome/user"
"github.com/xraph/warden"
wardenmem "github.com/xraph/warden/store/memory"
)
// ──────────────────────────────────────────────────
// E2E Multi-Auth test helpers
// ──────────────────────────────────────────────────
// e2eEngineWithAPIKey creates an engine with the API key plugin registered.
// The memory.Store satisfies both store.Store and apikey.Store interfaces.
func e2eEngineWithAPIKey(t *testing.T) (*authsome.Engine, *memory.Store) {
t.Helper()
s := memory.New()
akPlugin := apikeyPlugin.New()
w, err := warden.NewEngine(warden.WithStore(wardenmem.New()))
require.NoError(t, err)
eng, err := authsome.NewEngine(
authsome.WithStore(s),
authsome.WithWarden(w),
authsome.WithDisableMigrate(),
authsome.WithAppID("aapp_01jf0000000000000000000000"),
authsome.WithPlugin(akPlugin),
)
require.NoError(t, err)
ctx := context.Background()
require.NoError(t, eng.Start(ctx))
t.Cleanup(func() { _ = eng.Stop(ctx) })
return eng, s
}
// e2eRouter sets up a forge router with the layered auth middleware.
func e2eRouter(eng *authsome.Engine) forge.Router {
router := forge.NewRouter()
mw := middleware.AuthMiddlewareWithStrategies(
eng.ResolveSessionByToken,
eng.ResolveUser,
eng.Strategies(),
eng.Logger(),
)
router.Use(mw)
return router
}
// e2eCreateAPIKey creates an API key for a user in the store and returns the raw key and key entity.
func e2eCreateAPIKey(t *testing.T, store *memory.Store, appID id.AppID, userID id.UserID) (string, *apikey.APIKey) {
t.Helper()
raw, hash, prefix, err := apikey.GenerateKey()
require.NoError(t, err)
now := time.Now()
key := &apikey.APIKey{
ID: id.NewAPIKeyID(),
AppID: appID,
UserID: userID,
Name: "test-key",
KeyHash: hash,
KeyPrefix: prefix,
CreatedAt: now,
UpdatedAt: now,
}
err = store.CreateAPIKey(context.Background(), key)
require.NoError(t, err)
return raw, key
}
// ──────────────────────────────────────────────────
// E2E: Multi-Auth — Bearer Session Auth
// ──────────────────────────────────────────────────
func TestE2E_MultiAuth_BearerSessionAuth(t *testing.T) {
eng, _ := e2eEngineWithAPIKey(t)
ctx := context.Background()
appID := e2eAppID(t)
// Step 1: Sign up a user
u, _, err := eng.SignUp(ctx, &account.SignUpRequest{
AppID: appID,
Email: "session-auth@example.com",
Password: "SecureP@ss1",
FirstName: "SessionUser",
})
require.NoError(t, err)
// Step 2: Sign in to get a session token
_, sess, err := eng.SignIn(ctx, &account.SignInRequest{
AppID: appID,
Email: "session-auth@example.com",
Password: "SecureP@ss1",
})
require.NoError(t, err)
require.NotEmpty(t, sess.Token)
// Step 3: Set up router with layered middleware
router := e2eRouter(eng)
var (
gotUser *user.User
gotMethod string
userOK bool
methodOK bool
)
router.GET("/test", func(ctx forge.Context) error {
gotUser, userOK = middleware.UserFrom(ctx.Context())
gotMethod, methodOK = middleware.AuthMethodFrom(ctx.Context())
return ctx.NoContent(http.StatusOK)
})
// Step 4: Send request with session token in Bearer header
req := httptest.NewRequestWithContext(context.Background(), "GET", "/test", nil)
req.Header.Set("Authorization", "Bearer "+sess.Token)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
// Step 5: Verify
assert.Equal(t, http.StatusOK, rec.Code)
assert.True(t, userOK, "user should be in context")
assert.Equal(t, u.ID, gotUser.ID)
assert.Equal(t, "session-auth@example.com", gotUser.Email)
assert.True(t, methodOK, "auth method should be set")
assert.Equal(t, "session", gotMethod)
}
// ──────────────────────────────────────────────────
// E2E: Multi-Auth — API Key Auth
// ──────────────────────────────────────────────────
func TestE2E_MultiAuth_APIKeyAuth(t *testing.T) {
eng, store := e2eEngineWithAPIKey(t)
ctx := context.Background()
appID := e2eAppID(t)
// Step 1: Sign up a user
u, _, err := eng.SignUp(ctx, &account.SignUpRequest{
AppID: appID,
Email: "apikey-auth@example.com",
Password: "SecureP@ss1",
FirstName: "APIKeyUser",
})
require.NoError(t, err)
// Step 2: Create API key in the store
rawKey, _ := e2eCreateAPIKey(t, store, appID, u.ID)
// Step 3: Set up router
router := e2eRouter(eng)
var (
gotUser *user.User
gotMethod string
userOK bool
methodOK bool
)
router.GET("/test", func(ctx forge.Context) error {
gotUser, userOK = middleware.UserFrom(ctx.Context())
gotMethod, methodOK = middleware.AuthMethodFrom(ctx.Context())
return ctx.NoContent(http.StatusOK)
})
// Step 4: Send request with API key in X-API-Key header
req := httptest.NewRequestWithContext(context.Background(), "GET", "/test", nil)
req.Header.Set("X-API-Key", rawKey)
req.Header.Set("X-App-ID", appID.String())
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
// Step 5: Verify
assert.Equal(t, http.StatusOK, rec.Code)
assert.True(t, userOK, "user should be in context via API key strategy")
assert.Equal(t, u.ID, gotUser.ID)
assert.Equal(t, "apikey-auth@example.com", gotUser.Email)
assert.True(t, methodOK, "auth method should be set")
assert.Equal(t, "strategy", gotMethod)
}
// ──────────────────────────────────────────────────
// E2E: Multi-Auth — Bearer Fails, API Key Succeeds
// ──────────────────────────────────────────────────
func TestE2E_MultiAuth_BearerFailsAPIKeySucceeds(t *testing.T) {
eng, store := e2eEngineWithAPIKey(t)
ctx := context.Background()
appID := e2eAppID(t)
// Step 1: Sign up a user
u, _, err := eng.SignUp(ctx, &account.SignUpRequest{
AppID: appID,
Email: "fallback-auth@example.com",
Password: "SecureP@ss1",
FirstName: "FallbackUser",
})
require.NoError(t, err)
// Step 2: Create API key
rawKey, _ := e2eCreateAPIKey(t, store, appID, u.ID)
// Step 3: Set up router
router := e2eRouter(eng)
var (
gotUser *user.User
gotMethod string
userOK bool
methodOK bool
)
router.GET("/test", func(ctx forge.Context) error {
gotUser, userOK = middleware.UserFrom(ctx.Context())
gotMethod, methodOK = middleware.AuthMethodFrom(ctx.Context())
return ctx.NoContent(http.StatusOK)
})
// Step 4: Send request with invalid session token in Bearer + valid API key in X-API-Key
req := httptest.NewRequestWithContext(context.Background(), "GET", "/test", nil)
req.Header.Set("Authorization", "Bearer invalid-session-token-abc123")
req.Header.Set("X-API-Key", rawKey)
req.Header.Set("X-App-ID", appID.String())
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
// Step 5: Verify — API key strategy should have resolved the identity
assert.Equal(t, http.StatusOK, rec.Code)
assert.True(t, userOK, "user should be in context via API key fallback")
assert.Equal(t, u.ID, gotUser.ID)
assert.True(t, methodOK, "auth method should be set")
assert.Equal(t, "strategy", gotMethod)
}
// ──────────────────────────────────────────────────
// E2E: Multi-Auth — Both Fail (unauthenticated)
// ──────────────────────────────────────────────────
func TestE2E_MultiAuth_BothFail(t *testing.T) {
eng, _ := e2eEngineWithAPIKey(t)
// Set up router
router := e2eRouter(eng)
var (
userOK bool
methodOK bool
)
router.GET("/test", func(ctx forge.Context) error {
_, userOK = middleware.UserFrom(ctx.Context())
_, methodOK = middleware.AuthMethodFrom(ctx.Context())
return ctx.NoContent(http.StatusOK)
})
// Send request with invalid session token and no API key
req := httptest.NewRequestWithContext(context.Background(), "GET", "/test", nil)
req.Header.Set("Authorization", "Bearer invalid-session-token-xyz")
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
// Request should pass through unauthenticated (middleware does not block)
assert.Equal(t, http.StatusOK, rec.Code)
assert.False(t, userOK, "no user should be in context")
assert.False(t, methodOK, "no auth method should be set")
}
// ──────────────────────────────────────────────────
// E2E: Multi-Auth — RequireAuth Blocks Unauthenticated
// ──────────────────────────────────────────────────
func TestE2E_MultiAuth_RequireAuthBlocksUnauthenticated(t *testing.T) {
eng, _ := e2eEngineWithAPIKey(t)
// Set up router with layered auth + RequireAuth
router := e2eRouter(eng)
router.Use(middleware.RequireAuth())
var called bool
router.GET("/protected", func(ctx forge.Context) error {
called = true
return ctx.NoContent(http.StatusOK)
})
// Send request with no valid auth headers
req := httptest.NewRequestWithContext(context.Background(), "GET", "/protected", nil)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
// Should be rejected with 401
assert.Equal(t, http.StatusUnauthorized, rec.Code)
assert.False(t, called, "handler should not be called without authentication")
assert.Contains(t, rec.Body.String(), "authentication required")
}
// ──────────────────────────────────────────────────
// E2E: Multi-Auth — API Key In X-API-Key Header
// ──────────────────────────────────────────────────
func TestE2E_MultiAuth_APIKeyInXAPIKeyHeader(t *testing.T) {
eng, store := e2eEngineWithAPIKey(t)
ctx := context.Background()
appID := e2eAppID(t)
// Step 1: Sign up a user
u, _, err := eng.SignUp(ctx, &account.SignUpRequest{
AppID: appID,
Email: "xapikey-user@example.com",
Password: "SecureP@ss1",
FirstName: "XAPIKeyUser",
})
require.NoError(t, err)
// Step 2: Create API key
rawKey, _ := e2eCreateAPIKey(t, store, appID, u.ID)
// Step 3: Set up router
router := e2eRouter(eng)
var (
gotUser *user.User
gotMethod string
userOK bool
methodOK bool
)
router.GET("/test", func(ctx forge.Context) error {
gotUser, userOK = middleware.UserFrom(ctx.Context())
gotMethod, methodOK = middleware.AuthMethodFrom(ctx.Context())
return ctx.NoContent(http.StatusOK)
})
// Step 4: Send request with API key via X-API-Key header (not Bearer)
req := httptest.NewRequestWithContext(context.Background(), "GET", "/test", nil)
req.Header.Set("X-API-Key", rawKey)
req.Header.Set("X-App-ID", appID.String())
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
// Step 5: Verify
assert.Equal(t, http.StatusOK, rec.Code)
assert.True(t, userOK, "user should be in context via X-API-Key header")
assert.Equal(t, u.ID, gotUser.ID)
assert.Equal(t, "xapikey-user@example.com", gotUser.Email)
assert.True(t, methodOK, "auth method should be set")
assert.Equal(t, "strategy", gotMethod)
}
// ──────────────────────────────────────────────────
// E2E: Multi-Auth — API Key With ask_ Prefix In Bearer
// ──────────────────────────────────────────────────
func TestE2E_MultiAuth_APIKeyWithAskPrefixInBearer(t *testing.T) {
eng, store := e2eEngineWithAPIKey(t)
ctx := context.Background()
appID := e2eAppID(t)
// Step 1: Sign up a user
u, _, err := eng.SignUp(ctx, &account.SignUpRequest{
AppID: appID,
Email: "bearer-ask@example.com",
Password: "SecureP@ss1",
FirstName: "BearerAskUser",
})
require.NoError(t, err)
// Step 2: Create API key (raw key starts with "ask_")
rawKey, _ := e2eCreateAPIKey(t, store, appID, u.ID)
require.True(t, len(rawKey) > 4 && rawKey[:4] == "ask_", "raw key must start with ask_")
// Step 3: Set up router
router := e2eRouter(eng)
var (
gotUser *user.User
gotMethod string
userOK bool
methodOK bool
)
router.GET("/test", func(ctx forge.Context) error {
gotUser, userOK = middleware.UserFrom(ctx.Context())
gotMethod, methodOK = middleware.AuthMethodFrom(ctx.Context())
return ctx.NoContent(http.StatusOK)
})
// Step 4: Send request with ask_ key in Authorization: Bearer header
// The middleware should skip session resolution and go directly to strategy chain
req := httptest.NewRequestWithContext(context.Background(), "GET", "/test", nil)
req.Header.Set("Authorization", "Bearer "+rawKey)
req.Header.Set("X-App-ID", appID.String())
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
// Step 5: Verify — routes to API key strategy (skips session)
assert.Equal(t, http.StatusOK, rec.Code)
assert.True(t, userOK, "user should be in context via API key strategy")
assert.Equal(t, u.ID, gotUser.ID)
assert.Equal(t, "bearer-ask@example.com", gotUser.Email)
assert.True(t, methodOK, "auth method should be set")
assert.Equal(t, "strategy", gotMethod)
}
// ──────────────────────────────────────────────────
// E2E: Multi-Auth — Revoked API Key Rejected
// ──────────────────────────────────────────────────
func TestE2E_MultiAuth_RevokedAPIKeyRejected(t *testing.T) {
eng, store := e2eEngineWithAPIKey(t)
ctx := context.Background()
appID := e2eAppID(t)
// Step 1: Sign up a user
_, _, err := eng.SignUp(ctx, &account.SignUpRequest{
AppID: appID,
Email: "revoked-key@example.com",
Password: "SecureP@ss1",
FirstName: "RevokedKeyUser",
})
require.NoError(t, err)
// Step 2: Create API key
rawKey, key := e2eCreateAPIKey(t, store, appID, e2eResolveUserID(t, eng, "revoked-key@example.com", appID))
// Step 3: Revoke the key
key.Revoked = true
key.UpdatedAt = time.Now()
err = store.UpdateAPIKey(ctx, key)
require.NoError(t, err)
// Step 4: Set up router
router := e2eRouter(eng)
var (
userOK bool
methodOK bool
)
router.GET("/test", func(ctx forge.Context) error {
_, userOK = middleware.UserFrom(ctx.Context())
_, methodOK = middleware.AuthMethodFrom(ctx.Context())
return ctx.NoContent(http.StatusOK)
})
// Step 5: Send request with the revoked API key
req := httptest.NewRequestWithContext(context.Background(), "GET", "/test", nil)
req.Header.Set("X-API-Key", rawKey)
req.Header.Set("X-App-ID", appID.String())
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
// Step 6: Verify — strategy returns error, falls through to unauthenticated
assert.Equal(t, http.StatusOK, rec.Code)
assert.False(t, userOK, "no user should be in context with revoked key")
assert.False(t, methodOK, "no auth method should be set with revoked key")
}
// e2eResolveUserID is a helper that signs in and returns the user's ID.
func e2eResolveUserID(t *testing.T, eng *authsome.Engine, email string, appID id.AppID) id.UserID {
t.Helper()
u, _, err := eng.SignIn(context.Background(), &account.SignInRequest{
AppID: appID,
Email: email,
Password: "SecureP@ss1",
})
require.NoError(t, err)
return u.ID
}
// ──────────────────────────────────────────────────
// E2E: Multi-Auth — Context Values Correct
// ──────────────────────────────────────────────────
func TestE2E_MultiAuth_ContextValuesCorrect(t *testing.T) {
eng, store := e2eEngineWithAPIKey(t)
ctx := context.Background()
appID := e2eAppID(t)
// Step 1: Sign up a user
u, _, err := eng.SignUp(ctx, &account.SignUpRequest{
AppID: appID,
Email: "context-check@example.com",
Password: "SecureP@ss1",
FirstName: "ContextUser",
})
require.NoError(t, err)
// Step 2: Create API key
rawKey, _ := e2eCreateAPIKey(t, store, appID, u.ID)
// Step 3: Set up router
router := e2eRouter(eng)
var (
gotUser *user.User
gotSession *session.Session
gotAppID id.AppID
gotUserID id.UserID
gotSessionID id.SessionID
gotMethod string
userOK bool
sessionOK bool
appIDOK bool
userIDOK bool
sessionIDOK bool
methodOK bool
)
router.GET("/test", func(ctx forge.Context) error {
gotUser, userOK = middleware.UserFrom(ctx.Context())
gotSession, sessionOK = middleware.SessionFrom(ctx.Context())
gotAppID, appIDOK = middleware.AppIDFrom(ctx.Context())
gotUserID, userIDOK = middleware.UserIDFrom(ctx.Context())
gotSessionID, sessionIDOK = middleware.SessionIDFrom(ctx.Context())
gotMethod, methodOK = middleware.AuthMethodFrom(ctx.Context())
return ctx.NoContent(http.StatusOK)
})
// Step 4: Send request with API key
req := httptest.NewRequestWithContext(context.Background(), "GET", "/test", nil)
req.Header.Set("X-API-Key", rawKey)
req.Header.Set("X-App-ID", appID.String())
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
// Step 5: Verify all context values
assert.Equal(t, http.StatusOK, rec.Code)
// User should be set
assert.True(t, userOK, "user should be in context")
assert.Equal(t, u.ID, gotUser.ID)
assert.Equal(t, "context-check@example.com", gotUser.Email)
assert.Equal(t, "ContextUser", gotUser.FirstName)
// AppID should match
assert.True(t, appIDOK, "app ID should be in context")
assert.Equal(t, appID, gotAppID)
// UserID should match the user
assert.True(t, userIDOK, "user ID should be in context")
assert.Equal(t, u.ID, gotUserID)
// Session should be set (synthetic session from API key strategy)
assert.True(t, sessionOK, "session should be in context (synthetic)")
assert.False(t, gotSession.ID.IsNil(), "synthetic session should have a non-nil ID")
assert.Equal(t, appID, gotSession.AppID)
assert.Equal(t, u.ID, gotSession.UserID)
// SessionID should be set
assert.True(t, sessionIDOK, "session ID should be in context")
assert.Equal(t, gotSession.ID, gotSessionID)
// Auth method should be "strategy"
assert.True(t, methodOK, "auth method should be set")
assert.Equal(t, "strategy", gotMethod)
}