olai-http 0.0.9

Cloud provider credential abstraction for AWS, Azure, and GCP
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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
//! Bearer-token authentication for arbitrary HTTP APIs.
//!
//! The per-cloud and Databricks modules sign requests for one specific
//! service. This module builds bearer tokens for *any* audience: a provider is
//! configured with the token endpoint, scope, or audience of the API it is
//! for, so the same flows work against an API fronted by Entra ID, Okta,
//! Keycloak, Cognito, Google, or a self-issued token service.
//!
//! [`BearerAuth`] holds the HTTP settings shared by every token request and
//! creates [`BearerCredentialProvider`]s:
//!
//! | Flow | Constructor |
//! |------|-------------|
//! | Static token | [`BearerAuth::static_token`] |
//! | OAuth 2.0 client credentials against any token endpoint | [`BearerAuth::client_credentials`] |
//! | Token read from an environment variable / a file (e.g. a projected Kubernetes token) | [`BearerAuth::token_env`], [`BearerAuth::token_file`] |
//! | Azure service principal, managed identity, workload identity, Azure CLI | [`BearerAuth::azure_client_secret`], [`BearerAuth::azure_managed_identity`], [`BearerAuth::azure_workload_identity`], [`BearerAuth::azure_cli`] |
//! | GCP ID token from the metadata server or a service-account key | [`BearerAuth::gcp_id_token`], [`BearerAuth::gcp_service_account_id_token`] |
//! | RFC 8693 token exchange of any of the above | [`BearerAuth::token_exchange`] |
//!
//! Hand a provider to [`CloudClient::new_with_bearer`] to sign requests with it.
//!
//! [`CloudClient::new_with_bearer`]: crate::CloudClient::new_with_bearer

use std::fmt;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use base64::Engine;
use reqwest::header::{ACCEPT, HeaderValue};
use reqwest::{Client, Method};
use serde::Deserialize;
use tokio::runtime::Handle;

use crate::azure::credential::{
    AzureCliCredential, AzureCredential, ClientSecretOAuthProvider, ImdsManagedIdentityProvider,
    WorkloadIdentityOAuthProvider,
};
use crate::gcp::credential::{ServiceAccountCredentials, ServiceAccountKey};
use crate::retry::RetryExt;
use crate::service::{HttpService, make_service};
use crate::{
    ClientOptions, CredentialProvider, Error, Result, RetryConfig, StaticCredentialProvider,
    TemporaryToken, TokenCredentialProvider, TokenProvider,
};

/// How long a token without a known expiry is used before it is fetched again.
const UNKNOWN_EXPIRY: Duration = Duration::from_secs(10 * 60);

/// RFC 8693 token-exchange grant type.
pub(crate) const TOKEN_EXCHANGE_GRANT: &str = "urn:ietf:params:oauth:grant-type:token-exchange";

/// RFC 8693 token type of an OpenID Connect ID token.
pub const ID_TOKEN_TYPE: &str = "urn:ietf:params:oauth:token-type:id_token";

/// RFC 8693 token type of an OAuth 2.0 access token.
pub const ACCESS_TOKEN_TYPE: &str = "urn:ietf:params:oauth:token-type:access_token";

/// RFC 8693 token type of a JWT.
pub const JWT_TOKEN_TYPE: &str = "urn:ietf:params:oauth:token-type:jwt";

/// RFC 7523 JWT-bearer grant type.
pub(crate) const JWT_BEARER_GRANT: &str = "urn:ietf:params:oauth:grant-type:jwt-bearer";

/// The token endpoint of Google service accounts that do not name one.
const GCP_DEFAULT_TOKEN_URI: &str = "https://oauth2.googleapis.com/token";

const GCP_METADATA_IDENTITY_URL: &str =
    "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity";

/// A bearer token, sent as `Authorization: Bearer <token>`.
#[derive(Clone, PartialEq, Eq)]
pub struct BearerCredential {
    /// The token. This is secret material.
    pub token: String,
}

impl fmt::Debug for BearerCredential {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BearerCredential")
            .field("token", &"***")
            .finish()
    }
}

/// A shared provider of [`BearerCredential`]s.
pub type BearerCredentialProvider = Arc<dyn CredentialProvider<Credential = BearerCredential>>;

