structured-webauthn 0.1.0

Server-side Web Authentication (WebAuthn) Relying Party (RP) API in pure Rust: registration and authentication ceremonies, passkeys, no OpenSSL.
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
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
#[cfg(test)]
mod tests;
use super::{
    super::{
        DynamicState, Metadata, RegisteredCredential, StaticState,
        response::{
            AuthenticatorAttachment,
            register::{
                Attestation, AttestationFormat, AuthenticatorExtensionOutput,
                ClientExtensionsOutputs, CredentialProtectionPolicy, HmacSecret, Registration,
                UncompressedPubKey,
                error::{ExtensionErr, RegCeremonyErr},
            },
        },
    },
    Ceremony, Challenge, CredentialMediationRequirement, ExtensionInfo, ExtensionReq, FIVE_MINUTES,
    Hints, Origin, PrfInput, PublicKeyCredentialDescriptor, RpId, SentChallenge, TimedCeremony,
    UserVerificationRequirement,
    register::error::CreationOptionsErr,
};
#[cfg(doc)]
use crate::{
    request::{
        AsciiDomain, AsciiDomainStatic, DomainOrigin, Url,
        auth::{AuthenticationVerificationOptions, PublicKeyCredentialRequestOptions},
    },
    response::{AuthTransports, AuthenticatorTransport, Backup, CollectedClientData, Flag},
};
use core::{
    borrow::Borrow,
    cmp::Ordering,
    fmt::{self, Display, Formatter},
    hash::{Hash, Hasher},
    mem,
    num::{NonZeroU32, NonZeroU64},
    time::Duration,
};
#[cfg(any(doc, not(feature = "serializable_server_state")))]
use std::time::Instant;
#[cfg(any(doc, feature = "serializable_server_state"))]
use std::time::SystemTime;
/// Contains functionality to (de)serialize data to a data store.
#[cfg(feature = "bin")]
pub mod bin;
/// Contains functionality that needs to be accessible when `bin` or `serde` are not enabled.
#[cfg(feature = "custom")]
mod custom;
/// Contains error types.
pub mod error;
/// Contains functionality to (de)serialize data to a client.
#[cfg(feature = "serde")]
pub mod ser;
/// Contains functionality to (de)serialize [`RegistrationServerState`] to a data store.
#[cfg(feature = "serializable_server_state")]
pub mod ser_server_state;
/// Backup requirements for the credential.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum BackupReq {
    /// No requirements (i.e., any [`Backup`] is allowed).
    #[default]
    None,
    /// Credential must not be eligible for backup.
    NotEligible,
    /// Credential must be eligible for backup.
    ///
    /// Note the existence of a backup is ignored. If a backup must exist, then use [`Self::Exists`]; if a
    /// backup must not exist, then use [`Self::EligibleNotExists`].
    Eligible,
    /// Credential must be eligible for backup, but a backup must not exist.
    EligibleNotExists,
    /// Credential must be backed up.
    Exists,
}
/// Used by [`Extension::cred_protect`] to enforce the [`CredentialProtectionPolicy`] sent by the client via
/// [`Registration`].
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum CredProtect {
    /// No `credProtect` request.
    #[default]
    None,
    /// Request
    /// [`userVerificationOptional`](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#userverificationoptional)
    /// but allow any.
    ///
    /// The `bool` corresponds to
    /// [`enforceCredentialProtectionPolicy`](https://fidoalliance.org/specs/fido-v2.2-ps-20250228/fido-client-to-authenticator-protocol-v2.2-ps-20250228.html#dom-authenticationextensionsclientinputs-enforcecredentialprotectionpolicy).
    UserVerificationOptional(bool, ExtensionInfo),
    /// Request
    /// [`userVerificationOptionalWithCredentialIDList`](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#userverificationoptionalwithcredentialidlist);
    /// and when enforcing the value, disallow [`CredentialProtectionPolicy::UserVerificationOptional`].
    ///
    /// The `bool` corresponds to
    /// [`enforceCredentialProtectionPolicy`](https://fidoalliance.org/specs/fido-v2.2-ps-20250228/fido-client-to-authenticator-protocol-v2.2-ps-20250228.html#dom-authenticationextensionsclientinputs-enforcecredentialprotectionpolicy).
    UserVerificationOptionalWithCredentialIdList(bool, ExtensionInfo),
    /// Request
    /// [`userVerificationRequired`](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#userverificationrequired);
    /// and when enforcing the value, only allow [`CredentialProtectionPolicy::UserVerificationRequired`].
    ///
    /// The `bool` corresponds to
    /// [`enforceCredentialProtectionPolicy`](https://fidoalliance.org/specs/fido-v2.2-ps-20250228/fido-client-to-authenticator-protocol-v2.2-ps-20250228.html#dom-authenticationextensionsclientinputs-enforcecredentialprotectionpolicy).
    UserVerificationRequired(bool, ExtensionInfo),
}
impl CredProtect {
    /// Validates `other` is allowed based on `self`.
    ///
    /// # Errors
    ///
    /// Errors iff other is a less "secure" policy than `self` when enforcing the value or other does not exist
    /// despite requiring a value to be sent back.
    ///
    /// Note a missing response is OK when enforcing a value.
    const fn validate(self, other: CredentialProtectionPolicy) -> Result<(), ExtensionErr> {
        match self {
            Self::None => Ok(()),
            Self::UserVerificationOptional(_, info) => {
                if matches!(other, CredentialProtectionPolicy::None) {
                    if matches!(
                        info,
                        ExtensionInfo::RequireEnforceValue | ExtensionInfo::RequireDontEnforceValue
                    ) {
                        Err(ExtensionErr::MissingCredProtect)
                    } else {
                        Ok(())
                    }
                } else {
                    Ok(())
                }
            }
            Self::UserVerificationOptionalWithCredentialIdList(_, info) => match info {
                ExtensionInfo::RequireEnforceValue => match other {
                    CredentialProtectionPolicy::None => Err(ExtensionErr::MissingCredProtect),
                    CredentialProtectionPolicy::UserVerificationOptional => {
                        Err(ExtensionErr::InvalidCredProtectValue(self, other))
                    }
                    CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIdList
                    | CredentialProtectionPolicy::UserVerificationRequired => Ok(()),
                },
                ExtensionInfo::RequireDontEnforceValue => {
                    if matches!(other, CredentialProtectionPolicy::None) {
                        Err(ExtensionErr::MissingCredProtect)
                    } else {
                        Ok(())
                    }
                }
                ExtensionInfo::AllowEnforceValue => {
                    if matches!(other, CredentialProtectionPolicy::UserVerificationOptional) {
                        Err(ExtensionErr::InvalidCredProtectValue(self, other))
                    } else {
                        Ok(())
                    }
                }
                ExtensionInfo::AllowDontEnforceValue => Ok(()),
            },
            Self::UserVerificationRequired(_, info) => match info {
                ExtensionInfo::RequireEnforceValue => match other {
                    CredentialProtectionPolicy::None => Err(ExtensionErr::MissingCredProtect),
                    CredentialProtectionPolicy::UserVerificationOptional
                    | CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIdList => {
                        Err(ExtensionErr::InvalidCredProtectValue(self, other))
                    }
                    CredentialProtectionPolicy::UserVerificationRequired => Ok(()),
                },
                ExtensionInfo::RequireDontEnforceValue => {
                    if matches!(other, CredentialProtectionPolicy::None) {
                        Err(ExtensionErr::MissingCredProtect)
                    } else {
                        Ok(())
                    }
                }
                ExtensionInfo::AllowEnforceValue => {
                    if matches!(
                        other,
                        CredentialProtectionPolicy::None
                            | CredentialProtectionPolicy::UserVerificationRequired
                    ) {
                        Ok(())
                    } else {
                        Err(ExtensionErr::InvalidCredProtectValue(self, other))
                    }
                }
                ExtensionInfo::AllowDontEnforceValue => Ok(()),
            },
        }
    }
}
impl Display for CredProtect {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            Self::None => f.write_str("do not sent a credProtect request"),
            Self::UserVerificationOptional(enforce, info) => {
                write!(
                    f,
                    "request user verification optional with enforcement {enforce} and {info}"
                )
            }
            Self::UserVerificationOptionalWithCredentialIdList(enforce, info) => write!(
                f,
                "user verification optional with credential ID list with enforcement {enforce} and {info}"
            ),
            Self::UserVerificationRequired(enforce, info) => {
                write!(
                    f,
                    "user verification required with enforcement {enforce} and {info}"
                )
            }
        }
    }
}
/// [`COSEAlgorithmIdentifier`](https://www.w3.org/TR/webauthn-3/#typedefdef-cosealgorithmidentifier).
///
/// Note the order of variants is the following:
///
/// [`Self::Mldsa87`] `<` [`Self::Mldsa65`] `<` [`Self::Mldsa44`] `<` [`Self::Eddsa`] `<` [`Self::Es256`]
/// `<` [`Self::Es384`] `<` [`Self::Rs256`].
///
/// This is relevant for [`CoseAlgorithmIdentifiers`]. For example a `CoseAlgorithmIdentifiers`
/// that contains `Self::Mldsa87` will prioritize it over all others.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum CoseAlgorithmIdentifier {
    /// [ML-DSA-87](https://www.iana.org/assignments/cose/cose.xhtml#algorithms).
    Mldsa87,
    /// [ML-DSA-65](https://www.iana.org/assignments/cose/cose.xhtml#algorithms).
    Mldsa65,
    /// [ML-DSA-44](https://www.iana.org/assignments/cose/cose.xhtml#algorithms).
    Mldsa44,
    /// [EdDSA](https://www.iana.org/assignments/cose/cose.xhtml#algorithms).
    ///
    /// Note that Ed25519 must be used for the `crv` parameter
    /// [per the spec](https://www.w3.org/TR/webauthn-3/#sctn-alg-identifier).
    Eddsa,
    /// [ES256](https://www.iana.org/assignments/cose/cose.xhtml#algorithms).
    ///
    /// Note the uncompressed form must be used and P-256 must be used for the `crv` parameter
    /// [per the spec](https://www.w3.org/TR/webauthn-3/#sctn-alg-identifier).
    Es256,
    /// [ES384](https://www.iana.org/assignments/cose/cose.xhtml#algorithms).
    ///
    /// Note the uncompressed form must be used and P-384 must be used for the `crv` parameter
    /// [per the spec](https://www.w3.org/TR/webauthn-3/#sctn-alg-identifier).
    Es384,
    /// [RS256](https://www.iana.org/assignments/cose/cose.xhtml#algorithms).
    Rs256,
}
impl CoseAlgorithmIdentifier {
    /// Transforms `self` into a `u8`.
    const fn to_u8(self) -> u8 {
        match self {
            Self::Mldsa87 => 0x1,
            Self::Mldsa65 => 0x2,
            Self::Mldsa44 => 0x4,
            Self::Eddsa => 0x8,
            Self::Es256 => 0x10,
            Self::Es384 => 0x20,
            Self::Rs256 => 0x40,
        }
    }
}
impl PartialEq<&Self> for CoseAlgorithmIdentifier {
    #[inline]
    fn eq(&self, other: &&Self) -> bool {
        *self == **other
    }
}
impl PartialEq<CoseAlgorithmIdentifier> for &CoseAlgorithmIdentifier {
    #[inline]
    fn eq(&self, other: &CoseAlgorithmIdentifier) -> bool {
        **self == *other
    }
}
/// Non-empty ordered set of [`CoseAlgorithmIdentifier`]s.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CoseAlgorithmIdentifiers(u8);
impl CoseAlgorithmIdentifiers {
    /// Contains all [`CoseAlgorithmIdentifier`]s.
    pub const ALL: Self = Self(0)
        .add(CoseAlgorithmIdentifier::Mldsa87)
        .add(CoseAlgorithmIdentifier::Mldsa65)
        .add(CoseAlgorithmIdentifier::Mldsa44)
        .add(CoseAlgorithmIdentifier::Eddsa)
        .add(CoseAlgorithmIdentifier::Es256)
        .add(CoseAlgorithmIdentifier::Es384)
        .add(CoseAlgorithmIdentifier::Rs256);
    /// Returns a `CoseAlgorithmIdentifiers` containing all `CoseAlgorithmIdentifier`s in `self` plus `alg`.
    const fn add(self, alg: CoseAlgorithmIdentifier) -> Self {
        Self(self.0 | alg.to_u8())
    }
    /// Returns a copy of `self` with `alg` removed if there would be at least one `CoseAlgorithmIdentifier`
    /// remaining; otherwise returns `self`.
    #[inline]
    #[must_use]
    pub const fn remove(self, alg: CoseAlgorithmIdentifier) -> Self {
        let val = alg.to_u8();
        if self.0 == val {
            self
        } else {
            Self(self.0 & !val)
        }
    }
    /// Returns `true` iff `self` contains `alg`.
    #[inline]
    #[must_use]
    pub const fn contains(self, alg: CoseAlgorithmIdentifier) -> bool {
        let val = alg.to_u8();
        self.0 & val == val
    }
    /// Validates `other` is allowed based on `self`.
    const fn validate(self, other: UncompressedPubKey<'_>) -> Result<(), RegCeremonyErr> {
        if match other {
            UncompressedPubKey::MlDsa87(_) => self.contains(CoseAlgorithmIdentifier::Mldsa87),
            UncompressedPubKey::MlDsa65(_) => self.contains(CoseAlgorithmIdentifier::Mldsa65),
            UncompressedPubKey::MlDsa44(_) => self.contains(CoseAlgorithmIdentifier::Mldsa44),
            UncompressedPubKey::Ed25519(_) => self.contains(CoseAlgorithmIdentifier::Eddsa),
            UncompressedPubKey::P256(_) => self.contains(CoseAlgorithmIdentifier::Es256),
            UncompressedPubKey::P384(_) => self.contains(CoseAlgorithmIdentifier::Es384),
            UncompressedPubKey::Rsa(_) => self.contains(CoseAlgorithmIdentifier::Rs256),
        } {
            Ok(())
        } else {
            Err(RegCeremonyErr::PublicKeyAlgorithmMismatch)
        }
    }
}
impl Default for CoseAlgorithmIdentifiers {
    /// Returns [`Self::ALL`].
    #[inline]
    fn default() -> Self {
        Self::ALL
    }
}
/// Four to sixty-three inclusively.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum FourToSixtyThree {
    /// 4.
    Four = 4,
    /// 5.
    Five,
    /// 6.
    Six,
    /// 7.
    Seven,
    /// 8.
    Eight,
    /// 9.
    Nine,
    /// 10.
    Ten,
    /// 11.
    Eleven,
    /// 12.
    Twelve,
    /// 13.
    Thirteen,
    /// 14.
    Fourteen,
    /// 15.
    Fifteen,
    /// 16.
    Sixteen,
    /// 17.
    Seventeen,
    /// 18.
    Eighteen,
    /// 19.
    Nineteen,
    /// 20.
    Twenty,
    /// 21.
    TwentyOne,
    /// 22.
    TwentyTwo,
    /// 23.
    TwentyThree,
    /// 24.
    TwentyFour,
    /// 25.
    TwentyFive,
    /// 26.
    TwentySix,
    /// 27.
    TwentySeven,
    /// 28.
    TwentyEight,
    /// 29.
    TwentyNine,
    /// 30.
    Thirty,
    /// 31.
    ThirtyOne,
    /// 32.
    ThirtyTwo,
    /// 33.
    ThirtyThree,
    /// 34.
    ThirtyFour,
    /// 35.
    ThirtyFive,
    /// 36.
    ThirtySix,
    /// 37.
    ThirtySeven,
    /// 38.
    ThirtyEight,
    /// 39.
    ThirtyNine,
    /// 40.
    Fourty,
    /// 41.
    FourtyOne,
    /// 42.
    FourtyTwo,
    /// 43.
    FourtyThree,
    /// 44.
    FourtyFour,
    /// 45.
    FourtyFive,
    /// 46.
    FourtySix,
    /// 47.
    FourtySeven,
    /// 48.
    FourtyEight,
    /// 49.
    FourtyNine,
    /// 50.
    Fifty,
    /// 51.
    FiftyOne,
    /// 52.
    FiftyTwo,
    /// 53.
    FiftyThree,
    /// 54.
    FiftyFour,
    /// 55.
    FiftyFive,
    /// 56.
    FiftySix,
    /// 57.
    FiftySeven,
    /// 58.
    FiftyEight,
    /// 59.
    FiftyNine,
    /// 60.
    Sixty,
    /// 61.
    SixtyOne,
    /// 62.
    SixtyTwo,
    /// 63.
    SixtyThree,
}
impl FourToSixtyThree {
    /// Returns the equivalent `u8`.
    #[expect(clippy::as_conversions, reason = "comment justifies correctness")]
    #[inline]
    #[must_use]
    pub const fn into_u8(self) -> u8 {
        // This is correct since `Self` is `repr(u8)`, and the initial discriminant has the value `4`
        // and subsequent discriminants are implicitly incremented by 1.
        self as u8
    }
    /// Returns `Some` representing `val` iff `val` is inclusively between 4 and 63.
    #[expect(unsafe_code, reason = "comment justifies correctness")]
    #[inline]
    #[must_use]
    pub const fn from_u8(val: u8) -> Option<Self> {
        match val {
            4..=63 => {
                // SAFETY:
                // `val` is inclusively between 4 and 63, and `Self` is `repr(u8)`; thus this
                // is safe and correct.
                Some(unsafe { mem::transmute::<u8, Self>(val) })
            }
            _ => None,
        }
    }
}
impl Display for FourToSixtyThree {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.into_u8().fmt(f)
    }
}
impl From<FourToSixtyThree> for u8 {
    #[inline]
    fn from(value: FourToSixtyThree) -> Self {
        value.into_u8()
    }
}
impl Default for FourToSixtyThree {
    #[inline]
    fn default() -> Self {
        Self::Four
    }
}
/// The [defined extensions](https://www.w3.org/TR/webauthn-3/#sctn-defined-extensions) to send to the client.
#[derive(Clone, Copy, Debug)]
pub struct Extension<'prf_first, 'prf_second> {
    /// [`credProps`](https://www.w3.org/TR/webauthn-3/#sctn-authenticator-credential-properties-extension).
    ///
    /// The best one can do to ensure a server-side credential is created is by sending
    /// [`ResidentKeyRequirement::Discouraged`]; however authenticators are still allowed
    /// to create a client-side credential. To more definitively check that a server-side credential is
    /// created, send this extension. Note that it can be difficult to impossible for a client/user agent to
    /// know that a server-side credential is created; thus even when the response is
    /// `Some(CredentialPropertiesOutput { rk: Some(false) })`, a client-side/"resident" credential could still
    /// have been created. One may have better luck checking if [`AuthTransports::contains`]
    /// [`AuthenticatorTransport::Internal`] and using that as an indicator if a client-side credential was created.
    ///
    /// In the event [`ClientExtensionsOutputs::cred_props`] is `Some(CredentialPropertiesOutput { rk: Some(false) })`
    /// and [`ResidentKeyRequirement::Required`] was sent, an error will happen regardless of this value.
    pub cred_props: Option<ExtensionReq>,
    /// [`credProtect`](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#sctn-credProtect-extension).
    pub cred_protect: CredProtect,
    /// [`minPinLength`](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#sctn-minpinlength-extension).
    ///
    /// When the value is enforced, that corresponds to
    /// [`minPinLength`](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#sctn-minpinlength-extension)
    /// in [`extensions`](https://www.w3.org/TR/webauthn-3/#authdata-extensions) set to a value at least as large
    /// as the contained `FourToSixtyThree`.
    pub min_pin_length: Option<(FourToSixtyThree, ExtensionInfo)>,
    /// [`prf`](https://www.w3.org/TR/webauthn-3/#prf-extension).
    ///
    /// When the value is enforced, that corresponds to
    /// [`enabled`](https://www.w3.org/TR/webauthn-3/#dom-authenticationextensionsprfoutputs-enabled) set to `true`.
    /// In contrast [`results`](https://www.w3.org/TR/webauthn-3/#dom-authenticationextensionsprfoutputs-results)
    /// must not exist, be `null`, or be an
    /// [`AuthenticationExtensionsPRFValues`](https://www.w3.org/TR/webauthn-3/#dictdef-authenticationextensionsprfvalues)
    /// such that [`first`](https://www.w3.org/TR/webauthn-3/#dom-authenticationextensionsprfvalues-first) is `null`
    /// and [`second`](https://www.w3.org/TR/webauthn-3/#dom-authenticationextensionsprfvalues-second) does not
    /// exist or is `null`. This is to ensure the decrypted outputs stay on the client.
    ///
    /// Note some authenticators can only enable `prf` during registration (e.g., CTAP authenticators that only
    /// support
    /// [`hmac-secret`](https://fidoalliance.org/specs/fido-v2.2-ps-20250228/fido-client-to-authenticator-protocol-v2.2-ps-20250228.html#sctn-hmac-secret-extension)
    /// and not
    /// [`hmac-secret-mc`](https://fidoalliance.org/specs/fido-v2.2-ps-20250228/fido-client-to-authenticator-protocol-v2.2-ps-20250228.html#sctn-hmac-secret-make-cred-extension));
    /// thus the value of `PrfInput` is ignored and only used as a signal to enable `prf`. For many such
    /// authenticators, not using this extension during registration will not preclude them from being used during
    /// authentication; however it is still encouraged to use the extension during registration since some
    /// authenticators actually require it.
    ///
    /// When the underlying credential is expected to be used during discoverable requests, it is likely that
    /// `'prf_first` will be `'static` and [`PrfInput::second`] is `None` since one will not be able to
    /// realistically rotate the underlying inputs and further the same input will likely be used for all credentials.
    /// For credentials intended to be used during non-discoverable requests, however, one is encouraged to rotate
    /// the inputs and have unique values for each credential.
    pub prf: Option<(PrfInput<'prf_first, 'prf_second>, ExtensionInfo)>,
}
impl<'prf_first, 'prf_second> Extension<'prf_first, 'prf_second> {
    /// Returns an empty `Extension`.
    #[inline]
    #[must_use]
    pub const fn none() -> Self {
        Self {
            cred_props: None,
            cred_protect: CredProtect::None,
            min_pin_length: None,
            prf: None,
        }
    }
    /// Same as [`Self::none`] except [`Self::cred_props`] is `Some` containing `req`.
    #[inline]
    #[must_use]
    pub const fn with_cred_props(req: ExtensionReq) -> Self {
        Self {
            cred_props: Some(req),
            ..Self::none()
        }
    }
    /// Same as [`Self::none`] except [`Self::cred_protect`] is `cred_protect`.
    #[inline]
    #[must_use]
    pub const fn with_cred_protect(cred_protect: CredProtect) -> Self {
        Self {
            cred_protect,
            ..Self::none()
        }
    }
    /// Same as [`Self::none`] except [`Self::min_pin_length`] is `Some` containing `min_len` and `info`.
    #[inline]
    #[must_use]
    pub const fn with_min_pin_length(min_len: FourToSixtyThree, info: ExtensionInfo) -> Self {
        Self {
            min_pin_length: Some((min_len, info)),
            ..Self::none()
        }
    }
    /// Same as [`Self::none`] except [`Self::prf`] is `Some` containing `input` and `info`.
    #[inline]
    #[must_use]
    pub const fn with_prf<'a: 'prf_first, 'b: 'prf_second>(
        input: PrfInput<'a, 'b>,
        info: ExtensionInfo,
    ) -> Self {
        Self {
            prf: Some((input, info)),
            ..Self::none()
        }
    }
}
impl Default for Extension<'_, '_> {
    /// Same as [`Self::none`].
    #[inline]
    fn default() -> Self {
        Self::none()
    }
}
#[cfg(test)]
impl PartialEq for Extension<'_, '_> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.cred_props == other.cred_props
            && self.cred_protect == other.cred_protect
            && self.min_pin_length == other.min_pin_length
            && self.prf == other.prf
    }
}
/// The maximum number of bytes a [`UserHandle`] can be made of per
/// [WebAuthn](https://www.w3.org/TR/webauthn-3/#user-handle).
pub const USER_HANDLE_MAX_LEN: usize = 64;
/// The minimum number of bytes a [`UserHandle`] can be made of per
/// [WebAuthn](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialuserentity-id).
pub const USER_HANDLE_MIN_LEN: usize = 1;
/// A [user handle](https://www.w3.org/TR/webauthn-3/#user-handle) that is made up of
/// [`USER_HANDLE_MIN_LEN`]–[`USER_HANDLE_MAX_LEN`] bytes.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct UserHandle<const LEN: usize>([u8; LEN]);
impl<const LEN: usize> UserHandle<LEN> {
    /// Returns the contained data as a `slice`.
    #[inline]
    #[must_use]
    pub const fn as_slice(&self) -> &[u8] {
        self.0.as_slice()
    }
    /// Returns the contained data as a shared reference to the array.
    #[inline]
    #[must_use]
    pub const fn as_array(&self) -> &[u8; LEN] {
        &self.0
    }
    /// Returns the contained data.
    #[inline]
    #[must_use]
    pub const fn into_array(self) -> [u8; LEN] {
        self.0
    }
}
/// Implements [`Default`] for [`UserHandle`] of array of length of the passed `usize` literal.
///
/// Only [`USER_HANDLE_MIN_LEN`]–[`USER_HANDLE_MAX_LEN`] inclusively are allowed to be passed.
macro_rules! user {
    ( $( $x:literal),* ) => {
        $(
impl Default for UserHandle<$x> {
    #[inline]
    fn default() -> Self {
        let mut data = [0; $x];
        rand::fill(data.as_mut_slice());
        Self(data)
    }
}
        )*
    };
}
// MUST only pass [`USER_HANDLE_MIN_LEN`]–[`USER_HANDLE_MAX_LEN`] inclusively.
user!(
    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
);
impl<const LEN: usize> UserHandle<LEN>
where
    Self: Default,
{
    /// Returns a new `UserHandle` based on `LEN` randomly-generated [`u8`]s.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::request::register::{UserHandle, UserHandle64, USER_HANDLE_MIN_LEN, USER_HANDLE_MAX_LEN};
    /// assert_eq!(
    ///     UserHandle::<USER_HANDLE_MIN_LEN>::new()
    ///         .as_ref()
    ///         .len(),
    ///     1
    /// );
    /// // The probability of an all-zero `UserHandle` being generated (assuming a good entropy
    /// // source) is 2^-512 ≈ 7.5 x 10^-155.
    /// assert_ne!(
    ///     UserHandle64::new().as_ref(),
    ///     [0; USER_HANDLE_MAX_LEN]
    /// );
    /// ```
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }
}
impl<const LEN: usize> AsRef<[u8]> for UserHandle<LEN> {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}
impl<const LEN: usize> Borrow<[u8]> for UserHandle<LEN> {
    #[inline]
    fn borrow(&self) -> &[u8] {
        self.as_slice()
    }
}
impl<const LEN: usize> PartialEq<&Self> for UserHandle<LEN> {
    #[inline]
    fn eq(&self, other: &&Self) -> bool {
        *self == **other
    }
}
impl<const LEN: usize> PartialEq<UserHandle<LEN>> for &UserHandle<LEN> {
    #[inline]
    fn eq(&self, other: &UserHandle<LEN>) -> bool {
        **self == *other
    }
}
impl<const LEN: usize> From<UserHandle<LEN>> for [u8; LEN] {
    #[inline]
    fn from(value: UserHandle<LEN>) -> Self {
        value.into_array()
    }
}
impl<'a: 'b, 'b, const LEN: usize> From<&'a UserHandle<LEN>> for &'b [u8; LEN] {
    #[inline]
    fn from(value: &'a UserHandle<LEN>) -> Self {
        value.as_array()
    }
}
/// `UserHandle` that is based on the [spec recommendation](https://www.w3.org/TR/webauthn-3/#user-handle).
pub type UserHandle64 = UserHandle<USER_HANDLE_MAX_LEN>;
/// `UserHandle` that is based on 16 bytes.
///
/// While not the recommended size like [`UserHandle64`], 16 bytes is common for many deployments since
/// it's the same size as [Universally Unique IDentifiers (UUIDs)](https://www.rfc-editor.org/rfc/rfc9562).
pub type UserHandle16 = UserHandle<16>;
impl UserHandle16 {
    /// Same as [`Self::new`] except 6 bits of metadata is encoded to conform with
    /// [UUID Version 4](https://www.rfc-editor.org/rfc/rfc9562#name-uuid-version-4).
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::request::register::UserHandle16;
    /// assert!(UserHandle16::new_uuid_v4().is_uuid_v4());
    /// ```
    #[inline]
    #[must_use]
    pub fn new_uuid_v4() -> Self {
        let mut this = Self::new();
        // The first 4 bits of the 6th octet (0-based index) represents the UUID version (i.e., 4).
        // We first 0-out the version bits retaining the other 4 bits, then set the version bits to 4.
        this.0[6] = (this.0[6] & 0x0F) | 0x40;
        // The first 2 bits of the 8th octet (0-based index) represents the UUID variant (i.e., 8,9,A,B)
        // which is defined to be 2.
        // We first 0-out the variant bits retaining the other 6 bits, then set the variant bits to 2.
        this.0[8] = (this.0[8] & 0x3F) | 0x80;
        this
    }
    /// Returns `true` iff `self` is a valid
    /// [UUID Version 4](https://www.rfc-editor.org/rfc/rfc9562#name-uuid-version-4).
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::request::register::UserHandle16;
    /// assert!(UserHandle16::new_uuid_v4().is_uuid_v4());
    /// let mut user = UserHandle16::new_uuid_v4().into_array();
    /// user[6] = 255;
    /// # #[cfg(feature = "custom")]
    /// assert!(!UserHandle16::from(user).is_uuid_v4());
    /// ```
    #[inline]
    #[must_use]
    pub const fn is_uuid_v4(&self) -> bool {
        // The first 4 bits of the 6th octet (0-based index) represents the UUID version (i.e., 4).
        // The first 2 bits of the 8th octet (0-based index) represents the UUID variant (i.e., 8,9,A,B) which
        // is defined to be 2.
        self.0[6] >> 4 == 0x4 && self.0[8] >> 6 == 0x2
    }
    /// Returns `Some` containing `uuid_v4` iff `uuid_v4` is a valid
    /// [UUID Version 4](https://www.rfc-editor.org/rfc/rfc9562#name-uuid-version-4).
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::request::register::UserHandle16;
    /// let mut user = UserHandle16::new_uuid_v4().into_array();
    /// assert!(UserHandle16::from_uuid_v4(user).is_some());
    /// user[8] = 255;
    /// assert!(UserHandle16::from_uuid_v4(user).is_none());
    /// ```
    #[cfg(feature = "custom")]
    #[inline]
    #[must_use]
    pub fn from_uuid_v4(uuid_v4: [u8; 16]) -> Option<Self> {
        let this = Self(uuid_v4);
        this.is_uuid_v4().then_some(this)
    }
}
/// [The `PublicKeyCredentialUserEntity`](https://www.w3.org/TR/webauthn-3/#dictdef-publickeycredentialuserentity)
/// sent to the client.
#[derive(Clone, Debug)]
pub struct PublicKeyCredentialUserEntity<'name, 'display_name, 'id, const LEN: usize> {
    /// [`name`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialentity-name).
    ///
    /// Note the spec recommends RPs enforce
    /// [RFC 8265 § 3.4.3](https://www.rfc-editor.org/rfc/rfc8265#section-3.4.3).
    pub name: &'name str,
    /// [`id`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialuserentity-id).
    pub id: &'id UserHandle<LEN>,
    /// [`displayName`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialuserentity-displayname).
    ///
    /// Note the spec recommends RPs enforce [RFC 8266 § 2.3](https://www.rfc-editor.org/rfc/rfc8266#section-2.3)
    /// when this isn't empty.
    pub display_name: &'display_name str,
}
/// `PublicKeyCredentialUserEntity` based on a [`UserHandle64`].
pub type PublicKeyCredentialUserEntity64<'name, 'display_name, 'id> =
    PublicKeyCredentialUserEntity<'name, 'display_name, 'id, USER_HANDLE_MAX_LEN>;
