keri-http-auth 0.1.0

KERI HTTP message signatures and request authentication
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
//! The `Signature-Input` header and the signature base it describes.
//!
//! A signed request carries two headers. `Signature` holds the signature material and is
//! [`crate::fields`]' concern; `Signature-Input` says *what* that signature covers, and
//! the bytes actually signed — the **signature base** — are derived from it:
//!
//! ```text
//! Signature-Input: signify=("@method" "@path" "signify-resource" "signify-timestamp");created=1609459200;keyid="DN54…";alg="ed25519"
//! ```
//!
//! ```text
//! "@method": POST
//! "@path": /boot
//! "signify-resource": EWJk…
//! "signify-timestamp": 2022-09-24T00:05:48.196795+00:00
//! "@signature-params: (@method @path signify-resource signify-timestamp);created=1609459200;keyid=DN54…;alg=ed25519"
//! ```
//!
//! This module ports `siginput` and `desiginput` from `src/keri/core/httping.ts`.
//!
//! # Two grammars, one value
//!
//! The header and the base spell the same information differently, and the difference is
//! protocol-required rather than accidental: the header quotes each covered component and each
//! textual parameter, while the base leaves both bare, and the base's trailing item puts the
//! opening quote *before* `@signature-params` rather than around it. Both spellings are reproduced
//! exactly.
//!
//! The header is close to an RFC 8941 dictionary but the accepted space is deliberately narrower,
//! so that every accepted header has exactly one signature base.

use core::fmt;
use std::error::Error;
use std::str::FromStr;

use crate::fields::{FieldText, SignatureFieldError};
use crate::header::{HeaderError, HeaderName, Headers};

/// Name of the header this module formats and parses.
pub const SIGNATURE_INPUT_HEADER_NAME: &str = "Signature-Input";

/// Name of the header carrying the timestamp a KERIA request covers.
pub const SIGNIFY_TIMESTAMP_HEADER_NAME: &str = "Signify-Timestamp";

/// Name of the header carrying the identifier a KERIA request speaks for.
pub const SIGNIFY_RESOURCE_HEADER_NAME: &str = "Signify-Resource";

/// Derived component naming the request method.
pub const METHOD_COMPONENT: &str = "@method";

/// Derived component naming the request path.
pub const PATH_COMPONENT: &str = "@path";

/// Name of the trailing signature-base item that commits to the parameters themselves.
pub const SIGNATURE_PARAMS_COMPONENT: &str = "@signature-params";

/// The `alg` parameter value every Signify signature carries.
pub const ED25519_ALGORITHM: &str = "ed25519";

/// Default label for the one signature input a Signify client emits.
pub const SIGNIFY_LABEL: &str = "signify";

/// Largest `Signature-Input` value this module parses or produces, in bytes.
pub const MAX_SIGNATURE_INPUT_BYTES: usize = 16 * 1024;

/// Largest signature base this module produces, in bytes.
pub const MAX_SIGNATURE_BASE_BYTES: usize = 64 * 1024;

/// Largest label, in bytes.
pub const MAX_LABEL_BYTES: usize = 64;

/// Largest number of covered components in one input.
pub const MAX_COMPONENTS: usize = 64;

/// Largest number of labelled inputs in one header.
pub const MAX_INPUTS: usize = 16;

/// Largest request path, in bytes.
pub const MAX_REQUEST_PATH_BYTES: usize = 4096;

/// Largest request method, in bytes.
pub const MAX_REQUEST_METHOD_BYTES: usize = 64;

/// Largest value an RFC 8941 integer parameter may hold.
pub const MAX_PARAMETER_INTEGER: u64 = 999_999_999_999_999;

/// One validated dictionary label naming a signature input.
///
/// The accepted text is the RFC 8941 dictionary key: a lower-case letter or `*`, then lower-case
/// letters, digits, `_`, `-`, `.`, and `*`. The reference's own serializer refuses anything else,
/// so an upper-case label would produce a header it could not emit.
///
/// ```
/// use keri_http_auth::input::InputLabel;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// assert_eq!(InputLabel::signify().as_str(), "signify");
/// assert!(InputLabel::new("Signify").is_err());
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct InputLabel {
    text: String,
}

impl InputLabel {
    /// Returns the `signify` label every Signify client uses.
    #[must_use]
    pub fn signify() -> Self {
        Self {
            text: SIGNIFY_LABEL.to_owned(),
        }
    }

    /// Validates one label.
    ///
    /// # Errors
    ///
    /// Returns [`SignatureInputError::InvalidLabel`] for empty input, for a label above
    /// [`MAX_LABEL_BYTES`], or for a byte outside the dictionary-key set.
    pub fn new(text: impl Into<String>) -> Result<Self, SignatureInputError> {
        let text = text.into();
        let invalid = || SignatureInputError::InvalidLabel { label: bounded(&text) };
        if text.is_empty() || text.len() > MAX_LABEL_BYTES {
            return Err(invalid());
        }
        let mut bytes = text.bytes();
        match bytes.next() {
            Some(byte) if byte.is_ascii_lowercase() || byte == b'*' => {}
            _ => return Err(invalid()),
        }
        if !bytes.all(is_key_byte) {
            return Err(invalid());
        }
        Ok(Self { text })
    }

    /// Returns the label.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.text
    }
}

impl FromStr for InputLabel {
    type Err = SignatureInputError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        Self::new(input)
    }
}

impl fmt::Display for InputLabel {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.text)
    }
}

/// Returns whether a byte may appear after the first character of a dictionary key.
const fn is_key_byte(byte: u8) -> bool {
    byte.is_ascii_lowercase() || byte.is_ascii_digit() || matches!(byte, b'_' | b'-' | b'.' | b'*')
}

/// One thing a signature covers.
///
/// The reference accepts any `@`-prefixed name but implements only `@method` and `@path`: every
/// other derived component is listed in the header and then contributes **nothing** to the base, so
/// the header advertises coverage the signature does not provide. Only the two that exist are
/// representable here.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum SignatureComponent {
    /// The request method, written `@method`.
    Method,
    /// The request path, written `@path`.
    Path,
    /// One request or response header.
    Header(HeaderName),
}

impl SignatureComponent {
    /// Reads one covered-component name.
    ///
    /// # Errors
    ///
    /// Returns [`SignatureInputError::UnknownDerivedComponent`] for an `@`-name other than
    /// `@method` or `@path`, and [`SignatureInputError::Header`] for an invalid field name.
    pub fn new(name: &str) -> Result<Self, SignatureInputError> {
        match name {
            METHOD_COMPONENT => Ok(Self::Method),
            PATH_COMPONENT => Ok(Self::Path),
            _ if name.starts_with('@') => Err(SignatureInputError::UnknownDerivedComponent { name: bounded(name) }),
            _ => Ok(Self::Header(HeaderName::new(name)?)),
        }
    }

    /// Returns the name this component is written under, in the header and in the base.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::Method => METHOD_COMPONENT,
            Self::Path => PATH_COMPONENT,
            Self::Header(name) => name.as_str(),
        }
    }

    /// Returns whether two components name the same thing.
    fn is_same(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Method, Self::Method) | (Self::Path, Self::Path) => true,
            (Self::Header(left), Self::Header(right)) => left == right,
            _ => false,
        }
    }
}

