gent-link 0.2.5

Connect Codex and Claude Code chats across machines with the Gent CLI and desktop companion.
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
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
//! Native account login. Service credentials never grant cluster key authority.
mod entitlements;
pub(crate) mod notifications;
mod pairing;
pub mod renewal;
pub(crate) mod source;
mod staging_access;
use crate::managed_file::{self, Snapshot};
use anyhow::{Context, Result, bail, ensure};
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use clap::Subcommand;
use gent_daemon::backend::{ACCOUNT, RELAY};
use gent_protocol::service::{
    AuthConfig, CALLBACK_PATH, CodeExchange, ServiceCluster, ServiceSession, TokenExchange,
    TransportConfig, TransportEnrollment, TransportSession,
};
use reqwest::{Client, Method, StatusCode, Url};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use std::{
    collections::BTreeMap,
    path::Path,
    time::{Duration, SystemTime, UNIX_EPOCH},
};
use tokio::{
    io::{AsyncReadExt, AsyncWriteExt},
    net::{TcpListener, TcpStream},
    time::{Instant, timeout},
};

const DEFAULT_SERVICE: &str = ACCOUNT.stable;
const STAGING_SERVICE: &str = "https://staging.gent.link";

#[derive(Subcommand)]
pub enum Command {
    Status,
    /// Open the billing page for the signed-in Gent account.
    Billing {
        /// Print a private five-minute sign-in link for another browser.
        #[arg(long)]
        no_browser: bool,
    },
    /// Renew an active session near expiry; never signs in or revives a revoked session.
    Renew,
    /// Sign in with GitHub. SSH sessions use the device-code flow by default.
    Login {
        #[arg(long)]
        headless: bool,
        /// Print the browser URL without opening it; still uses a local callback.
        #[arg(long, conflicts_with = "headless")]
        no_browser: bool,
        /// Alternate Gent service origin. HTTPS is required except on loopback.
        #[arg(long)]
        service: Option<String>,
    },
    /// Stop using this session and revoke it at the service. Retry if offline.
    Logout,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Saved {
    version: u8,
    service: String,
    token: String,
    phase: Phase,
    session: Option<ServiceSession>,
}
#[derive(Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
enum Phase {
    Pending,
    Active,
    Revoking,
}

pub(crate) fn origin(value: &str) -> Result<Url> {
    let url = Url::parse(value).context("invalid service origin")?;
    ensure!(
        url.username().is_empty()
            && url.password().is_none()
            && url.query().is_none()
            && url.fragment().is_none()
            && url.path() == "/",
        "service must be an origin without credentials, path, query or fragment"
    );
    ensure!(
        url.scheme() == "https" || loopback(&url),
        "service requires HTTPS except on loopback"
    );
    Ok(url)
}
pub(crate) fn loopback(url: &Url) -> bool {
    url.scheme() == "http" && matches!(url.host_str(), Some("127.0.0.1" | "[::1]"))
}
fn random() -> Result<[u8; 32]> {
    let mut bytes = [0; 32];
    getrandom::fill(&mut bytes).context("secure randomness unavailable")?;
    Ok(bytes)
}
pub(crate) fn now() -> Result<u64> {
    Ok(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs())
}
fn candidate() -> Result<String> {
    credential("gent_s1")
}
pub(crate) fn transport_candidate() -> Result<String> {
    credential("gent_t1")
}
pub(crate) fn invitation_candidate() -> Result<String> {
    credential("gent_j1")
}
fn credential(prefix: &str) -> Result<String> {
    let secret = random()?
        .iter()
        .map(|b| format!("{b:02x}"))
        .collect::<String>();
    Ok(format!("{prefix}.{}.{secret}", now()?))
}
fn read(path: &Path) -> Result<Option<Saved>> {
    let snapshot = Snapshot::read(path)?;
    snapshot.private()?;
    let value: Option<Saved> = match snapshot.text {
        Some(text) => serde_json::from_str(&text)
            .map_err(|_| anyhow::anyhow!("invalid saved Gent authentication"))?,
        None => None,
    };
    if let Some(value) = &value {
        ensure!(
            value.version == 1
                && gent_protocol::service::session_created_at(&value.token).is_some(),
            "invalid saved Gent authentication"
        );
        origin(&value.service)?;
    }
    Ok(value)
}
fn save(path: &Path, value: Option<&Saved>) -> Result<()> {
    let snapshot = Snapshot::read(path)?;
    snapshot.private()?;
    snapshot.write(&(serde_json::to_string(&value)? + "\n"))
}
fn client() -> Result<Client> {
    Ok(Client::builder()
        .redirect(reqwest::redirect::Policy::none())
        .connect_timeout(Duration::from_secs(10))
        .timeout(Duration::from_secs(20))
        .user_agent("gent-link")
        .build()?)
}

async fn response(request: reqwest::RequestBuilder) -> Result<(StatusCode, Value)> {
    response_limit(request, 64 * 1024).await
}

async fn response_limit(
    request: reqwest::RequestBuilder,
    limit: usize,
) -> Result<(StatusCode, Value)> {
    let (client, request) = request.build_split();
    let mut request = request.map_err(|_| anyhow::anyhow!("invalid authentication request"))?;
    staging_access::authorize(&mut request, std::env::var_os)?;
    let mut response = client
        .execute(request)
        .await
        .map_err(|_| anyhow::anyhow!("could not reach the authentication service"))?;
    let status = response.status();
    let mut bytes = Vec::new();
    while let Some(chunk) = response
        .chunk()
        .await
        .map_err(|_| anyhow::anyhow!("could not read the authentication response"))?
    {
        ensure!(
            bytes.len().saturating_add(chunk.len()) <= limit,
            "authentication response too large"
        );
        bytes.extend_from_slice(&chunk);
    }
    let value = if bytes.is_empty() {
        Value::Null
    } else {
        serde_json::from_slice(&bytes)
            .map_err(|_| anyhow::anyhow!("invalid authentication response"))?
    };
    Ok((status, value))
}
fn success(status: StatusCode, value: Value) -> Result<Value> {
    if status == StatusCode::FORBIDDEN && value["error"] == "free_cluster_node_limit" {
        bail!("Free includes one node per cluster. Manage your plan with gent auth billing.");
    }
    ensure!(
        status.is_success(),
        "authentication request failed (HTTP {})",
        status.as_u16()
    );
    Ok(value)
}
pub(crate) fn account_pin(raw: &str) -> Result<Url> {
    Ok(ACCOUNT.identity(origin(raw)?))
}

fn account_endpoint(home: &Path, raw: &str) -> Result<Url> {
    ACCOUNT.destination(home, origin(raw)?)
}

async fn service_session(
    client: &Client,
    endpoint: &Url,
    saved: &Saved,
) -> Result<Option<ServiceSession>> {
    let (status, value) = response(
        client
            .get(endpoint.join("v1/auth/session")?)
            .bearer_auth(&saved.token),
    )
    .await?;
    if status == StatusCode::UNAUTHORIZED {
        return Ok(None);
    }
    Ok(Some(
        serde_json::from_value(success(status, value)?)
            .map_err(|_| anyhow::anyhow!("invalid Gent session response"))?,
    ))
}

/// Holds the login mutation lock through setup. Account credentials are only
/// sent to the selected account backend, never to the discovered relay.
pub(crate) struct Service {
    _lock: std::fs::File,
    saved: Saved,
    client: Client,
    endpoint: Url,
    pub session: ServiceSession,
}
impl Service {
    pub async fn open(home: &Path) -> Result<Self> {
        Self::open_pinned(home, None).await
    }