/// OAuth 2.0 client-credentials grant configuration.
#[derive(Clone, Default)]
pub struct ClientCredentials {
    /// The token endpoint, e.g. `https://login.example.com/oauth2/token`.
    pub token_url: String,
    /// The client id.
    pub client_id: String,
    /// The client secret.
    pub client_secret: String,
    /// Space-separated scopes to request, if the issuer needs them.
    pub scope: Option<String>,
    /// The audience (resource) to request, if the issuer needs it (e.g. Auth0).
    pub audience: Option<String>,
}

impl fmt::Debug for ClientCredentials {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ClientCredentials")
            .field("token_url", &self.token_url)
            .field("client_id", &self.client_id)
            .field("client_secret", &"***")
            .field("scope", &self.scope)
            .field("audience", &self.audience)
            .finish()
    }
}

/// RFC 8693 token-exchange configuration.
#[derive(Clone)]
pub struct TokenExchange {
    /// The token-exchange endpoint.
    pub endpoint: String,
    /// The provider of the token to exchange.
    pub subject: BearerCredentialProvider,
    /// The type of the subject token; defaults to [`ID_TOKEN_TYPE`].
    pub subject_token_type: Option<String>,
    /// The type of token to request; defaults to [`ACCESS_TOKEN_TYPE`].
    pub requested_token_type: Option<String>,
    /// The audience to request, if the exchange needs it.
    pub audience: Option<String>,
    /// Space-separated scopes to request, if the exchange needs them.
    pub scope: Option<String>,
}

impl fmt::Debug for TokenExchange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TokenExchange")
            .field("endpoint", &self.endpoint)
            .field("subject", &self.subject)
            .field("subject_token_type", &self.subject_token_type)
            .field("requested_token_type", &self.requested_token_type)
            .field("audience", &self.audience)
            .field("scope", &self.scope)
            .finish()
    }
}

/// Azure managed-identity configuration.
#[derive(Debug, Clone, Default)]
pub struct AzureManagedIdentity {
    /// The resource to request a token for, e.g. `api://my-app`.
    pub resource: String,
    /// The client id of a user-assigned identity.
    pub client_id: Option<String>,
    /// The object id of a user-assigned identity.
    pub object_id: Option<String>,
    /// The Azure resource id of a user-assigned identity.
    pub msi_resource_id: Option<String>,
    /// Overrides the instance metadata token endpoint.
    pub endpoint: Option<String>,
}

/// A GCP service-account key.
#[derive(Clone)]
pub enum GcpServiceAccount {
    /// The path of a service-account JSON key file, e.g. the one named by
    /// `GOOGLE_APPLICATION_CREDENTIALS`.
    File(String),
    /// The contents of a service-account JSON key file. This is secret
    /// material.
    Key(String),
}

/// A [`GcpServiceAccount`] read and parsed.
pub(crate) struct LoadedServiceAccount {
    pub(crate) credentials: ServiceAccountCredentials,
    pub(crate) key: ServiceAccountKey,
    /// The key's token endpoint, or Google's.
    pub(crate) token_uri: String,
}

impl GcpServiceAccount {
    /// Reads and parses the key. Fails unless it is a `service_account` credential;
    /// `purpose` names what needs one, for the error.
    pub(crate) fn load(&self, purpose: &str) -> Result<LoadedServiceAccount> {
        let json = match self {
            Self::File(path) => std::fs::read_to_string(path).map_err(|e| {
                generic(format!("failed to read service-account key `{path}`: {e}"))
            })?,
            Self::Key(key) => key.clone(),
        };
        let value: serde_json::Value = serde_json::from_str(&json)
            .map_err(|e| generic(format!("invalid service-account key {self:?}: {e}")))?;
        if let Some(kind) = value.get("type").and_then(|kind| kind.as_str())
            && kind != "service_account"
        {
            return Err(generic(format!(
                "{self:?} holds `{kind}` credentials; {purpose} need a service-account key"
            )));
        }
        let credentials: ServiceAccountCredentials = serde_json::from_value(value)
            .map_err(|e| generic(format!("invalid service-account key {self:?}: {e}")))?;
        let key = ServiceAccountKey::from_pem(credentials.private_key.as_bytes())?;
        let token_uri = credentials
            .token_uri
            .clone()
            .unwrap_or_else(|| GCP_DEFAULT_TOKEN_URI.to_string());
        Ok(LoadedServiceAccount {
            credentials,
            key,
            token_uri,
        })
    }
}

impl fmt::Debug for GcpServiceAccount {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::File(path) => f.debug_tuple("File").field(path).finish(),
            Self::Key(_) => f.debug_tuple("Key").field(&"***").finish(),
        }
    }
}

