@@ -69,6 +69,7 @@ func TestOAuthServiceAccountClient(t *testing.T) {
69
69
t .Fatalf ("unexpected error: %v" , err )
70
70
}
71
71
clusterAdminOAuthClient := oauthclient .NewForConfigOrDie (clusterAdminClientConfig ).Oauth ()
72
+ clusterAdminUserClient := userclient .NewForConfigOrDie (clusterAdminClientConfig )
72
73
73
74
projectName := "hammer-project"
74
75
if _ , _ , err := testserver .CreateNewProject (clusterAdminClientConfig , projectName , "harold" ); err != nil {
@@ -375,6 +376,85 @@ func TestOAuthServiceAccountClient(t *testing.T) {
375
376
})
376
377
clusterAdminOAuthClient .OAuthClientAuthorizations ().Delete ("harold:" + oauthClientConfig .ClientId , nil )
377
378
}
379
+
380
+ {
381
+ oauthClientConfig := & osincli.ClientConfig {
382
+ ClientId : apiserverserviceaccount .MakeUsername (defaultSA .Namespace , defaultSA .Name ),
383
+ ClientSecret : string (oauthSecret .Data [kapi .ServiceAccountTokenKey ]),
384
+ AuthorizeUrl : clusterAdminClientConfig .Host + "/oauth/authorize" ,
385
+ TokenUrl : clusterAdminClientConfig .Host + "/oauth/token" ,
386
+ RedirectUrl : redirectURL ,
387
+ Scope : scope .Join ([]string {"user:info" , "role:edit:" + projectName }),
388
+ SendClientSecretInParams : true ,
389
+ }
390
+ t .Log ("Testing grant flow is reentrant" )
391
+ // First time, the approval steps are needed
392
+ // Second time, the approval steps are skipped
393
+ // Then we delete and recreate the user to make the client authorization UID no longer match
394
+ // Third time, the approval steps are needed
395
+ // Fourth time, the approval steps are skipped
396
+ runOAuthFlow (t , clusterAdminClientConfig , projectName , oauthClientConfig , nil , authorizationCodes , authorizationErrors , true , true , []string {
397
+ "GET /oauth/authorize" ,
398
+ "received challenge" ,
399
+ "GET /oauth/authorize" ,
400
+ "redirect to /oauth/authorize/approve" ,
401
+ "form" ,
402
+ "POST /oauth/authorize/approve" ,
403
+ "redirect to /oauth/authorize" ,
404
+ "redirect to /oauthcallback" ,
405
+ "code" ,
406
+ "scope:" + oauthClientConfig .Scope ,
407
+ })
408
+ runOAuthFlow (t , clusterAdminClientConfig , projectName , oauthClientConfig , nil , authorizationCodes , authorizationErrors , true , true , []string {
409
+ "GET /oauth/authorize" ,
410
+ "received challenge" ,
411
+ "GET /oauth/authorize" ,
412
+ "redirect to /oauthcallback" ,
413
+ "code" ,
414
+ "scope:" + oauthClientConfig .Scope ,
415
+ })
416
+
417
+ // Delete the user to make the client authorization UID no longer match
418
+ // runOAuthFlow will cause the creation of the same user with a different UID during its challenge phase
419
+ if err := deleteUser (clusterAdminUserClient , "harold" ); err != nil {
420
+ t .Fatalf ("Failed to delete and recreate harold user: %v" , err )
421
+ }
422
+
423
+ runOAuthFlow (t , clusterAdminClientConfig , projectName , oauthClientConfig , nil , authorizationCodes , authorizationErrors , true , true , []string {
424
+ "GET /oauth/authorize" ,
425
+ "received challenge" ,
426
+ "GET /oauth/authorize" ,
427
+ "redirect to /oauth/authorize/approve" ,
428
+ "form" ,
429
+ "POST /oauth/authorize/approve" ,
430
+ "redirect to /oauth/authorize" ,
431
+ "redirect to /oauthcallback" ,
432
+ "code" ,
433
+ "scope:" + oauthClientConfig .Scope ,
434
+ })
435
+ runOAuthFlow (t , clusterAdminClientConfig , projectName , oauthClientConfig , nil , authorizationCodes , authorizationErrors , true , true , []string {
436
+ "GET /oauth/authorize" ,
437
+ "received challenge" ,
438
+ "GET /oauth/authorize" ,
439
+ "redirect to /oauthcallback" ,
440
+ "code" ,
441
+ "scope:" + oauthClientConfig .Scope ,
442
+ })
443
+ clusterAdminOAuthClient .OAuthClientAuthorizations ().Delete ("harold:" + oauthClientConfig .ClientId , nil )
444
+ }
445
+ }
446
+
447
+ func deleteUser (clusterAdminUserClient userclient.UserInterface , name string ) error {
448
+ oldUser , err := clusterAdminUserClient .Users ().Get (name , metav1.GetOptions {})
449
+ if err != nil {
450
+ return err
451
+ }
452
+ for _ , identity := range oldUser .Identities {
453
+ if err := clusterAdminUserClient .Identities ().Delete (identity , nil ); err != nil {
454
+ return err
455
+ }
456
+ }
457
+ return clusterAdminUserClient .Users ().Delete (name , nil )
378
458
}
379
459
380
460
func drain (ch chan string ) {
0 commit comments