structured_webauthn/response/error.rs
1extern crate alloc;
2use super::{CRED_ID_MAX_LEN, CRED_ID_MIN_LEN};
3#[cfg(doc)]
4use super::{Challenge, CollectedClientData, CredentialId};
5use alloc::string::FromUtf8Error;
6use core::{
7 error::Error,
8 fmt::{self, Display, Formatter},
9 str::Utf8Error,
10};
11/// Error returned when a [`CredentialId`] does not have length inclusively between [`CRED_ID_MIN_LEN`] and
12/// [`CRED_ID_MAX_LEN`].
13#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14pub struct CredentialIdErr;
15impl Display for CredentialIdErr {
16 #[inline]
17 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
18 write!(
19 f,
20 "CredentialId did not have length inclusively between {CRED_ID_MIN_LEN} and {CRED_ID_MAX_LEN}",
21 )
22 }
23}
24impl Error for CredentialIdErr {}
25/// Error returned from [`CollectedClientData::from_client_data_json`].
26#[derive(Debug, Eq, PartialEq)]
27pub enum CollectedClientDataErr {
28 /// The `slice` had invalid length.
29 Len,
30 /// The `slice` did not begin with `{"type":"webauthn.`.
31 InvalidStart,
32 /// [`type`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-type)
33 /// was not `"webauthn.create"` during registration or `"webauthn.get"`
34 /// during authentication.
35 Type,
36 /// [`challenge`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-challenge)
37 /// without whitespace was not the second key in the object.
38 ChallengeKey,
39 /// [`challenge`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-challenge)
40 /// was not a valid base64url-encoding of [`Challenge`].
41 Challenge,
42 /// [`origin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-origin)
43 /// without whitespace was not the third key in the object.
44 OriginKey,
45 /// [`crossOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-crossorigin)
46 /// without whitespace was not the fourth key in the object.
47 CrossOriginKey,
48 /// [`crossOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-crossorigin)
49 /// was not `true` or `false`.
50 CrossOrigin,
51 /// The object was not a valid JSON object.
52 InvalidObject,
53 /// [`origin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-origin) or
54 /// [`topOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-toporigin) was
55 /// not escaped correctly (i.e., a Unicode scalar value in U+0000–U+001F was not escaped or another Unicode
56 /// scalar value, sans `"` and `\`, was escaped).
57 InvalidEscapedString,
58 /// [`origin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-origin) or
59 /// [`topOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-toporigin) was
60 /// not valid UTF-8.
61 Utf8(Utf8Error),
62 /// [`origin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-origin) or
63 /// [`topOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-toporigin) was
64 /// not valid UTF-8.
65 Utf8Owned(FromUtf8Error),
66 /// [`topOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-toporigin) existed
67 /// despite [`crossOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-crossorigin)
68 /// being `false`.
69 TopOriginWithoutCrossOrigin,
70 /// [`topOrigin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-toporigin) was the same value
71 /// as [`origin`](https://www.w3.org/TR/webauthn-3/#dom-collectedclientdata-origin).
72 TopOriginSameAsOrigin,
73}
74impl Display for CollectedClientDataErr {
75 #[inline]
76 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
77 match *self {
78 Self::Len => f.write_str("clientDataJSON had an invalid length"),
79 Self::InvalidStart => f.write_str(r#"clientDataJSON does not start with '{"type":"webauthn.'"#),
80 Self::Type => f.write_str(r#"clientDataJSON 'type' did not have value '"webauthn.create"' during registration or '"webauthn.get"' during authentication"#),
81 Self::ChallengeKey => f.write_str("clientDataJSON 'challenge' was not the second key in the object, or it had whitespace around it"),
82 Self::Challenge => f.write_str("clientDataJSON 'challenge' was not a valid base64url encoding of 16 bytes"),
83 Self::OriginKey => f.write_str("clientDataJSON 'origin' was not the third key in the object, or it had whitespace around it"),
84 Self::CrossOriginKey => f.write_str("clientDataJSON 'crossOrigin' was not the fourth key in the object, or it had whitespace around it"),
85 Self::CrossOrigin => f.write_str("clientDataJSON 'crossOrigin' was not false or true"),
86 Self::InvalidObject => f.write_str("clientDataJSON was an invalid object"),
87 Self::InvalidEscapedString => f.write_str("clientDataJSON 'origin' or 'topOrigin' was not escaped correctly"),
88 Self::Utf8(err) => write!(f, "clientDataJSON 'origin' or 'topOrigin' was not valid UTF-8: {err}"),
89 Self::Utf8Owned(ref err) => write!(f, "clientDataJSON 'origin' or 'topOrigin' was not valid UTF-8: {err}"),
90 Self::TopOriginWithoutCrossOrigin => f.write_str("clientDataJSON 'topOrigin' existed despite 'crossOrigin' being false"),
91 Self::TopOriginSameAsOrigin => f.write_str("clientDataJSON 'origin' and 'topOrigin' were the same"),
92 }
93 }
94}
95impl Error for CollectedClientDataErr {}