Skip to main content

bitwarden_user_crypto_management/pin_settings/
pin_settings_client.rs

1use bitwarden_core::key_management::{PinLockSystem, PinLockType, PinUnlockStatus};
2use bitwarden_error::bitwarden_error;
3use thiserror::Error;
4
5use crate::UserCryptoManagementClient;
6
7#[derive(Clone)]
8#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
9#[bitwarden_ffi::wasm_object]
10/// Sub-client for configuring PIN unlock behavior.
11pub struct PinSettingsClient {
12    pub(crate) client: bitwarden_core::Client,
13}
14
15#[derive(Debug, Error)]
16#[bitwarden_error(flat)]
17/// Errors returned by PIN settings operations.
18pub enum PinSettingsError {
19    #[error("Failed to set PIN state")]
20    /// Failed while enrolling or storing the PIN-protected key envelope.
21    SetPinState,
22}
23
24impl PinSettingsClient {
25    pub(crate) fn new(client: bitwarden_core::Client) -> Self {
26        Self { client }
27    }
28}
29
30#[bitwarden_ffi::wasm_export]
31#[cfg_attr(feature = "uniffi", uniffi::export(async_runtime = "tokio"))]
32impl PinSettingsClient {
33    /// Sets or updates the account PIN and stores the corresponding unlock state.
34    ///
35    /// The `lock_type` determines how PIN unlock behaves for this account.
36    /// Returns an error when the PIN-protected state cannot be persisted.
37    pub async fn set_pin(
38        &self,
39        pin: String,
40        lock_type: bitwarden_core::key_management::PinLockType,
41    ) -> Result<(), PinSettingsError> {
42        PinLockSystem::with_client(&self.client)
43            .set_pin(pin, lock_type)
44            .await
45            .map_err(|_| PinSettingsError::SetPinState)
46    }
47
48    /// Unenrolls from PIN-based unlock
49    pub async fn unset_pin(&self) {
50        PinLockSystem::with_client(&self.client).unset_pin().await
51    }
52
53    /// Returns the current status of PIN unlock
54    pub async fn get_status(&self) -> PinUnlockStatus {
55        PinLockSystem::with_client(&self.client)
56            .get_pin_status()
57            .await
58    }
59
60    /// Returns the configured PIN lock type, if a PIN lock is set.
61    pub async fn get_lock_type(&self) -> Option<PinLockType> {
62        PinLockSystem::with_client(&self.client)
63            .get_pin_lock_type()
64            .await
65    }
66
67    /// Validates whether `pin` matches the currently configured unlock PIN.
68    pub async fn validate_pin(&self, pin: String) -> bool {
69        PinLockSystem::with_client(&self.client)
70            .validate_pin(pin)
71            .await
72    }
73
74    /// Returns the currently configured PIN, if available.
75    pub async fn get_pin(&self) -> Option<String> {
76        PinLockSystem::with_client(&self.client).get_pin().await
77    }
78}
79
80#[bitwarden_ffi::wasm_export]
81#[cfg_attr(feature = "uniffi", uniffi::export)]
82impl UserCryptoManagementClient {
83    /// Returns the PIN settings sub-client.
84    pub fn pin_settings(&self) -> PinSettingsClient {
85        PinSettingsClient::new(self.client.clone())
86    }
87}