/// Builds [`BearerCredentialProvider`]s that share HTTP settings.
///
/// Providers that fetch tokens over HTTP use the configured
/// [`ClientOptions`] and [`RetryConfig`], and spawn their requests on the
/// runtime set with [`with_runtime`](Self::with_runtime). All of them cache
/// tokens and refresh them before they expire.
#[derive(Debug, Clone, Default)]
pub struct BearerAuth {
    client_options: ClientOptions,
    retry: RetryConfig,
    runtime: Option<Handle>,
}

impl BearerAuth {
    /// Create a builder with default HTTP settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the HTTP client options used for token requests.
    pub fn with_client_options(mut self, options: ClientOptions) -> Self {
        self.client_options = options;
        self
    }

    /// Set the retry configuration used for token requests.
    pub fn with_retry(mut self, retry: RetryConfig) -> Self {
        self.retry = retry;
        self
    }

    /// Spawn token requests on `handle`.
    pub fn with_runtime(mut self, handle: Handle) -> Self {
        self.runtime = Some(handle);
        self
    }

    /// A provider that always returns `token`.
    pub fn static_token(token: impl Into<String>) -> BearerCredentialProvider {
        Arc::new(StaticCredentialProvider::new(BearerCredential {
            token: token.into(),
        }))
    }

    /// OAuth 2.0 client credentials against any token endpoint.
    pub fn client_credentials(
        &self,
        config: ClientCredentials,
    ) -> Result<BearerCredentialProvider> {
        self.fetching(ClientCredentialsProvider(config), false)
    }

    /// A token read from the environment variable `var` on every refresh.
    ///
    /// A JWT is reused until shortly before its `exp`; other tokens are re-read
    /// every few minutes.
    pub fn token_env(&self, var: impl Into<String>) -> BearerCredentialProvider {
        Arc::new(LocalTokenProvider::new(TokenLocation::Env(var.into())))
    }

    /// A token read from the file at `path` on every refresh, e.g. a projected
    /// Kubernetes service-account token that rotates in place.
    ///
    /// A JWT is reused until shortly before its `exp`; other tokens are re-read
    /// every few minutes.
    pub fn token_file(&self, path: impl Into<String>) -> BearerCredentialProvider {
        Arc::new(LocalTokenProvider::new(TokenLocation::File(path.into())))
    }

    /// An Azure AD token for `scope` (e.g. `api://my-app/.default`) from a
    /// service principal's client secret.
    pub fn azure_client_secret(
        &self,
        tenant_id: &str,
        client_id: impl Into<String>,
        client_secret: impl Into<String>,
        scope: &str,
        authority_host: Option<String>,
    ) -> Result<BearerCredentialProvider> {
        let provider = ClientSecretOAuthProvider::new_with_scope(
            client_id.into(),
            client_secret.into(),
            tenant_id,
            authority_host,
            scope,
        );
        Ok(azure_bearer(self.azure_fetching(provider, false)?))
    }

    /// An Azure AD token from the managed identity of the host, optionally a
    /// user-assigned one.
    pub fn azure_managed_identity(
        &self,
        identity: AzureManagedIdentity,
    ) -> Result<BearerCredentialProvider> {
        let provider = ImdsManagedIdentityProvider::new_with_resource(
            identity.client_id,
            identity.object_id,
            identity.msi_resource_id,
            identity.endpoint,
            &identity.resource,
        );
        Ok(azure_bearer(self.azure_fetching(provider, true)?))
    }

    /// An Azure AD token for `scope` from a federated token file, as used by
    /// AKS workload identity (`AZURE_FEDERATED_TOKEN_FILE`).
    pub fn azure_workload_identity(
        &self,
        tenant_id: &str,
        client_id: impl Into<String>,
        federated_token_file: impl Into<String>,
        scope: &str,
        authority_host: Option<String>,
    ) -> Result<BearerCredentialProvider> {
        let provider = WorkloadIdentityOAuthProvider::new(
            client_id,
            federated_token_file,
            tenant_id,
            authority_host,
        )
        .with_scope(scope);
        Ok(azure_bearer(self.azure_fetching(provider, false)?))
    }

    /// An Azure AD token for `scope` from the signed-in Azure CLI
    /// (`az account get-access-token`).
    pub fn azure_cli(&self, scope: impl Into<String>) -> BearerCredentialProvider {
        azure_bearer(Arc::new(AzureCliCredential::new_with_scope(scope)))
    }