/// `PublicKeyCredentialUserEntity` based on a [`UserHandle16`].
pub type PublicKeyCredentialUserEntity16<'name, 'display_name, 'id> =
    PublicKeyCredentialUserEntity<'name, 'display_name, 'id, 16>;
/// [`ResidentKeyRequirement`](https://www.w3.org/TR/webauthn-3/#enumdef-residentkeyrequirement) sent to the client.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ResidentKeyRequirement {
    /// [`required`](https://www.w3.org/TR/webauthn-3/#dom-residentkeyrequirement-required).
    Required,
    /// [`discouraged`](https://www.w3.org/TR/webauthn-3/#dom-residentkeyrequirement-discouraged).
    Discouraged,
    /// [`preferred`](https://www.w3.org/TR/webauthn-3/#dom-residentkeyrequirement-preferred).
    Preferred,
}
impl AuthenticatorAttachment {
    /// Validates `self` against `other` based on `require_response`.
    const fn validate(self, require_response: bool, other: Self) -> Result<(), RegCeremonyErr> {
        match self {
            Self::None => {
                if require_response && matches!(other, Self::None) {
                    Err(RegCeremonyErr::MissingAuthenticatorAttachment)
                } else {
                    Ok(())
                }
            }
            Self::Platform => match other {
                Self::None => {
                    if require_response {
                        Err(RegCeremonyErr::MissingAuthenticatorAttachment)
                    } else {
                        Ok(())
                    }
                }
                Self::Platform => Ok(()),
                Self::CrossPlatform => Err(RegCeremonyErr::AuthenticatorAttachmentMismatch),
            },
            Self::CrossPlatform => match other {
                Self::None => {
                    if require_response {
                        Err(RegCeremonyErr::MissingAuthenticatorAttachment)
                    } else {
                        Ok(())
                    }
                }
                Self::CrossPlatform => Ok(()),
                Self::Platform => Err(RegCeremonyErr::AuthenticatorAttachmentMismatch),
            },
        }
    }
}
/// [`AuthenticatorSelectionCriteria`](https://www.w3.org/TR/webauthn-3/#dictionary-authenticatorSelection).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AuthenticatorSelectionCriteria {
    /// [`authenticatorAttachment`](https://www.w3.org/TR/webauthn-3/#dom-authenticatorselectioncriteria-authenticatorattachment).
    pub authenticator_attachment: AuthenticatorAttachment,
    /// [`residentKey`](https://www.w3.org/TR/webauthn-3/#dom-authenticatorselectioncriteria-residentkey).
    pub resident_key: ResidentKeyRequirement,
    /// [`userVerification`](https://www.w3.org/TR/webauthn-3/#dom-authenticatorselectioncriteria-userverification).
    pub user_verification: UserVerificationRequirement,
}
impl AuthenticatorSelectionCriteria {
    /// Returns an `AuthenticatorSelectionCriteria` useful for passkeys (i.e., [`Self::resident_key`] is set to
    /// [`ResidentKeyRequirement::Required`] and [`Self::user_verification`] is set to
    /// [`UserVerificationRequirement::Required`]).
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::{request::{
    /// #     register::{
    /// #         AuthenticatorSelectionCriteria, ResidentKeyRequirement,
    /// #     },
    /// #     UserVerificationRequirement,
    /// # }, response::AuthenticatorAttachment};
    /// let crit = AuthenticatorSelectionCriteria::passkey();
    /// assert_eq!(crit.authenticator_attachment, AuthenticatorAttachment::None);
    /// assert_eq!(crit.resident_key, ResidentKeyRequirement::Required);
    /// assert_eq!(crit.user_verification, UserVerificationRequirement::Required);
    /// ```
    #[inline]
    #[must_use]
    pub fn passkey() -> Self {
        Self {
            authenticator_attachment: AuthenticatorAttachment::default(),
            resident_key: ResidentKeyRequirement::Required,
            user_verification: UserVerificationRequirement::Required,
        }
    }
    /// Returns an `AuthenticatorSelectionCriteria` useful for second-factor flows (i.e., [`Self::resident_key`]
    /// is set to [`ResidentKeyRequirement::Discouraged`] and [`Self::user_verification`] is set to
    /// [`UserVerificationRequirement::Discouraged`]).
    ///
    /// Note some authenticators require user verification during credential registration (e.g.,
    /// [CTAP 2.0 authenticators](https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-client-to-authenticator-protocol-v2.0-id-20180227.html#authenticatorMakeCredential)).
    /// When an authenticator supports both CTAP 2.0 and
    /// [Universal 2nd Factor (U2F)](https://fidoalliance.org/specs/fido-u2f-v1.2-ps-20170411/fido-u2f-overview-v1.2-ps-20170411.html#registration-creating-a-key-pair)
    /// protocols, user agents will sometimes fall back to U2F when `UserVerificationRequirement::Discouraged`
    /// is requested since the latter allows for registration without user verification. If the user agent does not
    /// do this, then users will have an inconsistent experience when authenticating an already-registered
    /// credential. If this is undesirable, one can use [`UserVerificationRequirement::Required`] for this and
    /// [`PublicKeyCredentialRequestOptions::user_verification`] at the expense of requiring a user to verify
    /// themselves twice: once for the first factor and again here.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::{request::{
    /// #     register::{
    /// #         AuthenticatorSelectionCriteria, ResidentKeyRequirement,
    /// #     },
    /// #     UserVerificationRequirement,
    /// # }, response::AuthenticatorAttachment};
    /// let crit = AuthenticatorSelectionCriteria::second_factor();
    /// assert_eq!(crit.authenticator_attachment, AuthenticatorAttachment::None);
    /// assert_eq!(crit.resident_key, ResidentKeyRequirement::Discouraged);
    /// assert_eq!(crit.user_verification, UserVerificationRequirement::Discouraged);
    /// ```
    #[inline]
    #[must_use]
    pub fn second_factor() -> Self {
        Self {
            authenticator_attachment: AuthenticatorAttachment::default(),
            resident_key: ResidentKeyRequirement::Discouraged,
            user_verification: UserVerificationRequirement::Discouraged,
        }
    }
    /// Ensures a client-side credential was created when applicable. Also enforces `auth_attachment` when
    /// applicable.
    const fn validate(
        self,
        require_auth_attachment: bool,
        auth_attachment: AuthenticatorAttachment,
    ) -> Result<(), RegCeremonyErr> {
        self.authenticator_attachment
            .validate(require_auth_attachment, auth_attachment)
    }
}
/// Helper that verifies the overlap of [`CredentialCreationOptions::start_ceremony`] and
/// [`RegistrationServerState::decode`].
const fn validate_options_helper(
    auth_crit: AuthenticatorSelectionCriteria,
    extensions: ServerExtensionInfo,
) -> Result<(), CreationOptionsErr> {
    if matches!(
        auth_crit.user_verification,
        UserVerificationRequirement::Required
    ) {
        Ok(())
    } else if !matches!(extensions.prf, ServerPrfInfo::None) {
        Err(CreationOptionsErr::PrfWithoutUserVerification)
    } else if matches!(
        extensions.cred_protect,
        CredProtect::UserVerificationRequired(_, _)
    ) {
        Err(CreationOptionsErr::CredProtectRequiredWithoutUserVerification)
    } else {
        Ok(())
    }
}
/// The [`CredentialCreationOptions`](https://www.w3.org/TR/credential-management-1/#dictdef-credentialcreationoptions)
/// to send to the client when registering a new credential.
///
/// Upon saving the [`RegistrationServerState`] returned from [`Self::start_ceremony`], one MUST send
/// [`RegistrationClientState`] to the client ASAP. After receiving the newly created [`Registration`], it is
/// validated using [`RegistrationServerState::verify`].
#[derive(Debug)]
pub struct CredentialCreationOptions<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    const USER_LEN: usize,
> {
    /// [`mediation`](https://www.w3.org/TR/credential-management-1/#dom-credentialcreationoptions-mediation).
    ///
    /// Note if this is [`CredentialMediationRequirement::Conditional`], one may want to ensure
    /// [`AuthenticatorSelectionCriteria::user_verification`] is not [`UserVerificationRequirement::Required`]
    /// since some authenticators cannot enforce user verification during registration ceremonies when conditional
    /// mediation is used. Do note that in the event passkeys are to be created, one may want to set
    /// [`AuthenticationVerificationOptions::update_uv`] to `true` since [`Flag::user_verified`] will
    /// potentially be `false`.
    pub mediation: CredentialMediationRequirement,
    /// `public-key` [credential type](https://www.w3.org/TR/credential-management-1/#sctn-cred-type-registry).
    pub public_key: PublicKeyCredentialCreationOptions<
        'rp_id,
        'user_name,
        'user_display_name,
        'user_id,
        'prf_first,
        'prf_second,
        USER_LEN,
    >,
}
impl<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    const USER_LEN: usize,
>
    CredentialCreationOptions<
        'rp_id,
        'user_name,
        'user_display_name,
        'user_id,
        'prf_first,
        'prf_second,
        USER_LEN,
    >
{
    /// Sets [`Self::mediation`] to [`CredentialMediationRequirement::default`] and
    /// [`Self::public_key`] to [`PublicKeyCredentialCreationOptions::passkey`].
    #[inline]
    #[must_use]
    pub fn passkey<'a: 'rp_id, 'b: 'user_name, 'c: 'user_display_name, 'd: 'user_id>(
        rp_id: &'a RpId,
        user: PublicKeyCredentialUserEntity<'b, 'c, 'd, USER_LEN>,
        exclude_credentials: Vec<PublicKeyCredentialDescriptor<Box<[u8]>>>,
    ) -> Self {
        Self {
            mediation: CredentialMediationRequirement::default(),
            public_key: PublicKeyCredentialCreationOptions::passkey(
                rp_id,
                user,
                exclude_credentials,
            ),
        }
    }
    /// Sets [`Self::mediation`] to [`CredentialMediationRequirement::default`] and
    /// [`Self::public_key`] to [`PublicKeyCredentialCreationOptions::second_factor`].
    #[inline]
    #[must_use]
    pub fn second_factor<'a: 'rp_id, 'b: 'user_name, 'c: 'user_display_name, 'd: 'user_id>(
        rp_id: &'a RpId,
        user: PublicKeyCredentialUserEntity<'b, 'c, 'd, USER_LEN>,
        exclude_credentials: Vec<PublicKeyCredentialDescriptor<Box<[u8]>>>,
    ) -> Self {
        let mut opts = Self::passkey(rp_id, user, exclude_credentials);
        opts.public_key.authenticator_selection = AuthenticatorSelectionCriteria::second_factor();
        opts.public_key.extensions.cred_props = Some(ExtensionReq::Allow);
        opts.public_key.extensions.cred_protect =
            CredProtect::UserVerificationOptionalWithCredentialIdList(
                false,
                ExtensionInfo::AllowEnforceValue,
            );
        opts
    }
    /// Begins the [registration ceremony](https://www.w3.org/TR/webauthn-3/#registration-ceremony) consuming
    /// `self`. Note that the expiration [`Instant`]/[`SystemTime`] is saved, so `RegistrationClientState` MUST be
    /// sent ASAP. In order to complete registration, the returned `RegistrationServerState` MUST be saved so that
    /// it can later be used to verify the new credential with [`RegistrationServerState::verify`].
    ///
    /// # Errors
    ///
    /// Errors iff `self` contains incompatible configuration.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(not(feature = "serializable_server_state"))]
    /// # use std::time::Instant;
    /// # #[cfg(not(feature = "serializable_server_state"))]
    /// # use structured_webauthn::request::TimedCeremony as _;
    /// # use structured_webauthn::request::{
    /// #     register::{CredentialCreationOptions, PublicKeyCredentialUserEntity, UserHandle64},
    /// #     AsciiDomain, RpId
    /// # };
    /// # #[cfg(not(feature = "serializable_server_state"))]
    /// assert!(
    ///     CredentialCreationOptions::passkey(
    ///         &RpId::Domain(AsciiDomain::try_from("example.com".to_owned())?),
    ///         PublicKeyCredentialUserEntity {
    ///             name: "bernard.riemann",
    ///             id: &UserHandle64::new(),
    ///             display_name: "Georg Friedrich Bernhard Riemann",
    ///         },
    ///         Vec::new()
    ///     ).start_ceremony()?.0.expiration() > Instant::now()
    /// );
    /// # Ok::<_, structured_webauthn::AggErr>(())
    /// ```
    #[inline]
    pub fn start_ceremony(
        mut self,
    ) -> Result<
        (
            RegistrationServerState<USER_LEN>,
            RegistrationClientState<
                'rp_id,
                'user_name,
                'user_display_name,
                'user_id,
                'prf_first,
                'prf_second,
                USER_LEN,
            >,
        ),
        CreationOptionsErr,
    > {
        let extensions = self.public_key.extensions.into();
        validate_options_helper(self.public_key.authenticator_selection, extensions).and_then(
            |()| {
                match self
                    .public_key
                    .authenticator_selection
                    .authenticator_attachment
                {
                    AuthenticatorAttachment::None => Ok(()),
                    AuthenticatorAttachment::Platform => {
                        if self.public_key.hints.contains_cross_platform_hints() {
                            Err(CreationOptionsErr::HintsIncompatibleWithAuthAttachment)
                        } else {
                            Ok(())
                        }
                    }
                    AuthenticatorAttachment::CrossPlatform => {
                        if self.public_key.hints.contains_platform_hints() {
                            Err(CreationOptionsErr::HintsIncompatibleWithAuthAttachment)
                        } else {
                            Ok(())
                        }
                    }
                }
                .and_then(|()| {
                    #[cfg(not(feature = "serializable_server_state"))]
                    let now = Instant::now();
                    #[cfg(feature = "serializable_server_state")]
                    let now = SystemTime::now();
                    now.checked_add(Duration::from_millis(
                        NonZeroU64::from(self.public_key.timeout).get(),
                    ))
                    .ok_or(CreationOptionsErr::InvalidTimeout)
                    .map(|expiration| {
                        // We remove duplicates. The order has no significance, so this is OK.
                        self.public_key
                            .exclude_credentials
                            .sort_unstable_by(|a, b| a.id.as_ref().cmp(b.id.as_ref()));
                        self.public_key
                            .exclude_credentials
                            .dedup_by(|a, b| a.id.as_ref() == b.id.as_ref());
                        (
                            RegistrationServerState {
                                mediation: self.mediation,
                                challenge: SentChallenge(self.public_key.challenge.0),
                                pub_key_cred_params: self.public_key.pub_key_cred_params,
                                authenticator_selection: self.public_key.authenticator_selection,
                                extensions,
                                expiration,
                                user_id: *self.public_key.user.id,
                            },
                            RegistrationClientState(self),
                        )
                    })
                })
            },
        )
    }
}
/// `CredentialCreationOptions` based on a [`UserHandle64`].
pub type CredentialCreationOptions64<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
> = CredentialCreationOptions<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    USER_HANDLE_MAX_LEN,
>;
/// `CredentialCreationOptions` based on a [`UserHandle16`].
pub type CredentialCreationOptions16<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
> = CredentialCreationOptions<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    16,
>;
/// The [`PublicKeyCredentialCreationOptions`](https://www.w3.org/TR/webauthn-3/#dictdef-publickeycredentialcreationoptions)
/// to send to the client when registering a new credential.
#[derive(Debug)]
pub struct PublicKeyCredentialCreationOptions<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    const USER_LEN: usize,
> {
    /// [`rp`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-rp).
    pub rp_id: &'rp_id RpId,
    /// [`user`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-user).
    pub user: PublicKeyCredentialUserEntity<'user_name, 'user_display_name, 'user_id, USER_LEN>,
    /// [`challenge`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-challenge).
    pub challenge: Challenge,
    /// [`pubKeyCredParams`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-pubkeycredparams).
    pub pub_key_cred_params: CoseAlgorithmIdentifiers,
    /// [`timeout`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-timeout).
    ///
    /// Note we require a positive value despite the spec allowing an optional nonnegative value. This jives
    /// with the fact that in-memory storage is required when `serializable_server_state` is not enabled
    /// when attesting credentials as no timeout would make out-of-memory (OOM) conditions more likely.
    pub timeout: NonZeroU32,
    /// [`excludeCredentials`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-excludecredentials).
    pub exclude_credentials: Vec<PublicKeyCredentialDescriptor<Box<[u8]>>>,
    /// [`authenticatorSelection`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-authenticatorselection).
    pub authenticator_selection: AuthenticatorSelectionCriteria,
    /// [`hints`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-hints).
    pub hints: Hints,
    /// [`extensions`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-extensions).
    pub extensions: Extension<'prf_first, 'prf_second>,
}
impl<'rp_id, 'user_name, 'user_display_name, 'user_id, const USER_LEN: usize>
    PublicKeyCredentialCreationOptions<
        'rp_id,
        'user_name,
        'user_display_name,
        'user_id,
        '_,
        '_,
        USER_LEN,
    >
{
    /// Most deployments of passkeys should use this function. Specifically deployments that are both userless and
    /// passwordless and desire multi-factor authentication (MFA) to be done entirely on the authenticator. It
    /// is important `exclude_credentials` contains the information for _all_ [`RegisteredCredential`]s registered to
    /// [`PublicKeyCredentialUserEntity::id`] to avoid accidentally overwriting existing credentials that
    /// have been previously registered.
    ///
    /// Creates a `PublicKeyCredentialCreationOptions` that requires the authenticator to create a client-side
    /// discoverable credential enforcing any form of user verification. [`Self::timeout`] is [`FIVE_MINUTES`].
    /// [`Extension::cred_protect`] with [`CredProtect::UserVerificationRequired`] with `false` and
    /// [`ExtensionInfo::AllowEnforceValue`] is used.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::request::{
    /// #     register::{
    /// #         PublicKeyCredentialCreationOptions, PublicKeyCredentialUserEntity, UserHandle64
    /// #     },
    /// #     AsciiDomain, RpId, UserVerificationRequirement
    /// # };
    /// assert!(matches!(
    ///     PublicKeyCredentialCreationOptions::passkey(
    ///         &RpId::Domain(AsciiDomain::try_from("example.com".to_owned())?),
    ///         PublicKeyCredentialUserEntity {
    ///             name: "archimedes.of.syracuse",
    ///             id: &UserHandle64::new(),
    ///             display_name: "Αρχιμήδης ο Συρακούσιος",
    ///         },
    ///         Vec::new()
    ///     )
    ///     .authenticator_selection.user_verification, UserVerificationRequirement::Required
    /// ));
    /// # Ok::<_, structured_webauthn::AggErr>(())
    /// ```
    #[inline]
    #[must_use]
    pub fn passkey<'a: 'rp_id, 'b: 'user_name, 'c: 'user_display_name, 'd: 'user_id>(
        rp_id: &'a RpId,
        user: PublicKeyCredentialUserEntity<'b, 'c, 'd, USER_LEN>,
        exclude_credentials: Vec<PublicKeyCredentialDescriptor<Box<[u8]>>>,
    ) -> Self {
        Self {
            rp_id,
            user,
            challenge: Challenge::new(),
            pub_key_cred_params: CoseAlgorithmIdentifiers::default(),
            timeout: FIVE_MINUTES,
            exclude_credentials,
            authenticator_selection: AuthenticatorSelectionCriteria::passkey(),
            hints: Hints::EMPTY,
            extensions: Extension {
                cred_props: None,
                cred_protect: CredProtect::UserVerificationRequired(
                    false,
                    ExtensionInfo::AllowEnforceValue,
                ),
                min_pin_length: None,
                prf: None,
            },
        }
    }
    /// Deployments that want to incorporate a "something a user has" factor into a larger multi-factor
    /// authentication (MFA) setup. Specifically deployments that are _not_ userless or passwordless. It
    /// is important `exclude_credentials` contains the information for _all_ [`RegisteredCredential`]s registered
    /// to [`PublicKeyCredentialUserEntity::id`] to avoid accidentally overwriting existing credentials that
    /// have been previously registered.
    ///
    /// Creates a `PublicKeyCredentialCreationOptions` that prefers the authenticator to create a server-side
    /// credential without requiring user verification. [`Self::timeout`] is [`FIVE_MINUTES`].
    /// [`Extension::cred_props`] is [`ExtensionReq::Allow`]. [`Extension::cred_protect`] is
    /// [`CredProtect::UserVerificationOptionalWithCredentialIdList`] with `false` and
    /// [`ExtensionInfo::AllowEnforceValue`].
    ///
    /// Note some authenticators require user verification during credential registration (e.g.,
    /// [CTAP 2.0 authenticators](https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-client-to-authenticator-protocol-v2.0-id-20180227.html#authenticatorMakeCredential)).
    /// When an authenticator supports both CTAP 2.0 and
    /// [Universal 2nd Factor (U2F)](https://fidoalliance.org/specs/fido-u2f-v1.2-ps-20170411/fido-u2f-overview-v1.2-ps-20170411.html#registration-creating-a-key-pair)
    /// protocols, user agents will sometimes fall back to U2F when [`UserVerificationRequirement::Discouraged`]
    /// is requested since the latter allows for registration without user verification. If the user agent does not
    /// do this, then users will have an inconsistent experience when authenticating an already-registered
    /// credential. If this is undesirable, one can use [`UserVerificationRequirement::Required`] for this and
    /// [`PublicKeyCredentialRequestOptions::user_verification`] at the expense of requiring a user to verify
    /// themselves twice: once for the first factor and again here.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::request::{register::{
    /// #     PublicKeyCredentialCreationOptions, PublicKeyCredentialUserEntity, UserHandle64
    /// # }, AsciiDomain, RpId};
    /// assert_eq!(
    ///     PublicKeyCredentialCreationOptions::second_factor(
    ///         &RpId::Domain(AsciiDomain::try_from("example.com".to_owned())?),
    ///         PublicKeyCredentialUserEntity {
    ///             name: "carl.gauss",
    ///             id: &UserHandle64::new(),
    ///             display_name: "Johann Carl Friedrich Gauß",
    ///         },
    ///         Vec::new()
    ///     )
    ///     .timeout
    ///     .get(),
    ///     300_000
    /// );
    /// # Ok::<_, structured_webauthn::AggErr>(())
    /// ```
    #[inline]
    #[must_use]
    pub fn second_factor<'a: 'rp_id, 'b: 'user_name, 'c: 'user_display_name, 'd: 'user_id>(
        rp_id: &'a RpId,
        user: PublicKeyCredentialUserEntity<'b, 'c, 'd, USER_LEN>,
        exclude_credentials: Vec<PublicKeyCredentialDescriptor<Box<[u8]>>>,
    ) -> Self {
        let mut opts = Self::passkey(rp_id, user, exclude_credentials);
        opts.authenticator_selection = AuthenticatorSelectionCriteria::second_factor();
        opts.extensions.cred_props = Some(ExtensionReq::Allow);
        opts.extensions.cred_protect = CredProtect::UserVerificationOptionalWithCredentialIdList(
            false,
            ExtensionInfo::AllowEnforceValue,
        );
        opts
    }
}
/// `PublicKeyCredentialCreationOptions` based on a [`UserHandle64`].
pub type PublicKeyCredentialCreationOptions64<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
> = PublicKeyCredentialCreationOptions<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    USER_HANDLE_MAX_LEN,
>;
/// `PublicKeyCredentialCreationOptions` based on a [`UserHandle16`].
pub type PublicKeyCredentialCreationOptions16<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
> = PublicKeyCredentialCreationOptions<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    16,
>;
/// Container of a [`CredentialCreationOptions`] that has been used to start the registration ceremony.
/// This gets sent to the client ASAP.
#[derive(Debug)]
pub struct RegistrationClientState<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    const USER_LEN: usize,
>(
    CredentialCreationOptions<
        'rp_id,
        'user_name,
        'user_display_name,
        'user_id,
        'prf_first,
        'prf_second,
        USER_LEN,
    >,
);
impl<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    const USER_LEN: usize,
>
    RegistrationClientState<
        'rp_id,
        'user_name,
        'user_display_name,
        'user_id,
        'prf_first,
        'prf_second,
        USER_LEN,
    >
{
    /// Returns the `CredentialCreationOptions` that was used to start a registration ceremony.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::request::{register::{
    /// #     CoseAlgorithmIdentifiers, CredentialCreationOptions,
    /// #     PublicKeyCredentialUserEntity, UserHandle64,
    /// # }, AsciiDomain, RpId};
    /// assert_eq!(
    ///     CredentialCreationOptions::passkey(
    ///         &RpId::Domain(AsciiDomain::try_from("example.com".to_owned())?),
    ///         PublicKeyCredentialUserEntity {
    ///             name: "david.hilbert",
    ///             id: &UserHandle64::new(),
    ///             display_name: "David Hilbert",
    ///         },
    ///         Vec::new()
    ///     )
    ///     .start_ceremony()?
    ///     .1
    ///     .options()
    ///     .public_key
    ///     .rp_id.as_ref(),
    ///     "example.com"
    /// );
    /// # Ok::<_, structured_webauthn::AggErr>(())
    /// ```
    #[inline]
    #[must_use]
    pub const fn options(
        &self,
    ) -> &CredentialCreationOptions<
        'rp_id,
        'user_name,
        'user_display_name,
        'user_id,
        'prf_first,
        'prf_second,
        USER_LEN,
    > {
        &self.0
    }
}
/// `RegistrationClientState` based on a [`UserHandle64`].
pub type RegistrationClientState64<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
> = RegistrationClientState<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    USER_HANDLE_MAX_LEN,
>;
/// `RegistrationClientState` based on a [`UserHandle16`].
pub type RegistrationClientState16<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
> = RegistrationClientState<
    'rp_id,
    'user_name,
    'user_display_name,
    'user_id,
    'prf_first,
    'prf_second,
    16,
>;
/// Additional verification options to perform in [`RegistrationServerState::verify`].
#[derive(Clone, Copy, Debug)]
pub struct RegistrationVerificationOptions<'origins, 'top_origins, O, T> {
    /// Origins to use for [origin validation](https://www.w3.org/TR/webauthn-3/#sctn-validating-origin).
    ///
    /// When this is empty, the origin that will be used will be based on
    /// the [`RpId`] passed to [`RegistrationServerState::verify`]. If [`RpId::Domain`] or [`RpId::StaticDomain`],
    /// then the [`DomainOrigin`] returned from passing [`AsciiDomain::as_ref`] and [`AsciiDomainStatic::as_str`]
    /// to [`DomainOrigin::new`] respectively will be used; otherwise the [`Url`] in [`RpId::Url`] will be used.
    pub allowed_origins: &'origins [O],
    /// [Top-level origins](https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-origin)
    /// to use for [origin validation](https://www.w3.org/TR/webauthn-3/#sctn-validating-origin).
    ///
    /// When this is `Some`, [`CollectedClientData::cross_origin`] is allowed to be `true`. When the contained
    /// `slice` is empty, [`CollectedClientData::top_origin`] must be `None`. When this is `None`,
    /// `CollectedClientData::cross_origin` must be `false` and `CollectedClientData::top_origin` must be `None`.
    pub allowed_top_origins: Option<&'top_origins [T]>,
    /// The required [`Backup`] state of the credential.
    pub backup_requirement: BackupReq,
    /// Error when unsolicited extensions are sent back iff `true`.
    pub error_on_unsolicited_extensions: bool,
    /// [`AuthenticatorAttachment`] must be sent iff `true`.
    pub require_authenticator_attachment: bool,
    /// [`CollectedClientData::from_client_data_json_relaxed`] is used to extract [`CollectedClientData`] iff `true`.
    #[cfg(feature = "serde_relaxed")]
    pub client_data_json_relaxed: bool,
}
impl<O, T> RegistrationVerificationOptions<'_, '_, O, T> {
    /// Returns `Self` such that [`Self::allowed_origins`] is empty, [`Self::allowed_top_origins`] is `None`,
    /// [`Self::backup_requirement`] is [`BackupReq::None`], [`Self::error_on_unsolicited_extensions`] is `true`,
    /// [`Self::require_authenticator_attachment`] is `false`, and [`Self::client_data_json_relaxed`] is
    /// `true`.
    ///
    /// Note `O` and `T` should implement `PartialEq<Origin<'_>>` (e.g., `&str`).
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Self {
            allowed_origins: [].as_slice(),
            allowed_top_origins: None,
            backup_requirement: BackupReq::None,
            error_on_unsolicited_extensions: true,
            require_authenticator_attachment: false,
            #[cfg(feature = "serde_relaxed")]
            client_data_json_relaxed: true,
        }
    }
}
impl<O, T> Default for RegistrationVerificationOptions<'_, '_, O, T> {
    /// Same as [`Self::new`].
    #[inline]
    fn default() -> Self {
        Self {
            allowed_origins: &[],
            allowed_top_origins: None,
            backup_requirement: BackupReq::default(),
            error_on_unsolicited_extensions: true,
            require_authenticator_attachment: false,
            #[cfg(feature = "serde_relaxed")]
            client_data_json_relaxed: true,
        }
    }
}
/// `PrfInput` without the actual data sent to reduce memory usage when storing [`RegistrationServerState`] in an
/// in-memory collection.
#[derive(Clone, Copy, Debug)]
enum ServerPrfInfo {
    /// No `PrfInput`.
    None,
    /// `PrfInput::second` was `None`.
    One(ExtensionInfo),
    /// `PrfInput::second` was `Some`.
    Two(ExtensionInfo),
}
#[cfg(test)]
impl PartialEq for ServerPrfInfo {
    fn eq(&self, other: &Self) -> bool {
        match *self {
            Self::None => matches!(*other, Self::None),
            Self::One(info) => matches!(*other, Self::One(info2) if info == info2),
            Self::Two(info) => matches!(*other, Self::Two(info2) if info == info2),
        }
    }
}
impl From<Option<(PrfInput<'_, '_>, ExtensionInfo)>> for ServerPrfInfo {
    fn from(value: Option<(PrfInput<'_, '_>, ExtensionInfo)>) -> Self {
        value.map_or(Self::None, |val| {
            val.0
                .second
                .map_or_else(|| Self::One(val.1), |_| Self::Two(val.1))
        })
    }
}
/// `Extension` without the actual data sent to reduce memory usage when storing [`AuthenticationServerState`]
/// in an in-memory collection.
#[derive(Clone, Copy, Debug)]
struct ServerExtensionInfo {
    /// `Extension::cred_props`.
    cred_props: Option<ExtensionReq>,
    /// `Extension::cred_protect`.
    cred_protect: CredProtect,
    /// `Extension::min_pin_length`.
    min_pin_length: Option<(FourToSixtyThree, ExtensionInfo)>,
    /// `Extension::prf`.
    prf: ServerPrfInfo,
}
impl ServerExtensionInfo {
    /// Validates the extensions.
    fn validate(
        self,
        client_ext: ClientExtensionsOutputs,
        auth_ext: AuthenticatorExtensionOutput,
        error_unsolicited: bool,
    ) -> Result<(), ExtensionErr> {
        if error_unsolicited {
            self.validate_unsolicited(client_ext, auth_ext)
        } else {
            Ok(())
        }
        .and_then(|()| {
            self.validate_required(client_ext, auth_ext)
                .and_then(|()| self.validate_value(client_ext, auth_ext))
        })
    }
    /// Validates if there are any unsolicited extensions.
    ///
    /// Note no distinction is made between an extension that is empty and one that is not (i.e., we are checking
    ///  purely for the existence of extension keys).
    fn validate_unsolicited(
        mut self,
        client_ext: ClientExtensionsOutputs,
        auth_ext: AuthenticatorExtensionOutput,
    ) -> Result<(), ExtensionErr> {
        // For simpler code, we artificially set non-requested extensions after verifying there was not an error
        // and recursively call this function. There are so few extensions and the checks are fast that there
        // should be no worry of stack overflow or performance overhead.
        if self.cred_props.is_some() {
            if !matches!(self.cred_protect, CredProtect::None) {
                if self.min_pin_length.is_some() {
                    // This is the last extension, so recursion stops here.
                    if !matches!(self.prf, ServerPrfInfo::None) {
                        Ok(())
                    } else if client_ext.prf.is_some() {
                        Err(ExtensionErr::ForbiddenPrf)
                    } else if !matches!(auth_ext.hmac_secret, HmacSecret::None) {
                        Err(ExtensionErr::ForbiddenHmacSecret)
                    } else {
                        Ok(())
                    }
                } else if auth_ext.min_pin_length.is_some() {
                    Err(ExtensionErr::ForbiddenMinPinLength)
                } else {
                    // Pretend to set `minPinLength`, so we can check `prf`.
                    self.min_pin_length =
                        Some((FourToSixtyThree::Four, ExtensionInfo::RequireEnforceValue));
                    self.validate_unsolicited(client_ext, auth_ext)
                }
            } else if !matches!(auth_ext.cred_protect, CredentialProtectionPolicy::None) {
                Err(ExtensionErr::ForbiddenCredProtect)
            } else {
                // Pretend to set `credProtect`, so we can check `minPinLength` and `prf` extensions.
                self.cred_protect = CredProtect::UserVerificationOptional(
                    false,
                    ExtensionInfo::RequireEnforceValue,
                );
                self.validate_unsolicited(client_ext, auth_ext)
            }
        } else if client_ext.cred_props.is_some() {
            Err(ExtensionErr::ForbiddenCredProps)
        } else {
            // Pretend to set `credProps`; so we can check `credProtect`, `minPinLength`, and `prf` extensions.
            self.cred_props = Some(ExtensionReq::Require);
            self.validate_unsolicited(client_ext, auth_ext)
        }
    }
    /// Validates if any required extensions don't have a corresponding response.
    ///
    /// Note empty extensions are treated as missing. For example when requiring the `credProps` extension,
    /// all of the following responses would lead to a failure:
    /// `{"clientExtensionResults":{}}`: no extensions.
    /// `{"clientExtensionResults":{"prf":true}}`: only the `prf` extension.
    /// `{"clientExtensionResults":{"credProps":{}}}`: empty `credProps` extension.
    /// `{"clientExtensionResults":{"credProps":{"foo":false}}}`: `credProps` extension doesn't contain at least one
    /// expected field (i.e., still "empty").
    fn validate_required(
        self,
        client_ext: ClientExtensionsOutputs,
        auth_ext: AuthenticatorExtensionOutput,
    ) -> Result<(), ExtensionErr> {
        // We don't check `self.cred_protect` since `CredProtect::validate` checks for both a required response
        // and value enforcement; thus it only needs to be checked once (which it is in `Self::validate_value`).
        self.cred_props
            .map_or(Ok(()), |info| {
                if matches!(info, ExtensionReq::Require) {
                    if client_ext
                        .cred_props
                        .is_some_and(|props| props.rk.is_some())
                    {
                        Ok(())
                    } else {
                        Err(ExtensionErr::MissingCredProps)
                    }
                } else {
                    Ok(())
                }
            })
            .and_then(|()| {
                self.min_pin_length
                    .map_or(Ok(()), |info| {
                        if matches!(
                            info.1,
                            ExtensionInfo::RequireEnforceValue
                                | ExtensionInfo::RequireDontEnforceValue
                        ) {
                            auth_ext
                                .min_pin_length
                                .ok_or(ExtensionErr::MissingMinPinLength)
                                .map(|_| ())
                        } else {
                            Ok(())
                        }
                    })
                    .and_then(|()| match self.prf {
                        ServerPrfInfo::None => Ok(()),
                        ServerPrfInfo::One(info) | ServerPrfInfo::Two(info) => {
                            if matches!(
                                info,
                                ExtensionInfo::RequireEnforceValue
                                    | ExtensionInfo::RequireDontEnforceValue
                            ) {
                                if client_ext.prf.is_some() {
                                    Ok(())
                                } else {
                                    Err(ExtensionErr::MissingPrf)
                                }
                            } else {
                                Ok(())
                            }
                        }
                    })
            })
    }
    /// Validates the value of any extensions sent from the client.
    ///
    /// Note missing and empty extensions are always OK.
    fn validate_value(
        self,
        client_ext: ClientExtensionsOutputs,
        auth_ext: AuthenticatorExtensionOutput,
    ) -> Result<(), ExtensionErr> {
        // This also checks for a missing response. Instead of duplicating that check, we only call
        // `self.cred_protect.validate` once here and not also in `Self::validate_required`.
        self.cred_protect
            .validate(auth_ext.cred_protect)
            .and_then(|()| {
                self.min_pin_length
                    .map_or(Ok(()), |info| {
                        if matches!(
                            info.1,
                            ExtensionInfo::RequireEnforceValue | ExtensionInfo::AllowEnforceValue
                        ) {
                            auth_ext.min_pin_length.map_or(Ok(()), |pin| {
                                if pin >= info.0 {
                                    Ok(())
                                } else {
                                    Err(ExtensionErr::InvalidMinPinLength(info.0, pin))
                                }
                            })
                        } else {
                            Ok(())
                        }
                    })
                    .and_then(|()| match self.prf {
                        ServerPrfInfo::None => Ok(()),
                        ServerPrfInfo::One(info) | ServerPrfInfo::Two(info) => {
                            if matches!(
                                info,
                                ExtensionInfo::RequireEnforceValue
                                    | ExtensionInfo::AllowEnforceValue
                            ) {
                                client_ext
                                    .prf
                                    .map_or(Ok(()), |prf| {
                                        if prf.enabled {
                                            Ok(())
                                        } else {
                                            Err(ExtensionErr::InvalidPrfValue)
                                        }
                                    })
                                    .and({
                                        if matches!(auth_ext.hmac_secret, HmacSecret::NotEnabled) {
                                            Err(ExtensionErr::InvalidHmacSecretValue)
                                        } else {
                                            Ok(())
                                        }
                                    })
                            } else {
                                Ok(())
                            }
                        }
                    })
            })
    }
}
impl From<Extension<'_, '_>> for ServerExtensionInfo {
    fn from(value: Extension<'_, '_>) -> Self {
        Self {
            cred_props: value.cred_props,
            cred_protect: value.cred_protect,
            min_pin_length: value.min_pin_length,
            prf: value.prf.into(),
        }
    }
}
#[cfg(test)]
impl PartialEq for ServerExtensionInfo {
    fn eq(&self, other: &Self) -> bool {
        self.prf == other.prf
    }
}
// This is essentially the `PublicKeyCredentialCreationOptions` used to create it; however to reduce
// memory usage, we remove all unnecessary data making an instance of this 48 bytes in size on
// `x86_64-unknown-linux-gnu` platforms when `USER_LEN` is `USER_HANDLE_MIN_LEN`.
/// State needed to be saved when beginning the registration ceremony.
///
/// Saves the necessary information associated with the [`CredentialCreationOptions`] used to create it
/// via [`CredentialCreationOptions::start_ceremony`] so that registration of a new credential can be
/// performed with [`Self::verify`].
///
/// `RegistrationServerState` implements [`Borrow`] of [`SentChallenge`]; thus to obtain the correct
/// `RegistrationServerState` associated with a [`Registration`], one should use its corresponding
/// [`Registration::challenge`].
#[derive(Debug)]
pub struct RegistrationServerState<const USER_LEN: usize> {
    /// [`mediation`](https://www.w3.org/TR/credential-management-1/#dom-credentialcreationoptions-mediation).
    mediation: CredentialMediationRequirement,
    // This is a `SentChallenge` since we need `RegistrationServerState` to be fetchable after receiving the
    // response from the client. This response must obviously be constructable; thus its challenge is a
    // `SentChallenge`.
    //
    // This must never be mutated since we want to ensure it is actually a `Challenge` (which
    // can only be constructed via `Challenge::new`). This is guaranteed to be true iff
    // `serializable_server_state` is not enabled. We avoid implementing `trait`s like `Hash` when that
    // is enabled.
    /// [`challenge`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-challenge).
    challenge: SentChallenge,
    /// [`pubKeyCredParams`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-pubkeycredparams).
    pub_key_cred_params: CoseAlgorithmIdentifiers,
    /// [`authenticatorSelection`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-authenticatorselection).
    authenticator_selection: AuthenticatorSelectionCriteria,
    /// [`extensions`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-extensions).
    extensions: ServerExtensionInfo,
    /// `Instant` the ceremony expires.
    #[cfg(not(feature = "serializable_server_state"))]
    expiration: Instant,
    /// `SystemTime` the ceremony expires.
    #[cfg(feature = "serializable_server_state")]
    expiration: SystemTime,
    /// User handle.
    user_id: UserHandle<USER_LEN>,
}
impl<const USER_LEN: usize> RegistrationServerState<USER_LEN> {
    #[cfg(all(test, feature = "custom", feature = "serializable_server_state"))]
    fn is_eq(&self, other: &Self) -> bool {
        self.mediation == other.mediation
            && self.challenge == other.challenge
            && self.pub_key_cred_params == other.pub_key_cred_params
            && self.authenticator_selection == other.authenticator_selection
            && self.extensions == other.extensions
            && self.expiration == other.expiration
            && self.user_id == other.user_id
    }
    /// Verifies `response` is valid based on `self` consuming `self` and returning a `RegisteredCredential` that
    /// borrows the necessary data from `response`.
    ///
    /// `rp_id` MUST be the same as the [`PublicKeyCredentialCreationOptions::rp_id`] used when starting the
    /// ceremony.
    ///
    /// It is _essential_ to ensure [`RegisteredCredential::id`] has not been previously registered; if
    /// so, the ceremony SHOULD be aborted and a failure reported. When saving `RegisteredCredential`, one may
    /// want to save the [`RpId`] and [`PublicKeyCredentialUserEntity`] information; however since [`RpId`] is
    /// likely static, that may not be necessary. User information is also likely static for a given [`UserHandle`]
    /// (which is saved in `RegisteredCredential`); so if such info is saved, one may want to save it once per
    /// `UserHandle` and not per `RegisteredCredential`.
    ///
    /// # Errors
    ///
    /// Errors iff `response` is not valid according to the
    /// [registration ceremony criteria](https://www.w3.org/TR/webauthn-3/#sctn-registering-a-new-credential)
    /// or violates any of the settings in `options`.
    #[inline]
    pub fn verify<'a, O: PartialEq<Origin<'a>>, T: PartialEq<Origin<'a>>>(
        self,
        rp_id: &RpId,
        response: &'a Registration,
        options: &RegistrationVerificationOptions<'_, '_, O, T>,
    ) -> Result<RegisteredCredential<'a, USER_LEN>, RegCeremonyErr> {
        // [Registration ceremony](https://www.w3.org/TR/webauthn-3/#sctn-registering-a-new-credential)
        // is handled by:
        //
        // 1. Calling code.
        // 2. Client code and the construction of `resp` (hopefully via [`Registration::deserialize`]).
        // 3. Client code and the construction of `resp` (hopefully via [`AuthenticatorAttestation::deserialize`]).
        // 4. Client code and the construction of `resp` (hopefully via [`ClientExtensionsOutputs::deserialize`]).
        // 5. [`Self::partial_validate`].
        // 6. [`Self::partial_validate`].
        // 7. [`Self::partial_validate`].
        // 8. [`Self::partial_validate`].
        // 9. [`Self::partial_validate`].
        // 10. [`Self::partial_validate`].
        // 11. [`Self::partial_validate`].
        // 12. [`Self::partial_validate`].
        // 13. [`Self::partial_validate`].
        // 14. [`Self::partial_validate`].
        // 15. Below.
        // 16. [`Self::partial_validate`].
        // 17. [`Self::partial_validate`].
        // 18. [`Self::partial_validate`].
        // 19. [`Self::partial_validate`].
        // 20. Below.
        // 21. [`Self::partial_validate`].
        // 22. [`Self::partial_validate`].
        // 23. N/A since only none and self attestations are supported.
        // 24. Always satisfied since only none and self attestations are supported (Item 3 is N/A).
        // 25. [`Self::partial_validate`].
        // 26. Calling code.
        // 27. Below.
        // 28. N/A since only none and self attestations are supported.
        // 29. Below.

