cpa-plugin 0.4.1

Part of cliproxy: the native plugin host (CLIProxyAPI C ABI 1, registration schema 6) and plugin store
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
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
//! Auth provider and frontend auth capabilities (internal/pluginhost/auth_provider.go,
//! adapters_auth.go).
//!
//! A plugin auth is a [`PluginAuth`]: Go's `coreauth.Auth` fields that plugins set, with
//! its storage (`pluginTokenStorage`) as raw JSON merged with metadata on save. The
//! server maps it onto its credential store.

use cpa_core::gostr::GoStr;
use std::collections::BTreeMap;

use bytes::Bytes;
use serde_json::Value;

use crate::abi::method;
use crate::api::{
    AuthData, AuthLoginPollRequest, AuthLoginPollResponse, AuthLoginStartRequest, AuthLoginStartResponse,
    AuthParseRequest, AuthParseResponse, AuthRefreshRequest, AuthRefreshResponse, FrontendAuthRequest,
    FrontendAuthResponse, HostConfigSummary, ModelAlias,
};
use crate::callbacks::RequestScope;
use crate::gojson::{GoTime, Metadata, StringMap};
use crate::host::{Host, Record, normalize_provider};
use crate::rpc::CallError;

pub const ATTRIBUTE_PATH: &str = "path";
pub const ATTRIBUTE_SOURCE: &str = "source";
pub const ATTRIBUTE_SOURCE_BACKEND: &str = "source_backend";
pub const ATTRIBUTE_FILE_PRIORITY: &str = "file_priority";
pub const AUTH_SOURCE_FILE: &str = "file";

/// Go `coreauth.Auth` as a plugin produces it.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PluginAuth {
    pub provider: String,
    pub id: String,
    pub file_name: String,
    pub label: String,
    pub prefix: String,
    pub proxy_url: String,
    pub disabled: bool,
    /// The plugin's raw storage JSON (`pluginTokenStorage.rawJSON`).
    pub storage_json: Bytes,
    /// Host-managed metadata; always carries `type`.
    pub metadata: Metadata,
    pub attributes: StringMap,
    pub next_refresh_after: GoTime,
}

/// Go `CanonicalCredentialMetadataKey`.
pub fn canonical_metadata_key(key: &str) -> &str {
    match key {
        "api-key" => "api_key",
        "base-url" => "base_url",
        "disable-cooling" => "disable_cooling",
        "excluded-models" => "excluded_models",
        "fingerprint-profile" => "fingerprint_profile",
        "model-aliases" => "model_aliases",
        "openapi-location" => "openapi_location",
        "openapi-models" => "openapi_models",
        "proxy-url" => "proxy_url",
        "request-retry" => "request_retry",
        "request-scoped-errors" => "request_scoped_errors",
        "tool-prefix-disabled" => "tool_prefix_disabled",
        other => other,
    }
}

/// Go `NormalizeCredentialMetadata`: legacy keys move to their canonical name unless
/// the canonical one is already present.
pub fn normalize_credential_metadata(metadata: &mut serde_json::Map<String, Value>) {
    let legacy: Vec<String> = metadata
        .keys()
        .filter(|k| canonical_metadata_key(k) != k.as_str())
        .cloned()
        .collect();
    for key in legacy {
        let value = metadata.remove(&key).expect("listed above");
        metadata.entry(canonical_metadata_key(&key).to_owned()).or_insert(value);
    }
}

/// Go `authIDForPath`: relative to the auth dir when inside it, slash-separated.
pub fn auth_id_for_path(path: &str, auth_dir: &str) -> String {
    let path = path.trim();
    if path.is_empty() {
        return String::new();
    }
    let auth_dir = auth_dir.trim();
    let id = match crate::platform::rel(auth_dir, path) {
        Some(rel) if !auth_dir.is_empty() && !rel.is_empty() && !rel.starts_with("..") => rel,
        _ => path.to_owned(),
    };
    let id = crate::platform::clean(std::path::Path::new(&id))
        .to_string_lossy()
        .into_owned();
    // Go `filepath.ToSlash` only rewrites the OS separator.
    if cfg!(windows) {
        id.replace('\\', "/").go_lower()
    } else {
        id
    }
}

fn first_non_empty(values: &[&str]) -> String {
    values
        .iter()
        .map(|v| v.trim())
        .find(|v| !v.is_empty())
        .unwrap_or_default()
        .to_owned()
}

impl PluginAuth {
    /// Go `pluginAuthDataToCoreAuth`. `None` without a provider.
    pub fn from_auth_data(data: AuthData, path: &str, file_name: &str, auth_dir: &str) -> Option<Self> {
        let provider = normalize_provider(&data.provider);
        if provider.is_empty() {
            return None;
        }
        let mut metadata = data.metadata;
        metadata.insert("type".into(), provider.clone().into());
        let mut attributes = data.attributes;
        let path = path.trim();
        if !path.is_empty() {
            for (key, value) in [
                (ATTRIBUTE_PATH, path),
                (ATTRIBUTE_SOURCE, path),
                (ATTRIBUTE_SOURCE_BACKEND, AUTH_SOURCE_FILE),
            ] {
                if attributes.get(key).is_none_or(|v| v.is_empty()) {
                    attributes.insert(key.into(), value.into());
                }
            }
        }
        let file_name = first_non_empty(&[&data.file_name, file_name]);
        if !file_name.is_empty() && attributes.get(ATTRIBUTE_SOURCE).is_none_or(|v| v.is_empty()) {
            attributes.insert(ATTRIBUTE_SOURCE.into(), file_name.clone());
        }
        let mut id = data.id.trim().to_owned();
        if id.is_empty() {
            id = auth_id_for_path(&first_non_empty(&[path, &file_name]), auth_dir);
        }
        Some(Self {
            provider,
            id,
            file_name,
            label: data.label.trim().to_owned(),
            prefix: data.prefix.trim().to_owned(),
            proxy_url: data.proxy_url.trim().to_owned(),
            disabled: data.disabled,
            storage_json: data.storage_json,
            metadata,
            attributes,
            next_refresh_after: data.next_refresh_after,
        })
    }

