google-cloud-auth 1.9.0

Google Cloud Client Libraries for Rust - Authentication
Documentation
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(test)]
mod tests {
    use google_cloud_auth::credentials::EntityTag;
    use google_cloud_auth::credentials::external_account::ProgrammaticBuilder;
    use google_cloud_auth::credentials::impersonated::Builder as ImpersonatedBuilder;
    use google_cloud_auth::credentials::mds::Builder as MdsBuilder;
    use google_cloud_auth::credentials::service_account::Builder as ServiceAccountBuilder;
    use google_cloud_auth::credentials::subject_token::{
        Builder as SubjectTokenBuilder, SubjectToken, SubjectTokenProvider,
    };
    use google_cloud_auth::credentials::user_account::Builder as UserAccountCredentialBuilder;
    use google_cloud_auth::credentials::{AccessToken, AccessTokenCredentials};
    use google_cloud_auth::credentials::{
        AccessTokenCredentialsProvider, Builder as AccessTokenCredentialBuilder, CacheableResource,
        Credentials, CredentialsProvider, api_key_credentials::Builder as ApiKeyCredentialsBuilder,
    };
    use google_cloud_auth::errors::CredentialsError;
    use google_cloud_auth::errors::SubjectTokenProviderError;
    use http::header::{AUTHORIZATION, HeaderName, HeaderValue};
    use http::{Extensions, HeaderMap};
    use httptest::{Expectation, Server, matchers::*, responders::*};
    use scoped_env::ScopedEnv;
    use serde_json::json;
    use serial_test::{parallel, serial};
    use std::error::Error;
    use std::fmt;
    use test_case::test_case;

    type Result<T> = anyhow::Result<T>;
    type TestResult = anyhow::Result<()>;