impl FromStr for SignatureComponent {
    type Err = SignatureInputError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        Self::new(input)
    }
}

impl fmt::Display for SignatureComponent {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// Returns the components `Authenticater.DefaultFields` covers, in its order.
///
/// Every KERIA request covers exactly these four: the method, the path, the identifier the request
/// speaks for, and its timestamp. The two header names are lower-case, as upstream, which is also
/// the spelling that interoperates with keripy.
///
/// # Errors
///
/// Returns [`SignatureInputError::Header`] only if the constant names ever stop being valid field
/// names, which the unit tests assert they do not.
pub fn default_components() -> Result<Vec<SignatureComponent>, SignatureInputError> {
    Ok(vec![
        SignatureComponent::Method,
        SignatureComponent::Path,
        SignatureComponent::Header(HeaderName::new(SIGNIFY_RESOURCE_HEADER_NAME.to_ascii_lowercase())?),
        SignatureComponent::Header(HeaderName::new(SIGNIFY_TIMESTAMP_HEADER_NAME.to_ascii_lowercase())?),
    ])
}

/// The method and path a signature covers.
///
/// The reference validates neither: a path carrying a newline forges a whole component line in the
/// signature base, since the base joins its items with one. Both are validated here.
///
/// The path must carry no query, matching every upstream call site — the client strips `?` before
/// signing — so a caller cannot accidentally cover a query the peer will not.
///
/// ```
/// use keri_http_auth::input::RequestTarget;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let target = RequestTarget::new("POST", "/boot")?;
/// assert_eq!(target.method(), "POST");
/// assert!(RequestTarget::new("GET", "/a\n\"@method\": DELETE").is_err());
/// assert!(RequestTarget::new("GET", "/identifiers?limit=10").is_err());
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RequestTarget {
    method: String,
    path: String,
}

impl RequestTarget {
    /// Validates one method and path.
    ///
    /// # Errors
    ///
    /// Returns [`SignatureInputError::InvalidRequestMethod`] for an empty, oversized, or
    /// non-token method, and [`SignatureInputError::InvalidRequestPath`] for a path that is empty,
    /// oversized, does not start with `/`, carries a query or fragment, or holds a byte outside
    /// visible ASCII.
    pub fn new(method: &str, path: &str) -> Result<Self, SignatureInputError> {
        if method.is_empty() || method.len() > MAX_REQUEST_METHOD_BYTES || !method.bytes().all(is_method_byte) {
            return Err(SignatureInputError::InvalidRequestMethod {
                method: bounded(method),
            });
        }
        if path.is_empty()
            || path.len() > MAX_REQUEST_PATH_BYTES
            || !path.starts_with('/')
            || !path.bytes().all(is_path_byte)
        {
            return Err(SignatureInputError::InvalidRequestPath { path: bounded(path) });
        }
        Ok(Self {
            method: method.to_owned(),
            path: path.to_owned(),
        })
    }

    /// Returns the method.
    #[must_use]
    pub fn method(&self) -> &str {
        &self.method
    }

    /// Returns the path, which carries no query.
    #[must_use]
    pub fn path(&self) -> &str {
        &self.path
    }
}

/// Returns whether a byte may appear in a request method.
const fn is_method_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'*' | b'!' | b'~' | b'\'' | b'+' | b'`')
}

/// Returns whether a byte may appear in a covered request path.
const fn is_path_byte(byte: u8) -> bool {
    byte.is_ascii_graphic() && !matches!(byte, b'?' | b'#' | b'"')
}

/// The exact bytes a signature commits to.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SignatureBase {
    text: String,
}

impl SignatureBase {
    /// Returns the base as text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.text
    }

    /// Returns the exact bytes signed, which are the UTF-8 encoding of the text.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        self.text.as_bytes()
    }
}

impl fmt::Display for SignatureBase {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.text)
    }
}

/// One labelled signature input: what a signature covers, and the parameters describing it.
///
/// Build one with [`SignatureInputBuilder`], or recover one by parsing a
/// [`SignatureInputHeader`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SignatureInput {
    label: InputLabel,
    components: Vec<SignatureComponent>,
    created: u64,
    expires: Option<u64>,
    nonce: Option<FieldText>,
    key_identifier: Option<FieldText>,
    context: Option<FieldText>,
    algorithm: Option<FieldText>,
}

impl SignatureInput {
    /// Returns the label this input is written under.
    #[must_use]
    pub const fn label(&self) -> &InputLabel {
        &self.label
    }

    /// Returns the covered components in the order they are signed.
    #[must_use]
    pub fn components(&self) -> &[SignatureComponent] {
        &self.components
    }

    /// Returns the creation time, in seconds since the Unix epoch.
    ///
    /// The reference takes this from the system clock. It is an input here, so a protocol crate
    /// never reads the clock and a signature is reproducible; deciding whether a received value is
    /// fresh enough is a replay policy, which belongs to the transport.
    #[must_use]
    pub const fn created(&self) -> u64 {
        self.created
    }

    /// Returns the expiry time, when one is present.
    #[must_use]
    pub const fn expires(&self) -> Option<u64> {
        self.expires
    }

    /// Returns the nonce, when one is present.
    #[must_use]
    pub fn nonce(&self) -> Option<&str> {
        self.nonce.as_ref().map(FieldText::as_str)
    }

    /// Returns the key identifier, when one is present.
    ///
    /// For a Signify request this is the signer's qualified verification key.
    #[must_use]
    pub fn key_identifier(&self) -> Option<&str> {
        self.key_identifier.as_ref().map(FieldText::as_str)
    }

    /// Returns the implementation-specific context, when one is present.
    #[must_use]
    pub fn context(&self) -> Option<&str> {
        self.context.as_ref().map(FieldText::as_str)
    }

    /// Returns the signature algorithm as written, when one is present.
    #[must_use]
    pub fn algorithm(&self) -> Option<&str> {
        self.algorithm.as_ref().map(FieldText::as_str)
    }

    /// Builds the exact bytes a signature over this input commits to.
    ///
    /// # Errors
    ///
    /// Returns [`SignatureInputError::MissingCoveredHeader`] when a covered header is absent — the
    /// reference silently drops it, so the caller gets a weaker signature than it asked for — and
    /// [`SignatureInputError::SignatureBaseTooLong`] above [`MAX_SIGNATURE_BASE_BYTES`].
    pub fn signature_base(
        &self,
        target: &RequestTarget,
        headers: &Headers,
    ) -> Result<SignatureBase, SignatureInputError> {
        let mut items: Vec<String> = Vec::with_capacity(self.components.len().saturating_add(1));
        for component in &self.components {
            let value = match component {
                SignatureComponent::Method => target.method(),
                SignatureComponent::Path => target.path(),
                SignatureComponent::Header(name) => {
                    headers
                        .get(name.as_str())
                        .ok_or_else(|| SignatureInputError::MissingCoveredHeader {
                            name: bounded(name.as_str()),
                        })?
                }
            };
            items.push(format!("\"{}\": {value}", component.as_str()));
        }
        // The opening quote sits before `@signature-params` and the closing one after the last
        // parameter, so the whole parameter list is inside one quoted item. That is the reference's
        // spelling, and changing it would change every signature.
        items.push(format!(
            "\"{SIGNATURE_PARAMS_COMPONENT}: {}\"",
            self.render_parameters(false)
        ));

        let text = items.join("\n");
        if text.len() > MAX_SIGNATURE_BASE_BYTES {
            return Err(SignatureInputError::SignatureBaseTooLong {
                length: text.len(),
                maximum: MAX_SIGNATURE_BASE_BYTES,
            });
        }
        Ok(SignatureBase { text })
    }

