ostrya-server 0.19.0

HTTP server of ostrya: the archive view of a repository for pull, and the receive endpoint of push
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
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
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
//! The authentication of the requests of a server by the client groups of
//! the repository: the reads of the archive view and of the ref listing,
//! and the requests of the receive endpoint.
//!
//! The doc of [`ServeOptions`](crate::ServeOptions) states the order of the
//! checks and the rules of the claims of a client certificate. The server
//! compares the digest of a credential with every `token-sha256` or
//! `basic-sha256` item in constant time, and does not stop at a match.

use std::borrow::Cow;
use std::collections::HashMap;
use std::future::{Future, ready};
use std::hint::black_box;
use std::net::{IpAddr, Ipv4Addr};
use std::sync::Arc;

use futures_rustls::pki_types::UnixTime;
use hyper::header::{AUTHORIZATION, HeaderValue, WWW_AUTHENTICATE};
use hyper::http::Extensions;
use hyper::http::request::Parts;
use hyper::{HeaderMap, StatusCode};
use ostrya::access::{AuthItem, Claims, ClientAccess, ClientGroup, IpNet, ReadAccess};
use ostrya::remote::proto::Hello;
use ostrya::{Checksum, ReceivePolicies, ReceivePolicy};

use crate::cert::{ClientCert, ForwardedCerts};
use crate::event::{EventSink, ServerEvent};
use crate::forwarded::{Forwarding, Origin};
use crate::receive_auth::{ReceiveAuth, Refusal, RequestKind, SessionSetup};

