passkey_types/
webauthn.rs

1//! Implementation of the types defined in [WebAuthn Level 3]
2//!
3//! [WebAuthn Level 3]: https://w3c.github.io/webauthn
4
5use serde::{Deserialize, Serialize};
6#[cfg(feature = "typeshare")]
7use typeshare::typeshare;
8
9use crate::{Bytes, utils::serde::ignore_unknown};
10
11mod assertion;
12mod attestation;
13mod common;
14mod extensions;
15mod well_known;
16
17// re-export types
18pub use self::{assertion::*, attestation::*, common::*, extensions::*, well_known::*};
19
20mod sealed {
21    pub trait Sealed {}
22
23    impl Sealed for super::AuthenticatorAssertionResponse {}
24    impl Sealed for super::AuthenticatorAttestationResponse {}
25}
26
27/// Marker trait for response types
28pub trait AuthenticatorResponse: sealed::Sealed {}
29
30impl AuthenticatorResponse for AuthenticatorAssertionResponse {}
31impl AuthenticatorResponse for AuthenticatorAttestationResponse {}
32
33/// This is the response from a successful creation or assertion of a credential.
34///
35/// It is recommended to use the type aliases depending on which response you are expecting:
36/// * Credential Creation: [CreatedPublicKeyCredential]
37/// * Credential assertion: [AuthenticatedPublicKeyCredential]
38///
39/// <https://w3c.github.io/webauthn/#iface-pkcredential>
40#[derive(Debug, Deserialize, Serialize)]
41#[serde(rename_all = "camelCase")]
42#[cfg_attr(
43    feature = "typeshare",
44    typeshare(
45        swift = "Equatable, Hashable",
46        swiftGenericConstraints = "R: Equatable & Hashable"
47    )
48)]
49pub struct PublicKeyCredential<R: AuthenticatorResponse> {
50    /// The id contains the credential ID, chosen by the authenticator. This is usually the base64url
51    /// encoded data of [Self::raw_id]
52    ///
53    /// The credential ID is used to look up credentials for use and is therefore expected to be
54    /// globally unique with high probability across all credentials of the same type across all
55    /// authenticators.
56    ///
57    /// > NOTE: This API does not constrain the format or length of this identifier, except that it
58    /// MUST be sufficient for the authenticator to uniquely select a key.
59    pub id: String,
60
61    /// The raw byte containing the credential ID, see [Self::id] for more information.
62    pub raw_id: Bytes,
63
64    /// Always [PublicKeyCredentialType]
65    #[serde(rename = "type")]
66    pub ty: PublicKeyCredentialType,
67
68    /// This contains the authenticator's response to the client's request to either:
69    /// * create a public key in which case it is of type [AuthenticatorAttestationResponse] or
70    /// * generate an authentication assertion in which case it is of type [AuthenticatorAssertionResponse]
71    pub response: R,
72
73    /// This reports the modality of the communication between the client and authenticator.
74    #[serde(
75        default,
76        skip_serializing_if = "Option::is_none",
77        deserialize_with = "ignore_unknown"
78    )]
79    pub authenticator_attachment: Option<AuthenticatorAttachment>,
80
81    /// This object is a map containing extension identifier → client extension output entries
82    /// produced by the extension’s client extension processing.
83    #[serde(default)]
84    pub client_extension_results: AuthenticationExtensionsClientOutputs,
85}