    /// Renders the covered list and parameters, quoted for the header or bare for the base.
    fn render_parameters(&self, quoted: bool) -> String {
        let names = self
            .components
            .iter()
            .map(|component| {
                if quoted {
                    format!("\"{}\"", component.as_str())
                } else {
                    component.as_str().to_owned()
                }
            })
            .collect::<Vec<_>>()
            .join(" ");

        let mut rendered = format!("({names});created={}", self.created);
        if let Some(expires) = self.expires {
            rendered.push_str(";expires=");
            rendered.push_str(&expires.to_string());
        }
        for (name, value) in [
            ("nonce", self.nonce.as_ref()),
            ("keyid", self.key_identifier.as_ref()),
            ("context", self.context.as_ref()),
            ("alg", self.algorithm.as_ref()),
        ] {
            let Some(value) = value else { continue };
            rendered.push(';');
            rendered.push_str(name);
            rendered.push('=');
            if quoted {
                rendered.push('"');
                rendered.push_str(value.as_str());
                rendered.push('"');
            } else {
                rendered.push_str(value.as_str());
            }
        }
        rendered
    }

    /// Renders this input as one dictionary member.
    fn render(&self) -> String {
        format!("{}={}", self.label, self.render_parameters(true))
    }
}

/// Builds one [`SignatureInput`].
///
/// ```
/// use keri_http_auth::input::{
///     InputLabel, SignatureInputBuilder, SignatureInputHeader, default_components,
/// };
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let input = SignatureInputBuilder::new(InputLabel::signify(), default_components()?, 1_609_459_200)
///     .algorithm("ed25519".parse()?)
///     .build()?;
/// let header = SignatureInputHeader::new(vec![input])?;
///
/// assert_eq!(
///     header.to_header_value()?,
///     concat!(
///         r#"signify=("@method" "@path" "signify-resource" "signify-timestamp");"#,
///         r#"created=1609459200;alg="ed25519""#,
///     ),
/// );
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct SignatureInputBuilder {
    label: InputLabel,
    components: Vec<SignatureComponent>,
    created: u64,
    expires: Option<u64>,
    nonce: Option<FieldText>,
    key_identifier: Option<FieldText>,
    context: Option<FieldText>,
    algorithm: Option<FieldText>,
}

impl SignatureInputBuilder {
    /// Starts an input from the three things it always has: a label, what it covers, and when it
    /// was created.
    #[must_use]
    pub const fn new(label: InputLabel, components: Vec<SignatureComponent>, created: u64) -> Self {
        Self {
            label,
            components,
            created,
            expires: None,
            nonce: None,
            key_identifier: None,
            context: None,
            algorithm: None,
        }
    }

    /// Sets the expiry time, in seconds since the Unix epoch.
    #[must_use]
    pub const fn expires(mut self, expires: u64) -> Self {
        self.expires = Some(expires);
        self
    }

    /// Sets the nonce.
    #[must_use]
    pub fn nonce(mut self, nonce: FieldText) -> Self {
        self.nonce = Some(nonce);
        self
    }

    /// Sets the key identifier, which for a Signify request is the qualified verification key.
    #[must_use]
    pub fn key_identifier(mut self, key_identifier: FieldText) -> Self {
        self.key_identifier = Some(key_identifier);
        self
    }

    /// Sets the implementation-specific context.
    #[must_use]
    pub fn context(mut self, context: FieldText) -> Self {
        self.context = Some(context);
        self
    }

    /// Sets the signature algorithm, which Signify always spells [`ED25519_ALGORITHM`].
    #[must_use]
    pub fn algorithm(mut self, algorithm: FieldText) -> Self {
        self.algorithm = Some(algorithm);
        self
    }

    /// Validates the input.
    ///
    /// # Errors
    ///
    /// Returns [`SignatureInputError::NoComponents`] when nothing is covered — the reference emits
    /// and accepts a signature binding neither method, path, nor header —
    /// [`SignatureInputError::TooManyComponents`] above [`MAX_COMPONENTS`],
    /// [`SignatureInputError::DuplicateComponent`] when one thing is covered twice;
    /// [`SignatureInputError::ParameterOutOfRange`] when a time exceeds
    /// [`MAX_PARAMETER_INTEGER`], which the header grammar cannot express; and
    /// [`SignatureInputError::InvalidParameterValue`] for a value carrying a backslash, which the
    /// header would escape and the base would not — the reference's own escaping defect.
    pub fn build(self) -> Result<SignatureInput, SignatureInputError> {
        if self.components.is_empty() {
            return Err(SignatureInputError::NoComponents);
        }
        if self.components.len() > MAX_COMPONENTS {
            return Err(SignatureInputError::TooManyComponents {
                count: self.components.len(),
                maximum: MAX_COMPONENTS,
            });
        }
        for (position, component) in self.components.iter().enumerate() {
            if self
                .components
                .iter()
                .take(position)
                .any(|earlier| earlier.is_same(component))
            {
                return Err(SignatureInputError::DuplicateComponent {
                    name: bounded(component.as_str()),
                });
            }
        }
        for (name, value) in [("created", Some(self.created)), ("expires", self.expires)] {
            if value.is_some_and(|value| value > MAX_PARAMETER_INTEGER) {
                return Err(SignatureInputError::ParameterOutOfRange { name });
            }
        }
        // `FieldText` already excludes every byte that would break either grammar except the
        // backslash, which an RFC 8941 string escapes and the signature base does not — so a value
        // carrying one would render a header the parser refuses and bytes the peer never signed.
        for (name, value) in [
            ("nonce", self.nonce.as_ref()),
            ("keyid", self.key_identifier.as_ref()),
            ("context", self.context.as_ref()),
            ("alg", self.algorithm.as_ref()),
        ] {
            if value.is_some_and(|value| value.as_str().contains('\\')) {
                return Err(SignatureInputError::InvalidParameterValue { name });
            }
        }

        Ok(SignatureInput {
            label: self.label,
            components: self.components,
            created: self.created,
            expires: self.expires,
            nonce: self.nonce,
            key_identifier: self.key_identifier,
            context: self.context,
            algorithm: self.algorithm,
        })
    }
}

/// One complete `Signature-Input` header value: a dictionary of labelled inputs.
///
/// A Signify client emits exactly one member; the parser accepts several, as the reference does.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SignatureInputHeader {
    inputs: Vec<SignatureInput>,
}

impl SignatureInputHeader {
    /// Name of the header this value belongs to.
    pub const NAME: &'static str = SIGNATURE_INPUT_HEADER_NAME;

