@@ -360,3 +360,87 @@ func TestArgon2InvalidInputs(t *testing.T) {
360
360
assert .Equal (t , http .StatusForbidden , resp .StatusCode )
361
361
})
362
362
}
363
+
364
+ func TestBasicAuthWithBcryptHashAndPrompt (t * testing.T ) {
365
+ hashedPassword , err := bcrypt .GenerateFromPassword ([]byte ("good" ), bcrypt .MinCost )
366
+ require .NoError (t , err )
367
+ t .Logf ("hashed password: %s" , string (hashedPassword ))
368
+
369
+ mw := BasicAuthWithBcryptHashAndPrompt ("dev" , string (hashedPassword ))
370
+
371
+ ts := httptest .NewServer (mw (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
372
+ t .Logf ("request %s" , r .URL )
373
+ w .WriteHeader (http .StatusOK )
374
+ _ , err := w .Write ([]byte ("blah" ))
375
+ require .NoError (t , err )
376
+ assert .True (t , IsAuthorized (r .Context ()))
377
+ })))
378
+ defer ts .Close ()
379
+
380
+ u := fmt .Sprintf ("%s%s" , ts .URL , "/something" )
381
+ client := http.Client {Timeout : 5 * time .Second }
382
+
383
+ tests := []struct {
384
+ name string
385
+ username string
386
+ password string
387
+ expectedStatus int
388
+ checkPrompt bool
389
+ }{
390
+ {
391
+ name : "no auth provided" ,
392
+ username : "" ,
393
+ password : "" ,
394
+ expectedStatus : http .StatusUnauthorized ,
395
+ checkPrompt : true ,
396
+ },
397
+ {
398
+ name : "correct credentials" ,
399
+ username : "dev" ,
400
+ password : "good" ,
401
+ expectedStatus : http .StatusOK ,
402
+ checkPrompt : false ,
403
+ },
404
+ {
405
+ name : "wrong username" ,
406
+ username : "wrong" ,
407
+ password : "good" ,
408
+ expectedStatus : http .StatusUnauthorized ,
409
+ checkPrompt : true ,
410
+ },
411
+ {
412
+ name : "wrong password" ,
413
+ username : "dev" ,
414
+ password : "bad" ,
415
+ expectedStatus : http .StatusUnauthorized ,
416
+ checkPrompt : true ,
417
+ },
418
+ {
419
+ name : "empty password" ,
420
+ username : "dev" ,
421
+ password : "" ,
422
+ expectedStatus : http .StatusUnauthorized ,
423
+ checkPrompt : true ,
424
+ },
425
+ }
426
+
427
+ for _ , tc := range tests {
428
+ t .Run (tc .name , func (t * testing.T ) {
429
+ req , err := http .NewRequest ("GET" , u , http .NoBody )
430
+ require .NoError (t , err )
431
+
432
+ if tc .username != "" || tc .password != "" {
433
+ req .SetBasicAuth (tc .username , tc .password )
434
+ }
435
+
436
+ resp , err := client .Do (req )
437
+ require .NoError (t , err )
438
+ assert .Equal (t , tc .expectedStatus , resp .StatusCode )
439
+
440
+ if tc .checkPrompt {
441
+ assert .Equal (t , `Basic realm="restricted", charset="UTF-8"` , resp .Header .Get ("WWW-Authenticate" ),
442
+ "should include WWW-Authenticate header" )
443
+ }
444
+ })
445
+ }
446
+ }
0 commit comments