Skip to main content

ts_webauthn/options/
creation.rs

1//! Options for a credential creation request.
2
3use serde::{Deserialize, Serialize};
4use ts_io::Base64Url;
5
6use crate::{credential::CredentialDescriptor, supported_algorithms::SupportedAlgorithm};
7
8/// <https://w3c.github.io/webauthn/#dictionary-makecredentialoptions>
9#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
10#[serde(rename_all = "camelCase")]
11pub struct CredentialCreationOptions {
12    /// The relying party details.
13    pub rp: RelyingParty,
14    /// The user details.
15    pub user: User,
16    /// The challenge for the authenticator.
17    pub challenge: Base64Url,
18    /// The acceptable public key parameters.
19    pub pub_key_cred_params: Vec<CredentialParameters>,
20    /// The duration in milliseconds the relying party is willing to wait.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub timeout: Option<u64>,
23    /// The exiting credentials for this user, used to prevent the user from accidentally registering
24    /// the same authenticator multiple times.
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub exclude_credentials: Option<Vec<CredentialDescriptor>>,
27    /// The selection criteria for the user-agent to use.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub authenticator_selection: Option<AuthenticatorSelectionCriteria>,
30    /// The attestation preference of the relying party, should be one of:
31    /// * `none`
32    /// * `indirect`
33    /// * `direct`
34    /// * `enterprise`
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub attestation: Option<String>,
37    /// The preference for the format of an attestation statement, should be one of:
38    /// * `packed`
39    /// * `tpm`
40    /// * `android-key`
41    /// * `android-safetynet`
42    /// * `fido-u2f`
43    /// * `apple`
44    /// * `none`
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub attestation_formats: Option<Vec<String>>,
47    /// Any extension data.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub extensions: Option<serde_json::Map<String, serde_json::Value>>,
50}
51
52/// <https://w3c.github.io/webauthn/#dictdef-publickeycredentialrpentity>
53#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
54#[serde(rename_all = "camelCase")]
55pub struct RelyingParty {
56    /// The user facing relying party name.
57    pub name: String,
58    /// The relying party ID.
59    pub id: Option<String>,
60}
61
62/// <https://w3c.github.io/webauthn/#dictdef-publickeycredentialuserentity>
63#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
64#[serde(rename_all = "camelCase")]
65pub struct User {
66    /// The `base64` encoded user ID.
67    pub id: Base64Url,
68    /// The user display name.
69    pub display_name: String,
70    /// The username.
71    pub name: String,
72}
73
74/// <https://w3c.github.io/webauthn/#dictdef-publickeycredentialparameters>
75#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
76#[serde(rename_all = "camelCase")]
77pub struct CredentialParameters {
78    /// The credential type, should be `public-key`.
79    pub r#type: String,
80    /// The algorithm.
81    pub alg: i64,
82}
83
84/// <https://w3c.github.io/webauthn/#dictdef-authenticatorselectioncriteria>
85#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
86#[serde(rename_all = "camelCase")]
87pub struct AuthenticatorSelectionCriteria {
88    /// Preference for authenticator attachment.
89    pub authenticator_attachment: Option<Vec<String>>,
90    /// Preference for a resident key, should be one of:
91    /// * `preferred`
92    /// * `discouraged`
93    /// * `required`
94    pub resident_key: Option<String>,
95    /// If a resident key is required, exists for compatibility purposes.
96    pub require_resident_key: Option<bool>,
97    /// The user verification preference, should be one of:
98    /// * `preferred`
99    /// * `discouraged`
100    /// * `required`
101    pub user_verification: Option<String>,
102}
103
104impl From<SupportedAlgorithm> for CredentialParameters {
105    fn from(value: SupportedAlgorithm) -> Self {
106        Self {
107            r#type: "public-key".to_string(),
108            alg: value.into(),
109        }
110    }
111}