/// The `WWW-Authenticate` headers of a 401: the two schemes of a credential.
const CHALLENGES: [&str; 2] = [r#"Bearer realm="ostrya""#, r#"Basic realm="ostrya""#];

/// What the connection of a request states about its client. The server puts
/// it into the extensions of each request.
#[derive(Clone, Debug)]
pub(crate) struct Peer {
    /// `true` if the connection runs over TLS.
    pub(crate) tls: bool,
    /// The peer address of the connection: the source address of its PROXY
    /// header, else the TCP peer address.
    pub(crate) addr: IpAddr,
    /// The end-entity certificate that the client presented in the TLS
    /// handshake. The handshake verified it against the client CA. The
    /// server reads it one time for each connection, and each request of
    /// the connection shares it.
    pub(crate) cert: Option<Arc<ClientCert>>,
}

#[cfg(test)]
thread_local! {
    /// The number of digest compares on this thread.
    static COMPARES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

/// Returns `true` if two digests are equal, in a time that does not depend
/// on their bytes. The function reads each digest as four 64-bit words and
/// ORs the XOR of each word pair into one word. No step branches on a byte.
fn digest_eq(a: &[u8; 32], b: &[u8; 32]) -> bool {
    #[cfg(test)]
    COMPARES.with(|n| n.set(n.get() + 1));
    let (a, _) = a.as_chunks::<8>();
    let (b, _) = b.as_chunks::<8>();
    let mut diff = 0u64;
    for (x, y) in a.iter().zip(b) {
        diff |= black_box(u64::from_ne_bytes(*x) ^ u64::from_ne_bytes(*y));
    }
    black_box(diff) == 0
}

/// One `token-sha256:HEX` item.
#[derive(Debug)]
struct TokenEntry {
    digest: [u8; 32],
    client: Client,
}

/// One `basic-sha256:USER:HEX` item, with the SHA-256 digest of its user
/// name.
#[derive(Debug)]
struct BasicEntry {
    user: [u8; 32],
    password: [u8; 32],
    client: Client,
}

/// The auth items of the client groups, in the form that each step of the
/// identification reads. An `ssh:NAME` item has no place, because a request
/// over HTTP never matches it. Each item has its [`Client`], so a request
/// that matches an item clones two `Arc`s alone.
#[derive(Debug, Default)]
struct CredentialIndex {
    tokens: Vec<TokenEntry>,
    basics: Vec<BasicEntry>,
    tls_sha256: HashMap<[u8; 32], Client>,
    tls_names: HashMap<String, Client>,
    tls_any: Option<Client>,
    nets: Vec<(IpNet, Client)>,
    anonymous: Option<Client>,
}

impl CredentialIndex {
    fn new(access: &ClientAccess) -> CredentialIndex {
        let mut index = CredentialIndex::default();
        for item in access.items() {
            let claims = item.is_tls() && access.groups_of(item).any(ClientGroup::tls_accept_claim);
            let client = Client::new(item.clone(), claims);
            match item {
                AuthItem::Anonymous => index.anonymous = Some(client),
                AuthItem::TlsAny => index.tls_any = Some(client),
                AuthItem::TlsName(name) => {
                    index.tls_names.insert(name.clone(), client);
                }
                AuthItem::TlsSha256(digest) => {
                    index.tls_sha256.insert(*digest, client);
                }
                AuthItem::TokenSha256(digest) => index.tokens.push(TokenEntry {
                    digest: *digest,
                    client,
                }),
                AuthItem::BasicSha256 { user, digest } => index.basics.push(BasicEntry {
                    user: *Checksum::sha256(user.as_bytes()).as_bytes(),
                    password: *digest,
                    client,
                }),
                AuthItem::Ssh(_) => {}
                AuthItem::Net(net) => index.nets.push((*net, client)),
            }
        }
        index
    }

    /// The `token-sha256` item of the bearer token `token`. The function
    /// compares the digest of the token with every item, also after a match.
    /// An empty token matches no item.
    fn bearer(&self, token: &[u8]) -> Option<Client> {
        if token.is_empty() {
            return None;
        }
        let digest = *Checksum::sha256(token).as_bytes();
        let mut found = None;
        for entry in &self.tokens {
            if digest_eq(&entry.digest, &digest) && found.is_none() {
                found = Some(&entry.client);
            }
        }
        found.cloned()
    }

    /// The `basic-sha256` item of the Basic credential `encoded`: the base64
    /// of the user name, a `:`, and the password, which can hold `:` too.
    /// The function compares the digest of the user name and the digest of
    /// the password with every item, also after a match. A credential with
    /// no `:`, a user name that is not UTF-8, and an empty password match no
    /// item.
    fn basic(&self, encoded: &[u8]) -> Option<Client> {
        let encoded = std::str::from_utf8(encoded).ok()?;
        let decoded = ostrya::base64::decode(encoded).ok()?;
        let colon = decoded.iter().position(|&b| b == b':')?;
        let user = std::str::from_utf8(&decoded[..colon]).ok()?;
        let password = &decoded[colon + 1..];
        if password.is_empty() {
            return None;
        }
        let user = *Checksum::sha256(user.as_bytes()).as_bytes();
        let password = *Checksum::sha256(password).as_bytes();
        let mut found = None;
        for entry in &self.basics {
            let user_eq = digest_eq(&entry.user, &user);
            let password_eq = digest_eq(&entry.password, &password);
            if user_eq & password_eq && found.is_none() {
                found = Some(&entry.client);
            }
        }
        found.cloned()
    }

    /// The first item that `cert` matches, in this order: `tls-sha256:HEX`,
    /// `tls:NAME`, `tls:*`.
    fn certificate(&self, cert: &ClientCert) -> Option<Client> {
        if let Some(client) = self.tls_sha256.get(cert.digest.as_bytes()) {
            return Some(client.clone());
        }
        if let Some(cn) = &cert.cn
            && let Some(client) = self.tls_names.get(cn)
        {
            return Some(client.clone());
        }
        self.tls_any.clone()
    }

    /// The `net:` item with the longest prefix that holds `addr`. A client
    /// with no address matches no item.
    fn net(&self, addr: Option<IpAddr>) -> Option<Client> {
        let addr = addr?;
        self.nets
            .iter()
            .filter(|(net, _)| net.contains(addr))
            .max_by_key(|(net, _)| net.prefix_len())
            .map(|(_, client)| client.clone())
    }

    /// Returns `true` if a `token-sha256` or a `basic-sha256` item exists.
    fn takes_credentials(&self) -> bool {
        !self.tokens.is_empty() || !self.basics.is_empty()
    }
}

/// The identified client of a request: its auth item, and the owner key of
/// its sessions.
#[derive(Debug, Clone)]
pub(crate) struct Client {
    item: Arc<AuthItem>,
    /// The text of `item`. For a certificate of `claims`, the
    /// `tls-sha256:HEX` text of the certificate alone.
    owner: Arc<str>,
    /// `true` if `item` is a TLS item and a group that carries it has
    /// `tls-accept-claim=true`.
    claims: bool,
    /// The certificate that gave `item`, when `claims` is `true`.
    cert: Option<Arc<ClientCert>>,
}

impl Client {
    fn new(item: AuthItem, claims: bool) -> Client {
        Client {
            owner: item.to_string().into(),
            item: Arc::new(item),
            claims,
            cert: None,
        }
    }

    /// The client that the certificate `cert` identifies. When a group of
    /// the item reads claims, the client keeps the certificate, and its
    /// owner key is the `tls-sha256:HEX` text of the certificate.
    ///
    /// The digest alone is a key with no collision. The certificate sets
    /// the item that it matches, and a `tls-sha256:HEX` item, the one other
    /// item with this text, matches only the certificate of that digest.
    fn with_cert(mut self, cert: &Arc<ClientCert>) -> Client {
        if self.claims {
            self.owner = cert.owner.clone();
            self.cert = Some(cert.clone());
        }
        self
    }
}

/// The authentication of `ostrya serve`: the client groups of the repository.
/// [`ServeOptions`](crate::ServeOptions) states the order of the checks and
/// the selection of a group.
pub(crate) struct GroupAuth {
    /// `true` if the server takes a bearer or Basic credential over plain
    /// HTTP.
    cleartext: bool,
    /// The client groups and their policies. The read rights of each request
    /// share the client groups.
    policies: Arc<ReceivePolicies>,
    /// The auth items of the groups.
    index: CredentialIndex,
    /// The trusted proxies and the header of the client address, or `None`
    /// with no trusted proxy.
    forwarding: Option<Forwarding>,
    /// The client certificates that trusted proxies forward, or `None` with
    /// no header of a forwarded certificate.
    forwarded_certs: Option<ForwardedCerts>,
    /// The callback of the events, or `None`.
    on_event: Option<EventSink>,
}

impl GroupAuth {
    /// The authentication by the groups of `policies`. With `cleartext`, the
    /// server takes a bearer or Basic credential over plain HTTP.
    pub(crate) fn new(policies: Arc<ReceivePolicies>, cleartext: bool) -> GroupAuth {
        GroupAuth {
            cleartext,
            index: CredentialIndex::new(policies.access()),
            policies,
            forwarding: None,
            forwarded_certs: None,
            on_event: None,
        }
    }

    /// The authentication with the forwarding data of the trusted proxies
    /// of `forwarding`, and the forwarded certificates of `certs`. The server
    /// reads a forwarded certificate only from a trusted proxy.
    pub(crate) fn with_proxies(
        mut self,
        forwarding: Option<Forwarding>,
        certs: Option<ForwardedCerts>,
    ) -> GroupAuth {
        self.forwarding = forwarding;
        self.forwarded_certs = certs;
        self
    }

    /// The authentication that reports its events to `on_event`.
    pub(crate) fn with_events(mut self, on_event: Option<EventSink>) -> GroupAuth {
        self.on_event = on_event;
        self
    }

    /// The auth item of a request with `headers` from `peer`, or the refusal
    /// of the request. The server takes the first step that applies, in the
    /// order that [`ServeOptions`](crate::ServeOptions#authentication) states.
    fn identify(&self, headers: &HeaderMap, peer: &Peer) -> Result<Client, Refusal> {
        self.identify_at(headers, peer, UnixTime::now())
    }

    /// [`identify`](GroupAuth::identify) at the time `now`, which checks the
    /// validity of a forwarded certificate.
    fn identify_at(
        &self,
        headers: &HeaderMap,
        peer: &Peer,
        now: UnixTime,
    ) -> Result<Client, Refusal> {
        let (origin, trusted) = match &self.forwarding {
            Some(forwarding) => (
                forwarding.origin(headers, peer.addr),
                forwarding.trusts(peer.addr),
            ),
            None => (
                Origin {
                    addr: Some(peer.addr),
                    https: false,
                },
                false,
            ),
        };
        let tls = peer.tls || origin.https;
        let mut values = headers.get_all(AUTHORIZATION).iter();
        let value = values.next();
        if values.next().is_some() {
            return Err(Refusal::unauthorized(
                "the request has more than one Authorization header",
            ));
        }
        if let Some(value) = value {
            let value = value.as_bytes();
            let (scheme, rest) = match value.iter().position(|&b| b == b' ') {
                Some(space) => (&value[..space], value[space..].trim_ascii_start()),
                None => (value, &value[value.len()..]),
            };
            let bearer = scheme.eq_ignore_ascii_case(b"Bearer");
            let basic = scheme.eq_ignore_ascii_case(b"Basic");
            if (bearer || basic) && !tls && !self.cleartext {
                return Err(Refusal::forbidden(
                    "the server takes no credential over plain HTTP",
                ));
            }
            let item = if bearer {
                self.index.bearer(rest)
            } else if basic {
                self.index.basic(rest)
            } else {
                None
            };
            return item.ok_or_else(|| {
                Refusal::unauthorized(
                    "the credential of the request matches no auth item of the server",
                )
            });
        }
        // From a trusted proxy with a header of a forwarded certificate, the
        // header alone gives the certificate. The certificate of the
        // handshake is then the certificate of the proxy.
        let forwarded;
        let cert = match &self.forwarded_certs {
            Some(certs) if trusted => {
                forwarded = certs
                    .certificate(headers, now)
                    .map_err(|bad| Refusal::forbidden(bad.message()))?;
                forwarded.as_ref()
            }
            _ => peer.cert.as_ref(),
        };
        if let Some(cert) = cert {
            return match self.index.certificate(cert) {
                Some(client) => Ok(client.with_cert(cert)),
                None => Err(Refusal::forbidden(
                    "the client certificate of the request matches no auth item of the server",
                )),
            };
        }
        if let Some(client) = self.index.net(origin.addr) {
            return Ok(client);
        }
        if let Some(client) = &self.index.anonymous {
            return Ok(client.clone());
        }
        if self.index.takes_credentials() {
            Err(Refusal::unauthorized("the request has no credential"))
        } else {
            Err(Refusal::forbidden(
                "the request matches no auth item of the server",
            ))
        }
    }

    /// The read rights of a request with `headers` from `peer`, or the
    /// refusal of the request.
    ///
    /// The identification takes the steps of [`identify`](GroupAuth::identify),
    /// and a 401 carries the two challenges. When a group with
    /// `tls-accept-claim=true` carries the item of a certificate, claims that
    /// do not parse get 403, and the server reports an event. Else the rights
    /// carry the item and the claims of the client.
    pub(crate) fn identify_read(
        &self,
        headers: &HeaderMap,
        peer: &Peer,
    ) -> Result<ReadAccess, Refusal> {
        let client = self.identify(headers, peer).map_err(challenge)?;
        let claims = match &client.cert {
            Some(cert) => Some(self.claims_of(cert)?.clone()),
            None => None,
        };
        Ok(ReadAccess::new(
            self.policies.access().clone(),
            client.item.clone(),
            claims,
        ))
    }

    /// The claims of `cert`, or the 403 of claims that do not parse. The
    /// refusal reports a [`ServerEvent::BadClaims`].
    fn claims_of<'c>(&self, cert: &'c ClientCert) -> Result<&'c Arc<Claims>, Refusal> {
        cert.claims.as_ref().map_err(|fault| {
            if let Some(on_event) = &self.on_event {
                on_event(&ServerEvent::BadClaims {
                    digest: cert.digest,
                    san: fault.san().map(str::to_owned),
                    reason: fault.reason(),
                });
            }
            Refusal::forbidden(format!(
                "the claims of the client certificate do not parse: {fault}"
            ))
        })
    }

    /// The setup of a session that `client` opens with `hello`.
    /// [`open`](ReceiveAuth::open) states the rules.
    fn setup(&self, client: &Client, _hello: &Hello) -> Result<SessionSetup, Refusal> {
        let access = self.policies.access();
        let Some((group, policy)) = self.policies.select(&client.item) else {
            return Err(Refusal::forbidden(
                "no client group carries the item of the request",
            ));
        };
        let claims = match &client.cert {
            Some(cert) if group.tls_accept_claim() => Some(self.claims_of(cert)?),
            _ => None,
        };
        let claimed = match access.effective_rules(group, claims.map(|c| &**c)) {
            Cow::Owned(rules) => Some(rules),
            Cow::Borrowed(_) => None,
        };
        let policy = match claimed {
            Some(rules) if !rules.opens_push_session() => {
                return Err(Refusal::forbidden(format!(
                    "the claims of the client certificate open no push session in client \
                     group '{}'",
                    group.name()
                )));
            }
            Some(rules) => {
                // Each field but `rules` by name, so a new field of the
                // policy needs a decision here.
                let ReceivePolicy {
                    rules: _,
                    verify,
                    allow_unsigned,
                    signers,
                    allow_privileged,
                    disable_verify_bindings,
                    summary_signers,
                    update_summary,
                    group,
                } = &**policy;
                Arc::new(ReceivePolicy {
                    rules,
                    verify: verify.clone(),
                    allow_unsigned: *allow_unsigned,
                    signers: signers.clone(),
                    allow_privileged: *allow_privileged,
                    disable_verify_bindings: *disable_verify_bindings,
                    summary_signers: summary_signers.clone(),
                    update_summary: *update_summary,
                    group: group.clone(),
                })
            }
            None if !policy.rules.opens_push_session() => {
                return Err(Refusal::forbidden(format!(
                    "client group '{}' opens no push session",
                    group.name()
                )));
            }
            None => policy.clone(),
        };
        Ok(SessionSetup {
            policy,
            hooks: None,
        })
    }
}