    /// Collects one or more inputs into a header value.
    ///
    /// # Errors
    ///
    /// Returns [`SignatureInputError::NoInputs`] for an empty list,
    /// [`SignatureInputError::TooManyInputs`] above [`MAX_INPUTS`], and
    /// [`SignatureInputError::DuplicateLabel`] for a repeated label — a dictionary keeps only the
    /// last member with a given key, so upstream the earlier input silently disappears.
    pub fn new(inputs: Vec<SignatureInput>) -> Result<Self, SignatureInputError> {
        if inputs.is_empty() {
            return Err(SignatureInputError::NoInputs);
        }
        if inputs.len() > MAX_INPUTS {
            return Err(SignatureInputError::TooManyInputs {
                count: inputs.len(),
                maximum: MAX_INPUTS,
            });
        }
        for (position, input) in inputs.iter().enumerate() {
            if inputs
                .iter()
                .take(position)
                .any(|earlier| earlier.label() == input.label())
            {
                return Err(SignatureInputError::DuplicateLabel {
                    label: bounded(input.label().as_str()),
                });
            }
        }
        Ok(Self { inputs })
    }

    /// Returns the inputs in header order.
    #[must_use]
    pub fn inputs(&self) -> &[SignatureInput] {
        &self.inputs
    }

    /// Returns the input written under one label.
    #[must_use]
    pub fn find(&self, label: &InputLabel) -> Option<&SignatureInput> {
        self.inputs.iter().find(|input| input.label() == label)
    }

    /// Renders the complete header value.
    ///
    /// # Errors
    ///
    /// Returns [`SignatureInputError::SignatureInputTooLong`] above
    /// [`MAX_SIGNATURE_INPUT_BYTES`].
    pub fn to_header_value(&self) -> Result<String, SignatureInputError> {
        // Two members are separated by a comma and a space, which is what the reference's
        // serializer emits; `fixtures/authing/v1.json` pins the exact spelling.
        let rendered = self
            .inputs
            .iter()
            .map(SignatureInput::render)
            .collect::<Vec<_>>()
            .join(", ");
        if rendered.len() > MAX_SIGNATURE_INPUT_BYTES {
            return Err(SignatureInputError::SignatureInputTooLong {
                length: rendered.len(),
                maximum: MAX_SIGNATURE_INPUT_BYTES,
            });
        }
        Ok(rendered)
    }

    /// Parses one complete header value.
    ///
    /// # Errors
    ///
    /// Returns [`SignatureInputError::SignatureInputTooLong`] above
    /// [`MAX_SIGNATURE_INPUT_BYTES`], [`SignatureInputError::Malformed`] for anything the narrowed
    /// dictionary grammar does not accept, [`SignatureInputError::MissingCreated`] for an input
    /// without the one required parameter, [`SignatureInputError::UnknownParameter`] for a
    /// parameter the reference would silently ignore, and
    /// [`SignatureInputError::ParametersOutOfOrder`] when the parameters are not in the order both
    /// implementations emit and rebuild them in.
    pub fn parse(value: &str) -> Result<Self, SignatureInputError> {
        if value.len() > MAX_SIGNATURE_INPUT_BYTES {
            return Err(SignatureInputError::SignatureInputTooLong {
                length: value.len(),
                maximum: MAX_SIGNATURE_INPUT_BYTES,
            });
        }
        let mut parser = Parser::new(value);
        let mut inputs = Vec::new();
        loop {
            inputs.push(parser.parse_input()?);
            if !parser.eat(b',') {
                break;
            }
            parser.skip_whitespace();
        }
        if !parser.is_finished() {
            return Err(parser.malformed("trailing material"));
        }
        Self::new(inputs)
    }
}

impl FromStr for SignatureInputHeader {
    type Err = SignatureInputError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        Self::parse(input)
    }
}

impl TryFrom<&str> for SignatureInputHeader {
    type Error = SignatureInputError;

    fn try_from(input: &str) -> Result<Self, Self::Error> {
        Self::parse(input)
    }
}

/// Every parameter name, in the order both implementations write and rebuild them.
const PARAMETER_ORDER: [&str; 6] = ["created", "expires", "nonce", "keyid", "context", "alg"];

/// A cursor over one header value.
struct Parser<'input> {
    bytes: &'input [u8],
    position: usize,
}

impl<'input> Parser<'input> {
    const fn new(value: &'input str) -> Self {
        Self {
            bytes: value.as_bytes(),
            position: 0,
        }
    }

    fn peek(&self) -> Option<u8> {
        self.bytes.get(self.position).copied()
    }

    fn eat(&mut self, byte: u8) -> bool {
        if self.peek() == Some(byte) {
            self.position = self.position.saturating_add(1);
            return true;
        }
        false
    }

    fn skip_whitespace(&mut self) {
        while matches!(self.peek(), Some(b' ' | b'\t')) {
            self.position = self.position.saturating_add(1);
        }
    }

    const fn is_finished(&self) -> bool {
        self.position >= self.bytes.len()
    }

    fn malformed(&self, reason: &'static str) -> SignatureInputError {
        SignatureInputError::Malformed {
            reason,
            offset: self.position,
        }
    }

