passkey_types/webauthn/assertion.rs
1//! Types used for public key authentication
2
3use serde::{Deserialize, Serialize};
4use typeshare::typeshare;
5
6use crate::{
7 utils::serde::{ignore_unknown, ignore_unknown_opt_vec, maybe_stringified},
8 webauthn::{
9 AttestationConveyancePreference, AttestationStatementFormatIdentifiers,
10 AuthenticationExtensionsClientInputs, PublicKeyCredential, PublicKeyCredentialDescriptor,
11 PublicKeyCredentialHints, UserVerificationRequirement,
12 },
13 Bytes,
14};
15
16#[cfg(doc)]
17use crate::{
18 ctap2::{AttestedCredentialData, AuthenticatorData},
19 webauthn::{
20 AuthenticatorAttestationResponse, CollectedClientData, PublicKeyCredentialUserEntity,
21 },
22};
23
24/// The response to the successful authentication of a [`PublicKeyCredential`]
25#[typeshare(swift = "Equatable")]
26pub type AuthenticatedPublicKeyCredential = PublicKeyCredential<AuthenticatorAssertionResponse>;
27
28/// This type supplies `get()` requests with the data it needs to generate an assertion.
29/// Its `challenge` member MUST be present, while its other members are OPTIONAL.
30///
31/// <https://w3c.github.io/webauthn/#dictdef-publickeycredentialrequestoptions>
32#[derive(Debug, Serialize, Deserialize)]
33#[serde(rename_all = "camelCase")]
34#[typeshare]
35pub struct PublicKeyCredentialRequestOptions {
36 /// This member specifies a challenge that the authenticator signs, along with other data, when
37 /// producing an authentication assertion. See the [Cryptographic Challenges] security consideration.
38 ///
39 /// [Cryptographic Challenges]: https://w3c.github.io/webauthn/#sctn-cryptographic-challenges
40 pub challenge: Bytes,
41
42 /// This OPTIONAL member specifies a time, in milliseconds, that the Relying Party is willing to
43 /// wait for the call to complete. The value is treated as a hint, and MAY be overridden by the
44 /// client.
45 #[serde(
46 default,
47 skip_serializing_if = "Option::is_none",
48 deserialize_with = "maybe_stringified"
49 )]
50 pub timeout: Option<u32>,
51
52 /// This OPTIONAL member specifies the [RP ID] claimed by the [Relying Party]. The client MUST
53 /// verify that the Relying Party's origin matches the scope of this RP ID. The authenticator
54 /// MUST verify that this RP ID exactly equals the rpId of the credential to be used for the
55 /// authentication ceremony.
56 ///
57 /// If omitted, its value will be the requesting origin's [effective domain].
58 ///
59 /// [RP ID]: https://w3c.github.io/webauthn/#rp-id
60 /// [Relying Party]: https://w3c.github.io/webauthn/#relying-party
61 /// [effective domain]: https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-effective-domain
62 #[serde(default, skip_serializing_if = "Option::is_none")]
63 pub rp_id: Option<String>,
64
65 /// This OPTIONAL member is used by the client to find authenticators eligible for this
66 /// authentication ceremony. It can be used in two ways:
67 ///
68 /// * If the user account to authenticate is already identified (e.g. if the user has entered a
69 /// username), then the Relying Party SHOULD use this member to list credential descriptors for
70 /// credential records in the user account. This SHOULD usually include all credential records
71 /// in the user account.
72 ///
73 /// The items SHOULD specify [`PublicKeyCredentialDescriptor::transports`] whenever possible.
74 /// This helps the client optimize the user experience for any given situation. Also note that
75 /// the Relying Party does not need to filter the list when requesting user verification — the
76 /// client will automatically ignore non-eligible credentials if [`Self::user_verification`]
77 /// is set to required.
78 ///
79 /// See also the [Privacy leak via credential IDs][privacy] privacy consideration.
80 ///
81 /// * If the user account to authenticate is not already identified, then the Relying Party MAY
82 /// leave this member empty or unspecified. In this case, only discoverable credentials will be
83 /// utilized in this authentication ceremony, and the user account MAY be identified by the
84 /// of the resulting [`AuthenticatorAssertionResponse::user_handle`]. If the available
85 /// authenticators contain more than one discoverable credential scoped to the Relying Party,
86 /// the credentials are displayed by the client platform or authenticator for the user to select
87 /// from.
88 ///
89 /// If not empty, the client MUST return an error if none of the listed credentials can be used.
90 ///
91 /// The list is ordered in descending order of preference: the first item in the list is the
92 /// most preferred credential, and the last is the least preferred.
93 ///
94 /// [privacy]: https://w3c.github.io/webauthn/#sctn-credential-id-privacy-leak
95 #[serde(
96 default,
97 skip_serializing_if = "Option::is_none",
98 deserialize_with = "ignore_unknown_opt_vec",
99 // On older versions of google play services, hybrid requests were not being transcribed
100 // correctly from the CTAP format to the webauthn format as is required by the credential
101 // manager API. This alias is present to mitigate the issue on devices that may not have
102 // received the update. It will be removed at a later date so do not rely on it.
103 alias = "allowList"
104 )]
105 pub allow_credentials: Option<Vec<PublicKeyCredentialDescriptor>>,
106
107 /// This OPTIONAL member specifies the Relying Party's requirements regarding user verification
108 /// for the `get()` operation. The value SHOULD be a member of [`UserVerificationRequirement`]
109 /// but client platforms MUST ignore unknown values, treating an unknown value as if the member
110 /// does not exist and using its default value. Eligible authenticators are filtered to only
111 /// those capable of satisfying this requirement.
112 ///
113 /// See [`UserVerificationRequirement`] for the description of this field's values and semantics.
114 #[serde(default, deserialize_with = "ignore_unknown")]
115 pub user_verification: UserVerificationRequirement,
116
117 /// This OPTIONAL member contains zero or more elements from [`PublicKeyCredentialHints`]` to
118 /// guide the user agent in interacting with the user.
119 ///
120 /// This field ignores unknown hint values at deserialization.
121 #[serde(
122 default,
123 skip_serializing_if = "Option::is_none",
124 deserialize_with = "ignore_unknown_opt_vec"
125 )]
126 pub hints: Option<Vec<PublicKeyCredentialHints>>,
127
128 /// The Relying Party MAY use this OPTIONAL member to specify a preference regarding attestation
129 /// conveyance. Its value SHOULD be a member of [`AttestationConveyancePreference`]. Client platforms
130 /// MUST ignore unknown values, treating an unknown value as if the member does not exist,
131 /// therefore acting as the default value.
132 ///
133 /// The default value is [`AttestationConveyancePreference::None`]
134 #[serde(default, deserialize_with = "ignore_unknown")]
135 pub attestation: AttestationConveyancePreference,
136
137 /// The Relying Party MAY use this OPTIONAL member to specify a preference regarding the attestation
138 /// statement format used by the authenticator. Values SHOULD be taken from the IANA "WebAuthn
139 /// Attestation Statement Format Identifiers" registry [IANA-WebAuthn-Registries] established by
140 /// [RFC8809]. Values are ordered from most preferable to least preferable. This parameter is
141 /// advisory and the authenticator MAY use an attestation statement not enumerated in this parameter.
142 ///
143 /// The default value is the empty list, which indicates no preference.
144 ///
145 /// [IANA-WebAuthn-Registries]: https://www.iana.org/assignments/webauthn/webauthn.xhtml#webauthn-attestation-statement-format-ids
146 /// [RFC8809]: https://www.rfc-editor.org/rfc/rfc8809
147 #[serde(
148 default,
149 skip_serializing_if = "Option::is_none",
150 deserialize_with = "ignore_unknown_opt_vec"
151 )]
152 pub attestation_formats: Option<Vec<AttestationStatementFormatIdentifiers>>,
153
154 /// The Relying Party MAY use this OPTIONAL member to provide client extension inputs requesting
155 /// additional processing by the client and authenticator.
156 ///
157 /// See [`AuthenticationExtensionsClientInputs`] for the list of currenly supported [WebAuthn Extensions].
158 ///
159 /// [WebAuthn Extensions]: https://w3c.github.io/webauthn/#webauthn-extensions
160 #[serde(
161 default,
162 skip_serializing_if = "Option::is_none",
163 deserialize_with = "ignore_unknown"
164 )]
165 pub extensions: Option<AuthenticationExtensionsClientInputs>,
166}
167
168/// This is the expected input to [`navigator.credentials.get`] when wanting to authenticate using a
169/// webauthn credential.
170///
171/// <https://w3c.github.io/webauthn/#sctn-credentialrequestoptions-extension>
172///
173/// [`navigator.credentials.get`]: https://developer.mozilla.org/en-US/docs/Web/API/CredentialsContainer/get
174#[derive(Debug, Serialize, Deserialize)]
175#[serde(rename_all = "camelCase")]
176#[typeshare]
177pub struct CredentialRequestOptions {
178 /// The key defining that this is a request for a webauthn credential.
179 pub public_key: PublicKeyCredentialRequestOptions,
180}
181
182/// This type represents an authenticator's response to a client’s request for generation of a new
183/// authentication assertion given the Relying Party's [challenge](PublicKeyCredentialRequestOptions)
184/// and OPTIONAL list of credentials it is aware of. This response contains a cryptographic signature
185/// proving possession of the credential private key, and optionally evidence of user consent to a
186/// specific transaction.
187///
188/// <https://w3c.github.io/webauthn/#iface-authenticatorassertionresponse>
189#[derive(Debug, Deserialize, Serialize)]
190#[serde(rename_all = "camelCase")]
191#[typeshare(swift = "Equatable")]
192pub struct AuthenticatorAssertionResponse {
193 /// This attribute contains the JSON serialization of [`CollectedClientData`] passed to the
194 /// authenticator by the client in order to generate this credential. The exact JSON serialization
195 /// MUST be preserved, as the hash of the serialized client data has been computed over it.
196 #[serde(rename = "clientDataJSON")]
197 pub client_data_json: Bytes,
198
199 /// This attribute contains the authenticator data returned by the authenticator. See [`AuthenticatorData`].
200 pub authenticator_data: Bytes,
201
202 /// This attribute contains the raw signature returned from the authenticator.
203 pub signature: Bytes,
204
205 /// This attribute contains the user handle returned from the authenticator, or null if the
206 /// authenticator did not return a user handle.
207 ///
208 /// This mirrors the [`PublicKeyCredentialUserEntity::id`] field.
209 #[serde(default, skip_serializing_if = "Option::is_none")]
210 pub user_handle: Option<Bytes>,
211
212 /// This OPTIONAL attribute contains an attestation object, if the authenticator supports attestation
213 /// in assertions. The attestation object, if present, includes an attestation statement. Unlike
214 /// the [`AuthenticatorAttestationResponse::attestation_object`], it does not contain an `authData`
215 /// key because the authenticator data is provided directly above in
216 /// [`AuthenticatorAssertionResponse::authenticator_data`] structure. For more details on attestation,
217 /// see [Attestation in assertions][1].
218 ///
219 /// [1]: https://w3c.github.io/webauthn/#sctn-attestation-in-assertions
220 #[serde(default, skip_serializing_if = "Option::is_none")]
221 pub attestation_object: Option<Bytes>,
222}