ultron-ai 1.0.2

Provider layer of packages/ai: message model, streaming events, every API implementation (Anthropic, OpenAI completions/responses/codex, Azure, Google, Vertex, Mistral, Bedrock, pi-messages, Claude Code CLI, Claude subscription), provider auth and every OAuth flow, images API, model catalog, user models.json, env API keys
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
//! Google application default credentials as `@google/genai`'s Vertex client resolves them through
//! `google-auth-library` (10.6.2, `GoogleAuth` with the `cloud-platform` scope): the key file named by
//! `GOOGLE_APPLICATION_CREDENTIALS`, else the ADC chain (the lower-case variable, the gcloud well-known file, the GCE
//! metadata server), then an access token for the credential (`authorized_user` refresh, `service_account` JWT bearer
//! exchange, `impersonated_service_account` through IAM credentials, Compute Engine metadata, and workload identity
//! federation in `external.rs`), sent as `authorization: Bearer <token>` plus `x-goog-user-project` for a quota project.
//! A service account outside the default universe signs its own JWT instead of exchanging one.
//!
//! Upstream builds a fresh `GoogleAuth` for every stream call, so nothing is cached here either. Error messages are
//! upstream's (gaxios' API error extraction, each client's rewording, Node's file errors), recorded by
//! `fixtures/generate-vertex-adc.mjs`. Environment lookups that upstream makes on `process.env` (HOME, the metadata
//! server settings, the quota project override) go through the provider env first, so a credential can scope them.

mod certificate;
mod external;

use crate::env::{ProviderEnv, provider_env_value};
use crate::http::{HttpRequest, HttpResponse, Transport};
use crate::oauth_flow::form_urlencode;
use serde_json::{Map, Value, json};
use std::path::{Path, PathBuf};
use std::time::Duration;

pub use external::{AwsCredentials, aws_subject_token, self_signed_jwt};

const SCOPE: &str = "https://www.googleapis.com/auth/cloud-platform";
const TOKEN_URL: &str = "https://oauth2.googleapis.com/token";
const AUTH_USER_AGENT: &str = "google-api-nodejs-client/10.6.2";
const JWT_BEARER: &str = "urn:ietf:params:oauth:grant-type:jwt-bearer";
const DEFAULT_UNIVERSE: &str = "googleapis.com";
const METADATA_HOST: &str = "http://169.254.169.254";
const METADATA_SECONDARY_HOST: &str = "http://metadata.google.internal.";
const NO_ADC_FOUND: &str = "Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.";
const KEY_TYPE_ERROR: &str = "key must be a string, a buffer or an object";
const BAD_KEY: &str = "error:1E08010C:DECODER routines::unsupported";

/// The headers `GoogleAuth.getRequestHeaders` adds to a Vertex request.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AuthHeaders {
    pub authorization: String,
    pub user_project: Option<String>,
}

/// A failure with upstream's message; `status` is the HTTP status of the request that failed, when one did.
#[derive(Clone, Debug, PartialEq, Eq)]
struct Fail {
    /// The JavaScript error class, for messages that embed `String(error)`.
    name: &'static str,
    message: String,
    status: Option<u16>,
    /// The parsed error response body.
    data: Option<Box<Value>>,
}

impl Fail {
    fn new(message: impl Into<String>) -> Self {
        Fail {
            name: "Error",
            message: message.into(),
            status: None,
            data: None,
        }
    }

    fn typed(name: &'static str, message: impl Into<String>) -> Self {
        Fail {
            name,
            ..Fail::new(message)
        }
    }
}

#[derive(Clone, Debug)]
enum Credential {
    User {
        client_id: String,
        client_secret: String,
        refresh_token: String,
        universe: String,
        quota: Option<String>,
    },
    ServiceAccount {
        email: String,
        key: String,
        key_id: Option<String>,
        universe: String,
        quota: Option<String>,
    },
    Impersonated {
        source: Box<Credential>,
        target: String,
        delegates: Value,
        universe: String,
        quota: Option<String>,
    },
    Compute {
        quota: Option<String>,
    },
    /// `external_account`: a subject token exchanged at STS (`external.rs`).
    External(Box<external::External>),
    /// `external_account_authorized_user`: a refresh token redeemed at STS.
    ExternalUser(Box<external::ExternalUser>),
    /// `fromStreamAsync`'s fallback for a key file that is not a usable credential JSON: a `JWT` with only `keyFile`.
    /// `scoped` when a failed service-account `fromJSON` had already put the scopes on the client options.
    KeyFile {
        path: PathBuf,
        scoped: bool,
    },
}

