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
extern crate alloc;
#[cfg(test)]
mod tests;
use crate::{
    request::{
        Challenge, RpId, Url,
        register::{PublicKeyCredentialUserEntity, UserHandle},
    },
    response::{
        auth::error::{
            AuthCeremonyErr, AuthenticatorDataErr as AuthAuthDataErr,
            AuthenticatorExtensionOutputErr as AuthAuthExtErr,
        },
        error::{CollectedClientDataErr, CredentialIdErr},
        register::error::{
            AttestationObjectErr, AttestedCredentialDataErr,
            AuthenticatorDataErr as RegAuthDataErr,
            AuthenticatorExtensionOutputErr as RegAuthExtErr, PubKeyErr, RegCeremonyErr,
        },
    },
};
use alloc::borrow::Cow;
use core::{
    borrow::Borrow,
    cmp::Ordering,
    convert::Infallible,
    fmt::{self, Display, Formatter},
    hash::{Hash, Hasher},
    str,
};
use rsa::sha2::{Sha256, digest::OutputSizeUser as _};
#[cfg(feature = "serde_relaxed")]
use ser_relaxed::SerdeJsonErr;
/// Contains functionality for completing the
/// [authentication ceremony](https://www.w3.org/TR/webauthn-3/#authentication-ceremony).
///
/// # Examples
///
/// ```no_run
/// # use core::convert;
/// # use structured_webauthn::{
/// #     hash::hash_set::{InsertRemoveExpired, MaxLenHashSet},
/// #     request::{auth::{error::DiscoverableCredentialRequestOptionsErr, DiscoverableAuthenticationClientState, DiscoverableCredentialRequestOptions, AuthenticationVerificationOptions}, register::{BackupReq, UserHandle, USER_HANDLE_MAX_LEN, UserHandle64}, RpId},
/// #     response::{auth::{error::AuthCeremonyErr, DiscoverableAuthentication64}, error::CollectedClientDataErr, register::{AuthenticatorExtensionOutputStaticState, ClientExtensionsOutputsStaticState, CredentialProtectionPolicy, DynamicState, Ed25519PubKey, CompressedPubKeyOwned, StaticState}, AuthenticatorAttachment, Backup, CollectedClientData, CredentialId},
/// #     AuthenticatedCredential, CredentialErr
/// # };
/// # #[derive(Debug)]
/// # enum E {
/// #     CollectedClientData(CollectedClientDataErr),
/// #     DiscoverableCredentialRequestOptions(DiscoverableCredentialRequestOptionsErr),
/// #     SerdeJson(serde_json::Error),
/// #     MissingUserHandle,
/// #     MissingCeremony,
/// #     UnknownCredential,
/// #     Credential(CredentialErr),
/// #     AuthCeremony(AuthCeremonyErr),
/// # }
/// # impl From<CollectedClientDataErr> for E {
/// #     fn from(value: CollectedClientDataErr) -> Self {
/// #         Self::CollectedClientData(value)
/// #     }
/// # }
/// # impl From<DiscoverableCredentialRequestOptionsErr> for E {
/// #     fn from(value: DiscoverableCredentialRequestOptionsErr) -> Self {
/// #         Self::DiscoverableCredentialRequestOptions(value)
/// #     }
/// # }
/// # impl From<serde_json::Error> for E {
/// #     fn from(value: serde_json::Error) -> Self {
/// #         Self::SerdeJson(value)
/// #     }
/// # }
/// # impl From<CredentialErr> for E {
/// #     fn from(value: CredentialErr) -> Self {
/// #         Self::Credential(value)
/// #     }
/// # }
/// # impl From<AuthCeremonyErr> for E {
/// #     fn from(value: AuthCeremonyErr) -> Self {
/// #         Self::AuthCeremony(value)
/// #     }
/// # }
/// const RP_ID: &RpId = &RpId::from_static_domain("example.com").unwrap();
/// let mut ceremonies = MaxLenHashSet::new(128);
/// let (server, client) = DiscoverableCredentialRequestOptions::passkey(RP_ID).start_ceremony()?;
/// assert_eq!(ceremonies.insert_remove_all_expired(server), InsertRemoveExpired::Success);
/// # #[cfg(feature = "serde")]
/// let authentication = serde_json::from_str::<DiscoverableAuthentication64>(get_authentication_json(client).as_str())?;
/// # #[cfg(feature = "serde")]
/// let user_handle = authentication.response().user_handle();
/// # #[cfg(feature = "serde")]
/// let (static_state, dynamic_state) = get_credential(authentication.raw_id(), &user_handle).ok_or(E::UnknownCredential)?;
/// # #[cfg(all(feature = "custom", feature = "serde"))]
/// let mut cred = AuthenticatedCredential::new(authentication.raw_id(), &user_handle, static_state, dynamic_state)?;
/// # #[cfg(all(feature = "custom", feature = "serde"))]
/// if ceremonies.take(&authentication.challenge()?).ok_or(E::MissingCeremony)?.verify(RP_ID, &authentication, &mut cred, &AuthenticationVerificationOptions::<&str, &str>::default())? {
///     update_cred(authentication.raw_id(), cred.dynamic_state());
/// }
/// /// Send `DiscoverableAuthenticationClientState` and receive `DiscoverableAuthentication64` JSON from client.
/// # #[cfg(feature = "serde")]
/// fn get_authentication_json(client: DiscoverableAuthenticationClientState<'_, '_, '_>) -> String {
///     // â‹®
/// #     let client_data_json = base64url_nopad::encode(serde_json::json!({
/// #         "type": "webauthn.get",
/// #         "challenge": client.options().public_key.challenge,
/// #         "origin": format!("https://{}", client.options().public_key.rp_id.as_ref()),
/// #         "crossOrigin": false
/// #     }).to_string().as_bytes());
/// #     serde_json::json!({
/// #         "id": "AAAAAAAAAAAAAAAAAAAAAA",
/// #         "rawId": "AAAAAAAAAAAAAAAAAAAAAA",
/// #         "response": {
/// #             "clientDataJSON": client_data_json,
/// #             "authenticatorData": "",
/// #             "signature": "",
/// #             "userHandle": "AA"
/// #         },
/// #         "clientExtensionResults": {},
/// #         "type": "public-key"
/// #     }).to_string()
/// }
/// /// Gets the `AuthenticatedCredential` parts associated with `id` and `user_handle` from the database.
/// fn get_credential(id: CredentialId<&[u8]>, user_handle: &UserHandle64) -> Option<(StaticState<CompressedPubKeyOwned>, DynamicState)> {
///     // â‹®
/// #     Some((StaticState { credential_public_key: CompressedPubKeyOwned::Ed25519(Ed25519PubKey::from([0; 32])), extensions: AuthenticatorExtensionOutputStaticState { cred_protect: CredentialProtectionPolicy::UserVerificationRequired, hmac_secret: None, }, client_extension_results: ClientExtensionsOutputsStaticState { prf: None, }, }, DynamicState { user_verified: true, backup: Backup::NotEligible, sign_count: 1, authenticator_attachment: AuthenticatorAttachment::None }))
/// }
/// /// Updates the current `DynamicState` associated with `id` in the database to
/// /// `dyn_state`.
/// fn update_cred(id: CredentialId<&[u8]>, dyn_state: DynamicState) {
///     // â‹®
/// }
/// # Ok::<_, E>(())
/// ```
pub mod auth;
/// Contains functionality to (de)serialize data to a data store.
#[cfg(feature = "bin")]
pub mod bin;
/// Contains constants useful for
/// [CTAP2 canonical CBOR encoding form](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#ctap2-canonical-cbor-encoding-form).
mod cbor;
/// Contains functionality that needs to be accessible when `bin` or `serde` are not enabled.
#[cfg(feature = "custom")]
pub mod custom;
/// Contains error types.
pub mod error;
/// Contains functionality for completing the
/// [registration ceremony](https://www.w3.org/TR/webauthn-3/#registration-ceremony).
///
/// # Examples
///
/// ```no_run
/// # use core::convert;
/// # use structured_webauthn::{
/// #     hash::hash_set::{InsertRemoveExpired, MaxLenHashSet},
/// #     request::{register::{error::CreationOptionsErr, CredentialCreationOptions, PublicKeyCredentialUserEntity, RegistrationClientState, UserHandle, UserHandle64, USER_HANDLE_MAX_LEN, RegistrationVerificationOptions}, PublicKeyCredentialDescriptor, RpId},
/// #     response::{register::{error::RegCeremonyErr, Registration}, error::CollectedClientDataErr, CollectedClientData},
/// #     RegisteredCredential
/// # };
/// # #[derive(Debug)]
/// # enum E {
/// #     CollectedClientData(CollectedClientDataErr),
/// #     CreationOptions(CreationOptionsErr),
/// #     SerdeJson(serde_json::Error),
/// #     MissingCeremony,
/// #     RegCeremony(RegCeremonyErr),
/// # }
/// # impl From<CollectedClientDataErr> for E {
/// #     fn from(value: CollectedClientDataErr) -> Self {
/// #         Self::CollectedClientData(value)
/// #     }
/// # }
/// # impl From<CreationOptionsErr> for E {
/// #     fn from(value: CreationOptionsErr) -> Self {
/// #         Self::CreationOptions(value)
/// #     }
/// # }
/// # impl From<serde_json::Error> for E {
/// #     fn from(value: serde_json::Error) -> Self {
/// #         Self::SerdeJson(value)
/// #     }
/// # }
/// # impl From<RegCeremonyErr> for E {
/// #     fn from(value: RegCeremonyErr) -> Self {
/// #         Self::RegCeremony(value)
/// #     }
/// # }
/// const RP_ID: &RpId = &RpId::from_static_domain("example.com").unwrap();
/// # #[cfg(feature = "custom")]
/// let mut ceremonies = MaxLenHashSet::new(128);
/// # #[cfg(feature = "custom")]
/// let user_handle = get_user_handle();
/// # #[cfg(feature = "custom")]
/// let user = get_user_entity(&user_handle);
/// # #[cfg(feature = "custom")]
/// let creds = get_registered_credentials(user_handle);
/// # #[cfg(feature = "custom")]
/// let (server, client) = CredentialCreationOptions::passkey(RP_ID, user, creds).start_ceremony()?;
/// # #[cfg(feature = "custom")]
/// assert_eq!(ceremonies.insert_remove_all_expired(server), InsertRemoveExpired::Success);
/// # #[cfg(all(feature = "serde_relaxed", feature = "custom"))]
/// let registration = serde_json::from_str::<Registration>(get_registration_json(client).as_str())?;
/// let ver_opts = RegistrationVerificationOptions::<&str, &str>::default();
/// # #[cfg(all(feature = "custom", feature = "serde_relaxed"))]
/// insert_cred(ceremonies.take(&registration.challenge()?).ok_or(E::MissingCeremony)?.verify(RP_ID, &registration, &ver_opts)?);
/// /// Extract `UserHandle` from session cookie if this is not the first credential registered.
/// # #[cfg(feature = "custom")]
/// fn get_user_handle() -> UserHandle64 {
///     // â‹®
/// #     [0; USER_HANDLE_MAX_LEN].into()
/// }
/// /// Fetch `PublicKeyCredentialUserEntity` info associated with `user`.
/// ///
/// /// If this is the first time a credential is being registered, then `PublicKeyCredentialUserEntity`
/// /// will need to be constructed with `name` and `display_name` passed from the client and `UserHandle::new`
/// /// used for `id`. Once created, this info can be stored such that the entity information
/// /// does not need to be requested for subsequent registrations.
/// # #[cfg(feature = "custom")]
/// fn get_user_entity(user: &UserHandle<USER_HANDLE_MAX_LEN>) -> PublicKeyCredentialUserEntity<'_, '_, '_, USER_HANDLE_MAX_LEN> {
///     // â‹®
/// #     PublicKeyCredentialUserEntity {
/// #         name: "foo",
/// #         id: user,
/// #         display_name: "",
/// #     }
/// }
/// /// Send `RegistrationClientState` and receive `Registration` JSON from client.
/// # #[cfg(feature = "serde")]
/// fn get_registration_json(client: RegistrationClientState<'_, '_, '_, '_, '_, '_, USER_HANDLE_MAX_LEN>) -> String {
///     // â‹®
/// #     let client_data_json = base64url_nopad::encode(serde_json::json!({
/// #         "type": "webauthn.create",
/// #         "challenge": client.options().public_key.challenge,
/// #         "origin": format!("https://{}", client.options().public_key.rp_id.as_ref()),
/// #         "crossOrigin": false
/// #     }).to_string().as_bytes());
/// #     serde_json::json!({
/// #         "response": {
/// #             "clientDataJSON": client_data_json,
/// #             "attestationObject": ""
/// #         }
/// #     }).to_string()
/// }
/// /// Fetch the `PublicKeyCredentialDescriptor`s associated with `user`.
/// ///
/// /// This doesn't need to be called when this is the first credential registered for `user`; instead
/// /// an empty `Vec` should be passed.
/// fn get_registered_credentials(
///     user: UserHandle<USER_HANDLE_MAX_LEN>,
/// ) -> Vec<PublicKeyCredentialDescriptor<Box<[u8]>>> {
///     // â‹®
/// #     Vec::new()
/// }
/// /// Inserts `RegisteredCredential::into_parts` into the database.
/// fn insert_cred(cred: RegisteredCredential<'_, USER_HANDLE_MAX_LEN>) {
///     // â‹®
/// }
/// # Ok::<_, E>(())
/// ```
pub mod register;
/// Contains functionality to (de)serialize data to/from a client.
#[cfg(feature = "serde")]
pub(crate) mod ser;
/// Contains functionality to deserialize data from a client in a "relaxed" way.
#[cfg(feature = "serde_relaxed")]
pub mod ser_relaxed;
/// [Backup eligibility](https://www.w3.org/TR/webauthn-3/#backup-eligibility) and
/// [backup state](https://www.w3.org/TR/webauthn-3/#backup-state).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Backup {
    /// [BE and BS](https://www.w3.org/TR/webauthn-3/#authdata-flags) flags are `0`.
    NotEligible,
    /// [BE and BS](https://www.w3.org/TR/webauthn-3/#authdata-flags) flags are `1` and `0` respectively.
    Eligible,
    /// [BE and BS](https://www.w3.org/TR/webauthn-3/#authdata-flags) flags are `1`.
    Exists,
}
impl PartialEq<&Self> for Backup {
    #[inline]
    fn eq(&self, other: &&Self) -> bool {
        *self == **other
    }
}
impl PartialEq<Backup> for &Backup {
    #[inline]
    fn eq(&self, other: &Backup) -> bool {
        **self == *other
    }
}
/// [`AuthenticatorTransport`](https://www.w3.org/TR/webauthn-3/#enumdef-authenticatortransport).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AuthenticatorTransport {
    /// [`ble`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-ble).
    Ble,
    /// [`hybrid`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-hybrid).
    Hybrid,
    /// [`internal`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-internal).
    Internal,
    /// [`nfc`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-nfc).
    Nfc,
    /// [`smart-card`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-smart-card).
    SmartCard,
    /// [`usb`](https://www.w3.org/TR/webauthn-3/#dom-authenticatortransport-usb).
    Usb,
}
impl AuthenticatorTransport {
    /// Returns the encoded [`u8`] that `self` represents.
    const fn to_u8(self) -> u8 {
        match self {
            Self::Ble => 0x1,
            Self::Hybrid => 0x2,
            Self::Internal => 0x4,
            Self::Nfc => 0x8,
            Self::SmartCard => 0x10,
            Self::Usb => 0x20,
        }
    }
}
/// Set of [`AuthenticatorTransport`]s.
#[derive(Clone, Copy, Debug)]
pub struct AuthTransports(u8);
impl AuthTransports {
    /// An empty `AuthTransports`.
    #[cfg(feature = "custom")]
    pub const NONE: Self = Self::new();
    /// An `AuthTransports` containing all possible [`AuthenticatorTransport`]s.
    #[cfg(feature = "custom")]
    pub const ALL: Self = Self::all();
    /// Construct an empty `AuthTransports`.
    #[cfg(any(feature = "bin", feature = "custom", feature = "serde"))]
    pub(super) const fn new() -> Self {
        Self(0)
    }
    #[cfg(any(feature = "bin", feature = "custom"))]
    /// Construct an `AuthTransports` containing all `AuthenticatorTransport`s.
    const fn all() -> Self {
        Self::new()
            .add_transport(AuthenticatorTransport::Ble)
            .add_transport(AuthenticatorTransport::Hybrid)
            .add_transport(AuthenticatorTransport::Internal)
            .add_transport(AuthenticatorTransport::Nfc)
            .add_transport(AuthenticatorTransport::SmartCard)
            .add_transport(AuthenticatorTransport::Usb)
    }
    /// Returns the number of [`AuthenticatorTransport`]s in `self`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::AuthTransports;
    /// # #[cfg(feature = "custom")]
    /// assert_eq!(AuthTransports::ALL.count(), 6);
    /// ```
    #[inline]
    #[must_use]
    pub const fn count(self) -> u32 {
        self.0.count_ones()
    }
    /// Returns `true` iff there are no [`AuthenticatorTransport`]s in `self`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::AuthTransports;
    /// # #[cfg(feature = "custom")]
    /// assert!(AuthTransports::NONE.is_empty());
    /// ```
    #[inline]
    #[must_use]
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }
    /// Returns `true` iff `self` contains `transport`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::{AuthTransports, AuthenticatorTransport};
    /// # #[cfg(feature = "custom")]
    /// assert!(AuthTransports::ALL.contains(AuthenticatorTransport::Ble));
    /// ```
    #[inline]
    #[must_use]
    pub const fn contains(self, transport: AuthenticatorTransport) -> bool {
        let val = transport.to_u8();
        self.0 & val == val
    }
    /// Returns a copy of `self` with `transport` added.
    ///
    /// `self` is returned iff `transport` already exists.
    #[cfg(any(feature = "bin", feature = "custom", feature = "serde"))]
    const fn add_transport(self, transport: AuthenticatorTransport) -> Self {
        Self(self.0 | transport.to_u8())
    }
    /// Returns a copy of `self` with `transport` added.
    ///
    /// `self` is returned iff `transport` already exists.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::{AuthTransports, AuthenticatorTransport};
    /// assert_eq!(
    ///     AuthTransports::NONE
    ///         .add(AuthenticatorTransport::Usb)
    ///         .count(),
    ///     1
    /// );
    /// assert_eq!(
    ///     AuthTransports::ALL.add(AuthenticatorTransport::Usb).count(),
    ///     6
    /// );
    /// ```
    #[cfg(feature = "custom")]
    #[inline]
    #[must_use]
    pub const fn add(self, transport: AuthenticatorTransport) -> Self {
        self.add_transport(transport)
    }
    /// Returns a copy of `self` with `transport` removed.
    ///
    /// `self` is returned iff `transport` did not exist.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::{AuthTransports, AuthenticatorTransport};
    /// assert_eq!(
    ///     AuthTransports::ALL
    ///         .remove(AuthenticatorTransport::Internal)
    ///         .count(),
    ///     5
    /// );
    /// assert_eq!(
    ///     AuthTransports::NONE.remove(AuthenticatorTransport::Usb).count(),
    ///     0
    /// );
    /// ```
    #[cfg(feature = "custom")]
    #[inline]
    #[must_use]
    pub const fn remove(self, transport: AuthenticatorTransport) -> Self {
        Self(self.0 & !transport.to_u8())
    }
}
/// [`AuthenticatorAttachment`](https://www.w3.org/TR/webauthn-3/#enumdef-authenticatorattachment).
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AuthenticatorAttachment {
    /// No attachment information.
    #[default]
    None,
    /// [`platform`](https://www.w3.org/TR/webauthn-3/#dom-authenticatorattachment-platform).
    Platform,
    /// [`cross-platform`](https://www.w3.org/TR/webauthn-3/#dom-authenticatorattachment-cross-platform).
    CrossPlatform,
}
impl PartialEq<&Self> for AuthenticatorAttachment {
    #[inline]
    fn eq(&self, other: &&Self) -> bool {
        *self == **other
    }
}
impl PartialEq<AuthenticatorAttachment> for &AuthenticatorAttachment {
    #[inline]
    fn eq(&self, other: &AuthenticatorAttachment) -> bool {
        **self == *other
    }
}
/// The maximum number of bytes that can make up a Credential ID
/// [per WebAuthn](https://www.w3.org/TR/webauthn-3/#credential-id).
pub const CRED_ID_MAX_LEN: usize = 1023;
/// The minimum number of bytes that can make up a Credential ID
/// [per WebAuthn](https://www.w3.org/TR/webauthn-3/#credential-id).
///
/// The spec does not call out this value directly instead it states the following:
///
/// > Credential IDs are generated by authenticators in two forms:
/// >
/// > * At least 16 bytes that include at least 100 bits of entropy, or
/// > * The [public key credential source](https://www.w3.org/TR/webauthn-3/#public-key-credential-source),
/// >   without its Credential ID or mutable items, encrypted so only its managing
/// >   authenticator can decrypt it. This form allows the authenticator to be nearly
/// >   stateless, by having the Relying Party store any necessary state.
///
/// One of the immutable items of the public key credential source is the private key
/// which for any real-world signature algorithm will always be at least 16 bytes.
pub const CRED_ID_MIN_LEN: usize = 16;
/// A [Credential ID](https://www.w3.org/TR/webauthn-3/#credential-id) that is made up of
/// [`CRED_ID_MIN_LEN`]–[`CRED_ID_MAX_LEN`] bytes.
#[derive(Clone, Copy, Debug)]
pub struct CredentialId<T>(T);
impl<T> CredentialId<T> {
    /// Returns the contained data consuming `self`.
    #[inline]
    pub fn into_inner(self) -> T {
        self.0
    }
    /// Returns the contained data.
    #[inline]
    pub const fn inner(&self) -> &T {
        &self.0
    }
}
impl<'a> CredentialId<&'a [u8]> {
    /// Creates a `CredentialId` from a `slice`.
    fn from_slice<'b: 'a>(value: &'b [u8]) -> Result<Self, CredentialIdErr> {
        if (CRED_ID_MIN_LEN..=CRED_ID_MAX_LEN).contains(&value.len()) {
            Ok(Self(value))
        } else {
            Err(CredentialIdErr)
        }
    }
}
impl<T: AsRef<[u8]>> AsRef<[u8]> for CredentialId<T> {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}
impl<T: Borrow<[u8]>> Borrow<[u8]> for CredentialId<T> {
    #[inline]
    fn borrow(&self) -> &[u8] {
        self.0.borrow()
    }
}
impl<'a: 'b, 'b> From<&'a CredentialId<Box<[u8]>>> for CredentialId<&'b Box<[u8]>> {
    #[inline]
    fn from(value: &'a CredentialId<Box<[u8]>>) -> Self {
        Self(&value.0)
    }
}
impl<'a: 'b, 'b> From<CredentialId<&'a Box<[u8]>>> for CredentialId<&'b [u8]> {
    #[inline]
    fn from(value: CredentialId<&'a Box<[u8]>>) -> Self {
        Self(value.0)
    }
}
impl<'a: 'b, 'b> From<&'a CredentialId<Box<[u8]>>> for CredentialId<&'b [u8]> {
    #[inline]
    fn from(value: &'a CredentialId<Box<[u8]>>) -> Self {
        Self(&value.0)
    }
}
impl From<CredentialId<&[u8]>> for CredentialId<Box<[u8]>> {
    #[inline]
    fn from(value: CredentialId<&[u8]>) -> Self {
        Self(value.0.into())
    }
}
impl<T: PartialEq<T2>, T2: PartialEq<T>> PartialEq<CredentialId<T>> for CredentialId<T2> {
    #[inline]
    fn eq(&self, other: &CredentialId<T>) -> bool {
        self.0 == other.0
    }
}
impl<T: PartialEq<T2>, T2: PartialEq<T>> PartialEq<CredentialId<T>> for &CredentialId<T2> {
    #[inline]
    fn eq(&self, other: &CredentialId<T>) -> bool {
        **self == *other
    }
}
impl<T: PartialEq<T2>, T2: PartialEq<T>> PartialEq<&CredentialId<T>> for CredentialId<T2> {
    #[inline]
    fn eq(&self, other: &&CredentialId<T>) -> bool {
        *self == **other
    }
}
impl<T: Eq> Eq for CredentialId<T> {}
impl<T: Hash> Hash for CredentialId<T> {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}
impl<T: PartialOrd<T2>, T2: PartialOrd<T>> PartialOrd<CredentialId<T>> for CredentialId<T2> {
    #[inline]
    fn partial_cmp(&self, other: &CredentialId<T>) -> Option<Ordering> {
        self.0.partial_cmp(&other.0)
    }
}
impl<T: Ord> Ord for CredentialId<T> {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.0.cmp(&other.0)
    }
}
// We define a separate type to ensure challenges sent to the client are always randomly generated;
// otherwise one could deserialize arbitrary data into a `Challenge`.
/// Copy of [`Challenge`] sent back from the client.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SentChallenge(pub u128);
impl SentChallenge {
    /// Transforms `value` into a `SentChallenge` by interpreting `value` as a
    /// little-endian `u128`.
    #[expect(
        clippy::little_endian_bytes,
        reason = "Challenge and SentChallenge need to be compatible, and we need to ensure the data is sent and received in the same order"
    )]
    #[inline]
    #[must_use]
    pub const fn from_array(value: [u8; 16]) -> Self {
        Self(u128::from_le_bytes(value))
    }
    /// Transforms `value` into a `SentChallenge`.
    #[inline]
    #[must_use]
    pub const fn from_challenge(value: Challenge) -> Self {
        Self(value.into_data())
    }
}
impl From<Challenge> for SentChallenge {
    #[inline]
    fn from(value: Challenge) -> Self {
        Self::from_challenge(value)
    }
}
impl From<[u8; 16]> for SentChallenge {
    #[inline]
    fn from(value: [u8; 16]) -> Self {
        Self::from_array(value)
    }
}
impl PartialEq<&Self> for SentChallenge {
    #[inline]
    fn eq(&self, other: &&Self) -> bool {
        *self == **other
    }
}
impl PartialEq<SentChallenge> for &SentChallenge {
    #[inline]
    fn eq(&self, other: &SentChallenge) -> bool {
        **self == *other
    }
}
impl PartialOrd for SentChallenge {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for SentChallenge {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.0.cmp(&other.0)
    }
}
impl Hash for SentChallenge {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u128(self.0);
    }
}
/// An [`origin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-origin) or
/// [`topOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-toporigin).
#[derive(Debug, Eq)]
pub struct Origin<'a>(pub Cow<'a, str>);
impl PartialEq<Origin<'_>> for Origin<'_> {
    #[inline]
    fn eq(&self, other: &Origin<'_>) -> bool {
        self.0 == other.0
    }
}
impl PartialEq<&Origin<'_>> for Origin<'_> {
    #[inline]
    fn eq(&self, other: &&Origin<'_>) -> bool {
        *self == **other
    }
}
impl PartialEq<Origin<'_>> for &Origin<'_> {
    #[inline]
    fn eq(&self, other: &Origin<'_>) -> bool {
        **self == *other
    }
}
impl PartialEq<str> for Origin<'_> {
    #[inline]
    fn eq(&self, other: &str) -> bool {
        self.0.as_ref() == other
    }
}
impl PartialEq<Origin<'_>> for str {
    #[inline]
    fn eq(&self, other: &Origin<'_>) -> bool {
        *other == *self
    }
}
impl PartialEq<&str> for Origin<'_> {
    #[inline]
    fn eq(&self, other: &&str) -> bool {
        *self == **other
    }
}
impl PartialEq<Origin<'_>> for &str {
    #[inline]
    fn eq(&self, other: &Origin<'_>) -> bool {
        **self == *other
    }
}
impl PartialEq<String> for Origin<'_> {
    #[inline]
    fn eq(&self, other: &String) -> bool {
        self.0 == *other
    }
}
impl PartialEq<Origin<'_>> for String {
    #[inline]
    fn eq(&self, other: &Origin<'_>) -> bool {
        *other == *self
    }
}
impl PartialEq<Url> for Origin<'_> {
    #[inline]
    fn eq(&self, other: &Url) -> bool {
        self.0.as_ref() == other.as_ref()
    }
}
impl PartialEq<Origin<'_>> for Url {
    #[inline]
    fn eq(&self, other: &Origin<'_>) -> bool {
        *other == *self
    }
}
impl PartialEq<&Url> for Origin<'_> {
    #[inline]
    fn eq(&self, other: &&Url) -> bool {
        *self == **other
    }
}
impl PartialEq<Origin<'_>> for &Url {
    #[inline]
    fn eq(&self, other: &Origin<'_>) -> bool {
        **self == *other
    }
}
/// [Authenticator data flags](https://www.w3.org/TR/webauthn-3/#authdata-flags).
#[derive(Clone, Copy, Debug)]
pub struct Flag {
    /// [`UP` flag](https://www.w3.org/TR/webauthn-3/#authdata-flags-up).
    ///
    /// Note this is always `true` when part of [`auth::AuthenticatorData::flags`].
    pub user_present: bool,
    /// [`UV` flag](https://www.w3.org/TR/webauthn-3/#concept-user-verified).
    pub user_verified: bool,
    /// [`BE`](https://www.w3.org/TR/webauthn-3/#backup-eligibility) and
    /// [`BS`](https://www.w3.org/TR/webauthn-3/#backup-state) flags.
    pub backup: Backup,
}
/// [Authenticator data](https://www.w3.org/TR/webauthn-3/#authenticator-data).
pub(super) trait AuthData<'a>: Sized {
    /// Error returned by [`Self::user_is_not_present`].
    ///
    /// This should be [`Infallible`] in the event user must not always be present.
    type UpBitErr;
    /// [`attestedCredentialData`](https://www.w3.org/TR/webauthn-3/#authdata-attestedcredentialdata).
    type CredData;
    /// [`extensions`](https://www.w3.org/TR/webauthn-3/#authdata-extensions).
    type Ext: AuthExtOutput + Copy;
    /// Errors iff the user must always be present.
    fn user_is_not_present() -> Result<(), Self::UpBitErr>;
    /// `true` iff `AT` bit (i.e., bit 6) in [`Self::flag_data`] can and must be set to 1.
    fn contains_at_bit() -> bool;
    /// Constructor.
    fn new(
        rp_id_hash: &'a [u8],
        flags: Flag,
        sign_count: u32,
        attested_credential_data: Self::CredData,
        extensions: Self::Ext,
    ) -> Self;
    /// [`rpIdHash`](https://www.w3.org/TR/webauthn-3/#authdata-rpidhash).
    fn rp_hash(&self) -> &'a [u8];
    /// [`flags`](https://www.w3.org/TR/webauthn-3/#authdata-flags).
    fn flag(&self) -> Flag;
}
/// [`CollectedClientData`](https://www.w3.org/TR/webauthn-3/#dictdef-collectedclientdata).
#[derive(Debug)]
pub struct CollectedClientData<'a> {
    /// [`challenge`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-challenge).
    pub challenge: SentChallenge,
    /// [`origin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-origin).
    pub origin: Origin<'a>,
    /// [`crossOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-crossorigin).
    pub cross_origin: bool,
    /// [`topOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-toporigin).
    ///
    /// When `CollectedClientData` is constructed via [`Self::from_client_data_json`], this can only be
    /// `Some` if [`Self::cross_origin`]; and if `Some`, it will be different than [`Self::origin`].
    pub top_origin: Option<Origin<'a>>,
}
impl<'a> CollectedClientData<'a> {
    /// Parses `json` based on the
    /// [limited verification algorithm](https://www.w3.org/TR/webauthn-3/#clientdatajson-verification).
    ///
    /// Additionally, [`topOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-toporigin) is only
    /// allowed to exist if it has a different value than
    /// [`origin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-origin) and
    /// [`crossOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-crossorigin) is `true`.
    ///
    /// `REGISTRATION` iff [`type`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-type) must be
    /// `"webauthn.create"`; otherwise it must be `"webauthn.get"`.
    ///
    /// # Errors
    ///
    /// Errors iff `json` cannot be parsed based on the aforementioned requirements.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::{error::CollectedClientDataErr, CollectedClientData};
    /// assert!(!CollectedClientData::from_client_data_json::<true>(br#"{"type":"webauthn.create","challenge":"AAAAAAAAAAAAAAAAAAAAAA","origin":"https://example.com","crossOrigin":false}"#.as_slice())?.cross_origin);
    /// assert!(!CollectedClientData::from_client_data_json::<false>(br#"{"type":"webauthn.get","challenge":"AAAAAAAAAAAAAAAAAAAAAA","origin":"https://example.com","crossOrigin":false}"#.as_slice())?.cross_origin);
    /// # Ok::<_, CollectedClientDataErr>(())
    /// ```
    #[inline]
    pub fn from_client_data_json<'b: 'a, const REGISTRATION: bool>(
        json: &'b [u8],
    ) -> Result<Self, CollectedClientDataErr> {
        LimitedVerificationParser::<REGISTRATION>::parse(json)
    }
    /// Parses `json` in a "relaxed" way.
    ///
    /// Unlike [`Self::from_client_data_json`] which requires `json` to be an output from the
    /// [JSON-compatible serialization of client data](https://www.w3.org/TR/webauthn-3/#clientdatajson-serialization),
    /// this parses `json` based entirely on the
    /// [`CollectedClientData`](https://www.w3.org/TR/webauthn-3/#dictdef-collectedclientdata) Web IDL `dictionary`.
    ///
    /// L1 clients predate the JSON-compatible serialization of client data; additionally there are L2 and L3
    /// clients that don't adhere to the JSON-compatible serialization of client data despite being required to.
    /// These clients serialize `CollectedClientData` so that it's valid JSON and conforms to the Web IDL `dictionary`
    /// and nothing more. Furthermore, when not relying on the
    /// [limited verification algorithm](https://www.w3.org/TR/webauthn-3/#clientdatajson-verification), the spec
    /// requires the data to be decoded in a way equivalent to
    /// [UTF-8 decode](https://encoding.spec.whatwg.org/#utf-8-decode) which both interprets a leading zero
    /// width no-breaking space (i.e., U+FEFF) as a byte-order mark (BOM) as well as replaces any sequences of
    /// invalid UTF-8 code units with the replacement character (i.e., U+FFFD). That is precisely what this
    /// function does.
    ///
    /// # Errors
    ///
    /// Errors iff any of the following is true:
    /// * The payload is not valid JSON _after_ ignoring a leading U+FEFF and replacing any sequences of invalid
    ///   UTF-8 code units with U+FFFD.
    /// * The JSON does not conform to the Web IDL `dictionary`.
    /// * [`type`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-type) is not `"webauthn.create"`
    ///   or `"webauthn.get"` when `REGISTRATION` and `!REGISTRATION` respectively.
    /// * [`challenge`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-challenge) is not a
    ///   base64url-encoded [`Challenge`].
    /// * Existence of duplicate keys for the keys that are expected.
    ///
    /// # Examples
    ///
    /// ```
    /// # use structured_webauthn::response::{ser_relaxed::SerdeJsonErr, CollectedClientData};
    /// assert!(!CollectedClientData::from_client_data_json_relaxed::<true>(b"\xef\xbb\xbf{
    ///   \"type\": \"webauthn.create\",
    ///   \"origin\": \"https://example.com\",
    ///   \"f\xffo\": 123,
    ///   \"topOrigin\": \"https://example.com\",
    ///   \"challenge\": \"AAAAAAAAAAAAAAAAAAAAAA\"
    /// }")?.cross_origin);
    /// # Ok::<_, SerdeJsonErr>(())
    /// ```
    #[cfg(feature = "serde_relaxed")]
    #[inline]
    pub fn from_client_data_json_relaxed<'b: 'a, const REGISTRATION: bool>(
        json: &'b [u8],
    ) -> Result<Self, SerdeJsonErr> {
        ser_relaxed::RelaxedClientDataJsonParser::<REGISTRATION>::parse(json)
    }
}
/// Parser of
/// [`JSON-compatible serialization of client data`](https://www.w3.org/TR/webauthn-3/#collectedclientdata-json-compatible-serialization-of-client-data).
trait ClientDataJsonParser {
    /// Error returned by [`Self::parse`].
    type Err;
    /// Parses `json` into `CollectedClientData` based on the value of
    /// [`type`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-type).
    ///
    /// # Errors
    ///
    /// Errors iff `json` cannot be parsed into a `CollectedClientData`.
    fn parse(json: &[u8]) -> Result<CollectedClientData<'_>, Self::Err>;
    /// Extracts [`challenge`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-challenge)
    /// from `json`.
    ///
    /// Note `json` should be minimally parsed such that only `challenge` is extracted; thus
    /// `Ok` being returned does _not_ mean `json` is in fact valid.
    fn get_sent_challenge(json: &[u8]) -> Result<SentChallenge, Self::Err>;
}
/// [`ClientDataJsonParser`] based on the
/// [limited verification algorithm](https://www.w3.org/TR/webauthn-3/#clientdatajson-verification)
/// with the following additional requirements:
/// * Unknown keys are not allowed.
/// * The entire payload is parsed; thus the payload is guaranteed to be valid UTF-8 and JSON.
/// * [`CollectedClientData::top_origin`] can only be `Some` if
///   [`crossOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-crossorigin).
/// * If `CollectedClientData::top_origin` is `Some`, then it does not equal [`CollectedClientData::origin`].
///
/// `REGISTRATION` iff [`ClientDataJsonParser::parse`] requires
/// [`type`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-type) to be `"webauthn.create"`;
/// otherwise it must be `"webauthn.get"`.
struct LimitedVerificationParser<const REGISTRATION: bool>;
impl<const R: bool> LimitedVerificationParser<R> {
    /// Parses `val` as a JSON string with possibly trailing data. `val` MUST NOT begin with an opening quote. Upon
    /// encountering the first non-escaped quote, the parsed value is returned in addition to the remaining
    /// portion of `val` _after_ the closing quote. The limited verification algorithm is adhered to; thus the
    /// _only_ Unicode scalar values that are allowed (and must) be hex-escaped are U+0000 to U+001F inclusively.
    /// Similarly only `b'\\'` and `b'"'` are allowed (and must) be escaped with `b'\\'`.
    #[expect(unsafe_code, reason = "comment justifies its correctness")]
    #[expect(
        clippy::arithmetic_side_effects,
        clippy::indexing_slicing,
        reason = "comments justify their correctness"
    )]
    fn parse_string(val: &[u8]) -> Result<(Cow<'_, str>, &'_ [u8]), CollectedClientDataErr> {
        /// Tracks the state of the current Unicode scalar value that is being parsed.
        enum State {
            /// We are not parsing `'"'`, `'\\'`, or U+0000 to U+001F.
            Normal,
            /// We just encountered the escape character.
            Escape,
            /// We just encountered `b"\\u"`.
            UnicodeEscape,
            /// We just encountered `b"\\u0"`.
            UnicodeHex1,
            /// We just encountered `b"\\u00"`.
            UnicodeHex2,
            /// We just encountered `b"\\u000"` or `b"\\u001"`. The contained `u8` is `0` iff the former; otherwise
            /// `0x10`.
            UnicodeHex3(u8),
        }
        // We parse this as UTF-8 only at the end iff it is not empty. This contains all the potential Unicode scalar
        // values after de-escaping.
        let mut utf8 = Vec::new();
        // We check for all `u8`s already; thus we might as well check if we encounter a non-ASCII `u8`.
        // If we don't, then we can rely on `str::from_utf8_unchecked`.
        let mut all_ascii = true;
        // This tracks the start index of the next slice to add. We add slices iff we encounter the escape character or
        // we return the parsed `Cow` (i.e., encounter an unescaped `b'"'`).
        let mut cur_idx = 0;
        // The state of the yet-to-be-parsed Unicode scalar value.
        let mut state = State::Normal;
        for (counter, &b) in val.iter().enumerate() {
            match state {
                State::Normal => {
                    match b {
                        b'"' => {
                            if utf8.is_empty() {
                                if all_ascii {
                                    // `cur_idx` is 0 or 1. The latter is true iff `val` starts with a
                                    // `b'\\'` or `b'"'` but contains no other escaped characters.
                                    let s = &val[cur_idx..counter];
                                    // SAFETY:
                                    // `all_ascii` is `false` iff we encountered any `u8` that was not
                                    // an ASCII `u8`; thus we know `s` is valid ASCII which in turn means
                                    // it's valid UTF-8.
                                    let v = unsafe { str::from_utf8_unchecked(s) };
                                    // `val.len() > counter`, so indexing is fine and overflow cannot happen.
                                    return Ok((Cow::Borrowed(v), &val[counter + 1..]));
                                }
                                // `cur_idx` is 0 or 1. The latter is true iff `val` starts with a
                                // `b'\\'` or `b'"'` but contains no other escaped characters.
                                return str::from_utf8(&val[cur_idx..counter])
                                    .map_err(CollectedClientDataErr::Utf8)
                                    // `val.len() > counter`, so indexing is fine and overflow cannot happen.
                                    .map(|v| (Cow::Borrowed(v), &val[counter + 1..]));
                            }
                            // `val.len() > counter && counter >= cur_idx`, so indexing is fine and overflow
                            // cannot happen.
                            utf8.extend_from_slice(&val[cur_idx..counter]);
                            if all_ascii {
                                // SAFETY:
                                // `all_ascii` is `false` iff we encountered any `u8` that was not
                                // an ASCII `u8`; thus we know `utf8` is valid ASCII which in turn means
                                // it's valid UTF-8.
                                let v = unsafe { String::from_utf8_unchecked(utf8) };
                                // `val.len() > counter`, so indexing is fine and overflow cannot happen.
                                return Ok((Cow::Owned(v), &val[counter + 1..]));
                            }
                            return String::from_utf8(utf8)
                                .map_err(CollectedClientDataErr::Utf8Owned)
                                // `val.len() > counter`, so indexing is fine and overflow cannot happen.
                                .map(|v| (Cow::Owned(v), &val[counter + 1..]));
                        }
                        b'\\' => {
                            // Write the current slice of data.
                            utf8.extend_from_slice(&val[cur_idx..counter]);
                            state = State::Escape;
                        }
                        // ASCII is a subset of UTF-8 and this is a subset of ASCII. The code unit that is used for an
                        // ASCII Unicode scalar value _never_ appears in multi-code-unit Unicode scalar values; thus we
                        // error immediately.
                        ..=0x1f => return Err(CollectedClientDataErr::InvalidEscapedString),
                        128.. => all_ascii = false,
                        _ => (),
                    }
                }
                State::Escape => {
                    match b {
                        b'"' | b'\\' => {
                            // We start the next slice here since we need to add it.
                            cur_idx = counter;
                            state = State::Normal;
                        }
                        b'u' => {
                            state = State::UnicodeEscape;
                        }
                        _ => {
                            return Err(CollectedClientDataErr::InvalidEscapedString);
                        }
                    }
                }
                State::UnicodeEscape => {
                    if b != b'0' {
                        return Err(CollectedClientDataErr::InvalidEscapedString);
                    }
                    state = State::UnicodeHex1;
                }
                State::UnicodeHex1 => {
                    if b != b'0' {
                        return Err(CollectedClientDataErr::InvalidEscapedString);
                    }
                    state = State::UnicodeHex2;
                }
                State::UnicodeHex2 => {
                    state = State::UnicodeHex3(match b {
                        b'0' => 0,
                        b'1' => 0x10,
                        _ => return Err(CollectedClientDataErr::InvalidEscapedString),
                    });
                }
                State::UnicodeHex3(v) => {
                    match b {
                        // Only and all _lowercase_ hex is allowed.
                        b'0'..=b'9' | b'a'..=b'f' => {
                            // When `b < b'a'`, then `b >= b'0'`; and `b'a' > 87`; thus underflow cannot happen.
                            // Note `b'a' - 10 == 87`.
                            utf8.push(v | (b - if b < b'a' { b'0' } else { 87 }));
                            // `counter < val.len()`, so overflow cannot happen.
                            cur_idx = counter + 1;
                            state = State::Normal;
                        }
                        _ => return Err(CollectedClientDataErr::InvalidEscapedString),
                    }
                }
            }
        }
        // We never encountered an unescaped `b'"'`; thus we could not parse a string.
        Err(CollectedClientDataErr::InvalidObject)
    }
}
impl<const R: bool> ClientDataJsonParser for LimitedVerificationParser<R> {
    type Err = CollectedClientDataErr;
    #[expect(
        clippy::little_endian_bytes,
        reason = "Challenge::serialize and this need to be consistent across architectures"
    )]
    #[expect(clippy::too_many_lines, reason = "110 lines is fine")]
    fn parse(json: &[u8]) -> Result<CollectedClientData<'_>, Self::Err> {
        // `{"type":"webauthn.<create|get>","challenge":"<22 bytes>","origin":"<bytes>","crossOrigin":<true|false>[,"topOrigin":"<bytes>"][,<anything>]}`.
        /// First portion of `value`.
        const HEADER: &[u8; 18] = br#"{"type":"webauthn."#;
        /// `get`.
        const GET: &[u8; 3] = b"get";
        /// `create`.
        const CREATE: &[u8; 6] = b"create";
        /// Value after type before the start of the base64url-encoded challenge.
        const AFTER_TYPE: &[u8; 15] = br#"","challenge":""#;
        /// Value after challenge before the start of the origin value.
        const AFTER_CHALLENGE: &[u8; 12] = br#"","origin":""#;
        /// Value after origin before the start of the crossOrigin value.
        const AFTER_ORIGIN: &[u8; 15] = br#","crossOrigin":"#;
        /// `true`.
        const TRUE: &[u8; 4] = b"true";
        /// `false`.
        const FALSE: &[u8; 5] = b"false";
        /// Value after crossOrigin before the start of the topOrigin value.
        const AFTER_CROSS: &[u8; 13] = br#""topOrigin":""#;
        json.split_last().ok_or(CollectedClientDataErr::Len).and_then(|(last, last_rem)| {
            if *last == b'}' {
                last_rem.split_at_checked(HEADER.len()).ok_or(CollectedClientDataErr::Len).and_then(|(header, header_rem)| {
                    if header == HEADER {
                        if R {
                            header_rem.split_at_checked(CREATE.len()).ok_or(CollectedClientDataErr::Len).and_then(|(create, create_rem)| {
                                if create == CREATE {
                                    Ok(create_rem)
                                } else {
                                    Err(CollectedClientDataErr::Type)
                                }
                            })
                        } else {
                            header_rem.split_at_checked(GET.len()).ok_or(CollectedClientDataErr::Len).and_then(|(get, get_rem)| {
                                if get == GET {
                                    Ok(get_rem)
                                } else {
                                    Err(CollectedClientDataErr::Type)
                                }
                            })
                        }.and_then(|type_rem| {
                            type_rem.split_at_checked(AFTER_TYPE.len()).ok_or(CollectedClientDataErr::Len).and_then(|(chall_key, chall_key_rem)| {
                                if chall_key == AFTER_TYPE {
                                    chall_key_rem.split_at_checked(Challenge::BASE64_LEN).ok_or(CollectedClientDataErr::Len).and_then(|(base64_chall, base64_chall_rem)| {
                                        let mut chall = [0; 16];
                                        base64url_nopad::decode_buffer_exact(base64_chall, chall.as_mut_slice()).map_err(|_e| CollectedClientDataErr::Challenge).and_then(|()| {
                                            base64_chall_rem.split_at_checked(AFTER_CHALLENGE.len()).ok_or(CollectedClientDataErr::Len).and_then(|(origin_key, origin_key_rem)| {
                                                if origin_key == AFTER_CHALLENGE {
                                                    Self::parse_string(origin_key_rem).and_then(|(origin, origin_rem)| {
                                                        origin_rem.split_at_checked(AFTER_ORIGIN.len()).ok_or(CollectedClientDataErr::Len).and_then(|(cross_key, cross_key_rem)| {
                                                            if cross_key == AFTER_ORIGIN {
                                                                // `FALSE.len() > TRUE.len()`, so we check for `FALSE` in `and_then`.
                                                                cross_key_rem.split_at_checked(TRUE.len()).ok_or(CollectedClientDataErr::Len).and_then(|(cross_true, cross_true_rem)| {
                                                                    if cross_true == TRUE {
                                                                        Ok((true, cross_true_rem))
                                                                    } else {
                                                                        cross_key_rem.split_at_checked(FALSE.len()).ok_or(CollectedClientDataErr::Len).and_then(|(cross_false, cross_false_rem)| {
                                                                            if cross_false == FALSE {
                                                                                Ok((false, cross_false_rem))
                                                                            } else {
                                                                                Err(CollectedClientDataErr::CrossOrigin)
                                                                            }
                                                                        })
                                                                    }.and_then(|(cross, cross_rem)| {
                                                                        cross_rem.split_first().map_or(Ok((cross, None)), |(comma, comma_rem)| {
                                                                            if *comma == b',' {
                                                                                comma_rem.split_at_checked(AFTER_CROSS.len()).map_or(Ok((cross, None)), |(top, top_rem)| {
                                                                                    if top == AFTER_CROSS {
                                                                                        if cross {
                                                                                            Self::parse_string(top_rem).and_then(|(top_origin, top_origin_rem)| {
                                                                                                top_origin_rem.first().map_or(Ok(()), |v| {
                                                                                                    if *v == b',' {
                                                                                                        Ok(())
                                                                                                    } else {
                                                                                                        Err(CollectedClientDataErr::InvalidObject)
                                                                                                    }
                                                                                                }).and_then(|()| {
                                                                                                    if origin == top_origin {
                                                                                                        Err(CollectedClientDataErr::TopOriginSameAsOrigin)
                                                                                                    } else {
                                                                                                        Ok((true, Some(Origin(top_origin))))
                                                                                                    }
                                                                                                })
                                                                                            })
                                                                                        } else {
                                                                                            Err(CollectedClientDataErr::TopOriginWithoutCrossOrigin)
                                                                                        }
                                                                                    } else {
                                                                                        Ok((cross, None))
                                                                                    }
                                                                                })
                                                                            } else {
                                                                                Err(CollectedClientDataErr::InvalidObject)
                                                                            }
                                                                        }).map(|(cross_origin, top_origin)| CollectedClientData { challenge: SentChallenge(u128::from_le_bytes(chall)), origin: Origin(origin), cross_origin, top_origin, })
                                                                    })
                                                                })
                                                            } else {
                                                                Err(CollectedClientDataErr::CrossOriginKey)
                                                            }
                                                        })
                                                    })
                                                } else {
                                                    Err(CollectedClientDataErr::OriginKey)
                                                }
                                            })
                                        })
                                    })
                                } else {
                                    Err(CollectedClientDataErr::ChallengeKey)
                                }
                            })
                        })
                    } else {
                        Err(CollectedClientDataErr::InvalidStart)
                    }
                })
            } else {
                Err(CollectedClientDataErr::InvalidObject)
            }
        })
    }
    #[expect(
        clippy::arithmetic_side_effects,
        reason = "comment justifies correctness"
    )]
    #[expect(
        clippy::little_endian_bytes,
        reason = "Challenge::serialize and this need to be consistent across architectures"
    )]
    fn get_sent_challenge(json: &[u8]) -> Result<SentChallenge, Self::Err> {
        // Index 39.
        // `{"type":"webauthn.create","challenge":"AAAAAAAAAAAAAAAAAAAAAA"...`.
        // Index 36.
        // `{"type":"webauthn.get","challenge":"AAAAAAAAAAAAAAAAAAAAAA"...`.
        let idx = if R { 39 } else { 36 };
        // This maxes at 39 + 22 = 61; thus overflow is not an issue.
        json.get(idx..idx + Challenge::BASE64_LEN)
            .ok_or(CollectedClientDataErr::Len)
            .and_then(|chall_slice| {
                let mut chall = [0; 16];
                base64url_nopad::decode_buffer_exact(chall_slice, chall.as_mut_slice())
                    .map_err(|_e| CollectedClientDataErr::Challenge)
                    .map(|()| SentChallenge(u128::from_le_bytes(chall)))
            })
    }
}
/// Authenticator extension outputs;
pub(super) trait AuthExtOutput {
    /// MUST return `true` iff there is no data.
    fn missing(self) -> bool;
}
/// Successful return type from [`FromCbor::from_cbor`].
struct CborSuccess<'a, T> {
    /// Value parsed from the slice.
    value: T,
    /// Remaining unprocessed data.
    remaining: &'a [u8],
}
/// Types that parse
/// [CTAP2 canonical CBOR encoding form](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#ctap2-canonical-cbor-encoding-form)
/// data without necessarily consuming all the data.
///
/// The purpose of this `trait` is to allow chains of types to progressively consume `cbor` by passing
/// [`CborSuccess::remaining`] into the next `FromCbor` type.
trait FromCbor<'a>: Sized {
    /// Error when conversion fails.
    type Err;
    /// Parses `cbor` into `Self`.
    ///
    /// # Errors
    ///
    /// Errors if `cbor` cannot be parsed into `Self:`.
    fn from_cbor(cbor: &'a [u8]) -> Result<CborSuccess<'a, Self>, Self::Err>;
}
/// Error returned from [`A::from_cbor`] `where A: AuthData`.
enum AuthenticatorDataErr<UpErr, CredData, AuthExt> {
    /// The `slice` had an invalid length.
    Len,
    /// [UP](https://www.w3.org/TR/webauthn-3/#authdata-flags-at) bit was 0.
    UserNotPresent(UpErr),
    /// Bit 1 in [`flags`](https://www.w3.org/TR/webauthn-3/#authdata-flags) is not 0.
    FlagsBit1Not0,
    /// Bit 5 in [`flags`](https://www.w3.org/TR/webauthn-3/#authdata-flags) is not 0.
    FlagsBit5Not0,
    /// [AT](https://www.w3.org/TR/webauthn-3/#authdata-flags-at) bit was 0 during registration or was 1
    /// during authentication.
    AttestedCredentialData,
    /// [BE](https://www.w3.org/TR/webauthn-3/#authdata-flags-be) and
    /// [BS](https://www.w3.org/TR/webauthn-3/#authdata-flags-bs) bits were 0 and 1 respectively.
    BackupWithoutEligibility,
    /// Error returned from [`AttestedCredentialData::from_cbor`].
    AttestedCredential(CredData),
    /// Error returned from [`register::AuthenticatorExtensionOutput::from_cbor`] and
    /// [`auth::AuthenticatorExtensionOutput::from_cbor`].
    AuthenticatorExtension(AuthExt),
    /// [ED](https://www.w3.org/TR/webauthn-3/#authdata-flags-ed) bit was 0, but
    /// [`extensions`](https://www.w3.org/TR/webauthn-3/#authdata-extensions) existed.
    NoExtensionBitWithData,
    /// [ED](https://www.w3.org/TR/webauthn-3/#authdata-flags-ed) bit was 1, but
    /// [`extensions`](https://www.w3.org/TR/webauthn-3/#authdata-extensions) did not exist.
    ExtensionBitWithoutData,
    /// There was trailing data that could not be deserialized.
    TrailingData,
}
impl<U, C: Display, A: Display> Display for AuthenticatorDataErr<U, C, A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Len => f.write_str("authenticator data had an invalid length"),
            Self::UserNotPresent(_) => f.write_str("user was not present"),
            Self::FlagsBit1Not0 => f.write_str("flags 1-bit was 1"),
            Self::FlagsBit5Not0 => f.write_str("flags 5-bit was 1"),
            Self::AttestedCredentialData => f.write_str("attested credential data was included during authentication or was not included during registration"),
            Self::BackupWithoutEligibility => {
                f.write_str("backup state bit was 1 despite backup eligibility being 0")
            }
            Self::AttestedCredential(ref err) => err.fmt(f),
            Self::AuthenticatorExtension(ref err) => err.fmt(f),
            Self::NoExtensionBitWithData => {
                f.write_str("extension bit was 0 despite extensions existing")
            }
            Self::ExtensionBitWithoutData => {
                f.write_str("extension bit was 1 despite no extensions existing")
            }
            Self::TrailingData => {
                f.write_str("slice had trailing data that could not be deserialized")
            }
        }
    }
}
impl From<AuthenticatorDataErr<Infallible, AttestedCredentialDataErr, RegAuthExtErr>>
    for RegAuthDataErr
{
    #[inline]
    fn from(
        value: AuthenticatorDataErr<Infallible, AttestedCredentialDataErr, RegAuthExtErr>,
    ) -> Self {
        match value {
            AuthenticatorDataErr::Len => Self::Len,
            AuthenticatorDataErr::UserNotPresent(v) => match v {},
            AuthenticatorDataErr::FlagsBit1Not0 => Self::FlagsBit1Not0,
            AuthenticatorDataErr::FlagsBit5Not0 => Self::FlagsBit5Not0,
            AuthenticatorDataErr::AttestedCredentialData => Self::AttestedCredentialDataNotIncluded,
            AuthenticatorDataErr::BackupWithoutEligibility => Self::BackupWithoutEligibility,
            AuthenticatorDataErr::AttestedCredential(err) => Self::AttestedCredential(err),
            AuthenticatorDataErr::AuthenticatorExtension(err) => Self::AuthenticatorExtension(err),
            AuthenticatorDataErr::NoExtensionBitWithData => Self::NoExtensionBitWithData,
            AuthenticatorDataErr::ExtensionBitWithoutData => Self::ExtensionBitWithoutData,
            AuthenticatorDataErr::TrailingData => Self::TrailingData,
        }
    }
}
impl From<AuthenticatorDataErr<(), Infallible, AuthAuthExtErr>> for AuthAuthDataErr {
    #[inline]
    fn from(value: AuthenticatorDataErr<(), Infallible, AuthAuthExtErr>) -> Self {
        match value {
            AuthenticatorDataErr::Len => Self::Len,
            AuthenticatorDataErr::UserNotPresent(()) => Self::UserNotPresent,
            AuthenticatorDataErr::FlagsBit1Not0 => Self::FlagsBit1Not0,
            AuthenticatorDataErr::FlagsBit5Not0 => Self::FlagsBit5Not0,
            AuthenticatorDataErr::AttestedCredentialData => Self::AttestedCredentialDataIncluded,
            AuthenticatorDataErr::AttestedCredential(val) => match val {},
            AuthenticatorDataErr::BackupWithoutEligibility => Self::BackupWithoutEligibility,
            AuthenticatorDataErr::AuthenticatorExtension(err) => Self::AuthenticatorExtension(err),
            AuthenticatorDataErr::NoExtensionBitWithData => Self::NoExtensionBitWithData,
            AuthenticatorDataErr::ExtensionBitWithoutData => Self::ExtensionBitWithoutData,
            AuthenticatorDataErr::TrailingData => Self::TrailingData,
        }
    }
}
impl<'a, A> FromCbor<'a> for A
where
    A: AuthData<'a>,
    A::CredData: FromCbor<'a>,
    A::Ext: FromCbor<'a>,
{
    type Err = AuthenticatorDataErr<
        A::UpBitErr,
        <A::CredData as FromCbor<'a>>::Err,
        <A::Ext as FromCbor<'a>>::Err,
    >;
    #[expect(clippy::big_endian_bytes, reason = "CBOR integers are in big-endian")]
    fn from_cbor(cbor: &'a [u8]) -> Result<CborSuccess<'a, Self>, Self::Err> {
        /// Length of `signCount`.
        const SIGN_COUNT_LEN: usize = 4;
        /// `UP` bit (i.e., bit 0) set to 1.
        const UP: u8 = 0b0000_0001;
        /// `RFU1` bit (i.e., bit 1) set to 1.
        const RFU1: u8 = UP << 1;
        /// `UV` bit (i.e., bit 2) set to 1.
        const UV: u8 = RFU1 << 1;
        /// `BE` bit (i.e., bit 3) set to 1.
        const BE: u8 = UV << 1;
        /// `BS` bit (i.e., bit 4) set to 1.
        const BS: u8 = BE << 1;
        /// `RFU2` bit (i.e., bit 5) set to 1.
        const RFU2: u8 = BS << 1;
        /// `AT` bit (i.e., bit 6) set to 1.
        const AT: u8 = RFU2 << 1;
        /// `ED` bit (i.e., bit 7) set to 1.
        const ED: u8 = AT << 1;
        cbor.split_at_checked(Sha256::output_size()).ok_or_else(|| AuthenticatorDataErr::Len).and_then(|(rp_id_slice, rp_id_rem)| {
            rp_id_rem.split_first().ok_or_else(|| AuthenticatorDataErr::Len).and_then(|(&flag, flag_rem)| {
                let user_present = flag & UP == UP;
                if user_present {
                    Ok(())
                } else {
                    A::user_is_not_present().map_err(AuthenticatorDataErr::UserNotPresent)
                }
                .and_then(|()| {
                    if flag & RFU1 == 0 {
                        if flag & RFU2 == 0 {
                            let at_bit = A::contains_at_bit();
                            if flag & AT == AT {
                                if at_bit {
                                    Ok(())
                                } else {
                                    Err(AuthenticatorDataErr::AttestedCredentialData)
                                }
                            } else if at_bit {
                                Err(AuthenticatorDataErr::AttestedCredentialData)
                            } else {
                                Ok(())
                            }.and_then(|()| {
                                let bs = flag & BS == BS;
                                if flag & BE == BE {
                                    if bs {
                                        Ok(Backup::Exists)
                                    } else {
                                        Ok(Backup::Eligible)
                                    }
                                } else if bs {
                                    Err(AuthenticatorDataErr::BackupWithoutEligibility)
                                } else {
                                    Ok(Backup::NotEligible)
                                }
                                .and_then(|backup| {
                                    flag_rem.split_at_checked(SIGN_COUNT_LEN).ok_or_else(|| AuthenticatorDataErr::Len).and_then(|(count_slice, count_rem)| {
                                        A::CredData::from_cbor(count_rem).map_err(AuthenticatorDataErr::AttestedCredential).and_then(|att_data| {
                                            A::Ext::from_cbor(att_data.remaining).map_err(AuthenticatorDataErr::AuthenticatorExtension).and_then(|ext| {
                                                if ext.remaining.is_empty() {
                                                    let ed = flag & ED == ED;
                                                    if ext.value.missing() {
                                                        if ed {
                                                            Err(AuthenticatorDataErr::ExtensionBitWithoutData)
                                                        } else {
                                                            Ok(())
                                                        }
                                                    } else if ed {
                                                        Ok(())
                                                    } else {
                                                        Err(AuthenticatorDataErr::NoExtensionBitWithData)
                                                    }.map(|()| {
                                                        let mut sign_count = [0; SIGN_COUNT_LEN];
                                                        sign_count.copy_from_slice(count_slice);
                                                        // `signCount` is in big-endian.
                                                        CborSuccess { value: A::new(rp_id_slice, Flag { user_present, user_verified: flag & UV == UV, backup, }, u32::from_be_bytes(sign_count), att_data.value, ext.value), remaining: ext.remaining, }
                                                    })
                                                } else {
                                                    Err(AuthenticatorDataErr::TrailingData)
                                                }
                                            })
                                        })
                                    })
                                })
                            })
                        } else {
                            Err(AuthenticatorDataErr::FlagsBit5Not0)
                        }
                    } else {
                        Err(AuthenticatorDataErr::FlagsBit1Not0)
                    }
                })
            })
        })
    }
}
/// Data returned by [`AuthDataContainer::from_data`].
pub(super) struct ParsedAuthData<'a, A> {
    /// The data the CBOR is parsed into.
    data: A,
    /// The raw authenticator data and 32-bytes of trailing data.
    auth_data_and_32_trailing_bytes: &'a [u8],
}
/// Error returned by [`AuthResponse::parse_data_and_verify_sig`].
pub(super) enum AuthRespErr<AuthDataErr> {
    /// Variant returned when parsing
    /// [`clientDataJSON`](https://www.w3.org/TR/webauthn-3/#dom-authenticatorresponse-clientdatajson)
    /// into [`CollectedClientData`] fails.
    CollectedClientData(CollectedClientDataErr),
    /// Variant returned when parsing
    /// [`clientDataJSON`](https://www.w3.org/TR/webauthn-3/#dom-authenticatorresponse-clientdatajson)
    /// in a "relaxed" way into [`CollectedClientData`] fails.
    #[cfg(feature = "serde_relaxed")]
    CollectedClientDataRelaxed(SerdeJsonErr),
    /// Variant returned when parsing [`AuthResponse::Auth`] fails.
    Auth(AuthDataErr),
    /// Variant when the [`CompressedPubKey`] or [`UncompressePubKey`] is not valid.
    PubKey(PubKeyErr),
    /// Variant returned when the signature, if one exists, associated with
    /// [`Self::AuthResponse`] is invalid.
    Signature,
}
impl<A: Display> Display for AuthRespErr<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            Self::CollectedClientData(ref err) => write!(f, "CollectedClientData could not be parsed: {err}"),
            #[cfg(feature = "serde_relaxed")]
            Self::CollectedClientDataRelaxed(ref err) => write!(f, "CollectedClientData could not be parsed: {err}"),
            Self::Auth(ref err) => write!(f, "auth data could not be parsed: {err}"),
            Self::PubKey(err) => err.fmt(f),
            Self::Signature => f.write_str("the signature over the authenticator data and CollectedClientData could not be verified"),
        }
    }
}
impl From<AuthRespErr<AttestationObjectErr>> for RegCeremonyErr {
    #[inline]
    fn from(value: AuthRespErr<AttestationObjectErr>) -> Self {
        match value {
            AuthRespErr::CollectedClientData(err) => Self::CollectedClientData(err),
            #[cfg(feature = "serde_relaxed")]
            AuthRespErr::CollectedClientDataRelaxed(err) => Self::CollectedClientDataRelaxed(err),
            AuthRespErr::Auth(err) => Self::AttestationObject(err),
            AuthRespErr::PubKey(err) => Self::PubKey(err),
            AuthRespErr::Signature => Self::AttestationSignature,
        }
    }
}
impl From<AuthRespErr<AuthAuthDataErr>> for AuthCeremonyErr {
    #[inline]
    fn from(value: AuthRespErr<AuthAuthDataErr>) -> Self {
        match value {
            AuthRespErr::CollectedClientData(err) => Self::CollectedClientData(err),
            #[cfg(feature = "serde_relaxed")]
            AuthRespErr::CollectedClientDataRelaxed(err) => Self::CollectedClientDataRelaxed(err),
            AuthRespErr::Auth(err) => Self::AuthenticatorData(err),
            AuthRespErr::PubKey(err) => Self::PubKey(err),
            AuthRespErr::Signature => Self::AssertionSignature,
        }
    }
}
/// [Authenticator data](https://www.w3.org/TR/webauthn-3/#authenticator-data)
/// container.
///
/// Note [`Self::Auth`] may be `Self`.
pub(super) trait AuthDataContainer<'a>: Sized {
    /// [Authenticator data](https://www.w3.org/TR/webauthn-3/#authenticator-data).
    type Auth: AuthData<'a>;
    /// Error returned from [`Self::from_data`].
    type Err;
    /// Converts `data` into [`ParsedAuthData`].
    ///
    /// # Errors
    ///
    /// Errors iff `data` cannot be converted into `ParsedAuthData`.
    fn from_data(data: &'a [u8]) -> Result<ParsedAuthData<'a, Self>, Self::Err>;
    /// Returns the contained
    /// [authenticator data](https://www.w3.org/TR/webauthn-3/#authenticator-data).
    fn authenticator_data(&self) -> &Self::Auth;
}
/// [`AuthenticatorResponse`](https://www.w3.org/TR/webauthn-3/#authenticatorresponse).
pub(super) trait AuthResponse {
    /// [Attestation object](https://www.w3.org/TR/webauthn-3/#attestation-object) or
    /// [authenticator data](https://www.w3.org/TR/webauthn-3/#authenticator-data).
    type Auth<'a>: AuthDataContainer<'a>
    where
        Self: 'a;
    /// Public key to use to verify the contained signature.
    type CredKey<'a>;
    /// Parses
    /// [`clientDataJSON`](https://www.w3.org/TR/webauthn-3/#dom-authenticatorresponse-clientdatajson)
    /// based on `RELAXED` and [`Self::Auth`] via [`AuthDataContainer::from_data`] in addition to
    /// verifying any possible signature over the concatenation of the raw
    /// [`AuthDataContainer::Auth`] and `clientDataJSON` using `key` or the contained
    /// public key if one exists. If `Self` contains a public key and should not be passed one, then it should set
    /// [`Self::CredKey`] to `()`.
    ///
    /// # Errors
    ///
    /// Errors iff parsing `clientDataJSON` errors, [`AuthDataContainer::from_data`] does, or the signature
    /// is invalid.
    ///
    /// # Panics
    ///
    /// `panic`s iff `relaxed` and `serde_relaxed` is not enabled.
    #[expect(
        clippy::type_complexity,
        reason = "type aliases with bounds are even more problematic at least until lazy_type_alias is stable"
    )]
    fn parse_data_and_verify_sig(
        &self,
        key: Self::CredKey<'_>,
        relaxed: bool,
    ) -> Result<
        (CollectedClientData<'_>, Self::Auth<'_>),
        AuthRespErr<<Self::Auth<'_> as AuthDataContainer<'_>>::Err>,
    >;
}
/// Ceremony response (i.e., [`PublicKeyCredential`](https://www.w3.org/TR/webauthn-3/#publickeycredential)).
pub(super) trait Response {
    /// [`AuthenticatorResponse`](https://www.w3.org/TR/webauthn-3/#authenticatorresponse).
    type Auth: AuthResponse;
    /// [`response`](https://www.w3.org/TR/webauthn-3/#dom-publickeycredential-response).
    fn auth(&self) -> &Self::Auth;
}
/// Error returned from [`Ceremony::partial_validate`].
pub(super) enum CeremonyErr<AuthDataErr> {
    /// Timeout occurred.
    Timeout,
    /// Read [`AuthRespErr`] for information.
    AuthResp(AuthRespErr<AuthDataErr>),
    /// Origin did not validate.
    OriginMismatch,
    /// Cross origin was `true` but was not allowed to be.
    CrossOrigin,
    /// Top origin did not validate.
    TopOriginMismatch,
    /// Challenges don't match.
    ChallengeMismatch,
    /// `rpIdHash` does not match the SHA-256 hash of the [`RpId`].
    RpIdHashMismatch,
    /// User was not verified despite being required to.
    UserNotVerified,
    /// [`Backup::NotEligible`] was not sent back despite [`BackupReq::NotEligible`].
    BackupEligible,
    /// [`Backup::NotEligible`] was sent back despite [`BackupReq::Eligible`].
    BackupNotEligible,
    /// [`Backup::Eligible`] was not sent back despite [`BackupReq::EligibleNoteExists`].
    BackupExists,
    /// [`Backup::Exists`] was not sent back despite [`BackupReq::Exists`].
    BackupDoesNotExist,
}
impl<A: Display> Display for CeremonyErr<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Timeout => f.write_str("ceremony timed out"),
            Self::AuthResp(ref err) => err.fmt(f),
            Self::OriginMismatch => {
                f.write_str("the origin sent from the client is not an allowed origin")
            }
            Self::CrossOrigin => {
                f.write_str("cross origin was from the client, but it is not allowed")
            }
            Self::TopOriginMismatch => {
                f.write_str("the top origin sent from the client is not an allowed top origin")
            }
            Self::ChallengeMismatch => f.write_str(
                "the challenge sent to the client does not match the challenge sent back",
            ),
            Self::RpIdHashMismatch => f.write_str(
                "the SHA-256 hash of the RP ID doesn't match the hash sent from the client",
            ),
            Self::UserNotVerified => f.write_str("user was not verified despite being required to"),
            Self::BackupEligible => f.write_str(
                "credential is eligible to be backed up despite requiring that it not be",
            ),
            Self::BackupNotEligible => f.write_str(
                "credential is not eligible to be backed up despite requiring that it be",
            ),
            Self::BackupExists => {
                f.write_str("credential backup exists despite requiring that a backup not exist")
            }
            Self::BackupDoesNotExist => f.write_str(
                "credential backup does not exist despite requiring that a backup exist",
            ),
        }
    }
}
impl From<CeremonyErr<AttestationObjectErr>> for RegCeremonyErr {
    #[inline]
    fn from(value: CeremonyErr<AttestationObjectErr>) -> Self {
        match value {
            CeremonyErr::Timeout => Self::Timeout,
            CeremonyErr::AuthResp(err) => err.into(),
            CeremonyErr::OriginMismatch => Self::OriginMismatch,
            CeremonyErr::CrossOrigin => Self::CrossOrigin,
            CeremonyErr::TopOriginMismatch => Self::TopOriginMismatch,
            CeremonyErr::ChallengeMismatch => Self::ChallengeMismatch,
            CeremonyErr::RpIdHashMismatch => Self::RpIdHashMismatch,
            CeremonyErr::UserNotVerified => Self::UserNotVerified,
            CeremonyErr::BackupEligible => Self::BackupEligible,
            CeremonyErr::BackupNotEligible => Self::BackupNotEligible,
            CeremonyErr::BackupExists => Self::BackupExists,
            CeremonyErr::BackupDoesNotExist => Self::BackupDoesNotExist,
        }
    }
}
impl From<CeremonyErr<AuthAuthDataErr>> for AuthCeremonyErr {
    #[inline]
    fn from(value: CeremonyErr<AuthAuthDataErr>) -> Self {
        match value {
            CeremonyErr::Timeout => Self::Timeout,
            CeremonyErr::AuthResp(err) => err.into(),
            CeremonyErr::OriginMismatch => Self::OriginMismatch,
            CeremonyErr::CrossOrigin => Self::CrossOrigin,
            CeremonyErr::TopOriginMismatch => Self::TopOriginMismatch,
            CeremonyErr::ChallengeMismatch => Self::ChallengeMismatch,
            CeremonyErr::RpIdHashMismatch => Self::RpIdHashMismatch,
            CeremonyErr::UserNotVerified => Self::UserNotVerified,
            CeremonyErr::BackupEligible => Self::BackupEligible,
            CeremonyErr::BackupNotEligible => Self::BackupNotEligible,
            CeremonyErr::BackupExists => Self::BackupExists,
            CeremonyErr::BackupDoesNotExist => Self::BackupDoesNotExist,
        }
    }
}
/// [`AllAcceptedCredentialsOptions`](https://www.w3.org/TR/webauthn-3/#dictdef-allacceptedcredentialsoptions).
///
/// This can be sent to _an already authenticated user_ to inform what credentials are currently registered.
/// This can be useful when a user deletes credentials on the RP's side but does not do so on the authenticator.
/// When the client forwards this response to the authenticator, it can remove all credentials that don't have
/// a [`CredentialId`] in [`Self::all_accepted_credential_ids`].
#[derive(Debug)]
pub struct AllAcceptedCredentialsOptions<'rp, 'user, const USER_LEN: usize> {
    /// [`rpId`](https://www.w3.org/TR/webauthn-3/#dictdef-allacceptedcredentialsoptions-rpid).
    pub rp_id: &'rp RpId,
    /// [`userId`](https://www.w3.org/TR/webauthn-3/#dictdef-allacceptedcredentialsoptions-userid).
    pub user_id: &'user UserHandle<USER_LEN>,
    /// [`allAcceptedCredentialIds`](https://www.w3.org/TR/webauthn-3/#dictdef-allacceptedcredentialsoptions-allacceptedcredentialids).
    pub all_accepted_credential_ids: Vec<CredentialId<Box<[u8]>>>,
}
/// [`CurrentUserDetailsOptions`](https://www.w3.org/TR/webauthn-3/#dictdef-currentuserdetailsoptions).
///
/// This can be sent to _an already authenticated user_ to inform the user information.
/// This can be useful when a user updates their user information on the RP's side but does not do so on the authenticator.
/// When the client forwards this response to the authenticator, it can update the user info for the associated credential.
#[derive(Debug)]
pub struct CurrentUserDetailsOptions<'rp_id, 'name, 'display_name, 'id, const LEN: usize> {
    /// [`rpId`](https://www.w3.org/TR/webauthn-3/#dictdef-currentuserdetailsoptions-rpid).
    pub rp_id: &'rp_id RpId,
    /// [`userId`](https://www.w3.org/TR/webauthn-3/#dictdef-currentuserdetailsoptions-userid),
    /// [`name`](https://www.w3.org/TR/webauthn-3/#dictdef-currentuserdetailsoptions-name), and
    /// [`displayName`](https://www.w3.org/TR/webauthn-3/#dictdef-currentuserdetailsoptions-displayname).
    pub user: PublicKeyCredentialUserEntity<'name, 'display_name, 'id, LEN>,
}
/// [`hmac-secret`](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#sctn-hmac-secret-extension)
/// during authentication and
/// [`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)
/// during registration.
///
/// `REG` iff `hmac-secret-mc`.
enum HmacSecretGet<const REG: bool> {
    /// No `hmac-secret` response.
    None,
    /// One encrypted `hmac-secret`.
    One,
    /// Two encrypted `hmac-secret`s.
    Two,
}
/// Error returned by [`HmacSecretGet::from_cbor`]
enum HmacSecretGetErr {
    /// Error related to the length of the CBOR input.
    Len,
    /// Error related to the type of the CBOR key.
    Type,
    /// Error related to the value of the CBOR value.
    Value,
}
impl<const REG: bool> FromCbor<'_> for HmacSecretGet<REG> {
    type Err = HmacSecretGetErr;
    fn from_cbor(cbor: &[u8]) -> Result<CborSuccess<'_, Self>, Self::Err> {
        /// AES block size.
        const AES_BLOCK_SIZE: usize = 16;
        /// HMAC-SHA-256 output length.
        const HMAC_SHA_256_LEN: usize = 32;
        /// Length of two HMAC-SHA-256 outputs concatenated together.
        const TWO_HMAC_SHA_256_LEN: usize = HMAC_SHA_256_LEN << 1;
        // We need the smallest multiple of `AES_BLOCK_SIZE` that
        // is strictly greater than `HMAC_SHA_256_LEN`.
        /// AES-256 output length on a 32-byte input.
        #[expect(
            clippy::integer_division_remainder_used,
            reason = "doesn't need to be constant time"
        )]
        const ONE_SECRET_LEN: usize =
            HMAC_SHA_256_LEN + (AES_BLOCK_SIZE - (HMAC_SHA_256_LEN % AES_BLOCK_SIZE));
        // We need the smallest multiple of `AES_BLOCK_SIZE` that
        // is strictly greater than `TWO_HMAC_SHA_256_LEN`.
        /// AES-256 output length on a 64-byte input.
        #[expect(
            clippy::integer_division_remainder_used,
            reason = "doesn't need to be constant time"
        )]
        const TWO_SECRET_LEN: usize =
            TWO_HMAC_SHA_256_LEN + (AES_BLOCK_SIZE - (TWO_HMAC_SHA_256_LEN % AES_BLOCK_SIZE));
        /// `hmac-secret-mc`.
        ///
        /// This is the key iff `REG`.
        const KEY: [u8; 15] = [
            cbor::TEXT_14,
            b'h',
            b'm',
            b'a',
            b'c',
            b'-',
            b's',
            b'e',
            b'c',
            b'r',
            b'e',
            b't',
            b'-',
            b'm',
            b'c',
        ];
        /// Helper that unifies `HmacSecretGet`.
        enum CborVal<'a> {
            /// Extension does not exist with remaining payload
            Success,
            /// Extension exists with remaining payload.
            Continue(&'a [u8]),
        }
        if REG {
            cbor.split_at_checked(KEY.len())
                .map_or(Ok(CborVal::Success), |(key, key_rem)| {
                    if key == KEY {
                        Ok(CborVal::Continue(key_rem))
                    } else {
                        Ok(CborVal::Success)
                    }
                })
        } else {
            cbor.split_at_checked(cbor::HMAC_SECRET.len()).map_or(
                Ok(CborVal::Success),
                |(key, key_rem)| {
                    if key == cbor::HMAC_SECRET {
                        Ok(CborVal::Continue(key_rem))
                    } else {
                        Ok(CborVal::Success)
                    }
                },
            )
        }
        .and_then(|cbor_val| match cbor_val {
            CborVal::Success => Ok(CborSuccess {
                value: Self::None,
                remaining: cbor,
            }),
            CborVal::Continue(key_rem) => key_rem
                .split_first()
                .ok_or(HmacSecretGetErr::Len)
                .and_then(|(bytes, bytes_rem)| {
                    if *bytes == cbor::BYTES_INFO_24 {
                        bytes_rem
                            .split_first()
                            .ok_or(HmacSecretGetErr::Len)
                            .and_then(|(&len, len_rem)| {
                                len_rem
                                    .split_at_checked(usize::from(len))
                                    .ok_or(HmacSecretGetErr::Len)
                                    .and_then(|(_, remaining)| match usize::from(len) {
                                        ONE_SECRET_LEN => Ok(CborSuccess {
                                            value: Self::One,
                                            remaining,
                                        }),
                                        TWO_SECRET_LEN => Ok(CborSuccess {
                                            value: Self::Two,
                                            remaining,
                                        }),
                                        _ => Err(HmacSecretGetErr::Value),
                                    })
                            })
                    } else {
                        Err(HmacSecretGetErr::Type)
                    }
                }),
        })
    }
}