    fn find_source_error<'a, T: Error + 'static>(
        error: &'a (dyn Error + 'static),
    ) -> Option<&'a T> {
        let mut source = error.source();
        while let Some(err) = source {
            if let Some(target_err) = err.downcast_ref::<T>() {
                return Some(target_err);
            }
            source = err.source();
        }
        None
    }

    pub(crate) fn write_cred_json(contents: &str) -> tempfile::TempPath {
        let file = tempfile::NamedTempFile::new().unwrap();
        let path = file.into_temp_path();
        std::fs::write(&path, contents).expect("Unable to write to temporary file.");
        path
    }

    #[tokio::test]
    #[serial]
    async fn create_access_token_credentials_fallback_to_mds() {
        let _e1 = ScopedEnv::remove("GOOGLE_APPLICATION_CREDENTIALS");
        let _e2 = ScopedEnv::remove("HOME"); // For posix
        let _e3 = ScopedEnv::remove("APPDATA"); // For windows

        let mds = AccessTokenCredentialBuilder::default().build().unwrap();
        let fmt = format!("{mds:?}");
        assert!(fmt.contains("MDSCredentials"));
    }

    #[tokio::test]
    #[serial]
    async fn create_access_token_credentials_errors_if_adc_env_is_not_a_file() {
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", "file-does-not-exist.json");
        let err = AccessTokenCredentialBuilder::default().build().unwrap_err();
        assert!(err.is_loading(), "{err:?}");
        let msg = err.to_string();
        assert!(msg.contains("file-does-not-exist.json"));
        assert!(msg.contains("GOOGLE_APPLICATION_CREDENTIALS"));
    }

    #[tokio::test]
    #[serial]
    async fn create_access_token_credentials_malformed_adc_is_error() {
        for contents in ["{}", r#"{"type": 42}"#] {
            let path = write_cred_json(contents);
            let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

            let err = AccessTokenCredentialBuilder::default().build().unwrap_err();
            assert!(err.is_parsing(), "{err:?}");
            assert!(err.to_string().contains("`type` field"), "{err}");
        }
    }

    #[tokio::test]
    #[serial]
    async fn create_access_token_credentials_adc_unimplemented_credential_type() {
        let contents = r#"{
            "type": "some_unknown_credential_type"
        }"#;

        let path = write_cred_json(contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let err = AccessTokenCredentialBuilder::default().build().unwrap_err();
        assert!(err.is_unknown_type(), "{err:?}");
        assert!(
            err.to_string().contains("some_unknown_credential_type"),
            "{err}"
        );
    }

    #[tokio::test]
    #[serial]
    async fn create_access_token_credentials_adc_user_credentials() {
        let contents = r#"{
            "client_id": "test-client-id",
            "client_secret": "test-client-secret",
            "refresh_token": "test-refresh-token",
            "type": "authorized_user"
        }"#;

        let path = write_cred_json(contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let uc = AccessTokenCredentialBuilder::default().build().unwrap();
        let fmt = format!("{uc:?}");
        assert!(fmt.contains("UserCredentials"));
    }

    #[tokio::test]
    #[serial]
    async fn create_access_token_credentials_json_user_credentials() {
        let contents = r#"{
            "client_id": "test-client-id",
            "client_secret": "test-client-secret",
            "refresh_token": "test-refresh-token",
            "type": "authorized_user"
        }"#;

        let quota_project = "test-quota-project";

        let path = write_cred_json(contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let uc = AccessTokenCredentialBuilder::default()
            .with_quota_project_id(quota_project)
            .build()
            .unwrap();

        let fmt = format!("{uc:?}");
        assert!(fmt.contains("UserCredentials"));
        assert!(fmt.contains(quota_project));
    }

    #[tokio::test]
    #[serial]
    async fn create_access_token_credentials_adc_impersonated_service_account() {
        let contents = json!({
            "type": "impersonated_service_account",
            "service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/test-principal:generateAccessToken",
            "source_credentials": {
                "type": "authorized_user",
                "client_id": "test-client-id",
                "client_secret": "test-client-secret",
                "refresh_token": "test-refresh-token"
            }
        }).to_string();

        let path = write_cred_json(&contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let ic = AccessTokenCredentialBuilder::default().build().unwrap();
        let fmt = format!("{ic:?}");
        assert!(fmt.contains("ImpersonatedServiceAccount"));
    }

    #[tokio::test]
    #[serial]
    async fn create_access_token_credentials_json_impersonated_service_account() {
        let contents = json!({
            "type": "impersonated_service_account",
            "service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/test-principal:generateAccessToken",
            "source_credentials": {
                "type": "authorized_user",
                "client_id": "test-client-id",
                "client_secret": "test-client-secret",
                "refresh_token": "test-refresh-token"
            }
        });

        let quota_project = "test-quota-project";

        let path = write_cred_json(&contents.to_string());
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let ic = AccessTokenCredentialBuilder::default()
            .with_quota_project_id(quota_project)
            .build()
            .unwrap();

        let fmt = format!("{ic:?}");
        assert!(fmt.contains("ImpersonatedServiceAccount"));
        assert!(fmt.contains(quota_project));
    }

    #[tokio::test]
    #[serial]
    async fn create_access_token_credentials_adc_service_account_credentials() {
        let contents = r#"{
            "type": "service_account",
            "project_id": "test-project-id",
            "private_key_id": "test-private-key-id",
            "private_key": "-----BEGIN PRIVATE KEY-----\nBLAHBLAHBLAH\n-----END PRIVATE KEY-----\n",
            "client_email": "test-client-email",
            "universe_domain": "test-universe-domain"
        }"#;

        let path = write_cred_json(contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let sac = AccessTokenCredentialBuilder::default().build().unwrap();
        let fmt = format!("{sac:?}");
        assert!(fmt.contains("ServiceAccountCredentials"));
    }

    #[tokio::test]
    #[serial]
    async fn create_access_token_credentials_json_service_account_credentials() {
        let contents = r#"{
            "type": "service_account",
            "project_id": "test-project-id",
            "private_key_id": "test-private-key-id",
            "private_key": "-----BEGIN PRIVATE KEY-----\nBLAHBLAHBLAH\n-----END PRIVATE KEY-----\n",
            "client_email": "test-client-email",
            "universe_domain": "test-universe-domain"
        }"#;

        let quota_project = "test-quota-project";

        let path = write_cred_json(contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let sac = AccessTokenCredentialBuilder::default()
            .with_quota_project_id(quota_project)
            .build()
            .unwrap();
        let fmt = format!("{sac:?}");
        assert!(fmt.contains("ServiceAccountCredentials"));
        assert!(fmt.contains(quota_project));
    }

    #[tokio::test]
    #[parallel]
    async fn create_api_key_credentials_success() {
        let creds = ApiKeyCredentialsBuilder::new("test-api-key").build();
        let fmt = format!("{creds:?}");
        assert!(fmt.contains("ApiKeyCredentials"), "{fmt:?}");
        assert!(!fmt.contains("test-api-key"), "{fmt:?}");
    }

    #[tokio::test]
    #[serial]
    async fn create_external_account_access_token() -> TestResult {
        let source_token_response_body = json!({
            "access_token":"an_example_token",
        })
        .to_string();

        let token_response_body = json!({
            "access_token":"an_exchanged_token",
            "issued_token_type":"urn:ietf:params:oauth:token-type:access_token",
            "token_type":"Bearer",
            "expires_in":3600,
            "scope":"https://www.googleapis.com/auth/cloud-platform"
        })
        .to_string();

        let server = Server::run();
        server.expect(
            Expectation::matching(all_of![
                request::method_path("GET", "/source_token"),
                request::headers(contains(("metadata", "True",))),
            ])
            .respond_with(status_code(200).body(source_token_response_body)),
        );

        server.expect(
            Expectation::matching(all_of![
                request::method_path("POST", "/token"),
                request::body(url_decoded(contains(("subject_token", "an_example_token")))),
                request::body(url_decoded(contains((
                    "subject_token_type",
                    "urn:ietf:params:oauth:token-type:jwt"
                )))),
                request::body(url_decoded(contains(("audience", "some-audience")))),
                request::headers(contains((
                    "content-type",
                    "application/x-www-form-urlencoded"
                ))),
            ])
            .respond_with(status_code(200).body(token_response_body)),
        );

        let contents = json!({
          "type": "external_account",
          "audience": "some-audience",
          "subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
          "token_url": server.url("/token").to_string(),
          "credential_source": {
            "url": server.url("/source_token").to_string(),
            "headers": {
              "Metadata": "True"
            },
            "format": {
              "type": "json",
              "subject_token_field_name": "access_token"
            }
          }
        })
        .to_string();

        let path = write_cred_json(&contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let creds = AccessTokenCredentialBuilder::default().build().unwrap();

        // Use the debug output to verify the right kind of credentials are created.
        let fmt = format!("{creds:?}");
        assert!(fmt.contains("ExternalAccountCredentials"));

        let cached_headers = creds.headers(Extensions::new()).await?;
        match cached_headers {
            CacheableResource::New { data, .. } => {
                let token = data
                    .get(AUTHORIZATION)
                    .and_then(|token_value| token_value.to_str().ok())
                    .map(|s| s.to_string())
                    .unwrap();
                assert!(token.contains("Bearer an_exchanged_token"));
            }
            CacheableResource::NotModified => {
                unreachable!("Expecting a header to be present");
            }
        };

        Ok(())
    }

    #[tokio::test]
    #[serial]
    async fn create_external_account_access_token_fail() -> TestResult {
        let source_token_response_body = json!({
            "error":"invalid_token",
        })
        .to_string();

        let server = Server::run();
        server.expect(
            Expectation::matching(all_of![
                request::method_path("GET", "/source_token"),
                request::headers(contains(("metadata", "True",))),
            ])
            .respond_with(status_code(400).body(source_token_response_body)),
        );

        let contents = json!({
          "type": "external_account",
          "audience": "some-audience",
          "subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
          "token_url": server.url("/token").to_string(),
          "credential_source": {
            "url": server.url("/source_token").to_string(),
            "headers": {
              "Metadata": "True"
            },
            "format": {
              "type": "json",
              "subject_token_field_name": "access_token"
            }
          }
        })
        .to_string();

        let path = write_cred_json(&contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let creds = AccessTokenCredentialBuilder::default().build().unwrap();

        let error = creds.headers(Extensions::new()).await.unwrap_err();
        let original_error = find_source_error::<CredentialsError>(&error)
            .expect("source should be a CredentialsError");
        assert!(!original_error.is_transient());

        Ok(())
    }

    mockall::mock! {
        #[derive(Debug)]
        Credentials {}

        impl CredentialsProvider for Credentials {
            async fn headers(&self, extensions: Extensions) -> std::result::Result<CacheableResource<HeaderMap>, CredentialsError>;
            async fn universe_domain(&self) -> Option<String>;
        }

        impl AccessTokenCredentialsProvider for Credentials {
            async fn access_token(&self) -> std::result::Result<AccessToken, CredentialsError>;
        }
    }

    #[tokio::test]
    #[parallel]
    async fn mocking_with_default_values() -> Result<()> {
        let mut mock = MockCredentials::new();
        mock.expect_headers().return_once(|_extensions| {
            Ok(CacheableResource::New {
                entity_tag: EntityTag::default(),
                data: HeaderMap::new(),
            })
        });
        mock.expect_universe_domain().return_once(|| None);

        let creds = Credentials::from(mock);
        let cached_headers = creds.headers(Extensions::new()).await?;
        match cached_headers {
            CacheableResource::New { entity_tag, data } => {
                assert_eq!(entity_tag, EntityTag::default());
                assert!(data.is_empty(), "{data:?}");
            }
            CacheableResource::NotModified => {
                unreachable!("Expecting a header to be present");
            }
        };
        assert_eq!(creds.universe_domain().await, None);

        Ok(())
    }

    #[tokio::test]
    #[parallel]
    async fn mocking_access_token_credentials() -> Result<()> {
        let mut mock = MockCredentials::new();
        mock.expect_access_token().return_once(|| {
            Ok(AccessToken {
                token: "test-token".to_string(),
            })
        });

        let creds = AccessTokenCredentials::from(mock);
        let access_token = creds.access_token().await?;
        assert_eq!(access_token.token, "test-token");

        Ok(())
    }

    #[tokio::test]
    #[parallel]
    async fn mocking_with_custom_header() -> Result<()> {
        let mut mock = MockCredentials::new();
        let headers = HeaderMap::from_iter([(
            HeaderName::from_static("test-header"),
            HeaderValue::from_static("test-value"),
        )]);
        let cached_headers = CacheableResource::New {
            entity_tag: EntityTag::new(),
            data: headers,
        };
        let cached_headers_clone = cached_headers.clone();
        mock.expect_headers()
            .return_once(|_extensions| Ok(cached_headers));
        mock.expect_universe_domain().return_once(|| None);

        let creds = Credentials::from(mock);
        assert_eq!(
            creds.headers(Extensions::new()).await?,
            cached_headers_clone
        );
        assert_eq!(creds.universe_domain().await, None);

        Ok(())
    }

    #[tokio::test]
    #[parallel]
    async fn mocking_with_custom_universe_domain() -> Result<()> {
        let mut mock = MockCredentials::new();

        let universe_domain = "test-universe-domain";
        let universe_domain_clone = universe_domain.to_string();
        mock.expect_headers().return_once(|_extensions| {
            Ok(CacheableResource::New {
                entity_tag: EntityTag::default(),
                data: HeaderMap::new(),
            })
        });
        mock.expect_universe_domain()
            .return_once(|| Some(universe_domain_clone));

        let creds = Credentials::from(mock);
        match creds.headers(Extensions::new()).await? {
            CacheableResource::New { entity_tag, data } => {
                assert_eq!(entity_tag, EntityTag::default());
                assert!(data.is_empty(), "{data:?}");
            }
            CacheableResource::NotModified => {
                unreachable!("Expecting a header to be present");
            }
        };
        assert_eq!(creds.universe_domain().await.unwrap(), universe_domain);

        Ok(())
    }

    #[tokio::test]
    #[parallel]
    async fn get_mds_credentials_from_builder() -> Result<()> {
        let test_quota_project = "test-quota-project";
        let mdcs = MdsBuilder::default()
            .with_quota_project_id(test_quota_project)
            .build()?;
        let fmt = format!("{mdcs:?}");
        assert!(fmt.contains("MDSCredentials"));
        assert!(fmt.contains(test_quota_project));
        Ok(())
    }

    #[tokio::test]
    #[parallel]
    async fn get_service_account_credentials_from_builder() -> Result<()> {
        let test_quota_project = "test-quota-project";
        let service_account_info_json = json!({
            "client_email": "test-client-email",
            "private_key_id": "test-private-key-id",
            "private_key": "",
            "project_id": "test-project-id",
            "universe_domain": "test-universe-domain",
        });
        let service_account = ServiceAccountBuilder::new(service_account_info_json)
            .with_quota_project_id(test_quota_project)
            .build()?;
        let fmt = format!("{service_account:?}");
        assert!(fmt.contains("ServiceAccountCredentials"));
        assert!(fmt.contains(test_quota_project));
        Ok(())
    }

    #[tokio::test]
    #[parallel]
    async fn get_mds_access_token_credentials_from_builder() -> Result<()> {
        let server = Server::run();
        let response = json!({
            "access_token": "test-access-token",
            "expires_in": 3600,
            "token_type": "Bearer"
        });
        server.expect(
            Expectation::matching(request::path(
                "/computeMetadata/v1/instance/service-accounts/default/token",
            ))
            .respond_with(json_encoded(response)),
        );
        server.expect(
            Expectation::matching(request::path(
                "/computeMetadata/v1/instance/service-accounts/default/email",
            ))
            .times(0..)
            .respond_with(status_code(200).body("some-client-email")),
        ); // The access boundary background task might query the MDS for its email.
        server.expect(
            Expectation::matching(request::path(
                "/computeMetadata/v1/universe/universe-domain",
            ))
            .times(0..)
            .respond_with(status_code(404)),
        ); // The access boundary background task might query the MDS for its universe domain.

        let test_quota_project = "test-quota-project";
        let mdcs = MdsBuilder::default()
            .with_endpoint(server.url("").to_string().trim_end_matches("/"))
            .with_quota_project_id(test_quota_project)
            .build_access_token_credentials()?;

        let fmt = format!("{mdcs:?}");
        assert!(fmt.contains("MDSCredentials"));
        assert!(fmt.contains(test_quota_project));

        let access_token = mdcs.access_token().await?;
        assert_eq!(access_token.token, "test-access-token");
        Ok(())
    }

    #[tokio::test]
    #[parallel]
    async fn get_service_account_access_token_credentials_from_builder() -> Result<()> {
        let test_quota_project = "test-quota-project";
        let service_account_info_json = json!({
            "client_email": "test-client-email",
            "private_key_id": "test-private-key-id",
            "private_key": "-----BEGIN PRIVATE KEY-----\nBLAHBLAHBLAH\n-----END PRIVATE KEY-----\n",
            "project_id": "test-project-id",
            "universe_domain": "test-universe-domain",
        });
        let service_account = ServiceAccountBuilder::new(service_account_info_json)
            .with_quota_project_id(test_quota_project)
            .build_access_token_credentials()?;
        let fmt = format!("{service_account:?}");
        assert!(fmt.contains("ServiceAccountCredentials"));
        assert!(fmt.contains(test_quota_project));
        Ok(())
    }

    #[tokio::test]
    #[parallel]
    async fn get_user_account_access_token_credentials_from_builder() -> Result<()> {
        let test_quota_project = "test-quota-project";
        let authorized_user = serde_json::json!({
            "client_id": "test-client-id",
            "client_secret": "test-client-secret",
            "refresh_token": "test-refresh-token",
            "type": "authorized_user",
        });
        let user_account = UserAccountCredentialBuilder::new(authorized_user)
            .with_quota_project_id(test_quota_project)
            .build_access_token_credentials()?;
        let fmt = format!("{user_account:?}");
        assert!(fmt.contains("UserCredentials"), "{fmt:?}");
        assert!(fmt.contains(test_quota_project));
        Ok(())
    }

    #[tokio::test]
    #[parallel]
    async fn get_impersonated_access_token_credentials_from_builder() -> Result<()> {
        let contents = json!({
            "type": "impersonated_service_account",
            "service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/test-principal:generateAccessToken",
            "source_credentials": {
                "type": "authorized_user",
                "client_id": "test-client-id",
                "client_secret": "test-client-secret",
                "refresh_token": "test-refresh-token"
            }
        });

        let quota_project = "test-quota-project";

        let ic = ImpersonatedBuilder::new(contents)
            .with_quota_project_id(quota_project)
            .build_access_token_credentials()
            .unwrap();

        let fmt = format!("{ic:?}");
        assert!(fmt.contains("ImpersonatedServiceAccount"));
        assert!(fmt.contains(quota_project));
        Ok(())
    }

    #[tokio::test]
    #[parallel]
    async fn get_user_account_credentials_from_builder() -> Result<()> {
        let test_quota_project = "test-quota-project";
        let authorized_user = serde_json::json!({
            "client_id": "test-client-id",
            "client_secret": "test-client-secret",
            "refresh_token": "test-refresh-token",
            "type": "authorized_user",
        });
        let user_account = UserAccountCredentialBuilder::new(authorized_user)
            .with_quota_project_id(test_quota_project)
            .build()?;
        let fmt = format!("{user_account:?}");
        assert!(fmt.contains("UserCredentials"), "{fmt:?}");
        assert!(fmt.contains(test_quota_project));
        Ok(())
    }

    #[derive(Debug)]
    struct TestProviderError;

    impl fmt::Display for TestProviderError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "TestProviderError")
        }
    }

    impl Error for TestProviderError {}

    impl SubjectTokenProviderError for TestProviderError {
        fn is_transient(&self) -> bool {
            false
        }
    }

    #[derive(Debug)]
    struct TestSubjectTokenProvider;

    impl SubjectTokenProvider for TestSubjectTokenProvider {
        type Error = TestProviderError;

        async fn subject_token(&self) -> std::result::Result<SubjectToken, Self::Error> {
            Ok(SubjectTokenBuilder::new("test-subject-token".to_string()).build())
        }
    }

    #[test_case(Some(vec!["scope1".to_string(), "scope2".to_string()]), vec!["scope1", "scope2"]; "with custom scopes")]
    #[test_case(None, vec!["https://www.googleapis.com/auth/cloud-platform"]; "with default scopes")]
    #[tokio::test]
    async fn create_programmatic_external_account_access_token(
        scopes: Option<Vec<String>>,
        expected_scopes: Vec<&str>,
    ) -> TestResult {
        let token_response_body = json!({
            "access_token":"an_exchanged_token",
            "issued_token_type":"urn:ietf:params:oauth:token-type:access_token",
            "token_type":"Bearer",
            "expires_in":3600,
            "scope": expected_scopes.join(" "),
        })
        .to_string();

        let server = Server::run();
        server.expect(
            Expectation::matching(all_of![
                request::method_path("POST", "/token"),
                request::body(url_decoded(all_of![
                    contains(("subject_token", "test-subject-token")),
                    contains(("scope", expected_scopes.join(" "))),
                ]))
            ])
            .respond_with(status_code(200).body(token_response_body)),
        );

        let provider = TestSubjectTokenProvider;
        let mut builder = ProgrammaticBuilder::new(std::sync::Arc::new(provider))
            .with_audience("some-audience")
            .with_subject_token_type("urn:ietf:params:oauth:token-type:jwt")
            .with_token_url(server.url("/token").to_string());

        if let Some(scopes) = scopes {
            builder = builder.with_scopes(scopes);
        }

        let creds = builder.build().unwrap();

        let cached_headers = creds.headers(Extensions::new()).await?;
        match cached_headers {
            CacheableResource::New { data, .. } => {
                let token = data
                    .get(AUTHORIZATION)
                    .and_then(|token_value| token_value.to_str().ok())
                    .map(|s| s.to_string())
                    .unwrap();
                assert_eq!(token, "Bearer an_exchanged_token");
            }
            CacheableResource::NotModified => {
                unreachable!("Expecting a header to be present");
            }
        };

        Ok(())
    }

    #[derive(Debug)]
    struct FailingSubjectTokenProvider;

    impl SubjectTokenProvider for FailingSubjectTokenProvider {
        type Error = TestProviderError;

        async fn subject_token(&self) -> std::result::Result<SubjectToken, Self::Error> {
            Err(TestProviderError)
        }
    }

    #[tokio::test]
    #[parallel]
    async fn create_programmatic_token_provider_user_error() -> TestResult {
        let provider = FailingSubjectTokenProvider;
        let creds = ProgrammaticBuilder::new(std::sync::Arc::new(provider))
            .with_audience("some-audience")
            .with_subject_token_type("urn:ietf:params:oauth:token-type:jwt")
            .with_token_url("http://dummy.com/token")
            .build()
            .unwrap();

        let error = creds.headers(Extensions::new()).await.unwrap_err();

        assert!(!error.is_transient(), "Error should not be transient");
        let original_error = find_source_error::<TestProviderError>(&error)
            .expect("source should be a TestProviderError");
        assert!(original_error.to_string().contains("TestProviderError"));
        Ok(())
    }

    #[cfg(all(test, feature = "idtoken"))]
    #[tokio::test]
    #[serial]
    async fn create_id_token_credentials_fallback_to_mds() -> TestResult {
        let _e1 = ScopedEnv::remove("GOOGLE_APPLICATION_CREDENTIALS");
        let _e2 = ScopedEnv::remove("HOME"); // For posix
        let _e3 = ScopedEnv::remove("APPDATA"); // For windows

        let id_token_creds =
            google_cloud_auth::credentials::idtoken::Builder::new("test-audience").build()?;
        let fmt = format!("{id_token_creds:?}");
        assert!(fmt.contains("MDSCredentials"));

        Ok(())
    }

    #[cfg(all(test, feature = "idtoken"))]
    #[tokio::test]
    #[serial]
    async fn create_id_token_credentials_adc_service_account() -> TestResult {
        let contents = r#"{
            "type": "service_account",
            "project_id": "test-project-id",
            "private_key_id": "test-private-key-id",
            "private_key": "-----BEGIN PRIVATE KEY-----\nBLAHBLAHBLAH\n-----END PRIVATE KEY-----\n",
            "client_email": "test-client-email",
            "universe_domain": "test-universe-domain"
        }"#;
        let path = write_cred_json(contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let id_token_creds =
            google_cloud_auth::credentials::idtoken::Builder::new("test-audience").build()?;
        let fmt = format!("{id_token_creds:?}");
        assert!(fmt.contains("ServiceAccountCredentials"));

        Ok(())
    }

    #[cfg(all(test, feature = "idtoken"))]
    #[tokio::test]
    #[serial]
    async fn create_id_token_credentials_adc_impersonated_service_account() -> TestResult {
        let contents = json!({
            "type": "impersonated_service_account",
            "service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/test-principal:generateAccessToken",
            "source_credentials": {
                "type": "authorized_user",
                "client_id": "test-client-id",
                "client_secret": "test-client-secret",
                "refresh_token": "test-refresh-token"
            }
        })
        .to_string();

        let path = write_cred_json(&contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let id_token_creds =
            google_cloud_auth::credentials::idtoken::Builder::new("test-audience").build()?;
        let fmt = format!("{id_token_creds:?}");
        assert!(fmt.contains("ImpersonatedServiceAccount"));

        Ok(())
    }

    #[cfg(all(test, feature = "idtoken"))]
    #[tokio::test]
    #[serial]
    async fn create_id_token_credentials_adc_user_account_not_supported() -> TestResult {
        let contents = r#"{
                "client_id": "test-client-id",
                "client_secret": "test-client-secret",
                "refresh_token": "test-refresh-token",
                "type": "authorized_user"
            }"#;

        let path = write_cred_json(contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let err = google_cloud_auth::credentials::idtoken::Builder::new("test-audience")
            .build()
            .unwrap_err();

        assert!(err.is_not_supported());
        assert!(
            err.to_string()
                .contains("authorized_user, use idtoken::user_account::Builder directly.")
        );

        Ok(())
    }

    #[tokio::test]
    #[serial]
    async fn create_signer_credentials_fallback_to_mds() -> TestResult {
        let _e1 = ScopedEnv::remove("GOOGLE_APPLICATION_CREDENTIALS");
        let _e2 = ScopedEnv::remove("HOME"); // For posix
        let _e3 = ScopedEnv::remove("APPDATA"); // For windows

        let signer = google_cloud_auth::credentials::Builder::default().build_signer()?;
        let fmt = format!("{signer:?}");
        assert!(fmt.contains("MDSSigner"));

        Ok(())
    }

    #[tokio::test]
    #[serial]
    async fn create_signer_credentials_adc_service_account() -> TestResult {
        let contents = r#"{
            "type": "service_account",
            "project_id": "test-project-id",
            "private_key_id": "test-private-key-id",
            "private_key": "-----BEGIN PRIVATE KEY-----\nBLAHBLAHBLAH\n-----END PRIVATE KEY-----\n",
            "client_email": "test-client-email",
            "universe_domain": "test-universe-domain"
        }"#;
        let path = write_cred_json(contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let signer = google_cloud_auth::credentials::Builder::default().build_signer()?;
        let fmt = format!("{signer:?}");
        assert!(fmt.contains("ServiceAccountSigner"));

        Ok(())
    }

    #[tokio::test]
    #[serial]
    async fn create_signer_credentials_adc_impersonated_service_account() -> TestResult {
        let contents = json!({
            "type": "impersonated_service_account",
            "service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/test-principal:generateAccessToken",
            "source_credentials": {
                "type": "authorized_user",
                "client_id": "test-client-id",
                "client_secret": "test-client-secret",
                "refresh_token": "test-refresh-token"
            }
        })
        .to_string();

        let path = write_cred_json(&contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let signer = google_cloud_auth::credentials::Builder::default().build_signer()?;
        let fmt = format!("{signer:?}");
        assert!(fmt.contains("IamSigner"));

        Ok(())
    }

    #[tokio::test]
    #[serial]
    async fn create_signer_credentials_adc_user_account_not_supported() -> TestResult {
        let contents = r#"{
                "client_id": "test-client-id",
                "client_secret": "test-client-secret",
                "refresh_token": "test-refresh-token",
                "type": "authorized_user"
            }"#;

        let path = write_cred_json(contents);
        let _e = ScopedEnv::set("GOOGLE_APPLICATION_CREDENTIALS", path.to_str().unwrap());

        let err = google_cloud_auth::credentials::Builder::default()
            .build_signer()
            .unwrap_err();

        assert!(err.is_not_supported());
        assert!(
            err.to_string()
                .contains("authorized_user signer is not supported"),
            "{}",
            err
        );

        Ok(())
    }
}