    /// Go `mergedStorageJSON` (`pluginTokenStorage.RawJSON` / `SaveTokenToFile`): the raw
    /// storage object, overlaid with metadata, `type` set, legacy keys normalized,
    /// encoded with sorted keys.
    pub fn storage_payload(&self) -> Result<Bytes, String> {
        let mut out = serde_json::Map::new();
        if !self.storage_json.trim_ascii().is_empty() {
            match crate::gojson::parse(&self.storage_json).and_then(|n| n.to_value()) {
                Ok(Value::Object(map)) => out = map,
                Ok(Value::Null) => {}
                Ok(_) => return Err("decode plugin token storage: json: cannot unmarshal into map".into()),
                Err(e) => return Err(format!("decode plugin token storage: {e}")),
            }
        }
        for (key, value) in &self.metadata {
            out.insert(key.clone(), value.clone());
        }
        let provider = normalize_provider(&self.provider);
        if !provider.is_empty() {
            out.insert("type".into(), provider.into());
        }
        normalize_credential_metadata(&mut out);
        if out.is_empty() {
            return Err("plugin token storage payload is empty".into());
        }
        let mut payload = Vec::new();
        crate::gojson::encode_any(&Value::Object(out), &mut payload);
        Ok(Bytes::from(payload))
    }
}

impl PluginAuth {
    /// Go `MergeExistingAuthMetadata`: keys the auth does not set are taken from the
    /// existing file's JSON (token payload keys excepted), and its `disabled` unless
    /// the auth sets one.
    pub fn merge_existing(&mut self, existing: &serde_json::Map<String, Value>) {
        if existing.is_empty() {
            return;
        }
        if !self.metadata.contains_key("disabled")
            && let Some(Value::Bool(disabled)) = existing.get("disabled")
        {
            self.disabled = *disabled;
        }
        let meta = self.provider.trim().eq_ignore_ascii_case("meta");
        for (key, value) in existing {
            if is_auth_token_payload_key(key) {
                continue;
            }
            if meta
                && matches!(
                    canonical_metadata_key(key),
                    "api_key" | "dca_token" | "dca_expired" | "dca_expires_at"
                )
            {
                continue;
            }
            self.metadata.entry(key.clone()).or_insert_with(|| value.clone());
        }
    }

    /// Go `resolveAuthPath`: the file [`Self::save_file`] writes.
    pub fn file_path(&self, base_dir: &str) -> Result<String, String> {
        use std::path::Path;
        let base_dir = base_dir.trim();
        Ok(match self.attributes.get(ATTRIBUTE_PATH).map(|p| p.trim()) {
            Some(p) if !p.is_empty() => p.to_owned(),
            _ => match self.file_name.trim() {
                "" if self.id.is_empty() => return Err("auth filestore: missing id".into()),
                "" if Path::new(&self.id).is_absolute() => self.id.clone(),
                "" if base_dir.is_empty() => return Err("auth filestore: directory not configured".into()),
                "" => Path::new(base_dir).join(&self.id).to_string_lossy().into_owned(),
                name if Path::new(name).is_absolute() || base_dir.is_empty() => name.to_owned(),
                name => Path::new(base_dir).join(name).to_string_lossy().into_owned(),
            },
        })
    }

    /// Go `FileTokenStore.Save` for a plugin auth: the file under `base_dir` (or the
    /// auth's own path), written atomically with mode 0600 unless it already holds the
    /// same JSON. Returns the path; without `create` (Go's auth creation intent, which
    /// logins carry and command-line plugins do not) a disabled auth whose file does not
    /// exist is skipped with an empty path.
    pub fn save_file(mut self, base_dir: &str, create: bool) -> Result<String, String> {
        use std::path::Path;
        let mut metadata: serde_json::Map<String, Value> = std::mem::take(&mut self.metadata).into_iter().collect();
        normalize_credential_metadata(&mut metadata);
        // Go `ValidateAuthWeight`.
        if let Some(raw) = self.attributes.get("weight") {
            let weight = Value::String(raw.clone());
            cpa_core::config::credentials::parse_weight(&weight).map_err(|e| {
                format!(
                    "auth filestore: invalid attributes weight: {}",
                    crate::hostauth::go_weight_error(&weight, e)
                )
            })?;
        }
        if let Some(weight) = metadata.get("weight") {
            cpa_core::config::credentials::parse_weight(weight).map_err(|e| {
                format!(
                    "auth filestore: invalid metadata weight: {}",
                    crate::hostauth::go_weight_error(weight, e)
                )
            })?;
        }
        let path = self.file_path(base_dir)?;
        let path_ref = Path::new(&path);
        // Only a missing file skips the save; any other stat failure goes on to fail
        // in the write, as in Go.
        if self.disabled
            && !create
            && std::fs::metadata(path_ref).is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound)
        {
            return Ok(String::new());
        }
        if let Some(dir) = path_ref.parent().filter(|d| !d.as_os_str().is_empty()) {
            create_private_dir(dir).map_err(|e| {
                format!(
                    "auth filestore: create dir failed: {}",
                    crate::hostauth::path_error("mkdir", dir, &e)
                )
            })?;
        }
        metadata.insert("disabled".into(), self.disabled.into());
        self.metadata = metadata.into_iter().collect();
        // pluginTokenStorage.SaveTokenToFile.
        let payload = self.storage_payload()?;
        if let Ok(current) = std::fs::read(path_ref)
            && json_payload_equal(&current, &payload)
        {
            return Ok(path);
        }
        atomic_write(path_ref, &payload)?;
        Ok(path)
    }
}