    pub async fn open_pinned(
        home: &Path,
        pin: Option<&gent_daemon::config::ServiceConfig>,
    ) -> Result<Self> {
        let directory = source::directory(home)?;
        let directory = &directory;
        managed_file::directory(directory)?;
        let lock = managed_file::lock(&directory.join("session.lock"))?;
        let saved =
            read(&directory.join("session.json"))?.context("sign in first with gent auth login")?;
        ensure!(
            saved.phase == Phase::Active,
            "finish gent auth login or gent auth logout before configuring a service cluster"
        );
        if let Some(pin) = pin {
            ensure!(
                account_pin(&saved.service)? == account_pin(&pin.origin)?
                    && saved
                        .session
                        .as_ref()
                        .is_some_and(|s| s.account_id == pin.account_id),
                "account login does not match the node removal's pinned service and account"
            );
        }
        let client = client()?;
        let endpoint = account_endpoint(home, &saved.service)?;
        let session = service_session(&client, &endpoint, &saved)
            .await?
            .context("session is no longer active; run gent auth login")?;
        ensure!(
            saved
                .session
                .as_ref()
                .is_some_and(|s| s.account_id == session.account_id),
            "saved account does not match the active session"
        );
        Ok(Self {
            _lock: lock,
            saved,
            client,
            endpoint,
            session,
        })
    }