impl Credential {
    fn set_quota(&mut self, value: String) {
        match self {
            Credential::User { quota, .. }
            | Credential::ServiceAccount { quota, .. }
            | Credential::Impersonated { quota, .. }
            | Credential::Compute { quota } => *quota = Some(value),
            Credential::External(external) => external.quota = Some(value),
            Credential::ExternalUser(user) => user.quota = Some(value),
            Credential::KeyFile { .. } => {}
        }
    }

    fn quota(&self) -> Option<String> {
        match self {
            Credential::User { quota, .. }
            | Credential::ServiceAccount { quota, .. }
            | Credential::Impersonated { quota, .. }
            | Credential::Compute { quota } => quota.clone(),
            Credential::External(external) => external.quota.clone(),
            Credential::ExternalUser(user) => user.quota.clone(),
            Credential::KeyFile { .. } => None,
        }
    }

    /// The client's `universeDomain`.
    fn universe(&self) -> String {
        match self {
            Credential::User { universe, .. }
            | Credential::ServiceAccount { universe, .. }
            | Credential::Impersonated { universe, .. } => universe.clone(),
            Credential::External(external) => external.universe.clone(),
            Credential::ExternalUser(user) => user.universe.clone(),
            Credential::Compute { .. } | Credential::KeyFile { .. } => DEFAULT_UNIVERSE.into(),
        }
    }

    /// `OAuth2Client.getRequestMetadataAsync` rewords a 403/404 refresh failure; the external account clients are
    /// plain `AuthClient`s and do not.
    fn rewords_refresh_failures(&self) -> bool {
        !matches!(self, Credential::External(_) | Credential::ExternalUser(_))
    }
}

/// `GoogleAuth({ keyFilename: GOOGLE_APPLICATION_CREDENTIALS, scopes: [cloud-platform] }).getRequestHeaders()`.
pub async fn request_headers(env: Option<&ProviderEnv>, transport: &dyn Transport) -> Result<AuthHeaders, String> {
    let credential = match provider_env_value("GOOGLE_APPLICATION_CREDENTIALS", env) {
        Some(path) => key_file_credential(&path),
        None => application_default(env, transport).await,
    }
    .map_err(|f| f.message)?;
    let token = match &credential {
        // `JWT.getRequestMetadataAsync`: outside the default universe, a self-signed JWT with the scopes.
        Credential::ServiceAccount {
            email,
            key,
            key_id,
            universe,
            ..
        } if universe != DEFAULT_UNIVERSE => {
            let iat = jsrt::date::now_ms().div_euclid(1000);
            self_signed_jwt(email, key, key_id.as_deref(), iat).map_err(Fail::new)
        }
        _ => access_token(&credential, env, transport).await,
    };
    let token = token.map_err(|mut fail| {
        if credential.rewords_refresh_failures() && matches!(fail.status, Some(403 | 404)) {
            fail.message = format!("Could not refresh access token: {}", fail.message);
        }
        fail.message
    })?;
    Ok(AuthHeaders {
        authorization: format!("Bearer {token}"),
        user_project: credential.quota(),
    })
}

fn optional_string(json: &Value, key: &str) -> Option<String> {
    jsrt::value::truthy(json.get(key)).then(|| jsrt::value::string(json.get(key)))
}

/// Which `fromJSON` branch a credential JSON takes; the service-account branch mutates the shared client options.
enum Branch {
    Known,
    Jwt,
}

/// A JSON truthy string field, else the client default (`json.universe_domain || this.universeDomain`).
fn universe_of(json: &Value) -> String {
    optional_string(json, "universe_domain").unwrap_or_else(|| DEFAULT_UNIVERSE.into())
}