/// Go `IsAuthTokenPayloadKey`.
fn is_auth_token_payload_key(key: &str) -> bool {
    matches!(
        key.trim().go_lower().as_str(),
        "access_token"
            | "refresh_token"
            | "id_token"
            | "session_id"
            | "expired"
            | "last_refresh"
            | "expires_in"
            | "timestamp"
            | "token_type"
            | "user_code"
            | "verification_uri"
            | "verification_uri_complete"
    )
}

/// `os.MkdirAll(dir, 0o700)`.
fn create_private_dir(dir: &std::path::Path) -> std::io::Result<()> {
    let mut builder = std::fs::DirBuilder::new();
    builder.recursive(true);
    #[cfg(unix)]
    std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700);
    builder.create(dir)
}

/// Go `jsonPayloadEqual`: both decode and the values are deeply equal (numbers as
/// float64).
fn json_payload_equal(a: &[u8], b: &[u8]) -> bool {
    fn same(a: &Value, b: &Value) -> bool {
        match (a, b) {
            (Value::Number(x), Value::Number(y)) => x.as_f64() == y.as_f64(),
            (Value::Array(x), Value::Array(y)) => x.len() == y.len() && x.iter().zip(y).all(|(x, y)| same(x, y)),
            (Value::Object(x), Value::Object(y)) => {
                x.len() == y.len() && x.iter().all(|(k, v)| y.get(k).is_some_and(|w| same(v, w)))
            }
            _ => a == b,
        }
    }
    let decode = |raw: &[u8]| crate::gojson::parse(raw).and_then(|n| n.to_value());
    matches!((decode(a), decode(b)), (Ok(a), Ok(b)) if same(&a, &b))
}

/// Go `atomicWriteFile`: a 0600 temp file beside `path`, renamed over it.
pub fn atomic_write(path: &std::path::Path, data: &[u8]) -> Result<(), String> {
    use std::io::Write as _;
    let dir = match path.parent() {
        Some(d) if !d.as_os_str().is_empty() => d,
        _ => std::path::Path::new("."),
    };
    create_private_dir(dir).map_err(|e| {
        format!(
            "create auth directory: {}",
            crate::hostauth::path_error("mkdir", dir, &e)
        )
    })?;
    let mut options = std::fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
    // Go `os.CreateTemp(dir, ".plugin-auth-*.tmp")`: a fresh name until one is unused.
    let mut attempt = 0u32;
    let (tmp_path, mut tmp) = loop {
        let nanos = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |d| d.subsec_nanos());
        let candidate = dir.join(format!(".plugin-auth-{}{nanos}{attempt}.tmp", std::process::id()));
        match options.open(&candidate) {
            Ok(file) => break (candidate, file),
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists && attempt < 10_000 => attempt += 1,
            Err(e) => {
                return Err(format!(
                    "create temp auth file: {}",
                    crate::hostauth::path_error("open", &candidate, &e)
                ));
            }
        }
    };
    let result = tmp
        .write_all(data)
        .map_err(|e| {
            format!(
                "write temp auth file: {}",
                crate::hostauth::path_error("write", &tmp_path, &e)
            )
        })
        .and_then(|()| {
            drop(tmp);
            std::fs::rename(&tmp_path, path).map_err(|e| format!("rename temp auth file: {e}"))
        });
    let _ = std::fs::remove_file(&tmp_path);
    result
}

impl PluginAuth {
    /// The credential as the host reads it back: Go `storageJSONFromAuth` takes the
    /// storage's `RawJSON`, which is [`PluginAuth::storage_payload`] (empty on error).
    pub fn view(&self) -> AuthView {
        AuthView {
            id: self.id.clone(),
            provider: self.provider.clone(),
            file_name: self.file_name.clone(),
            label: self.label.clone(),
            prefix: self.prefix.clone(),
            proxy_url: self.proxy_url.clone(),
            storage_json: self.storage_payload().unwrap_or_default(),
            metadata: self.metadata.clone(),
            attributes: self.attributes.clone(),
            next_refresh_after: self.next_refresh_after,
        }
    }
}

/// An existing credential as the plugin sees it (Go `coreauth.Auth` read side).
#[derive(Debug, Clone, Default)]
pub struct AuthView {
    pub id: String,
    pub provider: String,
    pub file_name: String,
    pub label: String,
    pub prefix: String,
    pub proxy_url: String,
    /// Go `storageJSONFromAuth`.
    pub storage_json: Bytes,
    pub metadata: Metadata,
    pub attributes: StringMap,
    pub next_refresh_after: GoTime,
}