    pub fn origin(&self) -> Result<Url> {
        account_pin(&self.saved.service)
    }
    pub fn session_hash(&self) -> String {
        format!("{:x}", Sha256::digest(self.saved.token.as_bytes()))
    }
    pub async fn revoke_node(&self, cluster: &str, node: &str) -> Result<()> {
        for id in [cluster, node] {
            ensure!(
                uuid::Uuid::parse_str(id).is_ok_and(|u| !u.is_nil() && u.to_string() == id),
                "invalid node removal identity"
            );
        }
        let (status, _) = response(
            self.request(
                Method::DELETE,
                &format!("v1/clusters/{cluster}/nodes/{node}"),
            )?
            .bearer_auth(&self.saved.token),
        )
        .await?;
        ensure!(
            status == StatusCode::NO_CONTENT,
            "service node revocation was not confirmed (HTTP {})",
            status.as_u16()
        );
        Ok(())
    }
    fn request(&self, method: Method, path: &str) -> Result<reqwest::RequestBuilder> {
        Ok(self.client.request(method, self.endpoint.join(path)?))
    }
    pub async fn relay_endpoint(&self) -> Result<String> {
        let (status, value) = response(
            self.request(Method::GET, "v1/transport/config")?
                .bearer_auth(&self.saved.token),
        )
        .await?;
        let config: TransportConfig = serde_json::from_value(success(status, value)?)
            .map_err(|_| anyhow::anyhow!("invalid relay configuration response"))?;
        let relay = origin(&config.relay_endpoint)?;
        ensure!(
            relay.scheme() == "https" || loopback(&self.origin()?),
            "an HTTPS account service cannot select an insecure relay"
        );
        Ok(if ACCOUNT.contains(&self.endpoint) {
            RELAY.identity(relay)
        } else {
            relay
        }
        .into())
    }
    pub async fn initialize_configuration(
        &self,
        bootstrap: &gent_protocol::configuration_log::Bootstrap,
    ) -> Result<()> {
        let (status, value) = response(
            self.request(
                Method::POST,
                &format!("v1/clusters/{}/configuration", bootstrap.cluster),
            )?
            .bearer_auth(&self.saved.token)
            .json(bootstrap),
        )
        .await?;
        let saved: gent_protocol::configuration_log::Bootstrap =
            serde_json::from_value(success(status, value)?)?;
        ensure!(saved == *bootstrap, "configuration bootstrap changed");
        Ok(())
    }
    pub async fn reserve_cluster(&self, id: &str) -> Result<()> {
        let (status, value) = response(
            self.request(Method::PUT, &format!("v1/clusters/{id}"))?
                .bearer_auth(&self.saved.token),
        )
        .await?;
        let cluster: ServiceCluster = serde_json::from_value(success(status, value)?)
            .map_err(|_| anyhow::anyhow!("invalid service cluster response"))?;
        ensure!(
            cluster.id == id && cluster.role == "owner",
            "unexpected service cluster"
        );
        Ok(())
    }
    pub async fn require_cluster_owner(&self, id: &str) -> Result<()> {
        let (status, value) = response(
            self.request(Method::GET, "v1/clusters")?
                .bearer_auth(&self.saved.token),
        )
        .await?;
        let clusters: Vec<ServiceCluster> = serde_json::from_value(success(status, value)?)
            .map_err(|_| anyhow::anyhow!("invalid service cluster list"))?;
        ensure!(
            clusters.iter().any(|c| c.id == id && c.role == "owner"),
            "the signed-in account does not own this service cluster"
        );
        Ok(())
    }
    pub async fn register_invitation(
        &self,
        offer: &gent_crypto::invitation::SignedOffer,
    ) -> Result<()> {
        use gent_protocol::service::invitations::{Record, Registration};
        let offer = &offer.offer;
        self.check_invitation_service(&offer.service)?;
        let (status, value) = response(
            self.request(
                Method::PUT,
                &format!(
                    "v1/clusters/{}/invitations/{}",
                    offer.anchor.cluster_id, offer.id
                ),
            )?
            .bearer_auth(&self.saved.token)
            .json(&Registration {
                owner_node_id: offer.issuer_id.clone(),
                routing_hash: offer
                    .routing_digest
                    .iter()
                    .map(|b| format!("{b:02x}"))
                    .collect(),
                expires_at: offer.expires_at,
            }),
        )
        .await?;
        let record: Record = serde_json::from_value(success(status, value)?)
            .map_err(|_| anyhow::anyhow!("invalid invitation registration response"))?;
        ensure!(
            record.id == offer.id
                && record.cluster_id == offer.anchor.cluster_id
                && record.owner_node_id == offer.issuer_id
                && record.expires_at == offer.expires_at,
            "invitation registration scope changed"
        );
        Ok(())
    }
    pub fn check_invitation_service(
        &self,
        binding: &gent_crypto::invitation::ServiceBinding,
    ) -> Result<()> {
        binding.validate()?;
        ensure!(
            self.origin()? == account_pin(&binding.account_origin)?
                && self.session.account_id == binding.account_id,
            "sign into the invitation's Gent service and account before continuing"
        );
        Ok(())
    }
    pub async fn exchange_invitation(
        &self,
        invitation: &gent_crypto::invitation::Invitation,
        token: &str,
    ) -> Result<()> {
        use gent_protocol::service::invitations::{Exchange, Scope, Side};
        let offer = &invitation.offer().offer;
        self.check_invitation_service(&offer.service)?;
        let (status, value) = response(
            self.request(
                Method::POST,
                &format!("v1/invitations/{}/session", offer.id),
            )?
            .bearer_auth(&self.saved.token)
            .json(&Exchange {
                routing_secret: invitation
                    .expose_routing_secret()
                    .iter()
                    .map(|b| format!("{b:02x}"))
                    .collect(),
                join_token: token.into(),
            }),
        )
        .await?;
        let scope: Scope = serde_json::from_value(success(status, value)?)
            .map_err(|_| anyhow::anyhow!("invalid invitation session response"))?;
        ensure!(
            scope.invitation_id == offer.id
                && scope.cluster_id == offer.anchor.cluster_id
                && scope.owner_node_id == offer.issuer_id
                && scope.side == Side::Join
                && scope.deadline == offer.expires_at
                && scope.expires_at <= scope.deadline
                && scope.expires_at > now()?,
            "invitation session scope changed or expired"
        );
        Ok(())
    }
    pub async fn revoke_invitation(&self, cluster: &str, id: &str) -> Result<()> {
        ensure!(
            uuid::Uuid::parse_str(cluster).is_ok_and(|u| u.to_string() == cluster)
                && gent_protocol::service::invitations::issued_at(id).is_some(),
            "invalid invitation identity"
        );
        let (status, value) = response(
            self.request(
                Method::DELETE,
                &format!("v1/clusters/{cluster}/invitations/{id}"),
            )?
            .bearer_auth(&self.saved.token),
        )
        .await?;
        success(status, value)?;
        Ok(())
    }
    pub async fn transport_session(&self, token: &str) -> Result<Option<TransportSession>> {
        let (status, value) = response(
            self.request(Method::GET, "v1/transport/session")?
                .bearer_auth(token),
        )
        .await?;
        if status == StatusCode::UNAUTHORIZED {
            return Ok(None);
        }
        Ok(Some(
            serde_json::from_value(success(status, value)?)
                .map_err(|_| anyhow::anyhow!("invalid transport session response"))?,
        ))
    }
    /// None means the service's admission window is closed. Only that response
    /// permits replacing an unconfirmed candidate; a lost response does not.
    pub async fn enroll(
        &self,
        cluster: &str,
        node: &str,
        token: &str,
    ) -> Result<Option<TransportSession>> {
        let (status, value) = response(
            self.request(Method::PUT, &format!("v1/clusters/{cluster}/nodes/{node}"))?
                .bearer_auth(&self.saved.token)
                .json(&TransportEnrollment {
                    transport_token: token.into(),
                }),
        )
        .await?;
        if status == StatusCode::BAD_REQUEST && value["error"] == "transport_candidate_expired" {
            return Ok(None);
        }
        Ok(Some(
            serde_json::from_value(success(status, value)?)
                .map_err(|_| anyhow::anyhow!("invalid transport enrollment response"))?,
        ))
    }
}

pub(crate) fn desktop_account(home: &Path) -> Result<Value> {
    let directory = source::directory(home)?;
    let Some(saved) = read(&directory.join("session.json"))? else {
        return Ok(json!({"state":"Signed out"}));
    };
    let now = now()?;
    let state = match saved.phase {
        Phase::Revoking => "Sign-out pending",
        Phase::Pending => "Sign-in pending",
        Phase::Active if saved.session.as_ref().is_some_and(|s| s.expires_at > now) => "Signed in",
        Phase::Active => "Expired",
    };
    Ok(json!({"state":state,"login":saved.session.map(|s| s.github_login)}))
}

pub(crate) async fn desktop_login(home: &Path) -> Result<Value> {
    let directory = source::directory(home)?;
    let service = if gent_daemon::config::path(home).try_exists()? {
        crate::node_home::inspect(home)?.service.map(|s| s.origin)
    } else {
        None
    };
    execute_at(
        &directory,
        Command::Login {
            headless: false,
            no_browser: false,
            service,
        },
        true,
    )
    .await
}

fn billing_page(service: &Url) -> Result<Url> {
    match service.origin().ascii_serialization().as_str() {
        DEFAULT_SERVICE => Ok(Url::parse("https://gent.link/account")?),
        authority if authority == ACCOUNT.beta => Ok(Url::parse("https://beta.gent.link/account")?),
        STAGING_SERVICE => Ok(service.join("/account")?),
        _ => bail!("hosted billing requires a gent.link account"),
    }
}

fn browser_command() -> std::process::Command {
    let mut command = std::process::Command::new(if cfg!(target_os = "macos") {
        "open"
    } else {
        "xdg-open"
    });
    for name in staging_access::ENV_VARS {
        command.env_remove(name);
    }
    command
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null());
    command
}