/// The [`Peer`] in the extensions of a request. The server puts a peer into
/// each request when it has client groups.
///
/// If a request has no peer, a build with debug assertions panics. Other
/// builds take the request as plain HTTP from the unspecified address, with
/// no client certificate.
pub(crate) fn peer_of(extensions: &Extensions) -> Peer {
    let peer = extensions.get::<Peer>().cloned();
    debug_assert!(peer.is_some(), "the server puts a peer into each request");
    peer.unwrap_or(Peer {
        tls: false,
        addr: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
        cert: None,
    })
}

/// `refusal`, with the two challenges of a credential when it is 401.
fn challenge(refusal: Refusal) -> Refusal {
    if refusal.status != StatusCode::UNAUTHORIZED {
        return refusal;
    }
    CHALLENGES.into_iter().fold(refusal, |refusal, challenge| {
        refusal.with_header(WWW_AUTHENTICATE, HeaderValue::from_static(challenge))
    })
}

impl ReceiveAuth for GroupAuth {
    type Principal = Client;

    /// The text of the auth item.
    fn owner(principal: &Client) -> &str {
        &principal.owner
    }

    /// The client of the request, from its headers and the [`Peer`] in its
    /// extensions. The route does not change the answer. [`peer_of`] states
    /// the case of a request with no peer.
    fn authenticate(
        &self,
        parts: &Parts,
        _kind: RequestKind,
    ) -> impl Future<Output = Result<Client, Refusal>> + Send {
        let peer = peer_of(&parts.extensions);
        ready(self.identify(&parts.headers, &peer).map_err(challenge))
    }

    /// The policy of the client group that carries the item of `client`, and
    /// no hooks.
    ///
    /// [`ClientAccess::select`](ostrya::access::ClientAccess::select) states
    /// the selection. The group gives the rules of each namespace, and the
    /// session reads the rules of the namespace of each ref of `hello`.
    ///
    /// When the group has `tls-accept-claim=true` and a certificate gave the
    /// item, the claims of the certificate for each namespace replace the
    /// rules of the group for that namespace. Claims that do not parse get
    /// 403, and the server reports an event. With no group, and with rules
    /// that open no push session in any namespace, the refusal is 403.
    fn open(
        &self,
        client: &Client,
        hello: &Hello,
    ) -> impl Future<Output = Result<SessionSetup, Refusal>> + Send {
        ready(self.setup(client, hello))
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicU32, Ordering};

    use hyper::header::HeaderValue;

    use ostrya::access::Namespace;

    use super::*;
    use crate::session::tests::TmpRepo;
    use crate::test_pki::TestCa;

    /// The hex digest of `text`.
    fn hex(text: &str) -> String {
        Checksum::sha256(text.as_bytes()).to_hex()
    }

    /// The authentication by `groups`, in a new repository. The repository
    /// lives until the end of the test.
    fn auth(groups: &str) -> (GroupAuth, TmpRepo) {
        static N: AtomicU32 = AtomicU32::new(0);
        let tmp = TmpRepo::new(&format!("auth-{}", N.fetch_add(1, Ordering::Relaxed)));
        (GroupAuth::new(tmp.policies(groups), false), tmp)
    }

    /// Two groups of credentials: `tokens` with two bearer tokens, and
    /// `basics` with two Basic credentials. Both push to each ref.
    fn credentials() -> String {
        format!(
            "[ex-ostrya client \"tokens\"]\n\
             auth=token-sha256:{};token-sha256:{}\nallow=w:**\n\
             [ex-ostrya client \"basics\"]\n\
             auth=basic-sha256:alice:{};basic-sha256:bob:{}\nallow=w:**\n",
            hex("a-token"),
            hex("b-token"),
            hex("a-pass"),
            hex("pa:ss:"),
        )
    }

    fn peer(tls: bool, addr: &str, cert: Option<ClientCert>) -> Peer {
        Peer {
            tls,
            addr: addr.parse().unwrap(),
            cert: cert.map(Arc::new),
        }
    }

    fn plain() -> Peer {
        peer(false, "192.0.2.1", None)
    }

    fn tls() -> Peer {
        peer(true, "192.0.2.1", None)
    }

    /// A certificate with the digest of `der` and the CN `cn`.
    fn cert(der: &str, cn: Option<&str>) -> ClientCert {
        let digest = Checksum::sha256(der.as_bytes());
        ClientCert {
            owner: format!("tls-sha256:{digest}").into(),
            digest,
            cn: cn.map(str::to_owned),
            not_after: None,
            claims: Ok(Arc::new(ostrya::access::Claims::default())),
        }
    }

    fn headers(values: &[&str]) -> HeaderMap {
        let mut headers = HeaderMap::new();
        for value in values {
            headers.append(AUTHORIZATION, HeaderValue::from_str(value).unwrap());
        }
        headers
    }

    fn basic(user: &str, password: &str) -> String {
        format!(
            "Basic {}",
            ostrya::base64::encode(format!("{user}:{password}").as_bytes())
        )
    }

    /// The text of the item that `auth` gives to `values` from `peer`.
    fn item(auth: &GroupAuth, values: &[&str], peer: &Peer) -> String {
        auth.identify(&headers(values), peer)
            .unwrap()
            .owner
            .to_string()
    }

    /// The status of the refusal of `values` from `peer`.
    fn refused(auth: &GroupAuth, values: &[&str], peer: &Peer) -> StatusCode {
        auth.identify(&headers(values), peer).unwrap_err().status
    }

    /// The result of `f`, and the number of digest compares it made.
    fn counted<T>(f: impl FnOnce() -> T) -> (T, usize) {
        COMPARES.with(|n| n.set(0));
        let result = f();
        (result, COMPARES.with(|n| n.get()))
    }

    #[test]
    fn digests_compare_by_every_byte() {
        let a = *Checksum::sha256(b"a").as_bytes();
        assert!(digest_eq(&a, &a));
        for i in 0..32 {
            let mut b = a;
            b[i] ^= 0x80;
            assert!(!digest_eq(&a, &b), "byte {i}");
        }
    }

    /// The index holds each item of each group, and no `ssh:` item.
    #[test]
    fn the_index_holds_the_items_of_each_group() {
        let (auth, _tmp) = auth(&format!(
            "{}[ex-ostrya client \"other\"]\nauth=ssh:u;tls:*;net:10.0.0.0/8\n",
            credentials(),
        ));
        let index = &auth.index;
        assert_eq!(index.tokens.len(), 2);
        assert_eq!(index.basics.len(), 2);
        assert!(index.tls_any.is_some() && index.anonymous.is_none());
        assert_eq!(index.nets.len(), 1);
        assert!(index.tls_names.is_empty() && index.tls_sha256.is_empty());
    }

    /// A bearer token gives its `token-sha256` item, and a Basic credential
    /// its `basic-sha256` item. Each compare visits every item of its kind:
    /// one compare for each token, and two for each Basic item.
    #[test]
    fn a_credential_gives_its_item_and_visits_every_item() {
        let (auth, _tmp) = auth(&credentials());
        for (value, expected) in [
            (
                "Bearer a-token".to_owned(),
                format!("token-sha256:{}", hex("a-token")),
            ),
            (
                "bearer   b-token".to_owned(),
                format!("token-sha256:{}", hex("b-token")),
            ),
        ] {
            let (found, compares) = counted(|| item(&auth, &[&value], &tls()));
            assert_eq!((found, compares), (expected, 2), "{value}");
        }
        for (user, password) in [("alice", "a-pass"), ("bob", "pa:ss:")] {
            let value = basic(user, password);
            let (found, compares) = counted(|| item(&auth, &[&value], &tls()));
            let expected = format!("basic-sha256:{user}:{}", hex(password));
            assert_eq!((found, compares), (expected, 4), "{user}");
        }
        let upper = basic("alice", "a-pass").replacen("Basic", "BASIC", 1);
        assert!(item(&auth, &[&upper], &tls()).starts_with("basic-sha256:alice:"));
    }

    /// A credential that matches no item is 401, also with a client
    /// certificate, an anonymous item, and a `net:` item that holds the peer.
    /// A Basic credential with a wrong user name or a wrong password visits
    /// every item.
    #[test]
    fn a_credential_that_matches_no_item_is_401() {
        let (auth, _tmp) = auth(&format!(
            "{}[ex-ostrya client \"others\"]\nauth=anonymous;tls:*;net:0.0.0.0/0\nallow=w:**\n",
            credentials()
        ));
        let with_cert = peer(true, "192.0.2.1", Some(cert("cert", Some("host"))));
        for value in [
            "Bearer other".to_owned(),
            "Bearer".to_owned(),
            "Bearer ".to_owned(),
            basic("alice", "pa:ss:"),
            basic("bob", "a-pass"),
            basic("carol", "a-pass"),
            basic("alice", ""),
            "Basic !!!".to_owned(),
            format!("Basic {}", ostrya::base64::encode(b"alice")),
            format!("Basic {}", ostrya::base64::encode(b"\xff:a-pass")),
            "Digest a-token".to_owned(),
            "a-token".to_owned(),
        ] {
            assert_eq!(
                refused(&auth, &[&value], &with_cert),
                StatusCode::UNAUTHORIZED,
                "{value}"
            );
        }
        for (user, password) in [("alice", "b-pass"), ("carol", "a-pass")] {
            let value = basic(user, password);
            let (status, compares) = counted(|| refused(&auth, &[&value], &tls()));
            assert_eq!((status, compares), (StatusCode::UNAUTHORIZED, 4));
        }
        let (status, compares) = counted(|| refused(&auth, &["Bearer other"], &tls()));
        assert_eq!((status, compares), (StatusCode::UNAUTHORIZED, 2));
    }