/// `GoogleAuth.fromJSON`.
fn from_json(json: &Value) -> Result<Credential, (Fail, Branch)> {
    let kind = match json {
        Value::Null => {
            return Err((
                Fail::typed("TypeError", "Cannot read properties of null (reading 'type')"),
                Branch::Known,
            ));
        }
        Value::Object(map) => map.get("type").and_then(Value::as_str).unwrap_or_default(),
        _ => "",
    };
    let missing = |field: &str| Fail::new(format!("The incoming JSON object does not contain a {field} field"));
    match kind {
        "authorized_user" => {
            for field in ["client_id", "client_secret", "refresh_token"] {
                if !jsrt::value::truthy(json.get(field)) {
                    return Err((missing(field), Branch::Known));
                }
            }
            Ok(Credential::User {
                client_id: jsrt::value::string(json.get("client_id")),
                client_secret: jsrt::value::string(json.get("client_secret")),
                refresh_token: jsrt::value::string(json.get("refresh_token")),
                universe: universe_of(json),
                quota: optional_string(json, "quota_project_id"),
            })
        }
        "impersonated_service_account" => impersonated(json).map_err(|fail| (fail, Branch::Known)),
        "external_account" => external::External::from_json(json)
            .map(|client| Credential::External(Box::new(client)))
            .map_err(|fail| (fail, Branch::Known)),
        "external_account_authorized_user" => Ok(Credential::ExternalUser(Box::new(
            external::ExternalUser::from_json(json),
        ))),
        _ => {
            if !jsrt::value::truthy(Some(json)) {
                return Err((
                    Fail::new("Must pass in a JSON object containing the service account auth settings."),
                    Branch::Jwt,
                ));
            }
            for field in ["client_email", "private_key"] {
                if !jsrt::value::truthy(json.get(field)) {
                    return Err((missing(field), Branch::Jwt));
                }
            }
            Ok(Credential::ServiceAccount {
                email: jsrt::value::string(json.get("client_email")),
                key: jsrt::value::string(json.get("private_key")),
                key_id: optional_string(json, "private_key_id"),
                universe: universe_of(json),
                quota: optional_string(json, "quota_project_id"),
            })
        }
    }
}

/// `GoogleAuth.fromImpersonatedJSON`.
fn impersonated(json: &Value) -> Result<Credential, Fail> {
    let Some(source_json) = json.get("source_credentials").filter(|v| jsrt::value::truthy(Some(v))) else {
        return Err(Fail::new(
            "The incoming JSON object does not contain a source_credentials field",
        ));
    };
    if !jsrt::value::truthy(json.get("service_account_impersonation_url")) {
        return Err(Fail::new(
            "The incoming JSON object does not contain a service_account_impersonation_url field",
        ));
    }
    let source = from_json(source_json).map_err(|(fail, _)| fail)?;
    let url = jsrt::value::string(json.get("service_account_impersonation_url"));
    if url.encode_utf16().count() > 256 {
        return Err(Fail::typed(
            "RangeError",
            format!("Target principal is too long: {url}"),
        ));
    }
    // `/(?<target>[^/]+):(generateAccessToken|generateIdToken)$/`
    let target = ["generateAccessToken", "generateIdToken"].iter().find_map(|method| {
        let head = url.strip_suffix(method)?.strip_suffix(':')?;
        let name = head.rsplit('/').next().unwrap_or(head);
        (!name.is_empty()).then(|| name.to_string())
    });
    let Some(target) = target else {
        return Err(Fail::typed(
            "RangeError",
            format!("Cannot extract target principal from {url}"),
        ));
    };
    // `new Impersonated`: the source's universe, unless one is set explicitly, which must then match it.
    let universe = match external::camel_option(json, "universe_domain").filter(|v| jsrt::value::truthy(Some(v))) {
        None => source.universe(),
        Some(explicit) => {
            let explicit = jsrt::value::string(Some(explicit));
            if explicit != source.universe() {
                return Err(Fail::typed(
                    "RangeError",
                    format!(
                        "Universe domain {} in source credentials does not match {explicit} universe domain set for impersonated credentials.",
                        source.universe()
                    ),
                ));
            }
            explicit
        }
    };
    Ok(Credential::Impersonated {
        source: Box::new(source),
        target,
        delegates: json
            .get("delegates")
            .filter(|v| !v.is_null())
            .cloned()
            .unwrap_or_else(|| json!([])),
        universe,
        quota: optional_string(json, "quota_project_id"),
    })
}

fn absolute(path: &str) -> PathBuf {
    let path = Path::new(path);
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir().unwrap_or_default().join(path)
    }
}

/// The `keyFilename` path: `fromStreamAsync` over `fs.createReadStream(path.resolve(keyFilename))`, falling back to a
/// key-file `JWT` when the JSON is not a usable credential.
fn key_file_credential(key_file: &str) -> Result<Credential, Fail> {
    let path = absolute(key_file);
    let text = std::fs::read(&path).map_err(|e| Fail::new(jsrt::fs::error_message(&e, "open", &path)))?;
    let text = String::from_utf8_lossy(&text);
    let fallback = |scoped| Credential::KeyFile {
        path: path.clone(),
        scoped,
    };
    if jsrt::json::parse(&text).is_err() {
        return Ok(fallback(false));
    }
    let json: Value = serde_json::from_str(&text).unwrap_or(Value::Null);
    match from_json(&json) {
        Ok(credential) => Ok(credential),
        Err((_, Branch::Jwt)) => Ok(fallback(true)),
        Err((_, Branch::Known)) => Ok(fallback(false)),
    }
}