async fn billing(home: &Path, no_browser: bool) -> Result<Value> {
    let service = Service::open(home).await?;
    let mut url = billing_page(&service.endpoint)?;
    let (status, value) = response(
        service
            .request(Method::POST, "v1/billing/browser/grant")?
            .bearer_auth(&service.saved.token),
    )
    .await?;
    let value = success(status, value)?;
    let grant = value["grant"]
        .as_str()
        .filter(|s| {
            s.len() == 64
                && s.bytes()
                    .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
        })
        .context("invalid account handoff")?;
    url.set_fragment(Some(grant));
    if no_browser {
        return Ok(
            json!({"status":"billing_link","url":url.as_str(),"expires_at":value["expires_at"]}),
        );
    }
    let status = tokio::process::Command::from(browser_command())
        .arg(url.as_str())
        .stdin(std::process::Stdio::null())
        .status()
        .await
        .context("could not open billing; run gent auth billing --no-browser")?;
    ensure!(
        status.success(),
        "could not open billing; run gent auth billing --no-browser"
    );
    Ok(json!({"status":"billing_opened"}))
}

pub async fn execute(home: &Path, command: Command) -> Result<Value> {
    if let Command::Billing { no_browser } = command {
        return billing(home, no_browser).await;
    }
    if matches!(command, Command::Renew) {
        return renewal::renew(home, None).await;
    }
    let directory = source::directory(home)?;
    let result = execute_at(&directory, command, false).await?;
    Ok(result)
}

/// Diagnostics never lock, renew, sign in, persist or print account credentials.
pub(crate) async fn diagnose(
    home: &Path,
    pin: Option<&gent_daemon::config::ServiceConfig>,
    online: bool,
) -> Result<Value> {
    let directory = source::directory(home)?;
    let Some(saved) = read(&directory.join("session.json"))? else {
        return Ok(json!({"status":"signed_out","source":"local"}));
    };
    let check = |session: &ServiceSession| -> Result<()> {
        if let Some(pin) = pin {
            ensure!(
                account_pin(&saved.service)? == account_pin(&pin.origin)?
                    && session.account_id == pin.account_id,
                "account state does not match this cluster"
            );
        }
        Ok(())
    };
    if let Some(pin) = pin {
        ensure!(
            account_pin(&saved.service)? == account_pin(&pin.origin)?,
            "service pin mismatch"
        );
    }
    if let Some(session) = &saved.session {
        check(session)?;
    }
    match saved.phase {
        Phase::Pending => return Ok(json!({"status":"login_pending","source":"local"})),
        Phase::Revoking => return Ok(json!({"status":"revocation_pending","source":"local"})),
        Phase::Active => {}
    }
    let cached = saved
        .session
        .as_ref()
        .context("active session cache missing")?;
    if !online {
        return Ok(
            json!({"status":if cached.expires_at > now()? {"cached"} else {"expired"},
            "source":"local","expires_at":cached.expires_at}),
        );
    }
    let endpoint = account_endpoint(home, &saved.service)?;
    match timeout(
        Duration::from_secs(5),
        service_session(&client()?, &endpoint, &saved),
    )
    .await
    {
        Ok(Ok(Some(session))) => {
            check(&session)?;
            Ok(
                json!({"status":if session.expires_at > now()? {"active"} else {"expired"},
                "source":"service","expires_at":session.expires_at}),
            )
        }
        Ok(Ok(None)) => Ok(json!({"status":"signed_out","source":"service"})),
        _ => Ok(json!({"status":"unavailable","source":"service"})),
    }
}