    /// An ID token for `audience` from the GCP metadata server of the host's
    /// service account.
    ///
    /// `endpoint` overrides the metadata identity endpoint.
    pub fn gcp_id_token(
        &self,
        audience: impl Into<String>,
        endpoint: Option<String>,
    ) -> Result<BearerCredentialProvider> {
        let provider = GcpIdTokenProvider {
            audience: audience.into(),
            endpoint: endpoint.unwrap_or_else(|| GCP_METADATA_IDENTITY_URL.to_string()),
        };
        self.fetching(provider, true)
    }

    /// An ID token for `audience`, signed for by a service-account key.
    ///
    /// The key must be a `service_account` credential: user credentials from
    /// `gcloud auth application-default login` cannot mint ID tokens for an
    /// arbitrary audience.
    pub fn gcp_service_account_id_token(
        &self,
        account: GcpServiceAccount,
        audience: impl Into<String>,
    ) -> Result<BearerCredentialProvider> {
        let LoadedServiceAccount {
            credentials,
            key,
            token_uri,
        } = account.load("ID tokens")?;
        let provider = GcpServiceAccountIdTokenProvider {
            credentials,
            key,
            token_uri,
            audience: audience.into(),
        };
        self.fetching(provider, false)
    }

    /// Exchange the subject token for another token (RFC 8693), e.g. an
    /// identity-provider ID token for an API's own access token.
    pub fn token_exchange(&self, config: TokenExchange) -> Result<BearerCredentialProvider> {
        self.fetching(TokenExchangeProvider(config), false)
    }

    fn http(&self, metadata: bool) -> Result<(Client, Arc<dyn HttpService>)> {
        let client = if metadata {
            self.client_options.metadata_client()?
        } else {
            self.client_options.client()?
        };
        let service = make_service(client.clone(), self.runtime.as_ref());
        Ok((client, service))
    }

    fn fetching<T>(&self, provider: T, metadata: bool) -> Result<BearerCredentialProvider>
    where
        T: TokenProvider<Credential = BearerCredential> + 'static,
    {
        let (client, service) = self.http(metadata)?;
        Ok(Arc::new(TokenCredentialProvider::new(
            provider,
            client,
            service,
            self.retry.clone(),
        )))
    }

    fn azure_fetching<T>(
        &self,
        provider: T,
        metadata: bool,
    ) -> Result<Arc<dyn CredentialProvider<Credential = AzureCredential>>>
    where
        T: TokenProvider<Credential = AzureCredential> + 'static,
    {
        let (client, service) = self.http(metadata)?;
        Ok(Arc::new(TokenCredentialProvider::new(
            provider,
            client,
            service,
            self.retry.clone(),
        )))
    }
}

/// Signs requests with a bearer token from a [`BearerCredentialProvider`].
#[derive(Debug)]
pub(crate) struct BearerProviderSigner(pub(crate) BearerCredentialProvider);

impl crate::RequestSigner for BearerProviderSigner {
    fn sign<'a>(
        &'a self,
        req: reqwest::RequestBuilder,
    ) -> futures::future::BoxFuture<'a, Result<reqwest::RequestBuilder>> {
        Box::pin(async move {
            let credential = self.0.get_credential().await?;
            Ok(req.bearer_auth(&credential.token))
        })
    }
}

fn azure_bearer(
    inner: Arc<dyn CredentialProvider<Credential = AzureCredential>>,
) -> BearerCredentialProvider {
    Arc::new(AzureBearer(inner))
}

/// Serves an Azure AD bearer token as a [`BearerCredential`].
#[derive(Debug)]
struct AzureBearer(Arc<dyn CredentialProvider<Credential = AzureCredential>>);

#[async_trait]
impl CredentialProvider for AzureBearer {
    type Credential = BearerCredential;

    async fn get_credential(&self) -> Result<Arc<BearerCredential>> {
        let credential = self.0.get_credential().await?;
        let AzureCredential::BearerToken(token) = credential.as_ref();
        Ok(Arc::new(BearerCredential {
            token: token.clone(),
        }))
    }
}

#[derive(Debug, Deserialize)]
struct TokenResponse {
    access_token: String,
    #[serde(default)]
    expires_in: Option<u64>,
}