/// Go `preserveFileAuthPriority`.
fn preserve_file_auth_priority(data: &mut AuthData, auth: &AuthView) {
    if auth.attributes.get(ATTRIBUTE_SOURCE_BACKEND).map(String::as_str) != Some(AUTH_SOURCE_FILE) {
        return;
    }
    for key in [
        ATTRIBUTE_PATH,
        ATTRIBUTE_SOURCE,
        ATTRIBUTE_SOURCE_BACKEND,
        ATTRIBUTE_FILE_PRIORITY,
    ] {
        if let Some(value) = auth.attributes.get(key) {
            data.attributes.insert(key.into(), value.clone());
        }
    }
    if auth.attributes.get(ATTRIBUTE_FILE_PRIORITY).map(String::as_str) != Some("true") {
        data.attributes.remove(ATTRIBUTE_FILE_PRIORITY);
        return;
    }
    match auth.attributes.get("priority") {
        Some(p) => data.attributes.insert("priority".into(), p.clone()),
        None => data.attributes.remove("priority"),
    };
    match auth.metadata.get("priority") {
        Some(p) => data.metadata.insert("priority".into(), p.clone()),
        None => data.metadata.remove("priority"),
    };
}

/// Go `RefreshAuth`'s defaults: the refreshed record inherits what the plugin left out.
pub fn refreshed_auth_data(resp: AuthRefreshResponse, auth: &AuthView) -> AuthData {
    let mut data = resp.auth;
    let fill = |v: &mut String, from: &str| {
        if v.trim().is_empty() {
            *v = from.to_owned();
        }
    };
    fill(&mut data.provider, &auth.provider);
    fill(&mut data.id, &auth.id);
    fill(&mut data.file_name, &auth.file_name);
    fill(&mut data.label, &auth.label);
    fill(&mut data.prefix, &auth.prefix);
    fill(&mut data.proxy_url, &auth.proxy_url);
    if data.metadata.is_empty() {
        data.metadata = auth.metadata.clone();
    }
    if data.attributes.is_empty() {
        data.attributes = auth.attributes.clone();
    } else {
        for (k, v) in &auth.attributes {
            data.attributes.entry(k.clone()).or_insert_with(|| v.clone());
        }
    }
    preserve_file_auth_priority(&mut data, auth);
    if data.storage_json.is_empty() {
        data.storage_json = auth.storage_json.clone();
    }
    data.next_refresh_after = if resp.next_refresh_after.is_zero() {
        auth.next_refresh_after
    } else {
        resp.next_refresh_after
    };
    data
}

/// Go `pluginOAuthModelAliases`.
fn plugin_oauth_aliases(cfg: &cpa_core::config::Config) -> BTreeMap<String, Vec<ModelAlias>> {
    let mut out: BTreeMap<String, Vec<ModelAlias>> = BTreeMap::new();
    for (provider, aliases) in cpa_core::registry::dynamic::global_aliases(cfg) {
        let key = normalize_provider(&provider);
        if key.is_empty() {
            continue;
        }
        for alias in aliases {
            let (name, value) = (alias.name.trim(), alias.alias.trim());
            if !name.is_empty() && !value.is_empty() {
                out.entry(key.clone()).or_default().push(ModelAlias {
                    name: name.into(),
                    alias: value.into(),
                });
            }
        }
    }
    out
}

/// Go `hostConfigSummary` for `cfg`.
pub(crate) fn host_config_summary_of(cfg: &cpa_core::config::Config) -> HostConfigSummary {
    let proxy_url = cfg
        .document
        .get("requests")
        .and_then(|r| r.get("proxy-url"))
        .map(crate::config::yaml_string)
        .unwrap_or_default();
    HostConfigSummary {
        auth_dir: cfg.auth_dir.to_string_lossy().trim().to_owned(),
        proxy_url: proxy_url.trim().to_owned(),
        force_model_prefix: cfg.routing.force_model_prefix,
        oauth_model_alias: plugin_oauth_aliases(cfg),
        excluded_models: cpa_core::config::credentials::oauth_excluded(cfg)
            .into_iter()
            .filter_map(|(k, v)| {
                let k = normalize_provider(&k);
                (!k.is_empty()).then_some((k, v))
            })
            .collect(),
    }
}

impl Host {
    /// Go `hostConfigSummary`.
    pub fn host_config_summary(&self) -> HostConfigSummary {
        self.config()
            .map(|cfg| host_config_summary_of(&cfg))
            .unwrap_or_default()
    }

    /// Go `AuthProviderIdentifiers`.
    pub fn auth_provider_identifiers(&self) -> Vec<String> {
        self.active_records()
            .into_iter()
            .filter(|r| r.plugin.caps.auth_provider && !self.is_fused(&r.id))
            .map(|r| normalize_provider(&r.plugin.auth_identifier))
            .filter(|id| !id.is_empty())
            .collect()
    }

    /// Go `authProviderRecord`: the first active auth provider for `provider`.
    pub fn auth_provider_record(&self, provider: &str) -> Option<Record> {
        let provider = normalize_provider(provider);
        if provider.is_empty() {
            return None;
        }
        self.active_records().into_iter().find(|r| {
            r.plugin.caps.auth_provider
                && !self.is_fused(&r.id)
                && normalize_provider(&r.plugin.auth_identifier) == provider
        })
    }

    /// Go `HasAuthProvider`.
    pub fn has_auth_provider(&self, provider: &str) -> bool {
        self.auth_provider_record(provider).is_some()
    }

    /// Go `ParseAuths`. With a provider, only that provider's plugin is asked; otherwise
    /// every auth provider in order until one handles the material or fails. Blocking:
    /// credential loading is synchronous, as Go's synthesizer is.
    pub fn parse_auths(&self, req: AuthParseRequest) -> ParseAuthsResult {
        self.parse_auths_for(req, false)
    }