async fn execute_at(directory: &Path, command: Command, desktop: bool) -> Result<Value> {
    let home = directory.parent().context("missing account home")?;
    let path = directory.join("session.json");
    let client = client()?;
    if matches!(command, Command::Status) {
        let Some(saved) = read(&path)? else {
            return Ok(json!({"status":"signed_out"}));
        };
        if saved.phase == Phase::Revoking {
            return Ok(json!({"status":"revocation_pending","service":saved.service}));
        }
        let endpoint = account_endpoint(home, &saved.service)?;
        return match service_session(&client, &endpoint, &saved).await {
            Ok(Some(session)) => Ok(
                json!({"status":"active","service":saved.service,"endpoint":endpoint.as_str(),"account":session}),
            ),
            Ok(None) => Ok(
                json!({"status":"signed_out","service":saved.service,"endpoint":endpoint.as_str()}),
            ),
            Err(_) => Ok(
                json!({"status":"unavailable","service":saved.service,"endpoint":endpoint.as_str(),"cached_account":saved.session}),
            ),
        };
    }
    managed_file::directory(directory)?;
    let _lock = managed_file::lock(&directory.join("session.lock"))?;
    let mut saved = read(&path)?;
    if matches!(command, Command::Logout) {
        gent_daemon::entitlements::clear(home)?;
        let Some(saved) = &mut saved else {
            return Ok(json!({"status":"signed_out"}));
        };
        saved.phase = Phase::Revoking;
        save(&path, Some(saved))?;
        let endpoint = account_endpoint(home, &saved.service)?;
        let outcome = response(
            client
                .request(Method::DELETE, endpoint.join("v1/auth/session")?)
                .bearer_auth(&saved.token),
        )
        .await;
        if !matches!(outcome, Ok((StatusCode::NO_CONTENT, _))) {
            return Ok(json!({"status":"revocation_pending","service":saved.service}));
        }
        save(&path, None)?;
        return Ok(json!({"status":"signed_out"}));
    }
    let Command::Login {
        headless,
        no_browser,
        service,
    } = command
    else {
        unreachable!()
    };
    let configured =
        if service.is_none() && saved.is_none() && gent_daemon::config::path(home).try_exists()? {
            crate::node_home::inspect(home)?.service
        } else {
            None
        };
    let pin = account_pin(
        service
            .as_deref()
            .or_else(|| saved.as_ref().map(|s| s.service.as_str()))
            .or_else(|| configured.as_ref().map(|s| s.origin.as_str()))
            .unwrap_or(DEFAULT_SERVICE),
    )?;
    let base = ACCOUNT.destination(home, pin.clone())?;
    if let Some(previous) = &mut saved {
        ensure!(
            account_pin(&previous.service)? == pin,
            "sign out before switching Gent services"
        );
        ensure!(
            previous.phase != Phase::Revoking,
            "session revocation is pending; retry gent auth logout first"
        );
        if let Some(session) = service_session(&client, &base, previous).await? {
            if previous
                .session
                .as_ref()
                .is_some_and(|old| old.account_id != session.account_id)
            {
                gent_daemon::entitlements::clear(home)?;
                previous.phase = Phase::Revoking;
                save(&path, Some(previous))?;
                anyhow::bail!(
                    "account changed for the saved session; sign out before signing in again"
                );
            }
            previous.phase = Phase::Active;
            previous.session = Some(session.clone());
            save(&path, Some(previous))?;
            return Ok(json!({"status":"active","service":base.as_str(),"account":session}));
        }
    }
    // Invalidate the old lease before waiting on a replacement sign-in. Changing
    // the session snapshot also fences an in-flight refresh for the old login.
    gent_daemon::entitlements::clear(home)?;
    if let Some(previous) = &mut saved {
        previous.phase = Phase::Pending;
        previous.session = None;
        save(&path, Some(previous))?;
    }
    let (status, value) = response(client.get(base.join("v1/auth/config")?)).await?;
    let config: AuthConfig = serde_json::from_value(success(status, value)?)
        .map_err(|_| anyhow::anyhow!("invalid Gent authentication configuration"))?;
    ensure!(
        !config.github_client_id.is_empty()
            && config.github_client_id.len() <= 128
            && config
                .github_client_id
                .bytes()
                .all(|b| b.is_ascii_alphanumeric() || b == b'.'),
        "invalid GitHub client ID"
    );
    let github = github_origin(&base)?;
    let device =
        headless || (!desktop && !no_browser && std::env::var_os("SSH_CONNECTION").is_some());
    let credential = if device {
        Credential::Token(device_login(&client, &github, &config.github_client_id).await?)
    } else {
        let (code, verifier, redirect) =
            browser_login(&github, &config.github_client_id, no_browser, desktop).await?;
        Credential::Code {
            code,
            verifier,
            redirect,
        }
    };
    let pending = Saved {
        version: 1,
        service: pin.to_string(),
        token: candidate()?,
        phase: Phase::Pending,
        session: None,
    };
    // Persist the service candidate before exchange. A lost response can be
    // recovered by status/login without retaining any GitHub credential.
    save(&path, Some(&pending))?;
    let exchange = match credential {
        Credential::Token(token) => {
            client
                .post(base.join("v1/auth/github/token")?)
                .json(&TokenExchange {
                    github_access_token: token,
                    session_token: pending.token.clone(),
                })
        }
        Credential::Code {
            code,
            verifier,
            redirect,
        } => client
            .post(base.join("v1/auth/github/code")?)
            .json(&CodeExchange {
                code,
                code_verifier: verifier,
                redirect_uri: redirect,
                session_token: pending.token.clone(),
            }),
    };
    let result = response(exchange).await;
    let session = match result {
        Ok((status, value)) if status.is_success() => serde_json::from_value(value)
            .map_err(|_| anyhow::anyhow!("invalid Gent session response"))?,
        other => match service_session(&client, &base, &pending).await? {
            Some(session) => session,
            None => {
                if let Ok((status, _)) = other {
                    bail!(
                        "GitHub sign-in was not accepted by Gent (HTTP {})",
                        status.as_u16()
                    );
                }
                bail!("sign-in response lost; run gent auth status before retrying");
            }
        },
    };
    let active = Saved {
        phase: Phase::Active,
        session: Some(session),
        ..pending
    };
    save(&path, Some(&active))?;
    Ok(json!({"status":"active","service":base.as_str(),"account":active.session}))
}