    /// Reads a run of bytes satisfying `accept`, returning it as text.
    fn take_while(&mut self, accept: fn(u8) -> bool) -> &'input str {
        let start = self.position;
        while self.peek().is_some_and(accept) {
            self.position = self.position.saturating_add(1);
        }
        // Every accepted byte is ASCII, so the slice is always valid UTF-8.
        self.bytes
            .get(start..self.position)
            .and_then(|slice| core::str::from_utf8(slice).ok())
            .unwrap_or_default()
    }

    /// Reads one `label=(components);parameters` member.
    fn parse_input(&mut self) -> Result<SignatureInput, SignatureInputError> {
        let label = InputLabel::new(self.take_while(|byte| is_key_byte(byte) || byte == b'*'))?;
        if !self.eat(b'=') {
            return Err(self.malformed("a member needs a value"));
        }
        let components = self.parse_components()?;

        let mut created = None;
        let mut expires = None;
        let mut nonce = None;
        let mut key_identifier = None;
        let mut context = None;
        let mut algorithm = None;
        let mut last_position: Option<usize> = None;

        while self.eat(b';') {
            let name = self.take_while(is_key_byte);
            if name.is_empty() {
                return Err(self.malformed("a parameter needs a name"));
            }
            let Some(order) = PARAMETER_ORDER.iter().position(|known| *known == name) else {
                return Err(SignatureInputError::UnknownParameter { name: bounded(name) });
            };
            if last_position.is_some_and(|last| order <= last) {
                return Err(SignatureInputError::ParametersOutOfOrder);
            }
            last_position = Some(order);
            if !self.eat(b'=') {
                return Err(self.malformed("a parameter needs a value"));
            }
            match name {
                "created" => created = Some(self.parse_integer()?),
                "expires" => expires = Some(self.parse_integer()?),
                "nonce" => nonce = Some(self.parse_text()?),
                "keyid" => key_identifier = Some(self.parse_text()?),
                "context" => context = Some(self.parse_text()?),
                _ => algorithm = Some(self.parse_text()?),
            }
        }

        let created = created.ok_or(SignatureInputError::MissingCreated {
            label: bounded(label.as_str()),
        })?;
        let mut builder = SignatureInputBuilder::new(label, components, created);
        if let Some(expires) = expires {
            builder = builder.expires(expires);
        }
        if let Some(nonce) = nonce {
            builder = builder.nonce(nonce);
        }
        if let Some(key_identifier) = key_identifier {
            builder = builder.key_identifier(key_identifier);
        }
        if let Some(context) = context {
            builder = builder.context(context);
        }
        if let Some(algorithm) = algorithm {
            builder = builder.algorithm(algorithm);
        }
        builder.build()
    }

    /// Reads the parenthesized list of covered components.
    fn parse_components(&mut self) -> Result<Vec<SignatureComponent>, SignatureInputError> {
        if !self.eat(b'(') {
            return Err(self.malformed("a covered list must start with `(`"));
        }
        let mut components = Vec::new();
        self.skip_whitespace();
        loop {
            if self.eat(b')') {
                return Ok(components);
            }
            if components.len() >= MAX_COMPONENTS {
                return Err(SignatureInputError::TooManyComponents {
                    count: components.len().saturating_add(1),
                    maximum: MAX_COMPONENTS,
                });
            }
            let name = self.parse_quoted()?;
            components.push(SignatureComponent::new(name)?);
            match self.peek() {
                Some(b' ' | b'\t') => self.skip_whitespace(),
                Some(b')') => {}
                _ => return Err(self.malformed("a covered list needs a space or `)` after a name")),
            }
        }
    }

    /// Reads one quoted string, refusing the escapes the signature base cannot express.
    fn parse_quoted(&mut self) -> Result<&'input str, SignatureInputError> {
        if !self.eat(b'"') {
            return Err(self.malformed("a quoted string must start with `\"`"));
        }
        // A backslash escape renders differently in the header and in the base, so the two would
        // disagree about the bytes signed; it is refused rather than unescaped.
        let text = self.take_while(|byte| matches!(byte, 0x20..=0x7e) && byte != b'"' && byte != b'\\');
        if !self.eat(b'"') {
            return Err(self.malformed("a quoted string is unterminated or holds an escape"));
        }
        Ok(text)
    }

    /// Reads one quoted parameter value.
    fn parse_text(&mut self) -> Result<FieldText, SignatureInputError> {
        let text = self.parse_quoted()?;
        Ok(FieldText::new(text)?)
    }

    /// Reads one non-negative integer parameter.
    fn parse_integer(&mut self) -> Result<u64, SignatureInputError> {
        if self.peek() == Some(b'-') {
            return Err(self.malformed("a time parameter may not be negative"));
        }
        let digits = self.take_while(|byte| byte.is_ascii_digit());
        if digits.is_empty() {
            return Err(self.malformed("a time parameter needs digits"));
        }
        // A leading zero is not the canonical spelling, so it would not re-render unchanged.
        if digits.len() > 1 && digits.starts_with('0') {
            return Err(self.malformed("a time parameter may not carry a leading zero"));
        }
        digits
            .parse::<u64>()
            .ok()
            .filter(|value| *value <= MAX_PARAMETER_INTEGER)
            .ok_or(SignatureInputError::ParameterOutOfRange { name: "time" })
    }
}

/// Copies at most thirty-two characters of rejected text into an error.
fn bounded(text: &str) -> String {
    text.chars().take(32).collect()
}

/// Stable `Signature-Input` and canonicalization failures.
#[derive(Debug)]
#[non_exhaustive]
pub enum SignatureInputError {
    /// A header value exceeds [`MAX_SIGNATURE_INPUT_BYTES`].
    SignatureInputTooLong {
        /// Actual length in bytes.
        length: usize,
        /// Accepted maximum.
        maximum: usize,
    },
    /// A signature base exceeds [`MAX_SIGNATURE_BASE_BYTES`].
    SignatureBaseTooLong {
        /// Actual length in bytes.
        length: usize,
        /// Accepted maximum.
        maximum: usize,
    },
    /// A label is not an RFC 8941 dictionary key.
    InvalidLabel {
        /// Bounded copy of the rejected label.
        label: String,
    },
    /// A derived component other than `@method` or `@path` was named.
    ///
    /// The reference lists it and then covers nothing for it.
    UnknownDerivedComponent {
        /// Bounded copy of the rejected name.
        name: String,
    },
    /// A request method is empty, oversized, or not a token.
    InvalidRequestMethod {
        /// Bounded copy of the rejected method.
        method: String,
    },
    /// A request path is empty, oversized, relative, carries a query, or holds an unusable byte.
    InvalidRequestPath {
        /// Bounded copy of the rejected path.
        path: String,
    },
    /// An input covers nothing, so its signature binds nothing.
    NoComponents,
    /// An input covers more than [`MAX_COMPONENTS`] things.
    TooManyComponents {
        /// Actual count.
        count: usize,
        /// Accepted maximum.
        maximum: usize,
    },
    /// One thing is covered twice.
    DuplicateComponent {
        /// Bounded copy of the repeated name.
        name: String,
    },
    /// A covered header is absent, so the base cannot be built.
    ///
    /// The reference drops the component instead, silently weakening the signature.
    MissingCoveredHeader {
        /// Bounded copy of the missing name.
        name: String,
    },
    /// A header carries no input.
    NoInputs,
    /// A header carries more than [`MAX_INPUTS`] inputs.
    TooManyInputs {
        /// Actual count.
        count: usize,
        /// Accepted maximum.
        maximum: usize,
    },
    /// The same label appears twice, where a dictionary keeps only the last.
    DuplicateLabel {
        /// Bounded copy of the repeated label.
        label: String,
    },
    /// An input omits the required `created` parameter.
    MissingCreated {
        /// Bounded copy of the input's label.
        label: String,
    },
    /// A parameter this format does not define was present.
    ///
    /// The reference ignores it, which then makes verification fail with no explanation.
    UnknownParameter {
        /// Bounded copy of the rejected name.
        name: String,
    },
    /// The parameters are not in the order both implementations write them in.
    ParametersOutOfOrder,
    /// A numeric parameter is outside what the header grammar can express.
    ParameterOutOfRange {
        /// Which parameter.
        name: &'static str,
    },
    /// A textual parameter carries a backslash, which the header escapes and the base does not.
    InvalidParameterValue {
        /// Which parameter.
        name: &'static str,
    },
    /// The header value does not match the accepted grammar.
    Malformed {
        /// What the parser expected.
        reason: &'static str,
        /// Byte offset reached.
        offset: usize,
    },
    /// A covered component names an invalid header field.
    Header {
        /// Underlying failure.
        source: HeaderError,
    },
    /// A parameter value holds a byte the header or the base cannot carry unambiguously.
    Field {
        /// Underlying failure.
        source: SignatureFieldError,
    },
}

impl From<HeaderError> for SignatureInputError {
    fn from(source: HeaderError) -> Self {
        Self::Header { source }
    }
}