    /// [`Self::parse_auths`]; `save`: for a `host.auth.save` (see [`SaveParse`]).
    pub(crate) fn parse_auths_for(&self, req: AuthParseRequest, save: bool) -> ParseAuthsResult {
        if !req.provider.trim().is_empty() {
            let Some(record) = self.auth_provider_record(&req.provider) else {
                return ParseAuthsResult::default();
            };
            return self.call_parse_auths(&record, req, save);
        }
        for record in self.active_records() {
            if !record.plugin.caps.auth_provider || self.is_fused(&record.id) {
                continue;
            }
            let result = self.call_parse_auths(&record, req.clone(), save);
            if result.error.is_some() || result.handled {
                return result;
            }
        }
        ParseAuthsResult::default()
    }

    /// Go `callParseAuths`: an RPC failure is not handled; invalid auth data in a handled
    /// answer is.
    fn call_parse_auths(&self, record: &Record, mut req: AuthParseRequest, save: bool) -> ParseAuthsResult {
        if self.is_fused(&record.id) || !self.record_current(record) {
            return ParseAuthsResult::default();
        }
        if req.host.auth_dir.is_empty() {
            req.host = self.host_config_summary();
        }
        req.provider = normalize_provider(&req.provider);
        let identifier = normalize_provider(&record.plugin.auth_identifier);
        if req.provider.is_empty() {
            req.provider = identifier.clone();
        }
        let parsing = Parsing::new(self, record, &req.path, save);
        // The thread entry goes on the thread that calls the plugin, where its
        // synchronous callbacks arrive (not this one under a current-thread runtime).
        let (instance, path) = (parsing.instance.clone(), parsing.path.clone());
        let resp = record
            .client
            .call_blocking_in(method::AUTH_PARSE, &crate::gojson::to_vec(&req), move || {
                ParsingHere::enter(instance, path)
            })
            .map_err(CallError::from)
            .and_then(|raw| crate::rpc::decode_response(method::AUTH_PARSE, &raw));
        let resp = self.fuse_on_panic(&record.id, method::AUTH_PARSE, resp);
        drop(parsing);
        let resp: AuthParseResponse = match resp {
            Ok(resp) => resp,
            Err(e) => return ParseAuthsResult::failed(false, e),
        };
        if !resp.handled {
            return ParseAuthsResult::default();
        }
        let datas = if resp.auths.is_empty() {
            vec![resp.auth]
        } else {
            resp.auths
        };
        let auth_dir = self.host_config_summary().auth_dir;
        let mut auths = Vec::with_capacity(datas.len());
        for mut data in datas {
            if data.provider.trim().is_empty() {
                data.provider = req.provider.clone();
            }
            if data.provider.trim().is_empty() {
                data.provider = identifier.clone();
            }
            if normalize_provider(&data.provider).is_empty() {
                let message = format!("auth provider {} returned auth without provider", record.id);
                return ParseAuthsResult::failed(true, CallError::Other(message));
            }
            match PluginAuth::from_auth_data(data, &req.path, &req.file_name, &auth_dir) {
                Some(parsed) => auths.push(parsed),
                None => {
                    let message = format!("auth provider {} returned invalid auth data", record.id);
                    return ParseAuthsResult::failed(true, CallError::Other(message));
                }
            }
        }
        ParseAuthsResult {
            auths,
            handled: true,
            error: None,
        }
    }

    /// Go `StartLogin`: `Ok(None)` when no plugin owns `provider`.
    pub async fn start_login(
        &self,
        provider: &str,
        base_url: &str,
        metadata: Metadata,
        scope: &RequestScope,
    ) -> Result<Option<AuthLoginStartResponse>, CallError> {
        let Some(record) = self.auth_provider_record(provider) else {
            return Ok(None);
        };
        if !self.record_current(&record) {
            return Ok(None);
        }
        let req = AuthLoginStartRequest {
            provider: normalize_provider(provider),
            base_url: base_url.trim().to_owned(),
            host: self.host_config_summary(),
            metadata,
        };
        self.call_with_callback(&record, method::AUTH_LOGIN_START, &req, scope)
            .await
            .map(Some)
    }

    /// Go `PollLogin`.
    pub async fn poll_login(
        &self,
        provider: &str,
        state: &str,
        metadata: Metadata,
        scope: &RequestScope,
    ) -> Result<Option<AuthLoginPollResponse>, CallError> {
        let Some(record) = self.auth_provider_record(provider) else {
            return Ok(None);
        };
        if !self.record_current(&record) {
            return Ok(None);
        }
        let req = AuthLoginPollRequest {
            provider: normalize_provider(provider),
            state: state.trim().to_owned(),
            host: self.host_config_summary(),
            metadata,
        };
        self.call_with_callback(&record, method::AUTH_LOGIN_POLL, &req, scope)
            .await
            .map(Some)
    }