impl TokenResponse {
    fn into_token(self) -> TemporaryToken<Arc<BearerCredential>> {
        let expiry = match self.expires_in {
            Some(seconds) => Instant::now() + Duration::from_secs(seconds),
            None => {
                jwt_expiry(&self.access_token).unwrap_or_else(|| Instant::now() + UNKNOWN_EXPIRY)
            }
        };
        TemporaryToken {
            token: Arc::new(BearerCredential {
                token: self.access_token,
            }),
            expiry: Some(expiry),
        }
    }
}

#[derive(Debug)]
struct ClientCredentialsProvider(ClientCredentials);

#[async_trait]
impl TokenProvider for ClientCredentialsProvider {
    type Credential = BearerCredential;

    async fn fetch_token(
        &self,
        client: &Client,
        service: &Arc<dyn HttpService>,
        retry: &RetryConfig,
    ) -> Result<TemporaryToken<Arc<BearerCredential>>> {
        let config = &self.0;
        let mut form = vec![
            ("grant_type", "client_credentials"),
            ("client_id", config.client_id.as_str()),
            ("client_secret", config.client_secret.as_str()),
        ];
        if let Some(scope) = &config.scope {
            form.push(("scope", scope));
        }
        if let Some(audience) = &config.audience {
            form.push(("audience", audience));
        }
        let response: TokenResponse = client
            .request(Method::POST, &config.token_url)
            .header(ACCEPT, HeaderValue::from_static("application/json"))
            .form(&form)
            .retryable(retry, service.clone())
            .idempotent(true)
            .send()
            .await
            .map_err(|e| e.error())?
            .json()
            .await?;
        Ok(response.into_token())
    }
}

#[derive(Debug)]
struct TokenExchangeProvider(TokenExchange);

#[async_trait]
impl TokenProvider for TokenExchangeProvider {
    type Credential = BearerCredential;

    async fn fetch_token(
        &self,
        client: &Client,
        service: &Arc<dyn HttpService>,
        retry: &RetryConfig,
    ) -> Result<TemporaryToken<Arc<BearerCredential>>> {
        let config = &self.0;
        let subject = config.subject.get_credential().await?;
        let mut form = vec![
            ("grant_type", TOKEN_EXCHANGE_GRANT),
            ("subject_token", subject.token.as_str()),
            (
                "subject_token_type",
                config
                    .subject_token_type
                    .as_deref()
                    .unwrap_or(ID_TOKEN_TYPE),
            ),
            (
                "requested_token_type",
                config
                    .requested_token_type
                    .as_deref()
                    .unwrap_or(ACCESS_TOKEN_TYPE),
            ),
        ];
        if let Some(audience) = &config.audience {
            form.push(("audience", audience));
        }
        if let Some(scope) = &config.scope {
            form.push(("scope", scope));
        }
        let response: TokenResponse = client
            .request(Method::POST, &config.endpoint)
            .header(ACCEPT, HeaderValue::from_static("application/json"))
            .form(&form)
            .retryable(retry, service.clone())
            .idempotent(true)
            .send()
            .await
            .map_err(|e| e.error())?
            .json()
            .await?;
        Ok(response.into_token())
    }
}

#[derive(Debug)]
struct GcpIdTokenProvider {
    audience: String,
    endpoint: String,
}

#[async_trait]
impl TokenProvider for GcpIdTokenProvider {
    type Credential = BearerCredential;

    async fn fetch_token(
        &self,
        client: &Client,
        service: &Arc<dyn HttpService>,
        retry: &RetryConfig,
    ) -> Result<TemporaryToken<Arc<BearerCredential>>> {
        let token = client
            .request(Method::GET, &self.endpoint)
            .header("Metadata-Flavor", "Google")
            .query(&[("audience", self.audience.as_str()), ("format", "full")])
            .send_retry(retry, service.clone())
            .await
            .map_err(|e| e.error())?
            .text()
            .await?
            .trim()
            .to_string();
        let expiry = jwt_expiry(&token).unwrap_or_else(|| Instant::now() + UNKNOWN_EXPIRY);
        Ok(TemporaryToken {
            token: Arc::new(BearerCredential { token }),
            expiry: Some(expiry),
        })
    }
}

struct GcpServiceAccountIdTokenProvider {
    credentials: ServiceAccountCredentials,
    key: ServiceAccountKey,
    token_uri: String,
    audience: String,
}

impl fmt::Debug for GcpServiceAccountIdTokenProvider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("GcpServiceAccountIdTokenProvider")
            .field("client_email", &self.credentials.client_email)
            .field("token_uri", &self.token_uri)
            .field("audience", &self.audience)
            .finish_non_exhaustive()
    }
}