/// Node's `fs.realpathSync` (the JavaScript walk): the first component that cannot be `lstat`ed names the error.
fn realpath(path: &Path) -> Result<PathBuf, std::io::Error> {
    let mut prefix = PathBuf::new();
    for component in path.components() {
        prefix.push(component);
        if let Err(error) = std::fs::symlink_metadata(&prefix) {
            return Err(std::io::Error::new(
                error.kind(),
                jsrt::fs::error_message(&error, "lstat", &prefix),
            ));
        }
    }
    std::fs::canonicalize(path)
}

/// `_getApplicationCredentialsFromFilePath`: the ADC chain's file reader, which throws instead of falling back.
fn file_path_credential(file_path: &str) -> Result<Credential, Fail> {
    if file_path.is_empty() {
        return Err(Fail::new("The file path is invalid."));
    }
    let not_a_file = |shown: &str, inner: &str| {
        Fail::new(format!(
            "The file at {shown} does not exist, or it is not a file. {inner}"
        ))
    };
    let real = realpath(&absolute(file_path)).map_err(|e| not_a_file(file_path, &e.to_string()))?;
    let shown = real.display().to_string();
    match std::fs::symlink_metadata(&real) {
        Ok(meta) if meta.is_file() => {}
        Ok(_) => return Err(not_a_file(&shown, "")),
        Err(e) => return Err(not_a_file(&shown, &jsrt::fs::error_message(&e, "lstat", &real))),
    }
    let text = std::fs::read(&real).map_err(|e| Fail::new(jsrt::fs::error_message(&e, "open", &real)))?;
    let text = String::from_utf8_lossy(&text);
    if let Some(message) = jsrt::json::parse(&text).err() {
        return Err(Fail::typed("SyntaxError", message));
    }
    let json: Value = serde_json::from_str(&text).unwrap_or(Value::Null);
    from_json(&json).map_err(|(fail, _)| fail)
}

/// `getApplicationDefaultAsync`.
async fn application_default(env: Option<&ProviderEnv>, transport: &dyn Transport) -> Result<Credential, Fail> {
    let mut credential = if let Some(path) = provider_env_value("google_application_credentials", env) {
        file_path_credential(&path)?
    } else if let Some(path) = provider_env_value("HOME", env)
        .map(|home| Path::new(&home).join(".config/gcloud/application_default_credentials.json"))
        .filter(|path| path.exists())
    {
        file_path_credential(&path.display().to_string())?
    } else if gcp_residency(env) || metadata_available(env, transport).await? {
        Credential::Compute { quota: None }
    } else {
        return Err(Fail::new(NO_ADC_FOUND));
    };
    if let Some(quota) = provider_env_value("GOOGLE_CLOUD_QUOTA_PROJECT", env) {
        credential.set_quota(quota);
    }
    Ok(credential)
}

/// gcp-metadata's `detectGCPResidency`: a serverless runtime, a Google BIOS, or a GCE MAC address.
fn gcp_residency(env: Option<&ProviderEnv>) -> bool {
    if ["CLOUD_RUN_JOB", "FUNCTION_NAME", "K_SERVICE"]
        .iter()
        .any(|name| provider_env_value(name, env).is_some())
    {
        return true;
    }
    if cfg!(target_os = "linux")
        && Path::new("/sys/class/dmi/id/bios_date").exists()
        && std::fs::read_to_string("/sys/class/dmi/id/bios_vendor").is_ok_and(|v| v.contains("Google"))
    {
        return true;
    }
    std::fs::read_dir("/sys/class/net").is_ok_and(|entries| {
        entries.flatten().any(|entry| {
            std::fs::read_to_string(entry.path().join("address")).is_ok_and(|mac| mac.trim().starts_with("42:01"))
        })
    })
}

fn metadata_base(env: Option<&ProviderEnv>, host: Option<&str>) -> String {
    let host = host
        .map(str::to_string)
        .or_else(|| provider_env_value("GCE_METADATA_IP", env))
        .or_else(|| provider_env_value("GCE_METADATA_HOST", env))
        .unwrap_or_else(|| METADATA_HOST.to_string());
    let host = if host.starts_with("http://") || host.starts_with("https://") {
        host
    } else {
        format!("http://{host}")
    };
    reqwest::Url::parse(&host)
        .and_then(|url| url.join("/computeMetadata/v1"))
        .map_or_else(|_| format!("{host}/computeMetadata/v1"), |url| url.to_string())
}