    /// Go `RefreshAuth`: `Ok(None)` when no plugin owns the credential's provider.
    pub async fn refresh_auth(&self, auth: &AuthView, scope: &RequestScope) -> Result<Option<PluginAuth>, CallError> {
        let Some(record) = self.auth_provider_record(&auth.provider) else {
            return Ok(None);
        };
        if !self.record_current(&record) {
            return Ok(None);
        }
        let req = AuthRefreshRequest {
            auth_id: auth.id.clone(),
            auth_provider: auth.provider.clone(),
            storage_json: auth.storage_json.clone(),
            metadata: auth.metadata.clone(),
            attributes: auth.attributes.clone(),
            host: self.host_config_summary(),
        };
        let resp: AuthRefreshResponse = self
            .call_with_callback(&record, method::AUTH_REFRESH, &req, scope)
            .await?;
        let data = refreshed_auth_data(resp, auth);
        let path = auth.attributes.get(ATTRIBUTE_PATH).cloned().unwrap_or_default();
        let file_name = data.file_name.clone();
        PluginAuth::from_auth_data(data, &path, &file_name, &self.host_config_summary().auth_dir)
            .map(Some)
            .ok_or_else(|| CallError::Other("auth provider refresh returned invalid auth data".into()))
    }

    /// Go `RegisterFrontendAuthProviders`: `(access provider key, plugin ID)` for each
    /// active frontend auth provider in order, and the exclusive key (highest priority,
    /// then lowest ID) if any. Keys are `plugin:<id>:<identifier>`; identifiers are asked
    /// on every call, as Go does.
    pub async fn frontend_auth_providers(&self) -> (Vec<(String, String)>, Option<String>) {
        let mut providers = Vec::new();
        let mut exclusive: Option<(String, String, i64)> = None;
        for record in self.active_records() {
            if !record.plugin.caps.frontend_auth_provider || self.is_fused(&record.id) {
                continue;
            }
            let Some(key) = self.frontend_auth_key(&record).await else {
                continue;
            };
            if record.plugin.caps.frontend_auth_provider_exclusive {
                let better = match &exclusive {
                    None => true,
                    Some((_, id, priority)) => {
                        record.priority > *priority || (record.priority == *priority && record.id < *id)
                    }
                };
                if better {
                    exclusive = Some((key.clone(), record.id.clone(), record.priority));
                }
            }
            providers.push((key, record.id.clone()));
        }
        (providers, exclusive.map(|(key, _, _)| key))
    }

    /// Go `accessAdapter.Identifier` for a plugin: `plugin:<id>:<identifier>`, asking the
    /// plugin each time. `None` when the plugin is gone or answers empty.
    pub async fn frontend_auth_identifier(&self, plugin_id: &str) -> Option<String> {
        let record = self.record(plugin_id)?;
        if !record.plugin.caps.frontend_auth_provider {
            return None;
        }
        self.frontend_auth_key(&record).await
    }

    /// Go `accessAdapter.Identifier`.
    async fn frontend_auth_key(&self, record: &Record) -> Option<String> {
        let identifier = crate::rpc::identifier(&record.client, method::FRONTEND_AUTH_IDENTIFIER).await;
        let id = record.id.trim();
        (!id.is_empty() && !identifier.is_empty()).then(|| format!("plugin:{id}:{identifier}"))
    }

    /// Go `accessAdapter.Authenticate`: `None` is "not handled" (unavailable plugin, RPC
    /// failure, or not authenticated); the caller tries the next provider.
    pub async fn frontend_authenticate(
        &self,
        plugin_id: &str,
        req: &FrontendAuthRequest,
    ) -> Option<FrontendAuthOutcome> {
        let record = self.record(plugin_id)?;
        if !self.live(&record) {
            return None;
        }
        let resp: FrontendAuthResponse = self.call(&record, method::FRONTEND_AUTH_AUTHENTICATE, req).await.ok()?;
        if !resp.authenticated {
            return None;
        }
        let provider = self.frontend_auth_key(&record).await?;
        Some(FrontendAuthOutcome {
            provider,
            principal: resp.principal,
            metadata: resp.metadata,
        })
    }
}

/// Go `ParseAuths`' `([]*Auth, handled, error)`.
#[derive(Debug, Default)]
pub struct ParseAuthsResult {
    pub auths: Vec<PluginAuth>,
    pub handled: bool,
    pub error: Option<CallError>,
}

std::thread_local! {
    /// The `auth.parse` calls this thread is blocked in: the plugin instance and the auth
    /// file's cleaned absolute path. A native plugin's synchronous host callbacks arrive
    /// on the thread blocked in the plugin call (cgo runs an exported Go function locked
    /// to the calling C thread, and its C calls back on that thread).
    static PARSING_HERE: std::cell::RefCell<Vec<(std::sync::Arc<crate::client::CallbackInstance>, std::path::PathBuf)>> =
        const { std::cell::RefCell::new(Vec::new()) };
}

/// An `auth.parse` call in flight, for [`Host::save_parse`]. Dropped, unwinding included,
/// when the call returns.
struct Parsing<'a> {
    host: &'a Host,
    instance: std::sync::Arc<crate::client::CallbackInstance>,
    path: std::path::PathBuf,
    save: bool,
}

impl<'a> Parsing<'a> {
    fn new(host: &'a Host, record: &Record, path: &str, save: bool) -> Self {
        let instance = record.client.instance().clone();
        let path = cpa_core::config::credentials::absolute_clean_path(std::path::Path::new(path));
        crate::host::lock(&host.inner.auth_parses).push((instance.clone(), path.clone(), save));
        Self {
            host,
            instance,
            path,
            save,
        }
    }

    fn is(&self, instance: &std::sync::Arc<crate::client::CallbackInstance>, path: &std::path::Path) -> bool {
        std::sync::Arc::ptr_eq(instance, &self.instance) && path == self.path
    }
}