    /// More than one `Authorization` header is 401 before each other step,
    /// also over plain HTTP.
    #[test]
    fn more_than_one_authorization_header_is_401() {
        let (auth, _tmp) = auth(&credentials());
        let two = headers(&["Bearer a-token", "Bearer a-token"]);
        for peer in [tls(), plain()] {
            let refusal = auth.identify(&two, &peer).unwrap_err();
            assert_eq!(refusal.status, StatusCode::UNAUTHORIZED);
            assert_eq!(
                refusal.message,
                "the request has more than one Authorization header"
            );
        }
    }

    /// A bearer or Basic credential over plain HTTP is 403 before any
    /// compare, also where an anonymous item exists. With the cleartext
    /// switch, the credential gives its item.
    #[test]
    fn a_credential_over_plain_http_is_403() {
        let (mut auth, _tmp) = auth(&format!(
            "{}[ex-ostrya client \"anonymous\"]\nauth=anonymous\nallow=w:**\n",
            credentials()
        ));
        for value in [
            "Bearer a-token".to_owned(),
            "BEARER a-token".to_owned(),
            basic("alice", "a-pass"),
        ] {
            let (status, compares) = counted(|| refused(&auth, &[&value], &plain()));
            assert_eq!((status, compares), (StatusCode::FORBIDDEN, 0), "{value}");
        }
        assert_eq!(refused(&auth, &["Digest x"], &plain()), 401);
        assert_eq!(item(&auth, &[], &plain()), "anonymous");
        auth.cleartext = true;
        assert_eq!(
            item(&auth, &["Bearer a-token"], &plain()),
            format!("token-sha256:{}", hex("a-token"))
        );
    }

    /// A certificate gives the first item that it matches: `tls-sha256:HEX`,
    /// then `tls:NAME` by the CN, then `tls:*`. A certificate that matches
    /// no item is 403, also with an anonymous item and a `net:` item that
    /// holds the peer.
    #[test]
    fn a_certificate_gives_the_first_item_that_matches() {
        let (auth, _tmp) = auth(&format!(
            "[ex-ostrya client \"by-digest\"]\nauth=tls-sha256:{}\nallow=w:**\n\
             [ex-ostrya client \"by-name\"]\nauth=tls:host\nallow=w:**\n\
             [ex-ostrya client \"any\"]\nauth=tls:*\nallow=w:**\n",
            hex("pinned"),
        ));
        let with =
            |der: &str, cn: Option<&str>| item(&auth, &[], &peer(true, "::1", Some(cert(der, cn))));
        assert_eq!(
            with("pinned", Some("host")),
            format!("tls-sha256:{}", hex("pinned"))
        );
        assert_eq!(with("other", Some("host")), "tls:host");
        assert_eq!(with("other", Some("Host")), "tls:*");
        assert_eq!(with("other", None), "tls:*");
        assert_eq!(refused(&auth, &[], &tls()), StatusCode::FORBIDDEN);

        let (auth, _tmp) = self::auth(&format!(
            "[ex-ostrya client \"by-name\"]\nauth=tls:host\nallow=w:**\n\
             [ex-ostrya client \"others\"]\nauth=anonymous;net:0.0.0.0/0;token-sha256:{}\n\
             allow=w:**\n",
            hex("a-token"),
        ));
        let unknown = peer(true, "192.0.2.1", Some(cert("other", Some("guest"))));
        let refusal = auth.identify(&HeaderMap::new(), &unknown).unwrap_err();
        assert_eq!(refusal.status, StatusCode::FORBIDDEN);
        assert!(
            refusal.message.contains("client certificate"),
            "{refusal:?}"
        );
        assert_eq!(item(&auth, &[], &tls()), "net:0.0.0.0/0");
    }

    /// A request with no credential and no certificate gets the `net:` item
    /// with the longest prefix that holds its address. An IPv4-mapped IPv6
    /// address counts as its IPv4 address. A `net:` item wins over the
    /// anonymous item.
    #[test]
    fn a_net_item_with_the_longest_prefix_wins() {
        let (auth, _tmp) = auth(
            "[ex-ostrya client \"wide\"]\nauth=net:10.0.0.0/8;net:2001:db8::/32\nallow=w:**\n\
             [ex-ostrya client \"narrow\"]\nauth=net:10.0.1.0/24;net:2001:db8::/56\nallow=w:**\n\
             [ex-ostrya client \"anonymous\"]\nauth=anonymous\nallow=w:**\n",
        );
        for (addr, expected) in [
            ("10.0.1.7", "net:10.0.1.0/24"),
            ("10.0.2.7", "net:10.0.0.0/8"),
            ("::ffff:10.0.1.7", "net:10.0.1.0/24"),
            ("::ffff:10.9.0.1", "net:10.0.0.0/8"),
            ("2001:db8::1", "net:2001:db8::/56"),
            ("2001:db8:1::1", "net:2001:db8::/32"),
            ("192.0.2.1", "anonymous"),
            ("::1", "anonymous"),
        ] {
            assert_eq!(
                item(&auth, &[], &peer(false, addr, None)),
                expected,
                "{addr}"
            );
        }
    }

    /// With no credential, no certificate, and no `net:` match, a request
    /// gets the anonymous item. With no anonymous item, it gets 401 when a
    /// `token-sha256` or `basic-sha256` item exists, and 403 when none
    /// exists.
    #[test]
    fn a_request_with_nothing_is_anonymous_401_or_403() {
        let (auth, _tmp) = auth(&format!(
            "{}[ex-ostrya client \"anonymous\"]\nauth=anonymous\nallow=w:**\n",
            credentials()
        ));
        assert_eq!(item(&auth, &[], &tls()), "anonymous");
        let (auth, _tmp) = self::auth(&credentials());
        assert_eq!(refused(&auth, &[], &tls()), StatusCode::UNAUTHORIZED);
        let (auth, _tmp) =
            self::auth("[ex-ostrya client \"c\"]\nauth=tls:*;ssh:u;net:10.0.0.0/8\nallow=w:**\n");
        assert_eq!(refused(&auth, &[], &tls()), StatusCode::FORBIDDEN);
        assert_eq!(refused(&auth, &[], &plain()), StatusCode::FORBIDDEN);
    }

    /// The parts of a request with the `Authorization` values `values` and,
    /// when it is given, `peer` in its extensions.
    fn parts(values: &[&str], peer: Option<Peer>) -> Parts {
        let mut builder = hyper::Request::builder();
        for value in values {
            builder = builder.header(AUTHORIZATION, *value);
        }
        if let Some(peer) = peer {
            builder = builder.extension(peer);
        }
        builder.body(()).unwrap().into_parts().0
    }

    fn authenticate(auth: &GroupAuth, parts: &Parts) -> Result<Client, Refusal> {
        ostrya_rt::block_on(auth.authenticate(parts, RequestKind::Open))
    }

    /// `authenticate` reads the peer from the extensions of the request, and
    /// adds the two challenges to a 401 alone. The owner key is the text of
    /// the item. The route does not change the answer.
    #[test]
    fn authenticate_reads_the_peer_and_challenges_a_401_alone() {
        let (auth, _tmp) = auth(&format!(
            "{}[ex-ostrya client \"cert\"]\nauth=tls:*\nallow=w:**\n",
            credentials()
        ));
        let with_cert = peer(true, "192.0.2.1", Some(cert("cert", None)));
        let client = authenticate(&auth, &parts(&[], Some(with_cert))).unwrap();
        assert_eq!(GroupAuth::owner(&client), "tls:*");
        let token = parts(&["Bearer a-token"], Some(tls()));
        for kind in [
            RequestKind::Open,
            RequestKind::Have,
            RequestKind::Objects,
            RequestKind::Commit,
            RequestKind::Delete,
        ] {
            let client = ostrya_rt::block_on(auth.authenticate(&token, kind)).unwrap();
            let expected = format!("token-sha256:{}", hex("a-token"));
            assert_eq!(GroupAuth::owner(&client), expected);
        }

        let refusal = authenticate(&auth, &parts(&[], Some(tls()))).unwrap_err();
        assert_eq!(refusal.status, StatusCode::UNAUTHORIZED);
        let challenges: Vec<_> = CHALLENGES
            .into_iter()
            .map(|c| (WWW_AUTHENTICATE, HeaderValue::from_static(c)))
            .collect();
        assert_eq!(refusal.headers, challenges);

        let refusal = authenticate(&auth, &parts(&["Bearer a-token"], Some(plain()))).unwrap_err();
        assert_eq!(refusal.status, StatusCode::FORBIDDEN);
        assert!(refusal.headers.is_empty());
    }