fn metadata_timeout(env: Option<&ProviderEnv>) -> Option<Duration> {
    (!gcp_residency(env)).then_some(Duration::from_millis(3000))
}

fn metadata_request(url: String) -> HttpRequest {
    HttpRequest {
        method: "GET".into(),
        url,
        headers: [("metadata-flavor".to_string(), "Google".to_string())].into(),
        body: String::new(),
    }
}

/// `metadataAccessor`: the response must carry `Metadata-Flavor: Google`.
async fn metadata_get(
    transport: &dyn Transport,
    url: String,
    no_response_retries: u32,
    timeout: Option<Duration>,
) -> Result<Value, Fail> {
    let policy = Retry {
        no_response_retries,
        status_retries: STATUS_RETRIES,
        timeout,
    };
    let response = gaxios(transport, metadata_request(url), &policy, ResponseType::Text).await?;
    match response.headers.get("metadata-flavor").map(String::as_str) {
        Some("Google") => {}
        other => {
            return Err(Fail::typed(
                "RangeError",
                format!(
                    "Invalid response from metadata service: incorrect Metadata-Flavor header. Expected 'Google', got {}",
                    other.map_or_else(|| "no header".to_string(), |v| format!("'{v}'"))
                ),
            ));
        }
    }
    Ok(match &response.data {
        Value::String(text) => serde_json::from_str(text).unwrap_or_else(|_| response.data.clone()),
        other => other.clone(),
    })
}

/// gcp-metadata's `isAvailable`.
async fn metadata_available(env: Option<&ProviderEnv>, transport: &dyn Transport) -> Result<bool, Fail> {
    if let Some(value) = provider_env_value("METADATA_SERVER_DETECTION", env) {
        match value.trim().to_lowercase().as_str() {
            "assume-present" => return Ok(true),
            "none" => return Ok(false),
            "bios-only" => return Ok(gcp_residency(env)),
            "ping-only" => {}
            other => {
                return Err(Fail::typed(
                    "RangeError",
                    format!(
                        "Unknown `METADATA_SERVER_DETECTION` env variable. Got `{other}`, but it should be `assume-present`, `none`, `bios-only`, `ping-only`, or unset"
                    ),
                ));
            }
        }
    }
    let retries = provider_env_value("DETECT_GCP_RETRIES", env)
        .and_then(|v| v.parse().ok())
        .unwrap_or(0);
    let timeout = metadata_timeout(env);
    let primary = metadata_get(
        transport,
        format!("{}/instance", metadata_base(env, None)),
        retries,
        timeout,
    );
    let explicit_host =
        provider_env_value("GCE_METADATA_IP", env).is_some() || provider_env_value("GCE_METADATA_HOST", env).is_some();
    if explicit_host {
        return Ok(primary.await.is_ok());
    }
    // `fastFailMetadataRequest`: the first of the two hosts to answer.
    let secondary = metadata_get(
        transport,
        format!("{}/instance", metadata_base(env, Some(METADATA_SECONDARY_HOST))),
        retries,
        timeout,
    );
    let (primary, secondary) = (Box::pin(primary), Box::pin(secondary));
    Ok(match futures_select(primary, secondary).await {
        (Ok(_), _) => true,
        (Err(_), other) => other.await.is_ok(),
    })
}

