54
54
import com .google .auth .http .HttpTransportFactory ;
55
55
import com .google .auth .oauth2 .GoogleCredentialsTest .MockTokenServerTransportFactory ;
56
56
import com .google .common .collect .ImmutableList ;
57
+ import com .google .common .collect .ImmutableSet ;
57
58
import java .io .ByteArrayOutputStream ;
58
59
import java .io .IOException ;
59
60
import java .io .InputStream ;
66
67
import java .util .Date ;
67
68
import java .util .List ;
68
69
import java .util .Map ;
70
+ import java .util .Set ;
69
71
import org .junit .jupiter .api .BeforeEach ;
70
72
import org .junit .jupiter .api .Test ;
71
73
@@ -107,11 +109,11 @@ class ImpersonatedCredentialsTest extends BaseSerializationTest {
107
109
+ "CJzdWIiOiIxMDIxMDE1NTA4MzQyMDA3MDg1NjgifQ.redacted" ;
108
110
public static final String ACCESS_TOKEN = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
109
111
112
+ private static final Set <String > IMMUTABLE_SCOPES_SET = ImmutableSet .of ("scope1" , "scope2" );
110
113
private static final String PROJECT_ID = "project-id" ;
111
114
public static final String IMPERSONATED_CLIENT_EMAIL =
112
115
113
- private static final List <String > SCOPES =
114
- Arrays .asList ("https://www.googleapis.com/auth/devstorage.read_only" );
116
+ private static final List <String > IMMUTABLE_SCOPES_LIST = ImmutableList .of ("scope1" , "scope2" );
115
117
private static final int VALID_LIFETIME = 300 ;
116
118
private static final int INVALID_LIFETIME = 43210 ;
117
119
private static JsonFactory JSON_FACTORY = GsonFactory .getDefaultInstance ();
@@ -156,7 +158,7 @@ private GoogleCredentials getSourceCredentials() throws IOException {
156
158
.setClientEmail (SA_CLIENT_EMAIL )
157
159
.setPrivateKey (privateKey )
158
160
.setPrivateKeyId (SA_PRIVATE_KEY_ID )
159
- .setScopes (SCOPES )
161
+ .setScopes (IMMUTABLE_SCOPES_LIST )
160
162
.setProjectId (PROJECT_ID )
161
163
.setHttpTransportFactory (transportFactory )
162
164
.build ();
@@ -275,7 +277,7 @@ void createScopedRequired_False() {
275
277
sourceCredentials ,
276
278
IMPERSONATED_CLIENT_EMAIL ,
277
279
null ,
278
- SCOPES ,
280
+ IMMUTABLE_SCOPES_LIST ,
279
281
VALID_LIFETIME ,
280
282
mockTransportFactory );
281
283
assertFalse (targetCredentials .createScopedRequired ());
@@ -288,13 +290,36 @@ void createScoped() {
288
290
sourceCredentials ,
289
291
IMPERSONATED_CLIENT_EMAIL ,
290
292
DELEGATES ,
291
- SCOPES ,
293
+ IMMUTABLE_SCOPES_LIST ,
292
294
VALID_LIFETIME ,
293
295
mockTransportFactory ,
294
296
QUOTA_PROJECT_ID );
295
297
296
298
ImpersonatedCredentials scoped_credentials =
297
- (ImpersonatedCredentials ) targetCredentials .createScoped (Arrays .asList ("scope1" , "scope2" ));
299
+ (ImpersonatedCredentials ) targetCredentials .createScoped (IMMUTABLE_SCOPES_LIST );
300
+ assertEquals (targetCredentials .getAccount (), scoped_credentials .getAccount ());
301
+ assertEquals (targetCredentials .getDelegates (), scoped_credentials .getDelegates ());
302
+ assertEquals (targetCredentials .getLifetime (), scoped_credentials .getLifetime ());
303
+ assertEquals (
304
+ targetCredentials .getSourceCredentials (), scoped_credentials .getSourceCredentials ());
305
+ assertEquals (targetCredentials .getQuotaProjectId (), scoped_credentials .getQuotaProjectId ());
306
+ assertEquals (Arrays .asList ("scope1" , "scope2" ), scoped_credentials .getScopes ());
307
+ }
308
+
309
+ @ Test
310
+ void createScopedWithImmutableScopes () {
311
+ ImpersonatedCredentials targetCredentials =
312
+ ImpersonatedCredentials .create (
313
+ sourceCredentials ,
314
+ IMPERSONATED_CLIENT_EMAIL ,
315
+ DELEGATES ,
316
+ IMMUTABLE_SCOPES_LIST ,
317
+ VALID_LIFETIME ,
318
+ mockTransportFactory ,
319
+ QUOTA_PROJECT_ID );
320
+
321
+ ImpersonatedCredentials scoped_credentials =
322
+ (ImpersonatedCredentials ) targetCredentials .createScoped (IMMUTABLE_SCOPES_SET );
298
323
assertEquals (targetCredentials .getAccount (), scoped_credentials .getAccount ());
299
324
assertEquals (targetCredentials .getDelegates (), scoped_credentials .getDelegates ());
300
325
assertEquals (targetCredentials .getLifetime (), scoped_credentials .getLifetime ());
@@ -319,7 +344,7 @@ void refreshAccessToken_unauthorized() throws IOException {
319
344
sourceCredentials ,
320
345
IMPERSONATED_CLIENT_EMAIL ,
321
346
null ,
322
- SCOPES ,
347
+ IMMUTABLE_SCOPES_LIST ,
323
348
VALID_LIFETIME ,
324
349
mockTransportFactory );
325
350
@@ -348,7 +373,7 @@ void refreshAccessToken_malformedTarget() throws IOException {
348
373
sourceCredentials ,
349
374
invalidTargetEmail ,
350
375
null ,
351
- SCOPES ,
376
+ IMMUTABLE_SCOPES_LIST ,
352
377
VALID_LIFETIME ,
353
378
mockTransportFactory );
354
379
@@ -365,7 +390,7 @@ void refreshAccessToken_malformedTarget() throws IOException {
365
390
void credential_with_zero_lifetime () throws IllegalStateException {
366
391
ImpersonatedCredentials targetCredentials =
367
392
ImpersonatedCredentials .create (
368
- sourceCredentials , IMPERSONATED_CLIENT_EMAIL , null , SCOPES , 0 );
393
+ sourceCredentials , IMPERSONATED_CLIENT_EMAIL , null , IMMUTABLE_SCOPES_LIST , 0 );
369
394
assertEquals (3600 , targetCredentials .getLifetime ());
370
395
}
371
396
@@ -378,7 +403,11 @@ void credential_with_invalid_lifetime() throws IOException, IllegalStateExceptio
378
403
() -> {
379
404
ImpersonatedCredentials targetCredentials =
380
405
ImpersonatedCredentials .create (
381
- sourceCredentials , IMPERSONATED_CLIENT_EMAIL , null , SCOPES , INVALID_LIFETIME );
406
+ sourceCredentials ,
407
+ IMPERSONATED_CLIENT_EMAIL ,
408
+ null ,
409
+ IMMUTABLE_SCOPES_LIST ,
410
+ INVALID_LIFETIME );
382
411
targetCredentials .refreshAccessToken ().getTokenValue ();
383
412
},
384
413
String .format (
@@ -415,7 +444,7 @@ void refreshAccessToken_success() throws IOException, IllegalStateException {
415
444
sourceCredentials ,
416
445
IMPERSONATED_CLIENT_EMAIL ,
417
446
null ,
418
- SCOPES ,
447
+ IMMUTABLE_SCOPES_LIST ,
419
448
VALID_LIFETIME ,
420
449
mockTransportFactory );
421
450
@@ -433,7 +462,7 @@ void getRequestMetadata_withQuotaProjectId() throws IOException, IllegalStateExc
433
462
sourceCredentials ,
434
463
IMPERSONATED_CLIENT_EMAIL ,
435
464
null ,
436
- SCOPES ,
465
+ IMMUTABLE_SCOPES_LIST ,
437
466
VALID_LIFETIME ,
438
467
mockTransportFactory ,
439
468
QUOTA_PROJECT_ID );
@@ -456,7 +485,7 @@ void getRequestMetadata_withoutQuotaProjectId() throws IOException, IllegalState
456
485
sourceCredentials ,
457
486
IMPERSONATED_CLIENT_EMAIL ,
458
487
null ,
459
- SCOPES ,
488
+ IMMUTABLE_SCOPES_LIST ,
460
489
VALID_LIFETIME ,
461
490
mockTransportFactory );
462
491
@@ -476,7 +505,7 @@ void refreshAccessToken_delegates_success() throws IOException, IllegalStateExce
476
505
sourceCredentials ,
477
506
IMPERSONATED_CLIENT_EMAIL ,
478
507
delegates ,
479
- SCOPES ,
508
+ IMMUTABLE_SCOPES_LIST ,
480
509
VALID_LIFETIME ,
481
510
mockTransportFactory );
482
511
@@ -495,7 +524,7 @@ void refreshAccessToken_invalidDate() throws IllegalStateException {
495
524
sourceCredentials ,
496
525
IMPERSONATED_CLIENT_EMAIL ,
497
526
null ,
498
- SCOPES ,
527
+ IMMUTABLE_SCOPES_LIST ,
499
528
VALID_LIFETIME ,
500
529
mockTransportFactory );
501
530
@@ -517,7 +546,7 @@ void getAccount_sameAs() {
517
546
sourceCredentials ,
518
547
IMPERSONATED_CLIENT_EMAIL ,
519
548
null ,
520
- SCOPES ,
549
+ IMMUTABLE_SCOPES_LIST ,
521
550
VALID_LIFETIME ,
522
551
mockTransportFactory );
523
552
@@ -534,7 +563,7 @@ void sign_sameAs() {
534
563
sourceCredentials ,
535
564
IMPERSONATED_CLIENT_EMAIL ,
536
565
null ,
537
- SCOPES ,
566
+ IMMUTABLE_SCOPES_LIST ,
538
567
VALID_LIFETIME ,
539
568
mockTransportFactory );
540
569
@@ -556,7 +585,7 @@ void sign_requestIncludesDelegates() throws IOException {
556
585
sourceCredentials ,
557
586
IMPERSONATED_CLIENT_EMAIL ,
558
587
ImmutableList .
of (
"[email protected] " ),
559
- SCOPES ,
588
+ IMMUTABLE_SCOPES_LIST ,
560
589
VALID_LIFETIME ,
561
590
mockTransportFactory );
562
591
@@ -595,7 +624,7 @@ void sign_usesSourceCredentials() {
595
624
sourceCredentials ,
596
625
IMPERSONATED_CLIENT_EMAIL ,
597
626
ImmutableList .
of (
"[email protected] " ),
598
- SCOPES ,
627
+ IMMUTABLE_SCOPES_LIST ,
599
628
VALID_LIFETIME ,
600
629
mockTransportFactory );
601
630
@@ -620,7 +649,7 @@ void sign_accessDenied_throws() {
620
649
sourceCredentials ,
621
650
IMPERSONATED_CLIENT_EMAIL ,
622
651
null ,
623
- SCOPES ,
652
+ IMMUTABLE_SCOPES_LIST ,
624
653
VALID_LIFETIME ,
625
654
mockTransportFactory );
626
655
@@ -652,7 +681,7 @@ void sign_serverError_throws() {
652
681
sourceCredentials ,
653
682
IMPERSONATED_CLIENT_EMAIL ,
654
683
null ,
655
- SCOPES ,
684
+ IMMUTABLE_SCOPES_LIST ,
656
685
VALID_LIFETIME ,
657
686
mockTransportFactory );
658
687
@@ -685,7 +714,7 @@ void idTokenWithAudience_sameAs() throws IOException {
685
714
sourceCredentials ,
686
715
IMPERSONATED_CLIENT_EMAIL ,
687
716
null ,
688
- SCOPES ,
717
+ IMMUTABLE_SCOPES_LIST ,
689
718
VALID_LIFETIME ,
690
719
mockTransportFactory );
691
720
@@ -716,7 +745,7 @@ void idTokenWithAudience_withEmail() throws IOException {
716
745
sourceCredentials ,
717
746
IMPERSONATED_CLIENT_EMAIL ,
718
747
null ,
719
- SCOPES ,
748
+ IMMUTABLE_SCOPES_LIST ,
720
749
VALID_LIFETIME ,
721
750
mockTransportFactory );
722
751
@@ -746,7 +775,7 @@ void idToken_withServerError() {
746
775
sourceCredentials ,
747
776
IMPERSONATED_CLIENT_EMAIL ,
748
777
null ,
749
- SCOPES ,
778
+ IMMUTABLE_SCOPES_LIST ,
750
779
VALID_LIFETIME ,
751
780
mockTransportFactory );
752
781
@@ -776,7 +805,7 @@ void idToken_withOtherError() {
776
805
sourceCredentials ,
777
806
IMPERSONATED_CLIENT_EMAIL ,
778
807
null ,
779
- SCOPES ,
808
+ IMMUTABLE_SCOPES_LIST ,
780
809
VALID_LIFETIME ,
781
810
mockTransportFactory );
782
811
@@ -806,7 +835,7 @@ void hashCode_equals() throws IOException {
806
835
sourceCredentials ,
807
836
IMPERSONATED_CLIENT_EMAIL ,
808
837
null ,
809
- SCOPES ,
838
+ IMMUTABLE_SCOPES_LIST ,
810
839
VALID_LIFETIME ,
811
840
mockTransportFactory );
812
841
@@ -815,7 +844,7 @@ void hashCode_equals() throws IOException {
815
844
sourceCredentials ,
816
845
IMPERSONATED_CLIENT_EMAIL ,
817
846
null ,
818
- SCOPES ,
847
+ IMMUTABLE_SCOPES_LIST ,
819
848
VALID_LIFETIME ,
820
849
mockTransportFactory );
821
850
@@ -834,7 +863,7 @@ void serialize() throws IOException, ClassNotFoundException {
834
863
sourceCredentials ,
835
864
IMPERSONATED_CLIENT_EMAIL ,
836
865
null ,
837
- SCOPES ,
866
+ IMMUTABLE_SCOPES_LIST ,
838
867
VALID_LIFETIME ,
839
868
mockTransportFactory );
840
869
GoogleCredentials deserializedCredentials = serializeAndDeserialize (targetCredentials );
0 commit comments