impl From<SignatureFieldError> for SignatureInputError {
    fn from(source: SignatureFieldError) -> Self {
        Self::Field { source }
    }
}

impl fmt::Display for SignatureInputError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SignatureInputTooLong { length, maximum } => {
                write!(formatter, "signature input of {length} bytes exceeds maximum {maximum}")
            }
            Self::SignatureBaseTooLong { length, maximum } => {
                write!(formatter, "signature base of {length} bytes exceeds maximum {maximum}")
            }
            Self::InvalidLabel { label } => {
                write!(formatter, "signature input label {label:?} is not a dictionary key")
            }
            Self::UnknownDerivedComponent { name } => write!(
                formatter,
                "derived component {name:?} is not one of {METHOD_COMPONENT:?} or {PATH_COMPONENT:?}"
            ),
            Self::InvalidRequestMethod { method } => {
                write!(formatter, "request method {method:?} is not a token")
            }
            Self::InvalidRequestPath { path } => write!(
                formatter,
                "request path {path:?} must be absolute, query-free, and visible ASCII"
            ),
            Self::NoComponents => formatter.write_str("a signature input covers nothing"),
            Self::TooManyComponents { count, maximum } => {
                write!(
                    formatter,
                    "a signature input covers {count} components, above {maximum}"
                )
            }
            Self::DuplicateComponent { name } => {
                write!(formatter, "component {name:?} is covered more than once")
            }
            Self::MissingCoveredHeader { name } => {
                write!(formatter, "covered header {name:?} is absent from the message")
            }
            Self::NoInputs => formatter.write_str("a signature input header carries no input"),
            Self::TooManyInputs { count, maximum } => {
                write!(
                    formatter,
                    "a signature input header carries {count} inputs, above {maximum}"
                )
            }
            Self::DuplicateLabel { label } => {
                write!(formatter, "signature input label {label:?} appears more than once")
            }
            Self::MissingCreated { label } => {
                write!(formatter, "signature input {label:?} omits the required \"created\"")
            }
            Self::UnknownParameter { name } => {
                write!(formatter, "signature input parameter {name:?} is not defined")
            }
            Self::ParametersOutOfOrder => {
                write!(
                    formatter,
                    "signature input parameters must appear in the order {PARAMETER_ORDER:?}"
                )
            }
            Self::ParameterOutOfRange { name } => write!(
                formatter,
                "signature input parameter {name:?} exceeds maximum {MAX_PARAMETER_INTEGER}"
            ),
            Self::InvalidParameterValue { name } => write!(
                formatter,
                "signature input parameter {name:?} may not carry a backslash"
            ),
            Self::Malformed { reason, offset } => {
                write!(formatter, "malformed signature input at offset {offset}: {reason}")
            }
            Self::Header { .. } => formatter.write_str("a covered component is not a valid header name"),
            Self::Field { .. } => formatter.write_str("a signature input parameter value is invalid"),
        }
    }
}