/// The first of two futures to finish, and the other one.
async fn futures_select<'a, T>(
    a: std::pin::Pin<Box<dyn Future<Output = T> + Send + 'a>>,
    b: std::pin::Pin<Box<dyn Future<Output = T> + Send + 'a>>,
) -> (T, std::pin::Pin<Box<dyn Future<Output = T> + Send + 'a>>) {
    let (mut a, mut b) = (a, b);
    tokio::select! {
        first = &mut a => (first, b),
        first = &mut b => (first, a),
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum ResponseType {
    /// `responseType: 'json'`: parsed when it parses, else the text.
    Json,
    Text,
    /// Upstream's default: by content type.
    Unknown,
}

struct Retry {
    no_response_retries: u32,
    /// Retries of 1xx/408/429/5xx statuses: three under a retry config, none without one.
    status_retries: u32,
    timeout: Option<Duration>,
}

/// The retry policy every request here runs under: three retries of 1xx/408/429/5xx statuses for any method.
const STATUS_RETRIES: u32 = 3;

struct Response {
    headers: crate::http::HeaderMap,
    data: Value,
}

async fn read_text(response: &mut HttpResponse) -> Result<String, String> {
    let mut bytes = Vec::new();
    while let Some(chunk) = response.body.next_chunk().await? {
        bytes.extend_from_slice(&chunk);
    }
    Ok(String::from_utf8_lossy(&bytes).into_owned())
}

fn response_data(kind: ResponseType, content_type: Option<&str>, text: String) -> Value {
    let parsed = |text: String| serde_json::from_str(&text).unwrap_or(Value::String(text));
    match kind {
        ResponseType::Json => parsed(text),
        ResponseType::Text => Value::String(text),
        ResponseType::Unknown => match content_type.map(str::to_lowercase) {
            Some(ct) if ct.contains("application/json") => parsed(text),
            _ => Value::String(text),
        },
    }
}

/// `GaxiosError.extractAPIErrorFromResponse`.
fn api_error_message(status: u16, data: &Value) -> String {
    let mut message = format!("Request failed with status code {status}");
    if let Value::String(text) = data {
        message = text.clone();
    }
    if let Some(error) = data.get("error").filter(|e| jsrt::value::truthy(Some(e))) {
        match error {
            Value::String(text) => return text.clone(),
            Value::Object(object) => {
                if let Some(text) = object.get("message").and_then(Value::as_str) {
                    message = text.to_string();
                }
                if let Some(Value::Array(errors)) = object.get("errors") {
                    let joined = errors
                        .iter()
                        .filter_map(|e| e.get("message").and_then(Value::as_str))
                        .collect::<Vec<_>>()
                        .join("\n");
                    if !joined.is_empty() {
                        return joined;
                    }
                }
            }
            _ => {}
        }
    }
    message
}

/// `getNextRetryDelay` with gaxios' defaults: 100 ms, then `(2^n - 1) / 2` seconds.
fn retry_delay(attempt: u32) -> Duration {
    let base = if attempt == 0 { 100.0 } else { 0.0 };
    Duration::from_millis((base + (2f64.powi(attempt as i32) - 1.0) / 2.0 * 1000.0) as u64)
}

/// `Gaxios.request` with a retry config: status failures and missing responses are retried, then thrown as a
/// `GaxiosError` whose message comes from the response body.
async fn gaxios(
    transport: &dyn Transport,
    request: HttpRequest,
    policy: &Retry,
    kind: ResponseType,
) -> Result<Response, Fail> {
    let mut attempt = 0;
    loop {
        let sent = match policy.timeout {
            Some(limit) => match tokio::time::timeout(limit, transport.send(request.clone())).await {
                Ok(sent) => sent,
                Err(_) => Err(crate::provider::OPERATION_TIMED_OUT.into()),
            },
            None => transport.send(request.clone()).await,
        };
        let fail = match sent {
            Err(message) => {
                if attempt < policy.no_response_retries {
                    tokio::time::sleep(retry_delay(attempt)).await;
                    attempt += 1;
                    continue;
                }
                Fail::new(if message.contains("timeout") {
                    message
                } else {
                    "fetch failed".to_string()
                })
            }
            Ok(mut response) => {
                let content_type = response.headers.get("content-type").cloned();
                let text = read_text(&mut response).await.map_err(Fail::new)?;
                let data = response_data(kind, content_type.as_deref(), text);
                if (200..300).contains(&response.status) {
                    return Ok(Response {
                        headers: response.headers,
                        data,
                    });
                }
                let status = response.status;
                let retryable = matches!(status, 100..=199 | 408 | 429 | 500..=599);
                if retryable && attempt < policy.status_retries {
                    tokio::time::sleep(retry_delay(attempt)).await;
                    attempt += 1;
                    continue;
                }
                Fail {
                    name: "Error",
                    message: api_error_message(status, &data),
                    status: Some(status),
                    data: Some(Box::new(data)),
                }
            }
        };
        return Err(fail);
    }
}

fn form_request(body: &[(&str, &str)], json_response: bool) -> HttpRequest {
    let mut headers: crate::http::HeaderMap = [
        ("user-agent".to_string(), AUTH_USER_AGENT.to_string()),
        (
            "content-type".to_string(),
            "application/x-www-form-urlencoded;charset=UTF-8".to_string(),
        ),
    ]
    .into();
    if json_response {
        headers.insert("accept".into(), "application/json".into());
    }
    HttpRequest {
        method: "POST".into(),
        url: TOKEN_URL.into(),
        headers,
        body: form_urlencode(body),
    }
}

const AUTH_RETRY: Retry = Retry {
    no_response_retries: 2,
    status_retries: STATUS_RETRIES,
    timeout: None,
};

/// A request without a retry config: gaxios tries it once.
const NO_RETRY: Retry = Retry {
    no_response_retries: 0,
    status_retries: 0,
    timeout: None,
};

fn token_field(data: &Value, key: &str) -> String {
    jsrt::value::string(data.get(key))
}

/// `UserRefreshClient.refreshTokenNoCache`.
async fn refresh_user(
    transport: &dyn Transport,
    client_id: &str,
    client_secret: &str,
    refresh_token: &str,
) -> Result<String, Fail> {
    let request = form_request(
        &[
            ("refresh_token", refresh_token),
            ("client_id", client_id),
            ("client_secret", client_secret),
            ("grant_type", "refresh_token"),
        ],
        false,
    );
    match gaxios(transport, request, &AUTH_RETRY, ResponseType::Unknown).await {
        Ok(response) => Ok(token_field(&response.data, "access_token")),
        Err(mut fail) => {
            let reauth = fail.data.as_ref().is_some_and(|data| {
                data.get("error_description")
                    .map(|d| jsrt::value::string(Some(d)).to_lowercase().contains("reauth"))
                    .unwrap_or(false)
            });
            if fail.message == "invalid_grant" && reauth {
                fail.message = serde_json::to_string(fail.data.as_deref().unwrap_or(&Value::Null)).unwrap_or_default();
            }
            Err(fail)
        }
    }
}

/// The RS256 JWT `gtoken` signs for the JWT bearer grant.
pub fn service_account_assertion(email: &str, pem: &str, iat_seconds: i64) -> Result<String, String> {
    let header = jsrt::base64::encode_url(br#"{"alg":"RS256"}"#);
    let payload = json!({
        "iss": email,
        "scope": SCOPE,
        "aud": TOKEN_URL,
        "exp": iat_seconds + 3600,
        "iat": iat_seconds,
    });
    let payload = jsrt::base64::encode_url(serde_json::to_string(&payload).unwrap_or_default().as_bytes());
    let input = format!("{header}.{payload}");
    let signature = sign_rs256(pem, input.as_bytes())?;
    Ok(format!("{input}.{}", jsrt::base64::encode_url(&signature)))
}

fn sign_rs256(pem: &str, message: &[u8]) -> Result<Vec<u8>, String> {
    let body: String = pem
        .lines()
        .map(str::trim)
        .filter(|line| !line.starts_with("-----"))
        .collect();
    let der = jsrt::base64::atob(&body).ok_or_else(|| BAD_KEY.to_string())?;
    let pkcs1 = pem.contains("BEGIN RSA PRIVATE KEY");
    let key = if pkcs1 {
        ring::signature::RsaKeyPair::from_der(&der)
    } else {
        ring::signature::RsaKeyPair::from_pkcs8(&der)
    }
    .map_err(|_| BAD_KEY.to_string())?;
    let mut signature = vec![0; key.public().modulus_len()];
    key.sign(
        &ring::signature::RSA_PKCS1_SHA256,
        &ring::rand::SystemRandom::new(),
        message,
        &mut signature,
    )
    .map_err(|_| BAD_KEY.to_string())?;
    Ok(signature)
}

/// `JWT.refreshTokenNoCache` through `gtoken`.
async fn service_account_token(transport: &dyn Transport, email: &str, key: &str) -> Result<String, Fail> {
    let iat = jsrt::date::now_ms().div_euclid(1000);
    let assertion = service_account_assertion(email, key, iat).map_err(Fail::new)?;
    let request = form_request(&[("grant_type", JWT_BEARER), ("assertion", &assertion)], true);
    match gaxios(transport, request, &AUTH_RETRY, ResponseType::Json).await {
        Ok(response) => Ok(token_field(&response.data, "access_token")),
        Err(mut fail) => {
            if let Some(data) = &fail.data
                && jsrt::value::truthy(data.get("error"))
            {
                fail.message = format!(
                    "{}: {}",
                    jsrt::value::string(data.get("error")),
                    jsrt::value::string(data.get("error_description"))
                );
            }
            Err(fail)
        }
    }
}

/// `Compute.refreshTokenNoCache`.
async fn compute_token(env: Option<&ProviderEnv>, transport: &dyn Transport) -> Result<String, Fail> {
    let url = format!(
        "{}/instance/service-accounts/default/token?{}",
        metadata_base(env, None),
        form_urlencode(&[("scopes", SCOPE)])
    );
    match metadata_get(transport, url, 3, metadata_timeout(env)).await {
        Ok(data) => Ok(token_field(&data, "access_token")),
        Err(mut fail) if fail.name == "Error" => {
            fail.message = format!("Could not refresh access token: {}", fail.message);
            match fail.status {
                Some(403) => {
                    fail.message = format!(
                        "A Forbidden error was returned while attempting to retrieve an access token for the Compute Engine built-in service account. This may be because the Compute Engine instance does not have the correct permission scopes specified: {}",
                        fail.message
                    );
                }
                Some(404) => {
                    fail.message = format!(
                        "A Not Found error was returned while attempting to retrieve an accesstoken for the Compute Engine built-in service account. This may be because the Compute Engine instance does not have any permission scopes specified: {}",
                        fail.message
                    );
                }
                _ => {}
            }
            Err(fail)
        }
        Err(fail) => Err(fail),
    }
}

/// `Impersonated.refreshToken`.
async fn impersonated_token(
    env: Option<&ProviderEnv>,
    transport: &dyn Transport,
    source: &Credential,
    target: &str,
    delegates: &Value,
    universe: &str,
) -> Result<String, Fail> {
    let attempt = async {
        let source_token = Box::pin(access_token(source, env, transport)).await?;
        let mut headers: crate::http::HeaderMap = [
            ("user-agent".to_string(), AUTH_USER_AGENT.to_string()),
            ("content-type".to_string(), "application/json".to_string()),
            ("authorization".to_string(), format!("Bearer {source_token}")),
        ]
        .into();
        if let Some(quota) = source.quota() {
            headers.insert("x-goog-user-project".into(), quota);
        }
        let mut body = Map::new();
        body.insert("delegates".into(), delegates.clone());
        body.insert("scope".into(), json!([SCOPE]));
        body.insert("lifetime".into(), json!("3600s"));
        let request = HttpRequest {
            method: "POST".into(),
            url: format!(
                "https://iamcredentials.{universe}/v1/projects/-/serviceAccounts/{target}:generateAccessToken"
            ),
            headers,
            body: Value::Object(body).to_string(),
        };
        let response = gaxios(transport, request, &AUTH_RETRY, ResponseType::Unknown).await?;
        Ok(token_field(&response.data, "accessToken"))
    };
    attempt.await.map_err(|mut fail: Fail| {
        let error = fail.data.as_ref().and_then(|d| d.get("error"));
        let status = error
            .and_then(|e| e.get("status"))
            .filter(|s| jsrt::value::truthy(Some(s)));
        let message = error
            .and_then(|e| e.get("message"))
            .filter(|m| jsrt::value::truthy(Some(m)));
        fail.message = match (status, message) {
            (Some(status), Some(message)) => format!(
                "{}: unable to impersonate: {}",
                jsrt::value::string(Some(status)),
                jsrt::value::string(Some(message))
            ),
            _ => format!("unable to impersonate: {}: {}", fail.name, fail.message),
        };
        fail
    })
}

async fn access_token(
    credential: &Credential,
    env: Option<&ProviderEnv>,
    transport: &dyn Transport,
) -> Result<String, Fail> {
    match credential {
        Credential::User {
            client_id,
            client_secret,
            refresh_token,
            ..
        } => refresh_user(transport, client_id, client_secret, refresh_token).await,
        Credential::ServiceAccount { email, key, .. } => service_account_token(transport, email, key).await,
        Credential::Compute { .. } => compute_token(env, transport).await,
        Credential::Impersonated {
            source,
            target,
            delegates,
            universe,
            ..
        } => impersonated_token(env, transport, source, target, delegates, universe).await,
        Credential::External(client) => client.access_token(env, transport).await,
        Credential::ExternalUser(user) => user.access_token(transport).await,
        Credential::KeyFile { path, scoped } => Err(key_file_jwt_error(path, *scoped)),
    }
}

/// The key-file `JWT` fallback never yields a token: without scopes it self-signs with no key loaded; with scopes,
/// `gtoken` reads the key by extension and finds no usable key.
fn key_file_jwt_error(path: &Path, scoped: bool) -> Fail {
    if !scoped {
        return Fail::typed("TypeError", KEY_TYPE_ERROR);
    }
    let extension = path.extension().and_then(|e| e.to_str()).unwrap_or_default();
    Fail::new(match extension {
        "json" => "private_key and client_email are required.",
        "der" | "crt" | "pem" => BAD_KEY,
        "p12" | "pfx" => {
            "*.p12 certificates are not supported after v6.1.2. Consider utilizing *.json format or converting *.p12 to *.pem using the OpenSSL CLI."
        }
        _ => {
            "Unknown certificate type. Type is determined based on file extension. Current supported extensions are *.json, and *.pem."
        }
    })
}