@@ -141,13 +141,15 @@ func TestManagerHTTP(t *testing.T) {
141
141
})
142
142
143
143
t .Run ("suite=SessionAddAuthenticationMethod" , func (t * testing.T ) {
144
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
145
+
144
146
conf , reg := internal .NewFastRegistryWithMocks (t )
145
147
testhelpers .SetDefaultIdentitySchema (conf , "file://./stub/identity.schema.json" )
146
148
147
149
i := & identity.Identity {Traits : []byte ("{}" ), State : identity .StateActive }
148
150
require .NoError (t , reg .PrivilegedIdentityPool ().CreateIdentity (context .Background (), i ))
149
151
sess := session .NewInactiveSession ()
150
- require .NoError (t , sess .Activate (ctx , i , conf , time .Now ()))
152
+ require .NoError (t , sess .Activate (req , i , conf , time .Now ()))
151
153
require .NoError (t , reg .SessionPersister ().UpsertSession (context .Background (), sess ))
152
154
require .NoError (t , reg .SessionManager ().SessionAddAuthenticationMethods (context .Background (), sess .ID ,
153
155
session.AuthenticationMethod {
@@ -202,11 +204,12 @@ func TestManagerHTTP(t *testing.T) {
202
204
reg .RegisterPublicRoutes (context .Background (), rp )
203
205
204
206
t .Run ("case=valid" , func (t * testing.T ) {
205
- conf .MustSet (ctx , config .ViperKeySessionLifespan , "1m" )
207
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
208
+ conf .MustSet (req .Context (), config .ViperKeySessionLifespan , "1m" )
206
209
207
210
i := identity.Identity {Traits : []byte ("{}" )}
208
211
require .NoError (t , reg .PrivilegedIdentityPool ().CreateIdentity (context .Background (), & i ))
209
- s , _ = session .NewActiveSession (ctx , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
212
+ s , _ = session .NewActiveSession (req , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
210
213
211
214
c := testhelpers .NewClientWithCookies (t )
212
215
testhelpers .MockHydrateCookieClient (t , c , pts .URL + "/session/set" )
@@ -217,6 +220,7 @@ func TestManagerHTTP(t *testing.T) {
217
220
})
218
221
219
222
t .Run ("case=key rotation" , func (t * testing.T ) {
223
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
220
224
original := conf .GetProvider (ctx ).Strings (config .ViperKeySecretsCookie )
221
225
t .Cleanup (func () {
222
226
conf .MustSet (ctx , config .ViperKeySecretsCookie , original )
@@ -226,7 +230,7 @@ func TestManagerHTTP(t *testing.T) {
226
230
227
231
i := identity.Identity {Traits : []byte ("{}" )}
228
232
require .NoError (t , reg .PrivilegedIdentityPool ().CreateIdentity (context .Background (), & i ))
229
- s , _ = session .NewActiveSession (ctx , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
233
+ s , _ = session .NewActiveSession (req , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
230
234
231
235
c := testhelpers .NewClientWithCookies (t )
232
236
testhelpers .MockHydrateCookieClient (t , c , pts .URL + "/session/set" )
@@ -242,6 +246,7 @@ func TestManagerHTTP(t *testing.T) {
242
246
})
243
247
244
248
t .Run ("case=no panic on invalid cookie name" , func (t * testing.T ) {
249
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
245
250
conf .MustSet (ctx , config .ViperKeySessionLifespan , "1m" )
246
251
conf .MustSet (ctx , config .ViperKeySessionName , "$%˜\" " )
247
252
t .Cleanup (func () {
@@ -255,7 +260,7 @@ func TestManagerHTTP(t *testing.T) {
255
260
256
261
i := identity.Identity {Traits : []byte ("{}" )}
257
262
require .NoError (t , reg .PrivilegedIdentityPool ().CreateIdentity (context .Background (), & i ))
258
- s , _ = session .NewActiveSession (ctx , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
263
+ s , _ = session .NewActiveSession (req , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
259
264
260
265
c := testhelpers .NewClientWithCookies (t )
261
266
res , err := c .Get (pts .URL + "/session/set/invalid" )
@@ -264,11 +269,12 @@ func TestManagerHTTP(t *testing.T) {
264
269
})
265
270
266
271
t .Run ("case=valid and uses x-session-cookie" , func (t * testing.T ) {
272
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
267
273
conf .MustSet (ctx , config .ViperKeySessionLifespan , "1m" )
268
274
269
275
i := identity.Identity {Traits : []byte ("{}" )}
270
276
require .NoError (t , reg .PrivilegedIdentityPool ().CreateIdentity (context .Background (), & i ))
271
- s , _ = session .NewActiveSession (ctx , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
277
+ s , _ = session .NewActiveSession (req , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
272
278
273
279
c := testhelpers .NewClientWithCookies (t )
274
280
testhelpers .MockHydrateCookieClient (t , c , pts .URL + "/session/set" )
@@ -297,16 +303,17 @@ func TestManagerHTTP(t *testing.T) {
297
303
})
298
304
299
305
t .Run ("case=valid bearer auth as fallback" , func (t * testing.T ) {
306
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
300
307
conf .MustSet (ctx , config .ViperKeySessionLifespan , "1m" )
301
308
302
309
i := identity.Identity {Traits : []byte ("{}" ), State : identity .StateActive }
303
310
require .NoError (t , reg .PrivilegedIdentityPool ().CreateIdentity (context .Background (), & i ))
304
- s , err := session .NewActiveSession (ctx , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
311
+ s , err := session .NewActiveSession (req , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
305
312
require .NoError (t , err )
306
313
require .NoError (t , reg .SessionPersister ().UpsertSession (context .Background (), s ))
307
314
require .NotEmpty (t , s .Token )
308
315
309
- req , err : = http .NewRequest ("GET" , pts .URL + "/session/get" , nil )
316
+ req , err = http .NewRequest ("GET" , pts .URL + "/session/get" , nil )
310
317
require .NoError (t , err )
311
318
req .Header .Set ("Authorization" , "Bearer " + s .Token )
312
319
@@ -317,15 +324,16 @@ func TestManagerHTTP(t *testing.T) {
317
324
})
318
325
319
326
t .Run ("case=valid x-session-token auth even if bearer is set" , func (t * testing.T ) {
327
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
320
328
conf .MustSet (ctx , config .ViperKeySessionLifespan , "1m" )
321
329
322
330
i := identity.Identity {Traits : []byte ("{}" ), State : identity .StateActive }
323
331
require .NoError (t , reg .PrivilegedIdentityPool ().CreateIdentity (context .Background (), & i ))
324
- s , err := session .NewActiveSession (ctx , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
332
+ s , err := session .NewActiveSession (req , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
325
333
require .NoError (t , err )
326
334
require .NoError (t , reg .SessionPersister ().UpsertSession (context .Background (), s ))
327
335
328
- req , err : = http .NewRequest ("GET" , pts .URL + "/session/get" , nil )
336
+ req , err = http .NewRequest ("GET" , pts .URL + "/session/get" , nil )
329
337
require .NoError (t , err )
330
338
req .Header .Set ("Authorization" , "Bearer invalid" )
331
339
req .Header .Set ("X-Session-Token" , s .Token )
@@ -337,14 +345,15 @@ func TestManagerHTTP(t *testing.T) {
337
345
})
338
346
339
347
t .Run ("case=expired" , func (t * testing.T ) {
348
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
340
349
conf .MustSet (ctx , config .ViperKeySessionLifespan , "1ns" )
341
350
t .Cleanup (func () {
342
351
conf .MustSet (ctx , config .ViperKeySessionLifespan , "1m" )
343
352
})
344
353
345
354
i := identity.Identity {Traits : []byte ("{}" )}
346
355
require .NoError (t , reg .PrivilegedIdentityPool ().CreateIdentity (context .Background (), & i ))
347
- s , _ = session .NewActiveSession (ctx , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
356
+ s , _ = session .NewActiveSession (req , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
348
357
349
358
c := testhelpers .NewClientWithCookies (t )
350
359
testhelpers .MockHydrateCookieClient (t , c , pts .URL + "/session/set" )
@@ -357,11 +366,12 @@ func TestManagerHTTP(t *testing.T) {
357
366
})
358
367
359
368
t .Run ("case=revoked" , func (t * testing.T ) {
369
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
360
370
i := identity.Identity {Traits : []byte ("{}" )}
361
371
require .NoError (t , reg .PrivilegedIdentityPool ().CreateIdentity (context .Background (), & i ))
362
- s , _ = session .NewActiveSession (ctx , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
372
+ s , _ = session .NewActiveSession (req , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
363
373
364
- s , _ = session .NewActiveSession (ctx , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
374
+ s , _ = session .NewActiveSession (req , & i , conf , time .Now (), identity .CredentialsTypePassword , identity .AuthenticatorAssuranceLevel1 )
365
375
366
376
c := testhelpers .NewClientWithCookies (t )
367
377
testhelpers .MockHydrateCookieClient (t , c , pts .URL + "/session/set" )
@@ -379,6 +389,7 @@ func TestManagerHTTP(t *testing.T) {
379
389
conf .MustSet (ctx , config .ViperKeySessionLifespan , "1m" )
380
390
381
391
t .Run ("required_aal=aal2" , func (t * testing.T ) {
392
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
382
393
idAAL2 := createAAL2Identity (t , reg )
383
394
idAAL1 := createAAL1Identity (t , reg )
384
395
require .NoError (t , reg .PrivilegedIdentityPool ().CreateIdentity (context .Background (), idAAL1 ))
@@ -389,7 +400,7 @@ func TestManagerHTTP(t *testing.T) {
389
400
for _ , m := range complete {
390
401
s .CompletedLoginFor (m , "" )
391
402
}
392
- require .NoError (t , s .Activate (ctx , i , conf , time .Now ().UTC ()))
403
+ require .NoError (t , s .Activate (req , i , conf , time .Now ().UTC ()))
393
404
err := reg .SessionManager ().DoesSessionSatisfy ((& http.Request {}).WithContext (context .Background ()), s , requested )
394
405
if expectedError != nil {
395
406
require .ErrorAs (t , err , & expectedError )
@@ -424,7 +435,6 @@ func TestManagerHTTP(t *testing.T) {
424
435
}
425
436
426
437
func TestDoesSessionSatisfy (t * testing.T ) {
427
- ctx := context .Background ()
428
438
conf , reg := internal .NewFastRegistryWithMocks (t )
429
439
testhelpers .SetDefaultIdentitySchema (conf , "file://./stub/identity.schema.json" )
430
440
@@ -552,11 +562,12 @@ func TestDoesSessionSatisfy(t *testing.T) {
552
562
require .NoError (t , reg .PrivilegedIdentityPool ().DeleteIdentity (context .Background (), id .ID ))
553
563
})
554
564
565
+ req := x .NewTestHTTPRequest (t , "GET" , "/sessions/whoami" , nil )
555
566
s := session .NewInactiveSession ()
556
567
for _ , m := range tc .amr {
557
568
s .CompletedLoginFor (m .Method , m .AAL )
558
569
}
559
- require .NoError (t , s .Activate (ctx , id , conf , time .Now ().UTC ()))
570
+ require .NoError (t , s .Activate (req , id , conf , time .Now ().UTC ()))
560
571
561
572
err := reg .SessionManager ().DoesSessionSatisfy ((& http.Request {}).WithContext (context .Background ()), s , string (tc .requested ))
562
573
if tc .err != nil {
0 commit comments