        // Steps 5–14, 16–19, 21–22, and 25.
        self.partial_validate(rp_id, response, (), &options.into())
            .map_err(RegCeremonyErr::from)
            .and_then(|attestation_object| {
                let auth_data = attestation_object.auth_data();
                let flags = auth_data.flags();
                // Step 15.
                if matches!(self.mediation, CredentialMediationRequirement::Conditional)
                    || flags.user_present
                {
                    self.authenticator_selection
                        // Verify any required authenticator attachment modality.
                        .validate(
                            options.require_authenticator_attachment,
                            response.authenticator_attachment,
                        )
                        .and_then(|()| {
                            let attested_credential_data = auth_data.attested_credential_data();
                            self.pub_key_cred_params
                                // Step 20.
                                .validate(attested_credential_data.credential_public_key)
                                .and_then(|()| {
                                    let extensions = auth_data.extensions();
                                    // Step 27.
                                    self.extensions
                                        .validate(
                                            response.client_extension_results,
                                            extensions,
                                            options.error_on_unsolicited_extensions,
                                        )
                                        .map_err(RegCeremonyErr::Extension)
                                        .and_then(|()| {
                                            // Step 29.
                                            RegisteredCredential::new(
                                                attested_credential_data.credential_id,
                                                response.response.transports(),
                                                self.user_id,
                                                StaticState {
                                                    credential_public_key: attested_credential_data
                                                        .credential_public_key,
                                                    extensions: extensions.into(),
                                                    client_extension_results: response
                                                        .client_extension_results
                                                        .into(),
                                                },
                                                DynamicState {
                                                    user_verified: flags.user_verified,
                                                    backup: flags.backup,
                                                    sign_count: auth_data.sign_count(),
                                                    authenticator_attachment: response
                                                        .authenticator_attachment,
                                                },
                                                Metadata {
                                                    attestation: match attestation_object
                                                        .attestation()
                                                    {
                                                        AttestationFormat::None => {
                                                            Attestation::None
                                                        }
                                                        AttestationFormat::Packed(_) => {
                                                            Attestation::Surrogate
                                                        }
                                                    },
                                                    aaguid: attested_credential_data.aaguid,
                                                    extensions: extensions.into(),
                                                    client_extension_results: response
                                                        .client_extension_results
                                                        .into(),
                                                    resident_key: self
                                                        .authenticator_selection
                                                        .resident_key,
                                                },
                                            )
                                            .map_err(RegCeremonyErr::Credential)
                                        })
                                })
                        })
                } else {
                    Err(RegCeremonyErr::UserNotPresent)
                }
            })
    }
}
impl<const USER_LEN: usize> TimedCeremony for RegistrationServerState<USER_LEN> {
    #[cfg(any(doc, not(feature = "serializable_server_state")))]
    #[inline]
    fn expiration(&self) -> Instant {
        self.expiration
    }
    #[cfg(all(not(doc), feature = "serializable_server_state"))]
    #[inline]
    fn expiration(&self) -> SystemTime {
        self.expiration
    }
}
impl<const USER_LEN: usize> Ceremony<USER_LEN, false> for RegistrationServerState<USER_LEN> {
    type R = Registration;
    fn rand_challenge(&self) -> SentChallenge {
        self.challenge
    }
    #[cfg(not(feature = "serializable_server_state"))]
    fn expiry(&self) -> Instant {
        self.expiration
    }
    #[cfg(feature = "serializable_server_state")]
    fn expiry(&self) -> SystemTime {
        self.expiration
    }
    fn user_verification(&self) -> UserVerificationRequirement {
        self.authenticator_selection.user_verification
    }
}
impl<const USER_LEN: usize> Borrow<SentChallenge> for RegistrationServerState<USER_LEN> {
    #[inline]
    fn borrow(&self) -> &SentChallenge {
        &self.challenge
    }
}
impl<const USER_LEN: usize> PartialEq for RegistrationServerState<USER_LEN> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.challenge == other.challenge
    }
}
impl<const USER_LEN: usize> PartialEq<&Self> for RegistrationServerState<USER_LEN> {
    #[inline]
    fn eq(&self, other: &&Self) -> bool {
        *self == **other
    }
}
impl<const USER_LEN: usize> PartialEq<RegistrationServerState<USER_LEN>>
    for &RegistrationServerState<USER_LEN>
{
    #[inline]
    fn eq(&self, other: &RegistrationServerState<USER_LEN>) -> bool {
        **self == *other
    }
}
impl<const USER_LEN: usize> Eq for RegistrationServerState<USER_LEN> {}
impl<const USER_LEN: usize> Hash for RegistrationServerState<USER_LEN> {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.challenge.hash(state);
    }
}
impl<const USER_LEN: usize> PartialOrd for RegistrationServerState<USER_LEN> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl<const USER_LEN: usize> Ord for RegistrationServerState<USER_LEN> {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.challenge.cmp(&other.challenge)
    }
}
/// `RegistrationServerState` based on a [`UserHandle64`].
pub type RegistrationServerState64 = RegistrationServerState<USER_HANDLE_MAX_LEN>;
/// `RegistrationServerState` based on a [`UserHandle16`].
pub type RegistrationServerState16 = RegistrationServerState<16>;