Skip to main content

shared/settings/
webauthn.rs

1use super::{
2    ExtensionSettings, SettingsDeserializeExt, SettingsDeserializer, SettingsSerializeExt,
3    SettingsSerializer,
4};
5use compact_str::ToCompactString;
6use serde::{Deserialize, Serialize};
7use utoipa::ToSchema;
8
9#[derive(Clone, ToSchema, Serialize, Deserialize)]
10pub struct AppSettingsWebauthn {
11    pub enabled: bool,
12    pub allow_discoverable: bool,
13
14    pub rp_id: compact_str::CompactString,
15    pub rp_origin: compact_str::CompactString,
16
17    pub authentication_timeout_seconds: u64,
18    pub registration_timeout_seconds: u64,
19}
20
21#[async_trait::async_trait]
22impl SettingsSerializeExt for AppSettingsWebauthn {
23    async fn serialize(
24        &self,
25        serializer: SettingsSerializer,
26    ) -> Result<SettingsSerializer, anyhow::Error> {
27        Ok(serializer
28            .write_raw_setting("enabled", self.enabled.to_compact_string())
29            .write_raw_setting(
30                "allow_discoverable",
31                self.allow_discoverable.to_compact_string(),
32            )
33            .write_raw_setting("rp_id", &*self.rp_id)
34            .write_raw_setting("rp_origin", &*self.rp_origin)
35            .write_raw_setting(
36                "authentication_timeout_seconds",
37                self.authentication_timeout_seconds.to_compact_string(),
38            )
39            .write_raw_setting(
40                "registration_timeout_seconds",
41                self.registration_timeout_seconds.to_compact_string(),
42            ))
43    }
44}
45
46pub struct AppSettingsWebauthnDeserializer;
47
48#[async_trait::async_trait]
49impl SettingsDeserializeExt for AppSettingsWebauthnDeserializer {
50    async fn deserialize_boxed(
51        &self,
52        mut deserializer: SettingsDeserializer<'_>,
53    ) -> Result<ExtensionSettings, anyhow::Error> {
54        Ok(Box::new(AppSettingsWebauthn {
55            enabled: deserializer
56                .take_raw_setting("enabled")
57                .map(|s| s == "true")
58                .unwrap_or(true),
59            allow_discoverable: deserializer
60                .take_raw_setting("allow_discoverable")
61                .map(|s| s == "true")
62                .unwrap_or(true),
63            rp_id: deserializer
64                .take_raw_setting("rp_id")
65                .unwrap_or_else(|| "localhost".into()),
66            rp_origin: deserializer
67                .take_raw_setting("rp_origin")
68                .unwrap_or_else(|| "http://localhost".into()),
69            authentication_timeout_seconds: deserializer
70                .take_raw_setting("authentication_timeout_seconds")
71                .and_then(|s| s.parse().ok())
72                .unwrap_or(300),
73            registration_timeout_seconds: deserializer
74                .take_raw_setting("registration_timeout_seconds")
75                .and_then(|s| s.parse().ok())
76                .unwrap_or(300),
77        }))
78    }
79}