    /// In a build with debug assertions, a request with no peer panics.
    /// Otherwise it is plain HTTP with no client certificate, so a
    /// credential is 403.
    #[test]
    fn a_request_with_no_peer_is_plain_http() {
        let (auth, _tmp) = auth(&credentials());
        let no_peer = parts(&["Bearer a-token"], None);
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            authenticate(&auth, &no_peer)
        }));
        if cfg!(debug_assertions) {
            assert!(result.is_err());
        } else {
            assert_eq!(result.unwrap().unwrap_err().status, StatusCode::FORBIDDEN);
        }
    }

    /// `open` gives the policy of the group that carries the item, whatever
    /// the namespace of the refs of `Hello`. An item with no group, and a
    /// group that opens no push session, get 403. A `tls:NAME` item with no
    /// group gets 403, although a group of `tls:*` exists.
    #[test]
    fn open_selects_the_group_of_the_item() {
        let (auth, _tmp) = auth(
            "[ex-ostrya client \"push\"]\nauth=anonymous;tls:*\nallow=w:**\n\
             [ex-ostrya client \"read\"]\nauth=ssh:r;net:10.0.0.0/8\nallow=r:**\n\
             [ex-ostrya client \"host\"]\nauth=tls:host\n\
             allow=w:@org.example.Other:**;w:@org.example.Os:os/**\n",
        );
        let hello_in = |collection: Option<&str>| Hello {
            version: 1,
            agent: None,
            refs: vec![collection.map_or_else(|| "main".to_owned(), |id| format!("@{id}:main"))],
            one_way: false,
        };
        let hello = hello_in(None);
        let open_in = |text: &str, hello: &Hello| {
            let client = Client::new(AuthItem::parse(text).unwrap(), false);
            ostrya_rt::block_on(auth.open(&client, hello))
        };
        let open = |text: &str| open_in(text, &hello);
        for text in ["anonymous", "tls:*"] {
            let setup = open(text).unwrap_or_else(|r| panic!("{text}: {r:?}"));
            assert!(Arc::ptr_eq(
                &setup.policy,
                auth.policies.get("push").unwrap()
            ));
            assert_eq!(setup.policy.group.as_deref(), Some("push"));
            assert!(setup.hooks.is_none());
        }
        for (text, needle) in [
            (
                "net:10.0.0.0/8",
                "client group 'read' opens no push session",
            ),
            (
                "tls:other",
                "no client group carries the item of the request",
            ),
        ] {
            for collection in [None, Some("org.example.Os")] {
                let Err(refusal) = open_in(text, &hello_in(collection)) else {
                    panic!("{text} opens a session");
                };
                assert_eq!(refusal.status, StatusCode::FORBIDDEN, "{text}");
                assert_eq!(refusal.message, needle, "{text}");
            }
        }
        for (text, group) in [("tls:host", "host"), ("anonymous", "push")] {
            for collection in [None, Some("org.example.Os"), Some("org.example.Other")] {
                let setup = open_in(text, &hello_in(collection))
                    .unwrap_or_else(|r| panic!("{text} {collection:?}: {r:?}"));
                assert!(Arc::ptr_eq(
                    &setup.policy,
                    auth.policies.get(group).unwrap()
                ));
            }
        }
    }

    const CA_PEM: &[u8] = include_bytes!("../../../tests/fixtures/tls/ca.pem");
    const CLIENT_PEM: &str = include_str!("../../../tests/fixtures/tls/client.pem");
    const SERVER_PEM: &str = include_str!("../../../tests/fixtures/tls/server.pem");

    /// The base64 of the DER bytes of the one certificate of `pem`.
    fn base64_der(pem: &str) -> String {
        pem.lines()
            .filter(|line| !line.starts_with("-----"))
            .collect()
    }

    /// The authentication by `groups` behind the trusted proxies of
    /// 10.0.0.0/8, which forward the client address in `X-Forwarded-For` and
    /// the client certificate in `X-Client-Cert`. The fixture CA verifies the
    /// certificate.
    fn proxied(groups: &str) -> (GroupAuth, TmpRepo) {
        let (auth, tmp) = auth(groups);
        let forwarding = Forwarding::new(
            vec![IpNet::parse("10.0.0.0/8").unwrap()],
            ostrya::access::ForwardedHeader::XForwardedFor,
        );
        let verifier = ostrya_fetch::client_cert_verifier(CA_PEM).unwrap();
        let certs = ForwardedCerts::new(
            hyper::header::HeaderName::from_static("x-client-cert"),
            verifier,
        );
        (auth.with_proxies(Some(forwarding), Some(certs)), tmp)
    }

    /// The headers of the pairs `lines`.
    fn lines(lines: &[(&str, &str)]) -> HeaderMap {
        let mut headers = HeaderMap::new();
        for (name, value) in lines {
            headers.append(
                hyper::header::HeaderName::from_bytes(name.as_bytes()).unwrap(),
                HeaderValue::from_str(value).unwrap(),
            );
        }
        headers
    }

    fn proxy() -> Peer {
        peer(false, "10.0.0.1", None)
    }

    fn stranger() -> Peer {
        peer(false, "192.0.2.9", None)
    }

    /// `X-Forwarded-Proto: https` from a trusted proxy passes the step of a
    /// credential over plain HTTP. From another peer, it has no effect.
    #[test]
    fn https_from_a_trusted_proxy_counts_as_tls() {
        let (auth, _tmp) = proxied(&credentials());
        let request = lines(&[
            ("authorization", "Bearer a-token"),
            ("x-forwarded-proto", "https"),
        ]);
        let found = auth.identify(&request, &proxy()).unwrap();
        assert_eq!(&*found.owner, format!("token-sha256:{}", hex("a-token")));
        let refusal = auth.identify(&request, &stranger()).unwrap_err();
        assert_eq!(refusal.status, StatusCode::FORBIDDEN);
        assert!(refusal.message.contains("plain HTTP"), "{refusal:?}");
        let http = lines(&[
            ("authorization", "Bearer a-token"),
            ("x-forwarded-proto", "http"),
        ]);
        assert_eq!(auth.identify(&http, &proxy()).unwrap_err().status, 403);
    }

    /// A forwarded certificate from a trusted proxy matches `tls:NAME` and
    /// `tls-sha256:HEX`. From another peer, the server ignores it.
    #[test]
    fn a_forwarded_certificate_matches_the_tls_items() {
        let der = ostrya::base64::decode(&base64_der(CLIENT_PEM)).unwrap();
        let by_digest = format!("tls-sha256:{}", Checksum::sha256(&der).to_hex());
        for (groups, expected) in [
            (
                "[ex-ostrya client \"c\"]\nauth=tls:ostrya test\nallow=w:**\n\
                 [ex-ostrya client \"a\"]\nauth=anonymous\nallow=w:**\n"
                    .to_owned(),
                "tls:ostrya test".to_owned(),
            ),
            (
                format!(
                    "[ex-ostrya client \"c\"]\nauth={by_digest};tls:*\nallow=w:**\n\
                     [ex-ostrya client \"a\"]\nauth=anonymous\nallow=w:**\n"
                ),
                by_digest.clone(),
            ),
        ] {
            let (auth, _tmp) = proxied(&groups);
            let request = lines(&[("x-client-cert", &base64_der(CLIENT_PEM))]);
            assert_eq!(&*auth.identify(&request, &proxy()).unwrap().owner, expected);
            assert_eq!(
                &*auth.identify(&request, &stranger()).unwrap().owner,
                "anonymous"
            );
        }
    }

    /// A forwarded certificate that does not decode, that holds two
    /// certificates, or that the CA does not verify gets 403, also with an
    /// anonymous item. With an `Authorization` header, the credential
    /// decides first.
    #[test]
    fn a_bad_forwarded_certificate_is_403() {
        let (auth, _tmp) = proxied(&format!(
            "{}[ex-ostrya client \"a\"]\nauth=anonymous;tls:*\nallow=w:**\n",
            credentials()
        ));
        let client = base64_der(CLIENT_PEM);
        for (value, needle) in [
            ("!!!".to_owned(), "does not parse"),
            (format!("{client},{client}"), "more than one"),
            (base64_der(SERVER_PEM), "does not verify"),
        ] {
            let request = lines(&[("x-client-cert", &value)]);
            let refusal = auth.identify(&request, &proxy()).unwrap_err();
            assert_eq!(refusal.status, StatusCode::FORBIDDEN, "{value}");
            assert!(refusal.message.contains(needle), "{refusal:?}");
            let with_token = lines(&[
                ("x-client-cert", &value),
                ("authorization", "Bearer a-token"),
                ("x-forwarded-proto", "https"),
            ]);
            let found = auth.identify(&with_token, &proxy()).unwrap();
            assert_eq!(&*found.owner, format!("token-sha256:{}", hex("a-token")));
            assert_eq!(
                &*auth.identify(&request, &stranger()).unwrap().owner,
                "anonymous"
            );
        }
        let empty = lines(&[("x-client-cert", "")]);
        assert_eq!(
            &*auth.identify(&empty, &proxy()).unwrap().owner,
            "anonymous"
        );
    }

    /// From a trusted proxy with a header of a forwarded certificate, the
    /// header alone gives the certificate: with no header, or an empty
    /// header, the certificate of the handshake does not identify the
    /// client. From another peer, or with no header of a forwarded
    /// certificate, the certificate of the handshake applies.
    #[test]
    fn a_forwarded_certificate_replaces_the_handshake() {
        let groups = format!(
            "[ex-ostrya client \"c\"]\nauth=tls:ostrya test\nallow=w:**\n\
             [ex-ostrya client \"h\"]\nauth=tls-sha256:{}\nallow=w:**\n\
             [ex-ostrya client \"a\"]\nauth=anonymous\nallow=w:**\n",
            hex("handshake")
        );
        let (direct, _direct_tmp) = auth(&groups);
        let (auth, _tmp) = proxied(&groups);
        let handshake = peer(true, "10.0.0.1", Some(cert("handshake", Some("other"))));
        let request = lines(&[("x-client-cert", &base64_der(CLIENT_PEM))]);
        let by_handshake = format!("tls-sha256:{}", hex("handshake"));
        assert_eq!(
            &*auth.identify(&request, &handshake).unwrap().owner,
            "tls:ostrya test"
        );
        for request in [HeaderMap::new(), lines(&[("x-client-cert", "")])] {
            assert_eq!(
                &*auth.identify(&request, &handshake).unwrap().owner,
                "anonymous"
            );
        }
        let untrusted = peer(true, "192.0.2.9", Some(cert("handshake", Some("other"))));
        assert_eq!(
            &*auth.identify(&request, &untrusted).unwrap().owner,
            by_handshake
        );
        let forwarding = Forwarding::new(
            vec![IpNet::parse("10.0.0.0/8").unwrap()],
            ostrya::access::ForwardedHeader::XForwardedFor,
        );
        let auth = direct.with_proxies(Some(forwarding), None);
        assert_eq!(
            &*auth.identify(&request, &handshake).unwrap().owner,
            by_handshake
        );
    }

    /// A `net:` item matches the forwarded client address. A client with no
    /// address matches no `net:` item, and never takes the address of the
    /// proxy.
    #[test]
    fn a_net_item_matches_the_forwarded_address() {
        let (auth, _tmp) = proxied(
            "[ex-ostrya client \"farm\"]\nauth=net:192.0.2.0/24\nallow=w:**\n\
             [ex-ostrya client \"proxies\"]\nauth=net:10.0.0.0/8\nallow=w:**\n",
        );
        let request = |value: &str| lines(&[("x-forwarded-for", value)]);
        let owner = |value: &str, peer: &Peer| {
            auth.identify(&request(value), peer)
                .map(|client| client.owner.to_string())
                .map_err(|refusal| refusal.status)
        };
        assert_eq!(
            owner("192.0.2.7, 10.0.0.2", &proxy()).unwrap(),
            "net:192.0.2.0/24"
        );
        assert_eq!(owner("10.0.0.3", &proxy()).unwrap(), "net:10.0.0.0/8");
        assert_eq!(owner("unknown", &proxy()), Err(StatusCode::FORBIDDEN));
        assert_eq!(owner("", &proxy()).unwrap(), "net:10.0.0.0/8");
        assert_eq!(owner("10.9.9.9", &stranger()).unwrap(), "net:192.0.2.0/24");
    }

    /// The events that `auth` reports, in order.
    fn recording(auth: GroupAuth) -> (GroupAuth, Arc<std::sync::Mutex<Vec<ServerEvent>>>) {
        let events = Arc::new(std::sync::Mutex::new(Vec::new()));
        let sink = events.clone();
        let auth = auth.with_events(Some(Arc::new(move |event: &ServerEvent| {
            sink.lock().unwrap().push(event.clone());
        })));
        (auth, events)
    }

    fn hello() -> Hello {
        Hello {
            version: 1,
            agent: None,
            refs: vec!["main".into()],
            one_way: false,
        }
    }

    /// The setup of the session that a request with `headers` from `peer`
    /// opens.
    fn open_with(
        auth: &GroupAuth,
        headers: &HeaderMap,
        peer: &Peer,
    ) -> Result<SessionSetup, Refusal> {
        let client = auth.identify(headers, peer)?;
        ostrya_rt::block_on(auth.open(&client, &hello()))
    }

    /// A TLS peer with the certificate `der`.
    fn with_der(der: &[u8]) -> Peer {
        peer(true, "192.0.2.1", Some(ClientCert::from_der(der)))
    }

    /// The allow items and the deny items of `setup` in `namespace`, as
    /// text.
    fn rule_texts_in(setup: &SessionSetup, namespace: &Namespace) -> (Vec<String>, Vec<String>) {
        let rules = setup.policy.rules.get(namespace).unwrap();
        (
            rules.allow.iter().map(ToString::to_string).collect(),
            rules.deny.iter().map(|p| p.as_str().to_owned()).collect(),
        )
    }

    /// The allow items and the deny items of `setup` in the local refs, as
    /// text.
    fn rule_texts(setup: &SessionSetup) -> (Vec<String>, Vec<String>) {
        rule_texts_in(setup, &Namespace::Local)
    }

    /// A group of `tls:*` that reads the claims, reads all local refs, and
    /// accepts privileged content.
    const CLAIMS_GROUP: &str = "[ex-ostrya client \"certs\"]\nauth=tls:*\n\
                                tls-accept-claim=true\nallow=r:**\nallow-privileged=true\n";

    /// The claims of the local refs replace the allow items and the deny
    /// items of the group for the local refs, and the claims of a collection
    /// give the rules of that collection. The other values of the policy and
    /// the name of the group stay.
    #[test]
    fn the_claims_replace_the_rules_of_the_group() {
        let (auth, _tmp) = auth(CLAIMS_GROUP);
        let ca = TestCa::new();
        let der = ca.leaf(
            "c",
            &[
                "ostrya:ref:w:main",
                "ostrya:ref:d:@a.b:x",
                "ostrya:ref:!main/secret",
            ],
        );
        let setup = open_with(&auth, &HeaderMap::new(), &with_der(&der)).unwrap();
        assert_eq!(
            rule_texts(&setup),
            (vec!["w:main".to_owned()], vec!["main/secret".to_owned()])
        );
        assert_eq!(
            rule_texts_in(&setup, &Namespace::Collection("a.b".into())),
            (vec!["d:x".to_owned()], Vec::new())
        );
        let group = auth.policies.get("certs").unwrap();
        assert!(!Arc::ptr_eq(&setup.policy, group));
        assert_eq!(setup.policy.group.as_deref(), Some("certs"));
        assert!(setup.policy.allow_privileged);
        assert_eq!(group.rules.local.allow[0].to_string(), "r:**");
    }

    /// With no claim, the policy of the group applies. Claims of a
    /// collection alone keep the rules of the group for the local refs.
    #[test]
    fn with_no_claim_of_a_namespace_the_group_applies_there() {
        let (auth, _tmp) = auth(&CLAIMS_GROUP.replace("allow=r:**", "allow=w:**"));
        let ca = TestCa::new();
        let group = auth.policies.get("certs").unwrap();
        for uris in [&[][..], &["https://example.com/"]] {
            let setup = open_with(&auth, &HeaderMap::new(), &with_der(&ca.leaf("c", uris)))
                .unwrap_or_else(|r| panic!("{uris:?}: {r:?}"));
            assert!(Arc::ptr_eq(&setup.policy, group), "{uris:?}");
        }
        let uris = ["ostrya:ref:w:@a.b:main", "https://example.com/"];
        let setup = open_with(&auth, &HeaderMap::new(), &with_der(&ca.leaf("c", &uris))).unwrap();
        assert_eq!(setup.policy.rules.local, group.rules.local);
        assert_eq!(
            rule_texts_in(&setup, &Namespace::Collection("a.b".into())),
            (vec!["w:main".to_owned()], Vec::new())
        );
    }

    /// In a collection namespace, the claims of that collection give the
    /// rules. The claims of the local refs and of another collection give
    /// the rules of their own namespaces.
    #[test]
    fn the_claims_of_the_collection_apply_in_its_namespace() {
        let (auth, _tmp) = auth(CLAIMS_GROUP);
        let der = TestCa::new().leaf(
            "c",
            &[
                "ostrya:ref:w:main",
                "ostrya:ref:w:@org.example.Os:os/**",
                "ostrya:ref:!@org.example.Os:os/secret",
                "ostrya:ref:d:@org.example.Other:x",
            ],
        );
        let client = auth.identify(&HeaderMap::new(), &with_der(&der)).unwrap();
        let hello = Hello {
            refs: vec!["@org.example.Os:os/a".into()],
            ..hello()
        };
        let setup = ostrya_rt::block_on(auth.open(&client, &hello)).unwrap();
        let os = Namespace::Collection("org.example.Os".into());
        assert_eq!(
            rule_texts_in(&setup, &os),
            (vec!["w:os/**".to_owned()], vec!["os/secret".to_owned()])
        );
        assert_eq!(rule_texts(&setup), (vec!["w:main".to_owned()], Vec::new()));
        assert_eq!(
            rule_texts_in(&setup, &Namespace::Collection("org.example.Other".into())),
            (vec!["d:x".to_owned()], Vec::new())
        );
        assert_eq!(setup.policy.group.as_deref(), Some("certs"));
    }

    /// The own collection id of the repository names the local refs in a
    /// claim.
    #[test]
    fn a_claim_of_the_own_collection_names_the_local_refs() {
        static N: AtomicU32 = AtomicU32::new(0);
        let tmp = TmpRepo::new(&format!("auth-own-{}", N.fetch_add(1, Ordering::Relaxed)));
        let config = tmp.repo.path().join("config");
        let text = std::fs::read_to_string(&config).unwrap();
        let text = text.replacen("[core]\n", "[core]\ncollection-id=own.id\n", 1);
        std::fs::write(&config, text).unwrap();
        let auth = GroupAuth::new(tmp.policies(CLAIMS_GROUP), false);
        let der = TestCa::new().leaf("c", &["ostrya:ref:t:@own.id:release/**"]);
        let setup = open_with(&auth, &HeaderMap::new(), &with_der(&der)).unwrap();
        assert_eq!(
            rule_texts(&setup),
            (vec!["t:release/**".to_owned()], Vec::new())
        );
    }

    /// Claims that open no push session get 403 with a message that names
    /// the claims, also where the group opens a push session.
    #[test]
    fn claims_with_no_push_mode_open_no_session() {
        let (auth, _tmp) = auth(&CLAIMS_GROUP.replace("allow=r:**", "allow=w:**"));
        let ca = TestCa::new();
        for uris in [&["ostrya:ref:!secret"][..], &["ostrya:ref:main"]] {
            let refusal = open_with(&auth, &HeaderMap::new(), &with_der(&ca.leaf("c", uris)))
                .err()
                .unwrap();
            assert_eq!(refusal.status, StatusCode::FORBIDDEN);
            assert_eq!(
                refusal.message,
                "the claims of the client certificate open no push session in client group \
                 'certs'"
            );
        }
    }

    /// Claims that do not parse get 403 at the open of a session, and the
    /// server reports one event that names the digest and the URI SAN. The
    /// identification of the request succeeds.
    #[test]
    fn claims_that_do_not_parse_get_403_and_an_event() {
        let (auth, _tmp) = auth(CLAIMS_GROUP);
        let (auth, events) = recording(auth);
        let ca = TestCa::new();
        let many: Vec<String> = (0..=ostrya::access::MAX_CLAIMS)
            .map(|i| format!("ostrya:ref:w:r{i}"))
            .collect();
        let many: Vec<&str> = many.iter().map(String::as_str).collect();
        let san = crate::test_pki::uri_sans(&["ostrya:ref:w:main"]);
        // An id of 256 bytes, one byte over the limit.
        let long = format!("ostrya:ref:@a.{}:x", "b".repeat(254));
        for (der, san, reason) in [
            (
                ca.leaf("c", &["ostrya:ref:w:main", "ostrya:ref:x:a"]),
                Some("ostrya:ref:x:a"),
                "the modes are one or more of r, w, c, t, and d",
            ),
            (
                ca.leaf("c", &["Ostrya:ref:w:main"]),
                Some("Ostrya:ref:w:main"),
                "a claim starts with ostrya:ref:",
            ),
            (
                ca.leaf("c", &["ostrya:collection:a.b:r:a"]),
                Some("ostrya:collection:a.b:r:a"),
                "a claim starts with ostrya:ref:",
            ),
            (
                ca.leaf("c", &["ostrya:ref:@a.b:r:a"]),
                Some("ostrya:ref:@a.b:r:a"),
                "a part is not a valid ref-name component",
            ),
            (
                ca.leaf("c", &["ostrya:ref:@a.b:"]),
                Some("ostrya:ref:@a.b:"),
                "the pattern is empty",
            ),
            (
                ca.leaf("c", &["ostrya:ref:@:a"]),
                Some("ostrya:ref:@:a"),
                "the collection id fails the collection-id rule",
            ),
            (
                ca.leaf("c", &["ostrya:ref:w:@fresh:a"]),
                Some("ostrya:ref:w:@fresh:a"),
                "the collection id fails the collection-id rule",
            ),
            (
                ca.leaf("c", &[long.as_str()]),
                Some(long.as_str()),
                "the collection id is longer than 255 bytes",
            ),
            (
                ca.leaf("c", &many),
                None,
                "the certificate holds more than 256 claims",
            ),
            (
                ca.leaf_with("c", None, &[vec![0x30, 0x03, 0x86, 0x01, 0x80]]),
                None,
                "the subjectAltName extension of the certificate does not decode",
            ),
            (
                ca.leaf_with("c", None, &[san.clone(), san.clone()]),
                None,
                "the certificate holds more than one subjectAltName extension",
            ),
            (
                b"not a certificate".to_vec(),
                None,
                "the certificate does not decode",
            ),
        ] {
            events.lock().unwrap().clear();
            let peer = with_der(&der);
            auth.identify(&HeaderMap::new(), &peer).unwrap();
            let refusal = open_with(&auth, &HeaderMap::new(), &peer).err().unwrap();
            assert_eq!(refusal.status, StatusCode::FORBIDDEN, "{san:?}");
            let fault = match san {
                Some(san) => format!("malformed claim '{san}': {reason}"),
                None => reason.to_owned(),
            };
            assert_eq!(
                refusal.message,
                format!("the claims of the client certificate do not parse: {fault}")
            );
            let digest = Checksum::sha256(&der);
            assert_eq!(
                *events.lock().unwrap(),
                [ServerEvent::BadClaims {
                    digest,
                    san: san.map(str::to_owned),
                    reason: reason.to_owned(),
                }]
            );
            assert_eq!(
                events.lock().unwrap()[0].to_string(),
                format!("refused the client certificate tls-sha256:{digest}: {fault}")
            );
        }
    }

    /// A group with `tls-accept-claim=false` reads no claim: claims that do
    /// not parse get no 403 and no event, and the owner key is the item.
    #[test]
    fn a_group_that_reads_no_claim_ignores_the_claims() {
        let (auth, _tmp) = auth("[ex-ostrya client \"certs\"]\nauth=tls:*\nallow=w:**\n");
        let (auth, events) = recording(auth);
        let ca = TestCa::new();
        for uris in [&["ostrya:ref:x:a"][..], &["ostrya:ref:r:main"]] {
            let peer = with_der(&ca.leaf("c", uris));
            let setup = open_with(&auth, &HeaderMap::new(), &peer).unwrap();
            assert!(Arc::ptr_eq(
                &setup.policy,
                auth.policies.get("certs").unwrap()
            ));
            let client = auth.identify(&HeaderMap::new(), &peer).unwrap();
            assert_eq!(&*client.owner, "tls:*");
            assert!(client.cert.is_none());
        }
        assert!(events.lock().unwrap().is_empty());
    }

    /// A credential, a `net:` item, and the anonymous item take no claims,
    /// also with a client certificate in the request.
    #[test]
    fn an_item_other_than_a_certificate_takes_no_claims() {
        let (auth, _tmp) = auth(&format!(
            "[ex-ostrya client \"mixed\"]\nauth=tls-sha256:{};token-sha256:{}\n\
             tls-accept-claim=true\nallow=w:**\n\
             [ex-ostrya client \"anonymous\"]\nauth=anonymous\nallow=w:**\n",
            hex("unused"),
            hex("a-token")
        ));
        let (auth, events) = recording(auth);
        let der = TestCa::new().leaf("c", &["ostrya:ref:x:a"]);
        let peer = with_der(&der);
        let token = headers(&["Bearer a-token"]);
        let setup = open_with(&auth, &token, &peer).unwrap();
        assert!(Arc::ptr_eq(
            &setup.policy,
            auth.policies.get("mixed").unwrap()
        ));
        let client = auth.identify(&token, &peer).unwrap();
        assert_eq!(&*client.owner, format!("token-sha256:{}", hex("a-token")));
        let setup = open_with(&auth, &HeaderMap::new(), &tls()).unwrap();
        assert!(Arc::ptr_eq(
            &setup.policy,
            auth.policies.get("anonymous").unwrap()
        ));
        assert!(events.lock().unwrap().is_empty());
    }

    /// With a group that reads claims, the owner key of a certificate item
    /// is the digest of the certificate, so two certificates of one item
    /// have two owner keys.
    #[test]
    fn the_owner_key_is_the_digest_of_a_certificate_with_claims() {
        let (auth, _tmp) = auth(CLAIMS_GROUP);
        let ca = TestCa::new();
        let a = ca.leaf("a", &["ostrya:ref:w:a"]);
        let b = ca.leaf("b", &["ostrya:ref:w:b"]);
        let owner = |der: &[u8]| {
            let client = auth.identify(&HeaderMap::new(), &with_der(der)).unwrap();
            GroupAuth::owner(&client).to_owned()
        };
        assert_eq!(owner(&a), format!("tls-sha256:{}", Checksum::sha256(&a)));
        assert_ne!(owner(&a), owner(&b));
    }

    /// A `tls:NAME` item can hold a space. The item `tls:* tls-sha256:HEX`
    /// of a group that reads no claim and the certificate of that digest
    /// under `tls:*` of a group that reads claims have two owner keys.
    #[test]
    fn a_name_with_a_space_shares_no_owner_key_with_a_digest() {
        let ca = TestCa::new();
        let pinned = ca.leaf("pinned", &[]);
        let digest = Checksum::sha256(&pinned);
        let (auth, _tmp) = auth(&format!(
            "{CLAIMS_GROUP}[ex-ostrya client \"named\"]\n\
             auth=tls:* tls-sha256:{digest}\nallow=r:**\n"
        ));
        let named = ca.leaf(&format!("* tls-sha256:{digest}"), &[]);
        let owner = |der: &[u8]| {
            let client = auth.identify(&HeaderMap::new(), &with_der(der)).unwrap();
            GroupAuth::owner(&client).to_owned()
        };
        assert_eq!(owner(&named), format!("tls:* tls-sha256:{digest}"));
        assert_eq!(owner(&pinned), format!("tls-sha256:{digest}"));
        assert_ne!(owner(&named), owner(&pinned));
    }

    /// The claims of a forwarded certificate replace the rules of the group,
    /// as the claims of the certificate of a handshake do.
    #[test]
    fn a_forwarded_certificate_takes_its_claims() {
        let ca = TestCa::new();
        let (auth, _tmp) = auth(CLAIMS_GROUP);
        let forwarding = Forwarding::new(
            vec![IpNet::parse("10.0.0.0/8").unwrap()],
            ostrya::access::ForwardedHeader::XForwardedFor,
        );
        let verifier = ostrya_fetch::client_cert_verifier(ca.pem.as_bytes()).unwrap();
        let certs = ForwardedCerts::new(
            hyper::header::HeaderName::from_static("x-client-cert"),
            verifier,
        );
        let auth = auth.with_proxies(Some(forwarding), Some(certs));
        let (auth, events) = recording(auth);
        let good = ca.leaf("c", &["ostrya:ref:c:main"]);
        let request = lines(&[("x-client-cert", &ostrya::base64::encode(&good))]);
        let setup = open_with(&auth, &request, &proxy()).unwrap();
        assert_eq!(rule_texts(&setup), (vec!["c:main".to_owned()], Vec::new()));
        let bad = ca.leaf("c", &["ostrya:ref:c:main/"]);
        let request = lines(&[("x-client-cert", &ostrya::base64::encode(&bad))]);
        let refusal = open_with(&auth, &request, &proxy()).err().unwrap();
        assert_eq!(refusal.status, StatusCode::FORBIDDEN);
        let events = events.lock().unwrap();
        assert_eq!(events.len(), 1);
        assert!(
            matches!(&events[0], ServerEvent::BadClaims { digest, san: Some(san), .. }
                if *digest == Checksum::sha256(&bad) && san == "ostrya:ref:c:main/"),
            "{events:?}"
        );
    }

    /// A read takes the steps of the identification: a 401 carries the two
    /// challenges, a credential over plain HTTP gets 403, and a request
    /// with nothing gets the anonymous item. A `net:` item matches the
    /// address that a trusted proxy forwards. The rights carry the item.
    #[test]
    fn a_read_takes_the_steps_of_the_identification() {
        let (auth, _tmp) = proxied(&format!(
            "{}[ex-ostrya client \"farm\"]\nauth=net:198.51.100.0/24\nallow=r:farm/**\n\
             [ex-ostrya client \"anonymous\"]\nauth=anonymous\nallow=r:pub/**\n",
            credentials()
        ));
        let refusal = auth
            .identify_read(&headers(&["Bearer x", "Bearer y"]), &tls())
            .unwrap_err();
        assert_eq!(refusal.status, StatusCode::UNAUTHORIZED);
        assert_eq!(refusal.headers.len(), 2);
        let refusal = auth
            .identify_read(&headers(&["Bearer a-token"]), &stranger())
            .unwrap_err();
        assert_eq!(refusal.status, StatusCode::FORBIDDEN);
        assert!(refusal.headers.is_empty());

        let read = auth.identify_read(&HeaderMap::new(), &stranger()).unwrap();
        assert_eq!(read.item(), &AuthItem::Anonymous);
        assert!(read.can_read_path("refs/heads/pub/a"));
        assert!(!read.can_read_path("refs/heads/farm/a"));
        let forwarded = lines(&[("x-forwarded-for", "198.51.100.7")]);
        let read = auth.identify_read(&forwarded, &proxy()).unwrap();
        assert_eq!(read.item().to_string(), "net:198.51.100.0/24");
        assert!(read.can_read_path("refs/heads/farm/a"));
        assert!(!read.can_read_path("refs/heads/pub/a"));
        let read = auth
            .identify_read(&headers(&["Bearer a-token"]), &tls())
            .unwrap();
        assert!(read.can_read_path("refs/heads/any"));
        assert!(read.summary_digest().is_none());

        let (auth, _tmp) = self::auth(&credentials());
        let refusal = auth.identify_read(&HeaderMap::new(), &tls()).unwrap_err();
        assert_eq!(refusal.status, StatusCode::UNAUTHORIZED);
        assert_eq!(refusal.headers.len(), 2);
    }

    /// The claims of a certificate set the read rights in a group with
    /// `tls-accept-claim=true`. Claims that do not parse refuse each read
    /// with 403, and the server reports one event for each read.
    #[test]
    fn claims_set_the_read_rights_and_bad_claims_refuse_a_read() {
        let (auth, _tmp) = auth(CLAIMS_GROUP);
        let (auth, events) = recording(auth);
        let ca = TestCa::new();
        let good = with_der(&ca.leaf("c", &["ostrya:ref:r:main"]));
        let read = auth.identify_read(&HeaderMap::new(), &good).unwrap();
        assert!(read.can_read_path("refs/heads/main"));
        assert!(!read.can_read_path("refs/heads/other"));
        let plain = with_der(&ca.leaf("c", &[]));
        let read = auth.identify_read(&HeaderMap::new(), &plain).unwrap();
        assert!(read.can_read_path("refs/heads/other"));
        assert!(events.lock().unwrap().is_empty());

        let der = ca.leaf("c", &["ostrya:ref:x:a"]);
        let bad = with_der(&der);
        for _ in 0..2 {
            let refusal = auth.identify_read(&HeaderMap::new(), &bad).unwrap_err();
            assert_eq!(refusal.status, StatusCode::FORBIDDEN);
            assert!(refusal.headers.is_empty());
        }
        let events = events.lock().unwrap();
        assert_eq!(events.len(), 2);
        assert!(
            matches!(&events[0], ServerEvent::BadClaims { digest, .. }
                if *digest == Checksum::sha256(&der)),
            "{events:?}"
        );
    }

    /// A group with `tls-accept-claim=false` reads no claim on a read:
    /// claims that do not parse get no 403 and no event.
    #[test]
    fn a_read_of_a_group_that_reads_no_claim_ignores_the_claims() {
        let (auth, _tmp) = auth("[ex-ostrya client \"certs\"]\nauth=tls:*\nallow=r:**\n");
        let (auth, events) = recording(auth);
        let peer = with_der(&TestCa::new().leaf("c", &["ostrya:ref:x:a"]));
        let read = auth.identify_read(&HeaderMap::new(), &peer).unwrap();
        assert!(read.can_read_path("refs/heads/main"));
        assert!(events.lock().unwrap().is_empty());
    }

    /// The time of the request checks the validity of a forwarded
    /// certificate.
    #[test]
    fn an_expired_forwarded_certificate_is_403() {
        let (auth, _tmp) = proxied("[ex-ostrya client \"c\"]\nauth=tls:*\nallow=w:**\n");
        let request = lines(&[("x-client-cert", &base64_der(CLIENT_PEM))]);
        let late = UnixTime::since_unix_epoch(std::time::Duration::from_secs(4_938_667_341));
        let refusal = auth.identify_at(&request, &proxy(), late).unwrap_err();
        assert_eq!(refusal.status, StatusCode::FORBIDDEN);
        assert_eq!(&*auth.identify(&request, &proxy()).unwrap().owner, "tls:*");
    }
}