#[async_trait]
impl TokenProvider for GcpServiceAccountIdTokenProvider {
    type Credential = BearerCredential;

    async fn fetch_token(
        &self,
        client: &Client,
        service: &Arc<dyn HttpService>,
        retry: &RetryConfig,
    ) -> Result<TemporaryToken<Arc<BearerCredential>>> {
        #[derive(Deserialize)]
        struct IdTokenResponse {
            id_token: String,
        }

        let assertion =
            self.credentials
                .id_token_assertion(&self.key, &self.token_uri, &self.audience)?;
        let response: IdTokenResponse = client
            .request(Method::POST, &self.token_uri)
            .header(ACCEPT, HeaderValue::from_static("application/json"))
            .form(&[("grant_type", JWT_BEARER_GRANT), ("assertion", &assertion)])
            .retryable(retry, service.clone())
            .idempotent(true)
            .send()
            .await
            .map_err(|e| e.error())?
            .json()
            .await?;
        let token = response.id_token;
        let expiry = jwt_expiry(&token).unwrap_or_else(|| Instant::now() + UNKNOWN_EXPIRY);
        Ok(TemporaryToken {
            token: Arc::new(BearerCredential { token }),
            expiry: Some(expiry),
        })
    }
}

#[derive(Debug)]
enum TokenLocation {
    Env(String),
    File(String),
}

/// Reads a token from the environment or a file, re-reading it near expiry.
#[derive(Debug)]
struct LocalTokenProvider {
    location: TokenLocation,
    cache: crate::TokenCache<Arc<BearerCredential>>,
}

impl LocalTokenProvider {
    fn new(location: TokenLocation) -> Self {
        Self {
            location,
            cache: Default::default(),
        }
    }

    fn read(&self) -> Result<TemporaryToken<Arc<BearerCredential>>> {
        let token = match &self.location {
            TokenLocation::Env(var) => std::env::var(var)
                .map_err(|_| generic(format!("environment variable `{var}` is not set")))?,
            TokenLocation::File(path) => std::fs::read_to_string(path)
                .map_err(|e| generic(format!("failed to read token file `{path}`: {e}")))?,
        };
        let token = token.trim().to_string();
        if token.is_empty() {
            return Err(generic(format!(
                "the token in {:?} is empty",
                self.location
            )));
        }
        let expiry = jwt_expiry(&token).unwrap_or_else(|| Instant::now() + UNKNOWN_EXPIRY);
        Ok(TemporaryToken {
            token: Arc::new(BearerCredential { token }),
            expiry: Some(expiry),
        })
    }
}

#[async_trait]
impl CredentialProvider for LocalTokenProvider {
    type Credential = BearerCredential;

    async fn get_credential(&self) -> Result<Arc<BearerCredential>> {
        self.cache
            .get_or_insert_with(|| async { self.read() })
            .await
    }
}

/// The `exp` of a JWT as an [`Instant`], if `token` is a JWT with one.
///
/// The signature is not verified: the expiry only decides when to refresh.
fn jwt_expiry(token: &str) -> Option<Instant> {
    #[derive(Deserialize)]
    struct Claims {
        exp: u64,
    }
    let payload = token.split('.').nth(1)?;
    let claims = base64::engine::general_purpose::URL_SAFE_NO_PAD
        .decode(payload.trim_end_matches('='))
        .ok()?;
    let exp = serde_json::from_slice::<Claims>(&claims).ok()?.exp;
    let now = SystemTime::now().duration_since(UNIX_EPOCH).ok()?.as_secs();
    Some(Instant::now() + Duration::from_secs(exp.saturating_sub(now)))
}

fn generic(message: impl Into<String>) -> Error {
    Error::Generic {
        source: message.into().into(),
    }
}

#[cfg(test)]
mod tests {
    use mockito::Matcher;

    use super::*;
    use crate::{ClientConfigKey, CloudClient};

    fn auth() -> BearerAuth {
        BearerAuth::new().with_client_options(
            ClientOptions::new().with_config(ClientConfigKey::AllowHttp, "true"),
        )
    }