/// This thread's entry in [`PARSING_HERE`] while it calls the plugin's `auth.parse`.
/// Dropped, unwinding included, when the call returns.
struct ParsingHere {
    instance: std::sync::Arc<crate::client::CallbackInstance>,
    path: std::path::PathBuf,
}

impl ParsingHere {
    fn enter(instance: std::sync::Arc<crate::client::CallbackInstance>, path: std::path::PathBuf) -> Self {
        PARSING_HERE.with_borrow_mut(|here| here.push((instance.clone(), path.clone())));
        Self { instance, path }
    }
}

impl Drop for ParsingHere {
    fn drop(&mut self) {
        PARSING_HERE.with_borrow_mut(|here| {
            if let Some(at) = here
                .iter()
                .rposition(|(i, p)| std::sync::Arc::ptr_eq(i, &self.instance) && *p == self.path)
            {
                here.remove(at);
            }
        });
    }
}

impl Drop for Parsing<'_> {
    fn drop(&mut self) {
        let mut parses = crate::host::lock(&self.host.inner.auth_parses);
        if let Some(at) = parses
            .iter()
            .position(|(i, p, save)| self.is(i, p) && *save == self.save)
        {
            parses.swap_remove(at);
        }
    }
}

/// How a plugin's `host.auth.save` to an auth file treats the parse Go makes first.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SaveParse {
    /// Parse the saved file first, as Go does.
    Parse,
    /// The save comes from inside this plugin's `auth.parse` of the file, on the
    /// parsing thread: write without parsing again (Go recurses without bound). The
    /// parse's answer is published, and the watcher parses the saved bytes next.
    Nested,
    /// The save comes from another thread while a save of the file by this plugin is
    /// being parsed: refused, as it may be that parse recursing from a thread it
    /// spawned.
    Refused,
}

impl Host {
    /// How a `host.auth.save` from plugin `instance` to the auth file at `path` is parsed.
    pub(crate) fn save_parse(
        &self,
        instance: &std::sync::Arc<crate::client::CallbackInstance>,
        path: &std::path::Path,
    ) -> SaveParse {
        let path = cpa_core::config::credentials::absolute_clean_path(path);
        let same = |i: &std::sync::Arc<crate::client::CallbackInstance>, p: &std::path::PathBuf| {
            std::sync::Arc::ptr_eq(i, instance) && *p == path
        };
        if PARSING_HERE.with_borrow(|here| here.iter().any(|(i, p)| same(i, p))) {
            return SaveParse::Nested;
        }
        if crate::host::lock(&self.inner.auth_parses)
            .iter()
            .any(|(i, p, save)| *save && same(i, p))
        {
            return SaveParse::Refused;
        }
        SaveParse::Parse
    }
}

impl ParseAuthsResult {
    fn failed(handled: bool, error: CallError) -> Self {
        Self {
            auths: Vec::new(),
            handled,
            error: Some(error),
        }
    }
}

impl Host {
    /// This host as Go's `PluginAuthParser` for credential loading
    /// (`RegisterPluginAuthParser(pluginHost)`), held weakly. From then on, this host
    /// keeps cpa-core's auth-parser flag current, so without an auth provider loading
    /// skips the parser after one atomic load.
    pub fn auth_parser(&self) -> std::sync::Arc<dyn cpa_core::config::credentials::PluginAuthParser> {
        self.inner
            .auth_parser_installed
            .store(true, std::sync::atomic::Ordering::Release);
        let state = self.state();
        self.publish_hook_activity(&state, &self.snapshot().records);
        drop(state);
        // The same parser every time, so installing it again changes nothing.
        self.inner
            .auth_parser
            .get_or_init(|| std::sync::Arc::new(AuthParser(std::sync::Arc::downgrade(&self.inner))))
            .clone()
    }
}

struct AuthParser(std::sync::Weak<crate::host::Inner>);

impl cpa_core::config::credentials::PluginAuthParser for AuthParser {
    fn parse_auths(
        &self,
        req: &cpa_core::config::credentials::ParseRequest<'_>,
    ) -> cpa_core::config::credentials::Parsed {
        use cpa_core::config::credentials::Parsed;
        let Some(inner) = self.0.upgrade() else {
            return Parsed::NotHandled;
        };
        let result = Host::from_inner(inner).parse_auths(AuthParseRequest {
            provider: req.provider.to_owned(),
            path: req.path.display().to_string(),
            file_name: req.file_name.to_owned(),
            raw_json: Bytes::copy_from_slice(req.raw_json),
            ..Default::default()
        });
        // Go falls back to the built-in synthesizer on any parse error.
        if result.error.is_some() || !result.handled {
            return Parsed::NotHandled;
        }
        Parsed::Handled(result.auths.into_iter().map(PluginAuth::into_parsed).collect())
    }
}

impl PluginAuth {
    /// The auth as credential loading takes it. The credential keeps one JSON document,
    /// the one Go saves for it (`pluginTokenStorage`): the plugin's storage overlaid
    /// with its metadata.
    pub(crate) fn into_parsed(self) -> cpa_core::config::credentials::ParsedAuth {
        let metadata = self
            .storage_payload()
            .ok()
            .and_then(|payload| serde_json::from_slice(&payload).ok())
            .unwrap_or_else(|| self.metadata.clone().into_iter().collect());
        cpa_core::config::credentials::ParsedAuth {
            provider: self.provider,
            id: self.id,
            file_name: self.file_name,
            label: self.label,
            prefix: self.prefix,
            proxy_url: self.proxy_url,
            disabled: self.disabled,
            metadata,
            attributes: self.attributes,
            next_refresh_after: self.next_refresh_after.0.map(std::time::SystemTime::from),
        }
    }
}