impl Error for SignatureInputError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Header { source } => Some(source),
            Self::Field { source } => Some(source),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::header::HeaderValue;
    use std::error::Error;

    const KEYID: &str = "DN54yRad_BTqgZYUSi_NthRBQrxSnqQdJXWI5UHcGOQt";
    const RESOURCE: &str = "EWJkQCFvKuyxZi582yJPb0wcwuW3VXmFNuvbQuBpgmIs";
    const STAMP: &str = "2022-09-24T00:05:48.196795+00:00";

    fn headers() -> Result<Headers, Box<dyn Error>> {
        Ok(Headers::from_pairs([
            (HeaderName::new("Content-Type")?, HeaderValue::new("application/json")?),
            (HeaderName::new("Signify-Resource")?, HeaderValue::new(RESOURCE)?),
            (HeaderName::new("Signify-Timestamp")?, HeaderValue::new(STAMP)?),
        ])?)
    }

    fn default_input() -> Result<SignatureInput, Box<dyn Error>> {
        Ok(
            SignatureInputBuilder::new(InputLabel::signify(), default_components()?, 1_609_459_200)
                .key_identifier(FieldText::new(KEYID)?)
                .algorithm(FieldText::new(ED25519_ALGORITHM)?)
                .build()?,
        )
    }

    #[test]
    fn the_default_components_are_the_reference_field_list() -> Result<(), Box<dyn Error>> {
        let components = default_components()?;
        let names: Vec<&str> = components.iter().map(SignatureComponent::as_str).collect();
        assert_eq!(names, ["@method", "@path", "signify-resource", "signify-timestamp"]);
        Ok(())
    }

    #[test]
    fn the_base_and_the_header_spell_the_same_input_differently() -> Result<(), Box<dyn Error>> {
        let input = default_input()?;
        let target = RequestTarget::new("POST", "/boot")?;
        let base = input.signature_base(&target, &headers()?)?;

        assert_eq!(
            base.as_str(),
            concat!(
                "\"@method\": POST\n",
                "\"@path\": /boot\n",
                "\"signify-resource\": EWJkQCFvKuyxZi582yJPb0wcwuW3VXmFNuvbQuBpgmIs\n",
                "\"signify-timestamp\": 2022-09-24T00:05:48.196795+00:00\n",
                "\"@signature-params: (@method @path signify-resource signify-timestamp);",
                "created=1609459200;keyid=DN54yRad_BTqgZYUSi_NthRBQrxSnqQdJXWI5UHcGOQt;alg=ed25519\"",
            )
        );
        assert_eq!(base.as_bytes(), base.as_str().as_bytes());
        assert_eq!(base.to_string(), base.as_str());

        let header = SignatureInputHeader::new(vec![input])?;
        assert_eq!(
            header.to_header_value()?,
            concat!(
                "signify=(\"@method\" \"@path\" \"signify-resource\" \"signify-timestamp\");",
                "created=1609459200;keyid=\"DN54yRad_BTqgZYUSi_NthRBQrxSnqQdJXWI5UHcGOQt\";alg=\"ed25519\"",
            )
        );
        Ok(())
    }

    #[test]
    fn a_header_round_trips_through_parsing() -> Result<(), Box<dyn Error>> {
        let header = SignatureInputHeader::new(vec![
            SignatureInputBuilder::new(InputLabel::signify(), default_components()?, 1_609_459_200)
                .expires(1_609_545_600)
                .nonce(FieldText::new("0AAxyz")?)
                .key_identifier(FieldText::new(KEYID)?)
                .context(FieldText::new("keria")?)
                .algorithm(FieldText::new(ED25519_ALGORITHM)?)
                .build()?,
        ])?;
        let rendered = header.to_header_value()?;
        let parsed = SignatureInputHeader::parse(&rendered)?;
        assert_eq!(parsed, header);
        assert_eq!(parsed.to_header_value()?, rendered);
        assert_eq!(rendered.parse::<SignatureInputHeader>()?, header);
        assert_eq!(SignatureInputHeader::try_from(rendered.as_str())?, header);

        let input = parsed.find(&InputLabel::signify()).ok_or("no input")?;
        assert_eq!(input.created(), 1_609_459_200);
        assert_eq!(input.expires(), Some(1_609_545_600));
        assert_eq!(input.nonce(), Some("0AAxyz"));
        assert_eq!(input.key_identifier(), Some(KEYID));
        assert_eq!(input.context(), Some("keria"));
        assert_eq!(input.algorithm(), Some(ED25519_ALGORITHM));
        assert_eq!(input.label().as_str(), "signify");
        assert_eq!(input.components().len(), 4);
        assert!(parsed.find(&InputLabel::new("other")?).is_none());
        Ok(())
    }

    #[test]
    fn several_inputs_are_comma_separated() -> Result<(), Box<dyn Error>> {
        let value = "signify=(\"@method\");created=1609459200, other=(\"@path\");created=1609459201";
        let header = SignatureInputHeader::parse(value)?;
        assert_eq!(header.inputs().len(), 2);
        assert_eq!(header.to_header_value()?, value);
        // The comma may arrive without its space when an intermediary rewrites the header.
        assert_eq!(SignatureInputHeader::parse(&value.replace(", ", ","))?, header);
        Ok(())
    }

    #[test]
    fn a_missing_covered_header_is_refused() -> Result<(), Box<dyn Error>> {
        // Upstream the component is silently dropped, so the caller signs less than it asked to.
        let input = SignatureInputBuilder::new(
            InputLabel::signify(),
            vec![SignatureComponent::Method, SignatureComponent::new("x-absent")?],
            1,
        )
        .build()?;
        assert!(matches!(
            input.signature_base(&RequestTarget::new("GET", "/x")?, &headers()?),
            Err(SignatureInputError::MissingCoveredHeader { name }) if name == "x-absent"
        ));
        Ok(())
    }

    #[test]
    fn covered_header_lookup_ignores_case_but_the_base_keeps_it() -> Result<(), Box<dyn Error>> {
        let input = SignatureInputBuilder::new(
            InputLabel::signify(),
            vec![SignatureComponent::new("SIGNIFY-RESOURCE")?],
            1,
        )
        .build()?;
        let base = input.signature_base(&RequestTarget::new("GET", "/x")?, &headers()?)?;
        assert!(base.as_str().starts_with(&format!("\"SIGNIFY-RESOURCE\": {RESOURCE}")));
        Ok(())
    }

    #[test]
    fn only_the_two_implemented_derived_components_exist() -> Result<(), Box<dyn Error>> {
        assert_eq!(SignatureComponent::new("@method")?, SignatureComponent::Method);
        assert_eq!(SignatureComponent::new("@path")?, SignatureComponent::Path);
        assert_eq!(SignatureComponent::from_str("@path")?.to_string(), "@path");
        for name in ["@authority", "@query", "@target-uri", "@"] {
            assert!(
                matches!(
                    SignatureComponent::new(name),
                    Err(SignatureInputError::UnknownDerivedComponent { .. })
                ),
                "{name:?} was accepted",
            );
        }
        assert!(matches!(
            SignatureComponent::new("a b"),
            Err(SignatureInputError::Header { .. })
        ));
        Ok(())
    }

    #[test]
    fn labels_follow_the_dictionary_key_grammar() -> Result<(), Box<dyn Error>> {
        assert_eq!(InputLabel::new("sig0")?.as_str(), "sig0");
        assert_eq!(InputLabel::new("*x")?.to_string(), "*x");
        assert_eq!(InputLabel::from_str("signify")?, InputLabel::signify());
        for label in ["Signify", "0sig", "sig 0", "", "-sig", "sig=0"] {
            assert!(
                matches!(InputLabel::new(label), Err(SignatureInputError::InvalidLabel { .. })),
                "{label:?} was accepted",
            );
        }
        assert!(InputLabel::new("a".repeat(MAX_LABEL_BYTES + 1)).is_err());
        Ok(())
    }

    #[test]
    fn request_targets_cannot_forge_a_base_line() -> Result<(), Box<dyn Error>> {
        let target = RequestTarget::new("POST", "/identifiers/aid1")?;
        assert_eq!(target.method(), "POST");
        assert_eq!(target.path(), "/identifiers/aid1");
        assert_eq!(RequestTarget::new("GET", "/oobi/EB%2Fa")?.path(), "/oobi/EB%2Fa");

        // Upstream all of these are interpolated into the base unvalidated.
        for path in [
            "/a\n\"@method\": DELETE",
            "relative",
            "",
            "/a b",
            "/a?limit=1",
            "/a#top",
            "/a\"b",
        ] {
            assert!(
                matches!(
                    RequestTarget::new("GET", path),
                    Err(SignatureInputError::InvalidRequestPath { .. })
                ),
                "{path:?} was accepted",
            );
        }
        for method in ["", "GET POST", "GE\nT", "G:T"] {
            assert!(
                matches!(
                    RequestTarget::new(method, "/x"),
                    Err(SignatureInputError::InvalidRequestMethod { .. })
                ),
                "{method:?} was accepted",
            );
        }
        assert!(RequestTarget::new("GET", &format!("/{}", "a".repeat(MAX_REQUEST_PATH_BYTES))).is_err());
        assert!(RequestTarget::new(&"A".repeat(MAX_REQUEST_METHOD_BYTES + 1), "/x").is_err());
        Ok(())
    }

    #[test]
    fn empty_and_repeated_coverage_is_refused() -> Result<(), Box<dyn Error>> {
        // Upstream an empty covered list is emitted and parsed, binding nothing at all.
        assert!(matches!(
            SignatureInputBuilder::new(InputLabel::signify(), Vec::new(), 1).build(),
            Err(SignatureInputError::NoComponents)
        ));
        assert!(matches!(
            SignatureInputHeader::parse("signify=();created=1"),
            Err(SignatureInputError::NoComponents)
        ));
        assert!(matches!(
            SignatureInputBuilder::new(
                InputLabel::signify(),
                vec![
                    SignatureComponent::new("signify-resource")?,
                    SignatureComponent::new("Signify-Resource")?,
                ],
                1,
            )
            .build(),
            Err(SignatureInputError::DuplicateComponent { .. })
        ));
        assert!(matches!(
            SignatureInputBuilder::new(
                InputLabel::signify(),
                vec![SignatureComponent::Method; MAX_COMPONENTS + 1],
                1,
            )
            .build(),
            Err(SignatureInputError::TooManyComponents { .. })
        ));
        Ok(())
    }

    #[test]
    fn headers_hold_at_least_one_input_and_unique_labels() -> Result<(), Box<dyn Error>> {
        assert!(matches!(
            SignatureInputHeader::new(Vec::new()),
            Err(SignatureInputError::NoInputs)
        ));
        // Upstream a dictionary keeps the last member, so the earlier input silently disappears.
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");created=1, signify=(\"@path\");created=2"),
            Err(SignatureInputError::DuplicateLabel { .. })
        ));
        let inputs = (0..=MAX_INPUTS)
            .map(|position| {
                Ok(SignatureInputBuilder::new(
                    InputLabel::new(format!("sig{position}"))?,
                    vec![SignatureComponent::Method],
                    1,
                )
                .build()?)
            })
            .collect::<Result<Vec<_>, Box<dyn Error>>>()?;
        assert!(matches!(
            SignatureInputHeader::new(inputs),
            Err(SignatureInputError::TooManyInputs { .. })
        ));
        Ok(())
    }

    #[test]
    fn parameters_are_required_ordered_and_known() {
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");alg=\"ed25519\""),
            Err(SignatureInputError::MissingCreated { .. })
        ));
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");created=1;alg=\"ed25519\";keyid=\"x\""),
            Err(SignatureInputError::ParametersOutOfOrder)
        ));
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");created=1;created=2"),
            Err(SignatureInputError::ParametersOutOfOrder)
        ));
        // The reference ignores an unknown parameter, which then makes verification fail silently.
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");created=1;tag=\"x\""),
            Err(SignatureInputError::UnknownParameter { name }) if name == "tag"
        ));
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");created=-1"),
            Err(SignatureInputError::Malformed { .. })
        ));
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");created=01"),
            Err(SignatureInputError::Malformed { .. })
        ));
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");created=9999999999999999"),
            Err(SignatureInputError::ParameterOutOfRange { .. })
        ));
    }

    #[test]
    fn malformed_values_are_typed_errors() {
        for value in [
            "",
            "signify",
            "signify=",
            "signify=(",
            "signify=(\"@method\"",
            "signify=(\"@method\")",
            "signify=(@method);created=1",
            "signify=(\"@method\"\"@path\");created=1",
            "signify=(\"@method\");created=1;",
            "signify=(\"@method\");created",
            "signify=(\"@method\");created=1 trailing",
            "signify=(\"a\\\"b\");created=1",
            "signify=(\"@method\");created=1;keyid=x",
            "signify=(\"@method\");created=1;keyid=\"a b\"",
        ] {
            let parsed = SignatureInputHeader::parse(value);
            assert!(
                matches!(
                    parsed,
                    Err(SignatureInputError::Malformed { .. }
                        | SignatureInputError::InvalidLabel { .. }
                        | SignatureInputError::MissingCreated { .. }
                        | SignatureInputError::NoComponents
                        | SignatureInputError::UnknownDerivedComponent { .. }
                        | SignatureInputError::Field { .. })
                ),
                "{value:?} produced {parsed:?}",
            );
        }
    }

    #[test]
    fn oversized_input_is_bounded() -> Result<(), Box<dyn Error>> {
        assert!(matches!(
            SignatureInputHeader::parse(&"a".repeat(MAX_SIGNATURE_INPUT_BYTES + 1)),
            Err(SignatureInputError::SignatureInputTooLong { .. })
        ));

        // A base built from many long header values is bounded too.
        let mut headers = Headers::new();
        let mut components = Vec::new();
        for position in 0..MAX_COMPONENTS {
            let name = HeaderName::new(format!("x-{position}"))?;
            headers.insert(name.clone(), HeaderValue::new("v".repeat(MAX_HEADER_VALUE))?)?;
            components.push(SignatureComponent::Header(name));
        }
        let input = SignatureInputBuilder::new(InputLabel::signify(), components, 1).build()?;
        assert!(matches!(
            input.signature_base(&RequestTarget::new("GET", "/x")?, &headers),
            Err(SignatureInputError::SignatureBaseTooLong { .. })
        ));
        Ok(())
    }

    /// A header value long enough that sixty-four of them exceed the base bound.
    const MAX_HEADER_VALUE: usize = 2048;

    #[test]
    fn parameter_values_cannot_forge_a_parameter() {
        // Upstream the header escapes a quote while the base does not, and a semicolon in a value
        // adds a whole parameter to the signed bytes.
        for text in ["a\"b", "a;alg=forged", "a b", "a,b", "a=b"] {
            assert!(FieldText::new(text).is_err(), "{text:?} was accepted");
        }
        // The value parses as a quoted string and is then refused as a parameter value, so the
        // forged `alg` never reaches the signature base.
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");created=1;keyid=\"a;alg=forged\""),
            Err(SignatureInputError::Field { .. })
        ));
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");created=1;keyid=\"a\\\"b\""),
            Err(SignatureInputError::Malformed { .. })
        ));
    }

    /// Regression: a property test built a nonce of a single backslash. The header escaped it and
    /// the base did not, so the rendered header no longer parsed and the two spellings disagreed
    /// about the bytes signed.
    #[test]
    fn a_parameter_value_may_not_carry_a_backslash() -> Result<(), Box<dyn Error>> {
        assert!(matches!(
            SignatureInputBuilder::new(InputLabel::signify(), vec![SignatureComponent::Method], 1)
                .nonce(FieldText::new("\\")?)
                .build(),
            Err(SignatureInputError::InvalidParameterValue { name: "nonce" })
        ));
        for text in ["a\\b", "\\"] {
            assert!(
                SignatureInputBuilder::new(InputLabel::signify(), vec![SignatureComponent::Method], 1)
                    .key_identifier(FieldText::new(text)?)
                    .build()
                    .is_err(),
                "{text:?} was accepted",
            );
        }
        // The parser refuses the same spelling from the wire.
        assert!(matches!(
            SignatureInputHeader::parse("signify=(\"@method\");created=1;nonce=\"\\\""),
            Err(SignatureInputError::Malformed { .. })
        ));
        Ok(())
    }

    #[test]
    fn errors_carry_readable_text_and_sources() {
        for error in [
            SignatureInputError::SignatureInputTooLong {
                length: 1,
                maximum: MAX_SIGNATURE_INPUT_BYTES,
            },
            SignatureInputError::SignatureBaseTooLong {
                length: 1,
                maximum: MAX_SIGNATURE_BASE_BYTES,
            },
            SignatureInputError::InvalidLabel { label: "X".to_owned() },
            SignatureInputError::UnknownDerivedComponent {
                name: "@authority".to_owned(),
            },
            SignatureInputError::InvalidRequestMethod {
                method: "a b".to_owned(),
            },
            SignatureInputError::InvalidRequestPath { path: "x".to_owned() },
            SignatureInputError::NoComponents,
            SignatureInputError::TooManyComponents {
                count: 1,
                maximum: MAX_COMPONENTS,
            },
            SignatureInputError::DuplicateComponent { name: "x".to_owned() },
            SignatureInputError::MissingCoveredHeader { name: "x".to_owned() },
            SignatureInputError::NoInputs,
            SignatureInputError::TooManyInputs {
                count: 1,
                maximum: MAX_INPUTS,
            },
            SignatureInputError::DuplicateLabel {
                label: "signify".to_owned(),
            },
            SignatureInputError::MissingCreated {
                label: "signify".to_owned(),
            },
            SignatureInputError::UnknownParameter { name: "tag".to_owned() },
            SignatureInputError::ParametersOutOfOrder,
            SignatureInputError::ParameterOutOfRange { name: "time" },
            SignatureInputError::InvalidParameterValue { name: "nonce" },
            SignatureInputError::Malformed { reason: "x", offset: 0 },
        ] {
            assert_ne!(error.to_string(), "");
            assert!(error.source().is_none());
        }

        let layered = SignatureInputError::from(HeaderError::EmptyName);
        assert!(layered.source().is_some());
        let layered = SignatureInputError::from(SignatureFieldError::EmptyFieldText);
        assert!(layered.source().is_some());
    }
}