enum Credential {
    Token(String),
    Code {
        code: String,
        verifier: String,
        redirect: String,
    },
}
fn github_origin(service: &Url) -> Result<Url> {
    #[cfg(feature = "test-auth-provider")]
    if let Ok(value) = std::env::var("GENT_TEST_GITHUB_ORIGIN") {
        let url = origin(&value)?;
        ensure!(
            loopback(service) && loopback(&url),
            "test GitHub requires local service and provider origins"
        );
        return Ok(url);
    }
    let _ = service;
    Ok(Url::parse("https://github.com")?)
}

async fn device_login(client: &Client, github: &Url, client_id: &str) -> Result<String> {
    #[derive(Deserialize)]
    struct Device {
        device_code: String,
        user_code: String,
        verification_uri: String,
        expires_in: u64,
        interval: u64,
    }
    let (status, value) = response(
        client
            .post(github.join("login/device/code")?)
            .form(&[("client_id", client_id), ("scope", "")])
            .header("Accept", "application/json"),
    )
    .await?;
    let device: Device = serde_json::from_value(success(status, value)?)
        .map_err(|_| anyhow::anyhow!("invalid GitHub device response"))?;
    ensure!(
        device.verification_uri == github.join("login/device")?.as_str()
            && (1..=1800).contains(&device.expires_in)
            && (5..=60).contains(&device.interval)
            && !device.device_code.is_empty()
            && device.device_code.len() <= 256
            && device.user_code.len() <= 32
            && !device.user_code.is_empty()
            && device
                .user_code
                .bytes()
                .all(|b| b.is_ascii_alphanumeric() || b == b'-'),
        "invalid GitHub device authorization"
    );
    eprintln!(
        "Open {}\nEnter code: {}",
        device.verification_uri, device.user_code
    );
    let deadline = Instant::now() + Duration::from_secs(device.expires_in);
    let mut interval = device.interval;
    loop {
        ensure!(
            Instant::now() + Duration::from_secs(interval) < deadline,
            "GitHub device sign-in expired; retry gent auth login"
        );
        tokio::time::sleep(Duration::from_secs(interval)).await;
        let (status, value) = response(
            client
                .post(github.join("login/oauth/access_token")?)
                .form(&[
                    ("client_id", client_id),
                    ("device_code", device.device_code.as_str()),
                    ("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
                ])
                .header("Accept", "application/json"),
        )
        .await?;
        let value = success(status, value)?;
        if let Some(token) = value["access_token"].as_str() {
            ensure!(value["token_type"] == "bearer", "invalid GitHub token type");
            return Ok(token.into());
        }
        match value["error"].as_str() {
            Some("authorization_pending") => {}
            Some("slow_down") => interval = interval.saturating_add(5).min(300),
            Some("access_denied") => bail!("GitHub sign-in declined"),
            Some("expired_token") => bail!("GitHub device sign-in expired; retry gent auth login"),
            _ => bail!(
                "GitHub device sign-in failed; check that device flow is enabled for the Gent app"
            ),
        }
    }
}

async fn browser_login(
    github: &Url,
    client_id: &str,
    no_browser: bool,
    desktop: bool,
) -> Result<(String, String, String)> {
    let listener = TcpListener::bind("127.0.0.1:0").await?;
    let redirect = format!(
        "http://127.0.0.1:{}{CALLBACK_PATH}",
        listener.local_addr()?.port()
    );
    let state = URL_SAFE_NO_PAD.encode(random()?);
    let verifier = URL_SAFE_NO_PAD.encode(random()?);
    let challenge = URL_SAFE_NO_PAD.encode(Sha256::digest(verifier.as_bytes()));
    let mut url = github.join("login/oauth/authorize")?;
    url.query_pairs_mut().extend_pairs([
        ("client_id", client_id),
        ("redirect_uri", redirect.as_str()),
        ("scope", ""),
        ("state", state.as_str()),
        ("code_challenge", challenge.as_str()),
        ("code_challenge_method", "S256"),
    ]);
    if !desktop {
        eprintln!("Open this URL to sign in:\n{url}");
    }
    if !no_browser {
        let opened = browser_command().arg(url.as_str()).spawn();
        if desktop {
            opened.context("could not open the sign-in browser")?;
        }
    }
    let code = timeout(Duration::from_secs(600), async {
        loop {
            let (stream, _) = listener.accept().await?;
            if let Ok(Ok(Some(code))) =
                timeout(Duration::from_secs(3), callback(stream, &redirect, &state)).await
            {
                return code;
            }
        }
    })
    .await
    .context("browser sign-in timed out; use gent auth login --headless on a remote machine")??;
    Ok((code, verifier, redirect))
}

async fn callback(
    mut stream: TcpStream,
    redirect: &str,
    state: &str,
) -> Result<Option<Result<String>>> {
    let mut bytes = Vec::new();
    let mut buffer = [0u8; 1024];
    while !bytes.windows(4).any(|part| part == b"\r\n\r\n") {
        let n = stream.read(&mut buffer).await?;
        if n == 0 || bytes.len() + n > 8192 {
            return Ok(None);
        }
        bytes.extend_from_slice(&buffer[..n]);
    }
    let input = std::str::from_utf8(&bytes)?;
    let mut lines = input.split("\r\n");
    let request: Vec<_> = lines.next().unwrap_or("").split(' ').collect();
    let expected = Url::parse(redirect)?;
    let authority = format!(
        "127.0.0.1:{}",
        expected.port().context("missing callback port")?
    );
    let hosts: Vec<_> = lines
        .filter_map(|line| line.split_once(':'))
        .filter(|(name, _)| name.eq_ignore_ascii_case("host"))
        .collect();
    let value = if request.len() == 3
        && request[0] == "GET"
        && request[1].starts_with('/')
        && hosts.len() == 1
        && hosts[0].1.trim() == authority
    {
        callback_code(request[1], &expected, state)
    } else {
        None
    };
    let (status, text) = if value.is_some() {
        (
            "200 OK",
            "GitHub responded. Return to Gent to finish signing in.",
        )
    } else {
        ("400 Bad Request", "Invalid sign-in callback.")
    };
    let response = format!(
        "HTTP/1.1 {status}\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\nCache-Control: no-store\r\nReferrer-Policy: no-referrer\r\nContent-Security-Policy: default-src 'none'\r\n\r\n{text}",
        text.len()
    );
    stream.write_all(response.as_bytes()).await?;
    Ok(value)
}
fn callback_code(target: &str, expected: &Url, state: &str) -> Option<Result<String>> {
    let url = expected.join(target).ok()?;
    if url.origin() != expected.origin() || url.path() != CALLBACK_PATH || url.fragment().is_some()
    {
        return None;
    }
    let mut fields = BTreeMap::new();
    for (key, value) in url.query_pairs() {
        if fields
            .insert(key.into_owned(), value.into_owned())
            .is_some()
        {
            return None;
        }
    }
    if fields.get("state").map(String::as_str) != Some(state) {
        return None;
    }
    if fields.contains_key("error") {
        return Some(Err(anyhow::anyhow!("GitHub sign-in declined")));
    }
    fields
        .get("code")
        .filter(|code| !code.is_empty() && code.len() <= 256)
        .map(|code| Ok(code.clone()))
}

pub fn print(value: &Value) {
    if let Some(home) = value["shared_account"]["home"].as_str() {
        println!(
            "Shared account store: {}. Login, renewal and logout apply to all profiles using it.",
            home.escape_debug()
        );
    }
    if value["status"] == "active" && value.get("renewed").is_some() {
        println!(
            "{}",
            if value["renewed"] == true {
                "Service session renewed."
            } else if value["renewal_due"] == true {
                "Service session is active; renewal is still pending."
            } else {
                "Service session is active; renewal is not due yet."
            }
        );
        return;
    }
    match value["status"].as_str() {
        Some("billing_opened") => println!("Opened Gent account billing."),
        Some("billing_link") => println!(
            "Private sign-in link (expires in five minutes)\n{}",
            value["url"].as_str().unwrap_or_default()
        ),
        Some("active") => println!(
            "Signed in as @{} ({})",
            value["account"]["github_login"]
                .as_str()
                .unwrap_or("unknown"),
            value["endpoint"]
                .as_str()
                .or(value["service"].as_str())
                .unwrap_or("")
        ),
        Some("revocation_pending") => println!(
            "Signed out locally. Service revocation is pending; retry gent auth logout when connected."
        ),
        Some("unavailable") => {
            println!("Service unavailable; sign-in status could not be verified.")
        }
        _ => println!("Not signed in. Run gent auth login."),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn billing_uses_the_signed_in_hosted_account() -> Result<()> {
        for (service, expected) in [
            (DEFAULT_SERVICE, "https://gent.link/account"),
            (ACCOUNT.beta, "https://beta.gent.link/account"),
            (STAGING_SERVICE, "https://staging.gent.link/account"),
        ] {
            let mut page = billing_page(&origin(service)?)?;
            page.set_fragment(Some("grant"));
            assert_eq!(page.as_str(), format!("{expected}#grant"));
        }
        for unsupported in [
            "https://gent.link",
            "https://staging.gent.link:8443",
            "https://staging.gent.link.example",
            "http://127.0.0.1:8000",
        ] {
            assert!(billing_page(&origin(unsupported)?).is_err());
        }
        Ok(())
    }

    #[tokio::test]
    async fn account_http_client_returns_cross_origin_redirects_without_following() -> Result<()> {
        use axum::{Router, response::Redirect, routing::get};
        let listener = TcpListener::bind("127.0.0.1:0").await?;
        let address = listener.local_addr()?;
        let redirect = format!("http://localhost:{}/target", address.port());
        let app = Router::new()
            .route(
                "/",
                get(move || async move { Redirect::temporary(&redirect) }),
            )
            .route("/target", get(|| async { StatusCode::OK }));
        let server = tokio::spawn(async move { axum::serve(listener, app).await });
        let (status, _) = response(client()?.get(format!("http://{address}/"))).await?;
        server.abort();
        assert_eq!(status, StatusCode::TEMPORARY_REDIRECT);
        Ok(())
    }

    #[tokio::test]
    async fn diagnostic_account_checks_are_optional_pinned_and_do_not_mutate_credentials() {
        use std::{
            os::unix::fs::PermissionsExt,
            sync::{
                Arc,
                atomic::{AtomicUsize, Ordering},
            },
        };
        let directory = tempfile::tempdir().unwrap();
        std::fs::set_permissions(directory.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
        gent_daemon::config::initialize(directory.path(), "diagnostic").unwrap();
        let auth_dir = directory.path().join("auth");
        managed_file::directory(&auth_dir).unwrap();
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let origin = format!("http://{}", listener.local_addr().unwrap());
        let session = ServiceSession {
            account_id: uuid::Uuid::new_v4().to_string(),
            github_subject: "PRIVATE-SUBJECT".into(),
            github_login: "PRIVATE-LOGIN".into(),
            created_at: now().unwrap(),
            expires_at: now().unwrap() + 3600,
        };
        let pin = gent_daemon::config::ServiceConfig {
            origin: origin.clone(),
            account_id: session.account_id.clone(),
        };
        let saved = Saved {
            version: 1,
            service: origin,
            token: candidate().unwrap(),
            phase: Phase::Active,
            session: Some(session.clone()),
        };
        let path = auth_dir.join("session.json");
        save(&path, Some(&saved)).unwrap();
        let original = std::fs::read(&path).unwrap();
        let mode = Arc::new(AtomicUsize::new(0));
        let requests = Arc::new(AtomicUsize::new(0));
        let (m, r) = (mode.clone(), requests.clone());
        let task = tokio::spawn(async move {
            loop {
                let (mut socket, _) = listener.accept().await.unwrap();
                let mut head = Vec::new();
                while !head.ends_with(b"\r\n\r\n") {
                    head.push(socket.read_u8().await.unwrap());
                }
                assert!(head.starts_with(b"GET /v1/auth/session HTTP/1.1\r\n"));
                r.fetch_add(1, Ordering::SeqCst);
                let mode = m.load(Ordering::SeqCst);
                let mut response = session.clone();
                if mode == 2 {
                    response.account_id = uuid::Uuid::new_v4().to_string();
                }
                let (status, body) = if mode == 1 {
                    (401, "{}".into())
                } else {
                    (200, serde_json::to_string(&response).unwrap())
                };
                socket.write_all(format!("HTTP/1.1 {status} fixture\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",body.len()).as_bytes()).await.unwrap();
            }
        });
        assert_eq!(
            diagnose(directory.path(), Some(&pin), false).await.unwrap()["status"],
            "cached"
        );
        assert_eq!(requests.load(Ordering::SeqCst), 0);
        let value = diagnose(directory.path(), Some(&pin), true).await.unwrap();
        assert_eq!(value["status"], "active");
        let text = value.to_string();
        for secret in [
            &saved.token,
            &saved.session.as_ref().unwrap().github_login,
            &pin.account_id,
        ] {
            assert!(!text.contains(secret));
        }
        mode.store(1, Ordering::SeqCst);
        assert_eq!(
            diagnose(directory.path(), Some(&pin), true).await.unwrap()["status"],
            "signed_out"
        );
        mode.store(2, Ordering::SeqCst);
        assert!(diagnose(directory.path(), Some(&pin), true).await.is_err());
        let bad = gent_daemon::config::ServiceConfig {
            origin: "https://example.invalid".into(),
            ..pin
        };
        let count = requests.load(Ordering::SeqCst);
        assert!(diagnose(directory.path(), Some(&bad), true).await.is_err());
        assert_eq!(requests.load(Ordering::SeqCst), count);
        assert_eq!(std::fs::read(&path).unwrap(), original);
        assert!(!auth_dir.join("session.lock").exists());
        task.abort();
    }
    #[test]
    fn malformed_saved_authentication_never_echoes_values() {
        use std::os::unix::fs::PermissionsExt;
        let directory = tempfile::tempdir().unwrap();
        std::fs::set_permissions(directory.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
        let path = directory.path().join("session.json");
        Snapshot::read(&path)
            .unwrap()
            .write(r#"{"version":"fixture-secret-do-not-print"}"#)
            .unwrap();
        let error = read(&path).err().expect("malformed credential accepted");
        assert!(!format!("{error:#}").contains("fixture-secret-do-not-print"));
    }
    #[test]
    fn service_origins_and_callbacks_reject_credential_redirection() {
        for bad in [
            "http://gent.link",
            "https://user:password@gent.link",
            "https://gent.link/path",
            "https://gent.link/?token=secret",
            "http://localhost:1234",
        ] {
            assert!(origin(bad).is_err());
        }
        let callback = Url::parse("http://127.0.0.1:1234/gent/github/callback").unwrap();
        let ok = "/gent/github/callback?state=expected&code=abcd";
        assert_eq!(
            callback_code(ok, &callback, "expected").unwrap().unwrap(),
            "abcd"
        );
        for bad in [
            "//foreign.invalid/gent/github/callback?state=expected&code=abcd",
            "/gent/github/callback?state=wrong&code=abcd",
            "/gent/github/callback?state=expected&state=expected&code=abcd",
            "/other?state=expected&code=abcd",
        ] {
            assert!(callback_code(bad, &callback, "expected").is_none());
        }
    }
}