/// A frontend request a plugin accepted (Go `sdkaccess.Result`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FrontendAuthOutcome {
    pub provider: String,
    pub principal: String,
    pub metadata: StringMap,
}

#[cfg(test)]
mod tests {
    // File modes are only checked where they exist.
    #[cfg(unix)]
    #[test]
    fn save_file_follows_go_filestore() {
        use std::os::unix::fs::PermissionsExt as _;
        let dir = std::env::temp_dir().join(format!("cpa-plugin-save-file-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let base = dir.to_string_lossy().into_owned();
        let auth = |file: &str, disabled: bool| {
            let data = AuthData {
                provider: "Rec".into(),
                file_name: file.into(),
                disabled,
                storage_json: Bytes::from_static(br#"{"k":1,"base-url":"u"}"#),
                ..Default::default()
            };
            PluginAuth::from_auth_data(data, "", "", &base).unwrap()
        };
        // A disabled auth without a file is not created.
        assert_eq!(auth("gone.json", true).save_file(&base, false).unwrap(), "");
        assert!(!dir.join("gone.json").exists());
        // A new auth: merged storage, canonical keys, mode 0600.
        let path = auth("new.json", false).save_file(&base, false).unwrap();
        assert_eq!(path, dir.join("new.json").to_string_lossy());
        assert_eq!(
            std::fs::read_to_string(&path).unwrap(),
            r#"{"base_url":"u","disabled":false,"k":1,"type":"rec"}"#
        );
        assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
        // Equal JSON is left as written.
        std::fs::write(
            &path,
            r#"{ "k": 1.0, "type": "rec", "disabled": false, "base_url": "u" }"#,
        )
        .unwrap();
        auth("new.json", false).save_file(&base, false).unwrap();
        assert!(std::fs::read_to_string(&path).unwrap().starts_with("{ "));
        // A stat failure other than "not found" goes on to fail in the write, as in Go.
        // SAFETY: geteuid has no preconditions.
        if unsafe { libc::geteuid() } != 0 {
            let locked = dir.join("locked");
            std::fs::create_dir_all(&locked).unwrap();
            std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o000)).unwrap();
            let e = auth("locked/x.json", true).save_file(&base, false).unwrap_err();
            std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o700)).unwrap();
            assert!(e.starts_with("create temp auth file: open "), "{e}");
            assert!(e.ends_with(": permission denied"), "{e}");
        }
        let _ = std::fs::remove_dir_all(&dir);
    }

    use super::*;

    /// Go `pluginAuthDataToCoreAuth` and `mergedStorageJSON`.
    #[test]
    fn auth_data_conversion_and_storage_follow_go() {
        let data = AuthData {
            provider: " Plugin-X ".into(),
            storage_json: Bytes::from_static(br#"{"token":"t","priority":1,"base-url":"old"}"#),
            metadata: [
                ("base-url".to_owned(), Value::from("u")),
                ("email".to_owned(), Value::from("e")),
            ]
            .into(),
            ..Default::default()
        };
        let auth = PluginAuth::from_auth_data(data, "/auth/sub/a.json", "", "/auth").unwrap();
        assert_eq!(auth.provider, "plugin-x");
        assert_eq!(auth.id, "sub/a.json");
        assert_eq!(auth.attributes["source_backend"], "file");
        assert_eq!(auth.attributes["source"], "/auth/sub/a.json");
        assert_eq!(auth.metadata["type"], "plugin-x");
        let payload = auth.storage_payload().unwrap();
        assert_eq!(
            &payload[..],
            br#"{"base_url":"u","email":"e","priority":1,"token":"t","type":"plugin-x"}"#
        );
        assert!(PluginAuth::from_auth_data(AuthData::default(), "", "", "").is_none());
        // Expected values printed by Go's authIDForPath (filepath.Rel, Clean, ToSlash).
        for (path, dir, want) in [
            ("/auth/sub/a.json", "/auth", "sub/a.json"),
            ("/auth/../other/t.json", "/auth", "/other/t.json"),
            ("/elsewhere/x.json", "/auth", "/elsewhere/x.json"),
            ("/auth", "/auth", "."),
            ("/auth/", "/auth/", "."),
            ("rel/a.json", "rel", "a.json"),
            ("rel/a.json", "/auth", "rel/a.json"),
            ("/auth/..x/a.json", "/auth", "/auth/..x/a.json"),
            ("/auth/a\\b.json", "/auth", "a\\b.json"),
            ("/auth//x/./y.json", "/auth/", "x/y.json"),
            ("a.json", "", "a.json"),
            ("/a/b/c", "/a/x/y", "/a/b/c"),
            ("x/y", ".", "x/y"),
            ("../q", ".", "../q"),
        ] {
            assert_eq!(auth_id_for_path(path, dir), want, "{path} in {dir}");
        }
        for (targ, base, want) in [
            ("/a/b/c", "/a/x/y", Some("../../b/c")),
            ("rel/a.json", "/auth", None),
            ("/auth/../other/t.json", "/auth", Some("../other/t.json")),
            ("x/y", ".", Some("x/y")),
            (".", "a", Some("..")),
            (".", "a/b", Some("../..")),
            ("a/c", "a/b", Some("../c")),
        ] {
            assert_eq!(crate::platform::rel(base, targ).as_deref(), want, "{targ} from {base}");
        }
    }
}