    /// A JWT (unsigned) whose payload is `{"exp": <exp>}`.
    fn jwt(exp: u64) -> String {
        let payload =
            base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(format!(r#"{{"exp":{exp}}}"#));
        format!("eyJhbGciOiJub25lIn0.{payload}.")
    }

    fn unix_now() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
    }

    /// Send one request through a client signed by `provider` and assert the
    /// bearer it carried.
    async fn assert_signs(
        server: &mut mockito::Server,
        provider: BearerCredentialProvider,
        token: &str,
    ) {
        let api = server
            .mock("GET", "/api")
            .match_header("authorization", format!("Bearer {token}").as_str())
            .with_status(200)
            .create_async()
            .await;
        let options = ClientOptions::new().with_config(ClientConfigKey::AllowHttp, "true");
        let client =
            CloudClient::new_with_bearer(provider, &options, RetryConfig::default(), None).unwrap();
        let status = client
            .get(format!("{}/api", server.url()))
            .send()
            .await
            .unwrap()
            .status();
        assert_eq!(status, 200);
        api.assert_async().await;
    }

    #[tokio::test]
    async fn client_credentials_against_any_token_endpoint() {
        let mut server = mockito::Server::new_async().await;
        let token = server
            .mock("POST", "/oauth2/token")
            .match_body(Matcher::AllOf(vec![
                Matcher::UrlEncoded("grant_type".into(), "client_credentials".into()),
                Matcher::UrlEncoded("client_id".into(), "app".into()),
                Matcher::UrlEncoded("client_secret".into(), "secret".into()),
                Matcher::UrlEncoded("scope".into(), "catalog.read".into()),
                Matcher::UrlEncoded("audience".into(), "https://uc.example.com".into()),
            ]))
            .with_header("content-type", "application/json")
            .with_body(r#"{"access_token":"cc-token","expires_in":3600}"#)
            .expect(1)
            .create_async()
            .await;

        let provider = auth()
            .client_credentials(ClientCredentials {
                token_url: format!("{}/oauth2/token", server.url()),
                client_id: "app".into(),
                client_secret: "secret".into(),
                scope: Some("catalog.read".into()),
                audience: Some("https://uc.example.com".into()),
            })
            .unwrap();
        assert_signs(&mut server, provider, "cc-token").await;
        token.assert_async().await;
    }

    /// The subject token is exchanged per RFC 8693 and the issued token signs.
    #[tokio::test]
    async fn token_exchange_swaps_the_subject_token() {
        let mut server = mockito::Server::new_async().await;
        let exchanged = jwt(unix_now() + 3600);
        let exchange = server
            .mock("POST", "/api/1.0/unity-control/auth/tokens")
            .match_body(Matcher::AllOf(vec![
                Matcher::UrlEncoded("grant_type".into(), TOKEN_EXCHANGE_GRANT.into()),
                Matcher::UrlEncoded("subject_token".into(), "idp-id-token".into()),
                Matcher::UrlEncoded("subject_token_type".into(), ID_TOKEN_TYPE.into()),
                Matcher::UrlEncoded("requested_token_type".into(), ACCESS_TOKEN_TYPE.into()),
            ]))
            .with_header("content-type", "application/json")
            // No `expires_in`: the JWT's `exp` decides when to refresh.
            .with_body(format!(
                r#"{{"access_token":"{exchanged}","token_type":"Bearer"}}"#
            ))
            .expect(1)
            .create_async()
            .await;

        let provider = auth()
            .token_exchange(TokenExchange {
                endpoint: format!("{}/api/1.0/unity-control/auth/tokens", server.url()),
                subject: BearerAuth::static_token("idp-id-token"),
                subject_token_type: None,
                requested_token_type: None,
                audience: None,
                scope: None,
            })
            .unwrap();
        // Two calls, one exchange: the issued token is cached until near `exp`.
        provider.get_credential().await.unwrap();
        assert_signs(&mut server, provider, &exchanged).await;
        exchange.assert_async().await;
    }

    #[tokio::test]
    async fn azure_client_secret_uses_the_given_scope() {
        let mut server = mockito::Server::new_async().await;
        let aad = server
            .mock("POST", "/tenant-1/oauth2/v2.0/token")
            .match_body(Matcher::UrlEncoded(
                "scope".into(),
                "api://my-catalog/.default".into(),
            ))
            .with_header("content-type", "application/json")
            .with_body(r#"{"access_token":"aad-token","expires_in":3600}"#)
            .create_async()
            .await;

        let provider = auth()
            .azure_client_secret(
                "tenant-1",
                "app",
                "secret",
                "api://my-catalog/.default",
                Some(server.url()),
            )
            .unwrap();
        assert_signs(&mut server, provider, "aad-token").await;
        aad.assert_async().await;
    }

    #[tokio::test]
    async fn azure_managed_identity_uses_the_given_resource() {
        let mut server = mockito::Server::new_async().await;
        let imds = server
            .mock("GET", "/metadata/identity/oauth2/token")
            .match_query(Matcher::AllOf(vec![
                Matcher::UrlEncoded("resource".into(), "api://my-catalog".into()),
                Matcher::UrlEncoded("client_id".into(), "uami".into()),
            ]))
            .match_header("metadata", "true")
            .with_header("content-type", "application/json")
            .with_body(format!(
                r#"{{"access_token":"msi-token","expires_on":"{}","expires_in":"3600"}}"#,
                unix_now() + 3600
            ))
            .create_async()
            .await;

        let provider = auth()
            .azure_managed_identity(AzureManagedIdentity {
                resource: "api://my-catalog".into(),
                client_id: Some("uami".into()),
                endpoint: Some(format!("{}/metadata/identity/oauth2/token", server.url())),
                ..Default::default()
            })
            .unwrap();
        assert_signs(&mut server, provider, "msi-token").await;
        imds.assert_async().await;
    }

    #[test]
    fn gcp_service_account_id_token_rejects_user_credentials() {
        let key = r#"{"type":"authorized_user","client_id":"id","client_secret":"secret","refresh_token":"refresh"}"#;
        let err = auth()
            .gcp_service_account_id_token(GcpServiceAccount::Key(key.into()), "aud")
            .unwrap_err();
        assert!(err.to_string().contains("`authorized_user`"), "{err}");
    }

    #[test]
    fn gcp_service_account_id_token_validates_the_key() {
        let missing = auth()
            .gcp_service_account_id_token(
                GcpServiceAccount::File("/nonexistent.json".into()),
                "aud",
            )
            .unwrap_err();
        assert!(
            missing.to_string().contains("/nonexistent.json"),
            "{missing}"
        );

        let key = r#"{"type":"service_account","private_key":"not a key","private_key_id":"kid","client_email":"sa@example.iam.gserviceaccount.com"}"#;
        assert!(
            auth()
                .gcp_service_account_id_token(GcpServiceAccount::Key(key.into()), "aud")
                .is_err()
        );
        let debug = format!("{:?}", GcpServiceAccount::Key(key.into()));
        assert!(!debug.contains("private_key"), "{debug}");
    }

    #[tokio::test]
    async fn gcp_id_token_requests_the_audience() {
        let mut server = mockito::Server::new_async().await;
        let id_token = jwt(unix_now() + 3600);
        let metadata = server
            .mock("GET", "/identity")
            .match_query(Matcher::UrlEncoded(
                "audience".into(),
                "https://uc.example.com".into(),
            ))
            .match_header("metadata-flavor", "Google")
            .with_body(&id_token)
            .create_async()
            .await;

        let provider = auth()
            .gcp_id_token(
                "https://uc.example.com",
                Some(format!("{}/identity", server.url())),
            )
            .unwrap();
        assert_signs(&mut server, provider, &id_token).await;
        metadata.assert_async().await;
    }

    #[tokio::test]
    async fn token_file_is_trimmed_and_missing_files_error() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("token");
        std::fs::write(&path, "file-token\n").unwrap();

        let provider = auth().token_file(path.to_string_lossy());
        assert_eq!(provider.get_credential().await.unwrap().token, "file-token");

        let missing = auth().token_file(dir.path().join("absent").to_string_lossy());
        assert!(missing.get_credential().await.is_err());
    }

    #[test]
    fn jwt_expiry_reads_exp_and_ignores_opaque_tokens() {
        let in_an_hour = jwt_expiry(&jwt(unix_now() + 3600)).unwrap();
        let remaining = in_an_hour.saturating_duration_since(Instant::now());
        assert!(remaining > Duration::from_secs(3500) && remaining <= Duration::from_secs(3600));
        assert!(jwt_expiry("opaque-token").is_none());
        assert!(jwt_expiry("a.not-base64!.c").is_none());
    }

    #[test]
    fn debug_redacts_secrets() {
        let credential = BearerCredential {
            token: "super-secret".into(),
        };
        let config = ClientCredentials {
            client_secret: "super-secret".into(),
            ..Default::default()
        };
        for debug in [format!("{credential:?}"), format!("{config:?}")] {
            assert!(!debug.contains("super-secret"), "secret leaked: {debug}");
        }
    }
}